Skip to content

Commit 6e5dc67

Browse files
committed
never bake onmount in html
1 parent db3c4a2 commit 6e5dc67

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

src/parser/parser.test.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,70 @@ describe('Parser', () => {
237237

238238
});
239239

240+
describe('Booleans', () => {
241+
describe('Attributes', () => {
242+
243+
it('handles true as a string', () => {
244+
const bool = true;
245+
const template = rml`<div something="${bool}">Hello</div>`;
246+
247+
expect(template).toEqual(`<div something="true">Hello</div>`);
248+
});
249+
250+
it('handles false as a string', () => {
251+
const bool = false;
252+
const template = rml`<div something="${bool}">Hello</div>`;
253+
254+
expect(template).toEqual(`<div something="false">Hello</div>`);
255+
});
256+
257+
});
258+
259+
describe('Content', () => {
260+
261+
it('handles true as a string', () => {
262+
const bool = true;
263+
const template = rml`<div>${bool}</div>`;
264+
265+
expect(template).toEqual(`<div>${bool}</div>`);
266+
});
267+
268+
it('handles false as a string', () => {
269+
const bool = false;
270+
const template = rml`<div>${bool}</div>`;
271+
272+
expect(template).toEqual(`<div>${bool}</div>`);
273+
});
274+
275+
});
276+
277+
});
278+
279+
describe.skip('Symbols', () => {
280+
describe('Attributes', () => {
281+
282+
it('handles symbols as strings', () => {
283+
const sym = Symbol('test');
284+
const template = rml`<div something="${sym}">Hello</div>`;
285+
286+
expect(template).toEqual(`<div something="${sym.toString()}">Hello</div>`);
287+
});
288+
289+
});
290+
291+
describe('Content', () => {
292+
293+
it('handles symbols as strings', () => {
294+
const sym = Symbol('test');
295+
const template = rml`<div>${sym}</div>`;
296+
297+
expect(template).toEqual(`<div>${sym.toString()}</div>`);
298+
});
299+
300+
});
301+
302+
});
303+
240304
});
241305

242306
describe('Sources', () => {
@@ -384,6 +448,28 @@ describe('Parser', () => {
384448
);
385449
});
386450

451+
it('always defers onmount', () => {
452+
const fn = () => alert(123);
453+
const a = { 'onmount': fn };
454+
const template = rml`<div ...${a}>Hello</div>`;
455+
456+
expect(template).toMatch(/<div.*resolve="RMLREF\+0".*>Hello<\/div>/);
457+
expect(waitingElementHanlders.get('RMLREF+0')![0]).toStrictEqual(
458+
{ type: 'sink', t: 'mixin', source: { 'onmount': fn }, sink: AttributeObjectSink },
459+
);
460+
});
461+
462+
it('always defers rml:onmount', () => {
463+
const fn = () => alert(123);
464+
const a = { 'rml:onmount': fn };
465+
const template = rml`<div ...${a}>Hello</div>`;
466+
467+
expect(template).toMatch(/<div.*resolve="RMLREF\+0".*>Hello<\/div>/);
468+
expect(waitingElementHanlders.get('RMLREF+0')![0]).toStrictEqual(
469+
{ type: 'sink', t: 'mixin', source: { 'rml:onmount': fn }, sink: AttributeObjectSink },
470+
);
471+
});
472+
387473
});
388474

389475

src/parser/parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ export function rml(strings: TemplateStringsArray, ...expressions: RMLTemplateEx
258258
// Merge static (string, number) properties of the mixin inline in the rendered HTML
259259
// and pass the rest as a future sink
260260
const [staticAttributes, deferredAttributes] = Object.entries(expression as AttributeObject || {})
261-
.reduce((acc, [k, v]) => (acc[+(isFutureSinkAttributeValue(v) || isRMLEventListener(k, v) && isFunction(v))].push([k, v]), acc), [[] as [HTMLAttributeName, PresentSinkAttributeValue][], [] as [HTMLAttributeName, FutureSinkAttributeValue][]])
261+
.reduce((acc, [k, v]) => (acc[+(isFutureSinkAttributeValue(v) || isRMLEventListener(k, v) && isFunction(v) || /^(?:rml:)?onmount$/.test(k))].push([k, v]), acc), [[] as [HTMLAttributeName, PresentSinkAttributeValue][], [] as [HTMLAttributeName, FutureSinkAttributeValue][]])
262262
;
263263

264264
acc += staticAttributes.map(([k, v])=>`${k}="${v}"`).join(' ');

src/test-support.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ export interface MockElement extends HTMLElement {
1818
selectedIndex?: number;
1919
setAttribute(name: string, value: string): void;
2020
getAttribute(name: string): string | null;
21+
hasAttribute: (name: string) => boolean;
2122
removeAttribute(name: string): void;
2223
className: string;
2324
insertAdjacentHTML(pos: InsertPosition, html: string): void;
2425
addEventListener(eventName: string, handler: EventListenerOrEventListenerObject): void;
2526
removeEventListener(eventName: string, handler: EventListenerOrEventListenerObject): void;
2627
dispatchEvent(event: Event): boolean;
28+
childNodes: NodeListOf<ChildNode>;
2729
};
2830

2931
export const MockElement = (props?: Record<string, any>): MockElement => {
@@ -44,6 +46,9 @@ export const MockElement = (props?: Record<string, any>): MockElement => {
4446
getAttribute(name: string) {
4547
return this[name];
4648
},
49+
hasAttribute(name: string) {
50+
return this[name] !== undefined;
51+
},
4752
removeAttribute(name: string) {
4853
delete this[name];
4954
},
@@ -74,6 +79,7 @@ export const MockElement = (props?: Record<string, any>): MockElement => {
7479
dispatchEvent(event: Event) {
7580
(<HTMLEventSource>el)[`on${event.type}` as RMLEventAttributeName]?.(event);
7681
},
82+
childNodes: [] as NodeListOf<ChildNode>,
7783
...props,
7884
};
7985

0 commit comments

Comments
 (0)