-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
refactor(compiler-vapor): skip unnecessary attribute quoting in templates #13673
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: minor
Are you sure you want to change the base?
Changes from all commits
083c084
2fe9c1c
0b5b4ca
3e90f4e
ba9cd1c
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 |
---|---|---|
|
@@ -221,11 +221,30 @@ function transformNativeElement( | |
getEffectIndex, | ||
) | ||
} else { | ||
let needsQuoting = false | ||
|
||
for (const prop of propsResult[1]) { | ||
const { key, values } = prop | ||
if (key.isStatic && values.length === 1 && values[0].isStatic) { | ||
template += ` ${key.content}` | ||
if (values[0].content) template += `="${values[0].content}"` | ||
const value = values[0].content | ||
|
||
if (!needsQuoting) template += ` ` | ||
template += key.content | ||
|
||
if (value) { | ||
// https://html.spec.whatwg.org/multipage/introduction.html#intro-early-example | ||
needsQuoting = /[\s>]|^["'=]/.test(value) | ||
|
||
if (needsQuoting) { | ||
const encoded = value.replace(/"/g, '"') | ||
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. This is a partial fix for I've looked into how Vue compiler deals with entities and it seems that we only ever do decoding (which makes sense for the original compiler, but it seems bad for Vapor where we generate HTML templates, we don't want static HTML code examples to be parsed as anything but text) |
||
|
||
template += `="${encoded}"` | ||
} else { | ||
template += `=${value}` | ||
} | ||
} else { | ||
needsQuoting = false | ||
} | ||
} else { | ||
dynamicProps.push(key.content) | ||
context.registerEffect( | ||
|
Uh oh!
There was an error while loading. Please reload this page.
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.
I've noticed that the browser diverges slightly from the specification here, the backticks and open angled bracket aren't something browser parsers cares about, and single/double quotes only matters if it's at the start of the value.
This can be tested on the console:
Let me know if we should just align to what the specification says, or to add a note that we're diverging and matching the browser.
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.
When you say "the browser" have you tested that that works in ALL browsers? If not then it would be better to follow the spec to avoid breaking things.
Uh oh!
There was an error while loading. Please reload this page.
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.
yes, works fine in Gecko, WebKit and Blink. I can't test in Ladybird and Servo at the moment but I'd be surprised if it doesn't. this seems like one of those implementation-level quirk that ends up getting reimplemented across browsers, either that or this quirk is actually documented elsewhere and not on the page I referenced
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.
This behavior is not directly tested by WPT, but they pull some tests from html5lib which has (among the tests not included in WPT at this time) tests for these quirks, which seem to indicate that it's invalid but tokenized properly (and therefore likely to gracefully be accepted by browsers as-is). https://github.com/html5lib/html5lib-tests/blob/a9f44960a9fedf265093d22b2aa3c7ca123727b9/tokenizer/test3.test#L10461-L10466
While it's not as convincing as it could be given the fact it's not pulled by WPT, Servo does uses them