Skip to content

Commit db3c4a2

Browse files
committed
mix inline event listeners in
1 parent 60d8985 commit db3c4a2

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "rimmel",
3-
"version": "1.5.0-rc2",
3+
"version": "1.5.0-rc3",
44
"description": "A Streams-Oriented UI library for the Rx.Observable Universe",
55
"type": "module",
66
"_main": "dist/cjs/index.cjs",

src/parser/parser.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,28 @@ describe('Parser', () => {
365365
);
366366
});
367367

368+
describe('Event Handlers', () => {
369+
it('bakes string listeners inline', () => {
370+
const a = { 'onmouseover': 'alert(123)' };
371+
const template = rml`<div ...${a}>Hello</div>`;
372+
373+
expect(template).toMatch(/<div.*resolve="RMLREF\+0".* onmouseover="alert\(123\)">Hello<\/div>/);
374+
});
375+
376+
it('leaves referenced listeners to set on mount', () => {
377+
const fn = () => alert(123);
378+
const a = { 'onmouseover': fn };
379+
const template = rml`<div ...${a}>Hello</div>`;
380+
381+
expect(template).toMatch(/<div.*resolve="RMLREF\+0".*>Hello<\/div>/);
382+
expect(waitingElementHanlders.get('RMLREF+0')![0]).toStrictEqual(
383+
{ type: 'sink', t: 'mixin', source: { 'onmouseover': fn }, sink: AttributeObjectSink },
384+
);
385+
});
386+
387+
});
388+
389+
368390
});
369391

370392
describe('When multiple mixins are passed together', () => {

src/parser/parser.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,21 @@ import { isFutureSinkAttributeValue, isPresentSinkAttributeValue, isSinkBindingC
77

88
import { currentValue } from "../lib/current-value";
99
import { state, waitingElementHanlders } from "../internal-state";
10-
import { takeFirstSync } from "../utils/take-first-sync";
1110
import { BOOLEAN_ATTRIBUTES } from "../definitions/boolean-attributes";
1211
import { INTERACTIVE_NODE_START, INTERACTIVE_NODE_END, REF_TAG, RESOLVE_ATTRIBUTE, RML_DEBUG } from "../constants";
1312

1413
import { PreSink } from "../sinks/index";
1514
import { sinkByAttributeName } from '../parser/sink-map';
16-
import { DatasetItemPreSink } from '../sinks/dataset-sink';
1715
import { DOMAttributePreSink, FixedAttributePreSink, WritableElementAttribute } from "../sinks/attribute-sink";
1816
import { Mixin } from "../sinks/mixin-sink";
1917

2018
import { InnerHTML } from "../sinks/inner-html-sink";
2119
import { TextContent } from "../sinks/text-content-sink";
2220
import { StyleObjectSink, StylePreSink, STYLE_OBJECT_SINK_TAG } from "../sinks/style-sink";
23-
import { toListener } from "../utils/to-listener";
21+
import { isFunction } from "../utils/is-function";
2422
import { isObservable, isPromise } from "../types/futures"
23+
import { isRMLEventListener } from "../types/event-listener";
24+
import { toListener } from "../utils/to-listener";
2525

2626
export const addRef = (ref: string, data: BindingConfiguration) => {
2727
waitingElementHanlders.get(ref)?.push(data) ?? waitingElementHanlders.set(ref, [data]);
@@ -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)].push([k, v]), acc), [[] as [HTMLAttributeName, PresentSinkAttributeValue][], [] as [HTMLAttributeName, FutureSinkAttributeValue][]])
261+
.reduce((acc, [k, v]) => (acc[+(isFutureSinkAttributeValue(v) || isRMLEventListener(k, v) && isFunction(v))].push([k, v]), acc), [[] as [HTMLAttributeName, PresentSinkAttributeValue][], [] as [HTMLAttributeName, FutureSinkAttributeValue][]])
262262
;
263263

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

0 commit comments

Comments
 (0)