Skip to content

Commit

Permalink
consolidate tag scopes into a bitmask
Browse files Browse the repository at this point in the history
  • Loading branch information
gnoff committed Sep 7, 2023
1 parent c760130 commit 7e553f4
Showing 1 changed file with 28 additions and 58 deletions.
86 changes: 28 additions & 58 deletions packages/react-dom-bindings/src/server/ReactFizzConfigDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>, // the selected value(s) inside a <select>, or null outside <select>
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,
};
}

Expand All @@ -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(
Expand All @@ -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 <html> 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;
}
Expand Down Expand Up @@ -3256,7 +3226,7 @@ export function pushStartInstance(
props,
renderState,
formatContext.insertionMode,
formatContext.noscriptTagInScope,
!!(formatContext.tagScope & NOSCRIPT_SCOPE),
)
: pushStartTitle(target, props);
case 'link':
Expand All @@ -3267,7 +3237,7 @@ export function pushStartInstance(
renderState,
textEmbedded,
formatContext.insertionMode,
formatContext.noscriptTagInScope,
!!(formatContext.tagScope & NOSCRIPT_SCOPE),
);
case 'script':
return enableFloat
Expand All @@ -3277,7 +3247,7 @@ export function pushStartInstance(
resumableState,
textEmbedded,
formatContext.insertionMode,
formatContext.noscriptTagInScope,
!!(formatContext.tagScope & NOSCRIPT_SCOPE),
)
: pushStartGenericElement(target, props, type);
case 'style':
Expand All @@ -3288,7 +3258,7 @@ export function pushStartInstance(
renderState,
textEmbedded,
formatContext.insertionMode,
formatContext.noscriptTagInScope,
!!(formatContext.tagScope & NOSCRIPT_SCOPE),
);
case 'meta':
return pushMeta(
Expand All @@ -3297,7 +3267,7 @@ export function pushStartInstance(
renderState,
textEmbedded,
formatContext.insertionMode,
formatContext.noscriptTagInScope,
!!(formatContext.tagScope & NOSCRIPT_SCOPE),
);
// Newline eating tags
case 'listing':
Expand All @@ -3310,7 +3280,7 @@ export function pushStartInstance(
target,
props,
resumableState,
formatContext.pictureTagInScope,
!!(formatContext.tagScope & PICTURE_SCOPE),
)
: pushSelfClosing(target, props, type);
}
Expand Down

0 comments on commit 7e553f4

Please sign in to comment.