-
Notifications
You must be signed in to change notification settings - Fork 1k
[labs/ssr] fix server templates dropping top level td elements
#4519
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
Conversation
🦋 Changeset detectedLatest commit: 2c55b58 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
📊 Tachometer Benchmark ResultsSummarynop-update
render
update
update-reflect
Resultsthis-change
render
update
update-reflect
this-change, tip-of-tree, previous-release
render
update
nop-update
this-change, tip-of-tree, previous-release
render
update
this-change, tip-of-tree, previous-release
render
update
update-reflect
|
|
The size of lit-html.js and lit-core.min.js are as expected. |
|
I suspect there's a bit more we can do here with more advanced parts of the parse5 API. Looking at the implementation of import {Parser} from 'parse5';
const parser = Parser.getFragmentParser();
parser.tokenizer.write('<html foo><div></div></html>', true);
// This has the `foo` attribute
console.log(parser.treeAdapter.getFirstChild(parser.document));
// This only has the <div>
console.log(parser.getFragment());I'm not sure yet how to tie this into a fix, but it seems like more of the information is there internally. |
|
I agree Justin! I'll take a look a little more deeply into what parse5 gives us tomorrow. |
|
@justinfagnani I think an inexpensive regex approach is probably going to be the best to capture the couple page level elements. Using |
augustjk
left a comment
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.
Unless we unship the parse() change to regress on the page level element support, I like this is a strict improvement of what we currently have.
Trying to not use parse() all together could be explored on top of this change too.
| * we use a page-level template `parse`. | ||
| */ | ||
| const REGEXP_TEMPLATE_STARTS_WITH_PAGE_TAG = | ||
| /^\s*(<(!doctype|html|head|body))/i; |
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 think this will handle leading comments too:
| /^\s*(<(!doctype|html|head|body))/i; | |
| /^(\s*|<!--.*-->)*(<(!doctype|html|head|body))/is; |
This would need some test coverage, obviously!
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.
Great suggestion. I've updated regex and added a bunch of tests. Things look pretty great with the comment regex addition.
|
@AndrewJakubowicz thanks for looking into it! I think the limitations of using non-fragment should be documented along with the tag. I modified the regex to handle comments (I think) so maybe there wouldn't be any to mention. |
augustjk
left a comment
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.
Thank you!
Co-authored-by: Augustine Kim <[email protected]>
Fixes: #4513
Context
Using
parsefrom parse5 everywhere for server templates is not ideal, and unfortunately has consequences for some cases such as the issue template containing a top leveltdelement. Parsing this as a page level HTML causes the element to be removed (which matches browser behavior). However, we instead want to treat this case as a fragment.Fix
This is a possible fix. I'm not super stoked about it but it does improve the situation.
Basically, there are 3 tags that I could find that
parseFragmentwill not handle as a top-level element:html,head, andbody.Thus, detect those cases and only use
parseif one of these top-level tags is detected. Otherwise useparseFragment.This change also strips off comment nodes.
Test plan
Added a regression test for the template flagged in the issue.