From 7e553f44a5975f3d2ff055147f32a948544261cd Mon Sep 17 00:00:00 2001 From: Josh Story Date: Thu, 7 Sep 2023 12:41:15 -0700 Subject: [PATCH] consolidate tag scopes into a bitmask --- .../src/server/ReactFizzConfigDOM.js | 86 ++++++------------- 1 file changed, 28 insertions(+), 58 deletions(-) diff --git a/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js b/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js index f191a365663d9..67e8983cf9bd6 100644 --- a/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js +++ b/packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js @@ -499,25 +499,26 @@ const HTML_COLGROUP_MODE = 8; type InsertionMode = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8; +const NO_SCOPE = /* */ 0b00; +const NOSCRIPT_SCOPE = /* */ 0b01; +const PICTURE_SCOPE = /* */ 0b10; + // Lets us keep track of contextual state and pick it back up after suspending. export type FormatContext = { insertionMode: InsertionMode, // root/svg/html/mathml/table selectedValue: null | string | Array, // the selected value(s) inside a - noscriptTagInScope: boolean, - pictureTagInScope: boolean, + tagScope: number, }; function createFormatContext( insertionMode: InsertionMode, selectedValue: null | string, - noscriptTagInScope: boolean, - pictureTagInScope: boolean, + tagScope: number, ): FormatContext { return { insertionMode, selectedValue, - noscriptTagInScope, - pictureTagInScope, + tagScope, }; } @@ -528,7 +529,7 @@ export function createRootFormatContext(namespaceURI?: string): FormatContext { : namespaceURI === 'http://www.w3.org/1998/Math/MathML' ? MATHML_MODE : ROOT_HTML_MODE; - return createFormatContext(insertionMode, null, false, false); + return createFormatContext(insertionMode, null, NO_SCOPE); } export function getChildFormatContext( @@ -541,98 +542,67 @@ export function getChildFormatContext( return createFormatContext( HTML_MODE, null, - true, - parentContext.pictureTagInScope, + parentContext.tagScope | NOSCRIPT_SCOPE, ); case 'select': return createFormatContext( HTML_MODE, props.value != null ? props.value : props.defaultValue, - parentContext.noscriptTagInScope, - parentContext.pictureTagInScope, + parentContext.tagScope, ); case 'svg': - return createFormatContext( - SVG_MODE, - null, - parentContext.noscriptTagInScope, - parentContext.pictureTagInScope, - ); + return createFormatContext(SVG_MODE, null, parentContext.tagScope); case 'picture': return createFormatContext( HTML_MODE, null, - parentContext.noscriptTagInScope, - true, + parentContext.tagScope | PICTURE_SCOPE, ); case 'math': - return createFormatContext( - MATHML_MODE, - null, - parentContext.noscriptTagInScope, - parentContext.pictureTagInScope, - ); + return createFormatContext(MATHML_MODE, null, parentContext.tagScope); case 'foreignObject': - return createFormatContext( - HTML_MODE, - null, - parentContext.noscriptTagInScope, - parentContext.pictureTagInScope, - ); + return createFormatContext(HTML_MODE, null, parentContext.tagScope); // Table parents are special in that their children can only be created at all if they're // wrapped in a table parent. So we need to encode that we're entering this mode. case 'table': - return createFormatContext( - HTML_TABLE_MODE, - null, - parentContext.noscriptTagInScope, - parentContext.pictureTagInScope, - ); + return createFormatContext(HTML_TABLE_MODE, null, parentContext.tagScope); case 'thead': case 'tbody': case 'tfoot': return createFormatContext( HTML_TABLE_BODY_MODE, null, - parentContext.noscriptTagInScope, - parentContext.pictureTagInScope, + parentContext.tagScope, ); case 'colgroup': return createFormatContext( HTML_COLGROUP_MODE, null, - parentContext.noscriptTagInScope, - parentContext.pictureTagInScope, + parentContext.tagScope, ); case 'tr': return createFormatContext( HTML_TABLE_ROW_MODE, null, - parentContext.noscriptTagInScope, - parentContext.pictureTagInScope, + parentContext.tagScope, ); } if (parentContext.insertionMode >= HTML_TABLE_MODE) { // Whatever tag this was, it wasn't a table parent or other special parent, so we must have // entered plain HTML again. - return createFormatContext( - HTML_MODE, - null, - parentContext.noscriptTagInScope, - parentContext.pictureTagInScope, - ); + return createFormatContext(HTML_MODE, null, parentContext.tagScope); } if (parentContext.insertionMode === ROOT_HTML_MODE) { if (type === 'html') { // We've emitted the root and is now in mode. - return createFormatContext(HTML_HTML_MODE, null, false, false); + return createFormatContext(HTML_HTML_MODE, null, parentContext.tagScope); } else { // We've emitted the root and is now in plain HTML mode. - return createFormatContext(HTML_MODE, null, false, false); + return createFormatContext(HTML_MODE, null, parentContext.tagScope); } } else if (parentContext.insertionMode === HTML_HTML_MODE) { // We've emitted the document element and is now in plain HTML mode. - return createFormatContext(HTML_MODE, null, false, false); + return createFormatContext(HTML_MODE, null, parentContext.tagScope); } return parentContext; } @@ -3256,7 +3226,7 @@ export function pushStartInstance( props, renderState, formatContext.insertionMode, - formatContext.noscriptTagInScope, + !!(formatContext.tagScope & NOSCRIPT_SCOPE), ) : pushStartTitle(target, props); case 'link': @@ -3267,7 +3237,7 @@ export function pushStartInstance( renderState, textEmbedded, formatContext.insertionMode, - formatContext.noscriptTagInScope, + !!(formatContext.tagScope & NOSCRIPT_SCOPE), ); case 'script': return enableFloat @@ -3277,7 +3247,7 @@ export function pushStartInstance( resumableState, textEmbedded, formatContext.insertionMode, - formatContext.noscriptTagInScope, + !!(formatContext.tagScope & NOSCRIPT_SCOPE), ) : pushStartGenericElement(target, props, type); case 'style': @@ -3288,7 +3258,7 @@ export function pushStartInstance( renderState, textEmbedded, formatContext.insertionMode, - formatContext.noscriptTagInScope, + !!(formatContext.tagScope & NOSCRIPT_SCOPE), ); case 'meta': return pushMeta( @@ -3297,7 +3267,7 @@ export function pushStartInstance( renderState, textEmbedded, formatContext.insertionMode, - formatContext.noscriptTagInScope, + !!(formatContext.tagScope & NOSCRIPT_SCOPE), ); // Newline eating tags case 'listing': @@ -3310,7 +3280,7 @@ export function pushStartInstance( target, props, resumableState, - formatContext.pictureTagInScope, + !!(formatContext.tagScope & PICTURE_SCOPE), ) : pushSelfClosing(target, props, type); }