perf(jsx): skip the special behavior when the element is in the head. (#3352)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Taku Amano · committed by GitHub · parent 0712530 · Browse files · 3 files·+34 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+34 added−4 removed
- tests:+15 added−0 removed
- other:+19 added−4 removed
Provenance
- Commit
e4cc5aa- Tree
2057f52- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
0712530
3 files changed+34 added−4 removed
modified:src/jsx/intrinsic-element/components.ts+18 added-3 removed
104 unmodified linesExpand all10510610710810911010911011111211311411511611711811912012112212312412511912612712812913013113213313419 unmodified lines15415515615715815916016116216316416531 unmodified lines104 unmodified linesExpand all}
export const title: FC<PropsWithChildren> = ({ children, ...props }) => { const nameSpaceContext = getNameSpaceContext() if (nameSpaceContext && useContext(nameSpaceContext) === 'svg') { new JSXNode('title', props, toArray(children ?? []) as Child[]) if (nameSpaceContext) { const context = useContext(nameSpaceContext) if (context === 'svg' || context === 'head') { return new JSXNode( 'title', props, toArray(children ?? []) as Child[] ) as unknown as HtmlEscapedString } }
return documentMetadataTag('title', children, props, false)}export const script: FC<PropsWithChildren<IntrinsicElements['script']>> = ({ children, ...props}) => { if (['src', 'async'].some((k) => !props[k])) { const nameSpaceContext = getNameSpaceContext() if ( ['src', 'async'].some((k) => !props[k]) || (nameSpaceContext && useContext(nameSpaceContext) === 'head') ) { return returnWithoutSpecialBehavior('script', children, props) }
return documentMetadataTag('script', children, props, false)19 unmodified lines } return documentMetadataTag('link', children, props, 'precedence' in props)}export const meta: FC<PropsWithChildren> = ({ children, ...props }) => { const nameSpaceContext = getNameSpaceContext() if (nameSpaceContext && useContext(nameSpaceContext) === 'head') { return returnWithoutSpecialBehavior('meta', children, props) } return documentMetadataTag('meta', children, props, false)}
const newJSXNode = (tag: string, { children, ...props }: PropsWithChildren<unknown>) =>31 unmodified linesmodified:src/jsx/base.ts+1 added-1 removed
325 unmodified lines326 intrinsicElementTags[tag as keyof typeof intrinsicElementTags],327 props,328 children329 )330- } else if (tag === 'svg') {330+ } else if (tag === 'svg' || tag === 'head') {331 nameSpaceContext ||= createContext('')332 return new JSXNode(tag, props, [333 new JSXFunctionNode(334 nameSpaceContext,85 unmodified linesmodified:src/jsx/index.test.tsx+15 added
468 unmodified lines469 const template = <span data-text={escapedString}>Hello</span>470 expect(template.toString()).toBe('<span data-text="<html-escaped-string>">Hello</span>')471 })472 })473+474+ describe('head', () => {475+ it('Simple head elements should be rendered as is', () => {476+ const template = (477+ <head>478+ <title>Hono!</title>479+ <meta name='description' content='A description' />480+ <script src='script.js'></script>481+ </head>482+ )483+ expect(template.toString()).toBe(484+ '<head><title>Hono!</title><meta name="description" content="A description"/><script src="script.js"></script></head>'485+ )486+ })487+ })488 })489 490 describe('className', () => {491 it('should convert to class attribute for intrinsic elements', () => {575 unmodified lines