Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion src/parser/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,5 +588,20 @@ describe('Parser', () => {
});

describe('Plain Objects', () => {
});
describe('Content', () => {
it('renders 0 when an object has a .value of 0', () => {
const zeroValueObject = { value: 0 };
const template = rml`<div>${zeroValueObject}</div>`;
expect(template).toEqual('<div>0</div>');
});
});

describe('Attributes', () => {
it('renders 0 in an attribute when an object has a .value of 0', () => {
const zeroValueObject = { value: 0 };
const template = rml`<input value="${zeroValueObject}">`;
expect(template).toEqual(`<input value="0">`);
});
});
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are great, could you please move them inside the BehaviorSubject block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

});
19 changes: 17 additions & 2 deletions src/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,22 @@ export function rml(strings: TemplateStringsArray, ...expressions: RMLTemplateEx
acc = accPlusString +expression.join('');
} else {
// expression is a future or an object


if (
expression &&
typeof expression === 'object' &&
'value' in expression &&
!isPromise(expression) &&
!isObservable(expression)
) {
const valueType = typeof expression.value;
if (['string', 'number', 'boolean'].includes(valueType)) {
acc = accPlusString + (expression.value ?? '');
continue;
}
}
}
// Future / Object Sink
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change block seems redundant. Everything works well with the other change alone.
Let's get rid of this, then we are good, by the looks of it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

completed!

const nextString = strings[i+1];
// if it's a BehaviorSubject or any other sync stream (e.g.: startWith operator), pick its initial/current value to render it synchronously
const initialValue = currentValue(expression.source ?? expression);
Expand Down Expand Up @@ -268,7 +283,7 @@ export function rml(strings: TemplateStringsArray, ...expressions: RMLTemplateEx
addRef(ref, isSinkBindingConfiguration(_source) ? _source : InnerHTML(_source));
acc = acc
+(existingRef ? string : string.replace(/\s*>\s*$/, ` ${RESOLVE_ATTRIBUTE}="${ref}">`))
+(initialValue || '')
+(initialValue ?? '')
;

} else if(/>?\s*[^<]*$/m.test(string) && /^\s*[^<]*\s*<?/m.test(nextString)) {
Expand Down