Repository navigation
fix(compiler-ssr): map DOM prop aliases for dynamic bindings #16343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,6 +79,32 @@ const tagSpecificDOMProps: Record<string, string | undefined> = { | |
| meta: 'httpEquiv', | ||
| } | ||
|
|
||
| /** | ||
| * Resolve the attribute name a DOM prop alias should be rendered as, e.g. | ||
| * `htmlFor` -> `for`. Only props that actually reflect an attribute on the | ||
| * element are mapped: `htmlFor` is an alias of `for` on `<label>`, but not on | ||
| * `<div>`, and SVG keeps the raw name since its attributes are case sensitive. | ||
| */ | ||
| function resolveDOMPropAlias( | ||
| node: PlainElementNode, | ||
| name: string, | ||
| context: TransformContext, | ||
| ): string | undefined { | ||
| const alias = propsToAttrMap[name] | ||
| if ( | ||
| alias && | ||
| !context.isCustomElement(node.tag) && | ||
| (name === 'className' | ||
| ? node.ns !== Namespaces.SVG | ||
| : node.ns === Namespaces.HTML && | ||
| tagSpecificDOMProps[node.tag] === name) && | ||
| // HTML parsers keep the first occurrence of a repeated attribute. | ||
| !findProp(node, alias, false, true) | ||
| ) { | ||
| return alias | ||
| } | ||
| } | ||
|
|
||
| // static styles on plain elements are rendered by ssrTransformElement | ||
| export const ssrTransformStyle: NodeTransform = (node, context) => { | ||
| if ( | ||
|
|
@@ -339,9 +365,15 @@ export const ssrTransformElement: NodeTransform = (node, context) => { | |
| ), | ||
| ) | ||
| } else if (isSSRSafeAttrName(attrName)) { | ||
| // alias of a DOM prop, e.g. `:htmlFor` on `<label>` | ||
| const alias = resolveDOMPropAlias( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not map aliases on bindings that use For a nested
|
||
| node, | ||
| key.content, | ||
| context, | ||
| ) | ||
|
Comment on lines
+369
to
+373
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Resolve competing bindings after their keys have been normalized. For a nested
|
||
| openTag.push( | ||
| createCallExpression(context.helper(SSR_RENDER_ATTR), [ | ||
| key, | ||
| alias ? JSON.stringify(alias) : key, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the existing For a nested
|
||
| value, | ||
| ]), | ||
| ) | ||
|
|
@@ -374,7 +406,7 @@ export const ssrTransformElement: NodeTransform = (node, context) => { | |
| } | ||
| } else { | ||
| // special case: value on <textarea> | ||
| let name = prop.name | ||
| const name = prop.name | ||
| if (node.tag === 'textarea' && name === 'value' && prop.value) { | ||
| rawChildrenMap.set(node, escapeHtml(prop.value.content)) | ||
| } else if (!needMergeProps) { | ||
|
|
@@ -396,21 +428,8 @@ export const ssrTransformElement: NodeTransform = (node, context) => { | |
| } | ||
| continue | ||
| } | ||
| const mappedName = propsToAttrMap[name] | ||
| if ( | ||
| mappedName && | ||
| (name === 'className' | ||
| ? node.ns !== Namespaces.SVG | ||
| : node.ns === Namespaces.HTML && | ||
| tagSpecificDOMProps[node.tag] === name) && | ||
| !context.isCustomElement(node.tag) && | ||
| // HTML parsers keep the first occurrence of a repeated attribute. | ||
| !findProp(node, mappedName, false, true) | ||
| ) { | ||
| name = mappedName | ||
| } | ||
| openTag.push( | ||
| ` ${name}` + | ||
| ` ${resolveDOMPropAlias(node, name, context) ?? name}` + | ||
| (prop.value ? `="${escapeHtml(prop.value.content)}"` : ``), | ||
| ) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recognize existing HTML attributes written in uppercase.
For
<label :htmlFor="id" FOR="old"/>withid="new",findPropdoes not matchFOR. SSR now emitsfor="new"beforeFOR="old". The HTML parser treats both asforand keepsnew; before this change, it keptold. Compare candidate HTML attribute names using ASCII case folding soFORblocks the mapping just asfordoes. (raw.githubusercontent.com)