Skip to content

Commit ed9bc5e

Browse files
committed
Prevent widget constructors from being marked as functional components
A widget factory generated via asFactory() may be passed a "parent" factory as the second parameter to make it work as a selector. If the given function was not actually a factory but a widget constructor this would override the existing functionality of that constructor as a selector, resulting in very confusing behavior. Check that the parent factory is not a constructor for an object that can be used as a JSX Element (and therefore as a selector). Fix #2137 Change-Id: Ib0de0131436583f138adf8564914e30221771b18
1 parent 43ff821 commit ed9bc5e

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/tabris/JsxProcessor.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,16 @@ function createFactoryProxy(processor: JsxProcessor, constructor: ElementFn): El
214214
const handler: ProxyHandler<ElementFn> = {
215215
apply(target, _thisArg, args) {
216216
const [attributes, functionalComponent] = args;
217+
if (args.length > 1) {
218+
if (!(functionalComponent instanceof Function)) {
219+
throw new TypeError('Second parameter must be a function');
220+
}
221+
if (functionalComponent.prototype && functionalComponent.prototype[JSX.jsxFactory]) {
222+
throw new TypeError('Second parameter must be a factory');
223+
}
224+
}
217225
const result = processor.createElement(proxy, attributes);
218-
if (functionalComponent instanceof Function && result instanceof Object) {
226+
if (args.length > 1 && result instanceof Object) {
219227
functionalComponent[JSX.jsxType] = true;
220228
result[JSX.jsxType] = functionalComponent;
221229
}

test/tabris/JsxProcessor.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,10 @@ describe('JsxProcessor', function() {
806806
expect(parent.children('asdf').length).to.equal(0);
807807
});
808808

809+
it('throws for widget class as selector', function() {
810+
expect(() => CompositeFactory({}, Composite)).to.throw(TypeError);
811+
});
812+
809813
it('still has static members', function() {
810814
expect(CompositeFactory.defineChangeEvent).to.equal(Composite.defineChangeEvent);
811815
});

0 commit comments

Comments
 (0)