Fix newly-created bug with 'on' prefix mismatch

This commit is contained in:
Steve Sanderson 2019-09-26 14:00:25 +01:00 committed by Artak
parent d2887ccc1c
commit cdef672310
3 changed files with 13 additions and 10 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -284,15 +284,10 @@ export class BrowserRenderer {
private applyAttribute(batch: RenderBatch, componentId: number, toDomElement: Element, attributeFrame: RenderTreeFrame) { private applyAttribute(batch: RenderBatch, componentId: number, toDomElement: Element, attributeFrame: RenderTreeFrame) {
const frameReader = batch.frameReader; const frameReader = batch.frameReader;
const attributeName = frameReader.attributeName(attributeFrame)!; const attributeName = frameReader.attributeName(attributeFrame)!;
const browserRendererId = this.browserRendererId;
const eventHandlerId = frameReader.attributeEventHandlerId(attributeFrame); const eventHandlerId = frameReader.attributeEventHandlerId(attributeFrame);
if (eventHandlerId) { if (eventHandlerId) {
const firstTwoChars = attributeName.substring(0, 2); const eventName = stripOnPrefix(attributeName);
const eventName = attributeName.substring(2);
if (firstTwoChars !== 'on' || !eventName) {
throw new Error(`Attribute has nonzero event handler ID, but attribute name '${attributeName}' does not start with 'on'.`);
}
this.eventDelegator.setListener(toDomElement, eventName, eventHandlerId, componentId); this.eventDelegator.setListener(toDomElement, eventName, eventHandlerId, componentId);
return; return;
} }
@ -328,11 +323,11 @@ export class BrowserRenderer {
if (internalAttributeName.startsWith(eventStopBubblingAttributeNamePrefix)) { if (internalAttributeName.startsWith(eventStopBubblingAttributeNamePrefix)) {
// Stop bubbling // Stop bubbling
const eventName = internalAttributeName.substring(eventStopBubblingAttributeNamePrefix.length); const eventName = stripOnPrefix(internalAttributeName.substring(eventStopBubblingAttributeNamePrefix.length));
this.eventDelegator.setStopBubbling(element, eventName, attributeValue !== null); this.eventDelegator.setStopBubbling(element, eventName, attributeValue !== null);
} else if (internalAttributeName.startsWith(eventPreventDefaultAttributeNamePrefix)) { } else if (internalAttributeName.startsWith(eventPreventDefaultAttributeNamePrefix)) {
// Prevent default // Prevent default
const eventName = internalAttributeName.substring(eventPreventDefaultAttributeNamePrefix.length); const eventName = stripOnPrefix(internalAttributeName.substring(eventPreventDefaultAttributeNamePrefix.length));
this.eventDelegator.setPreventDefault(element, eventName, attributeValue !== null); this.eventDelegator.setPreventDefault(element, eventName, attributeValue !== null);
} else { } else {
// The prefix makes this attribute name reserved, so any other usage is disallowed // The prefix makes this attribute name reserved, so any other usage is disallowed
@ -502,3 +497,11 @@ function clearBetween(start: Node, end: Node): void {
// as it adds noise to the DOM. // as it adds noise to the DOM.
start.textContent = '!'; start.textContent = '!';
} }
function stripOnPrefix(attributeName: string) {
if (attributeName.startsWith('on')) {
return attributeName.substring(2);
}
throw new Error(`Attribute should be an event name, but doesn't start with 'on'. Value: '${attributeName}'`);
}