Skip to content
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

fix(ssr): correctly render foreign namespace element closing tags #4992

Merged

Conversation

jhefferman-sfdc
Copy link
Contributor

Details

Does this pull request introduce a breaking change?

  • 😮‍💨 No, it does not introduce a breaking change.

Does this pull request introduce an observable change?

  • 🤞 No, it does not introduce an observable change.

GUS work item

W-16871512

const isForeignElement = node.namespace !== HTML_NAMESPACE;
const hasChildren = childContent.length > 0;

if (isForeignElement && !hasChildren) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any non-HTML elements that are not self-closing when empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, or at least I couldn't find any SVG or MathML cases. The spec just says "Foreign elements must either have a start tag and an end tag, or a start tag that is marked as self-closing, in which case they must not have an end tag." but that is not exactly what you are asking.

const hasChildren = childContent.length > 0;

if (isForeignElement && !hasChildren) {
return [bYield(b.literal(`<${node.name}`)), ...yieldAttrsAndProps, bYield(b.literal(`/>`))];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be merged as a ternary in the return statement below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also removed the separate return for void html elements and they return here too. They were also missing the scope class and failed the test once that was added.

@@ -0,0 +1,19 @@
<template>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test cases for class attribute and lwc:inner-html on input or textarea or somthing.

Copy link
Contributor Author

@jhefferman-sfdc jhefferman-sfdc Dec 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding lwc:inner-html to a void element like input throws an exception in v1 (engine-server) and renders nothing so I didn't add it. For v2 it doesn't throw but it doesn't render anything. Should we open an issue or make a change to throw when inner-html is used on void elements? Or is it enough that it is a sort of noop

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should open an issue to make it throw an error. lwc:inner-html doesn't make any sense for void elements, so throwing an error is a good behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sounds good, I opened this issue

@jhefferman-sfdc jhefferman-sfdc requested a review from wjhsf December 4, 2024 00:18
<svg>
<pattern lwc:inner-html={foreignNamespaceInnerHtml}></pattern>
<image xlink:title="title"></image>
</svg>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be great to add a MathML block here for completeness.

Copy link
Contributor Author

@jhefferman-sfdc jhefferman-sfdc Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

including <math> namespace elements passed for ssr-compiler but generates a warning when engine-server fixtures are ran as it appears MathML elements are not recognized as a knownTag by template-compiler -> validateElement. To enable testing MathML for both I suppressed the warning here.

Is that OK or would you rather modify template-compiler -> validateElement to recognize MathML elements, or...?

I suppose it could be argued that the same warning / tag-check should be there for @lwc/ssr-compiler too, or not needed? I'm not sure as tags used in the correct namespace shouldn't really generate a warning, so should we be reproducing this behavior in v2 regardless?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, opened an issue for this: #5010

MathML should be supported.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thanks, seems wrong. I updated the warning suppression in the fixture to reference the issue you opened.

bYield(b.literal(`>`)),
...childContent,
bYield(b.literal(`</${node.name}>`)),
isForeignSelfClosingElement ? bYield(b.literal(`/>`)) : bYield(b.literal(`>`)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
isForeignSelfClosingElement ? bYield(b.literal(`/>`)) : bYield(b.literal(`>`)),
bYield(b.literal(isForeignSelfClosingElement ? `/>` : `>`)),

Copy link
Collaborator

@nolanlawson nolanlawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM :shipit:

code === 'CIRCULAR_DEPENDENCY';
code === 'CIRCULAR_DEPENDENCY' ||
// template-compiler -> index -> validateElement generates UNKNOWN_HTML_TAG_IN_TEMPLATE for MathML elements
message.includes('LWC1123');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is bizarre... we should fix this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, updated the comment with a TODO on that issue.

</mi>
<mo class="lwc-4q9u0sqsfc0">
</mo>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be great to have an example of a self-closing tag in MathML to make sure we do it correctly.

Also &InvisibleTimes; is very confusing here since it renders invisibly. Tricked me into thinking that this should be self-closing when it isn't.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait – this should be self-closing, right? Since it's actually just a comment?

Copy link
Contributor Author

@jhefferman-sfdc jhefferman-sfdc Dec 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait – this should be self-closing, right? Since it's actually just a comment?

I'm not sure... it is actually appearing as an invisible character aka ZWJ (yellow box) in Visual Studio in both the component and the expected output? It passes in both engine-server and ssr-compiler (not to say that means it is correct). It is a confusing example though and I can remove it.

It'd be great to have an example of a self-closing tag in MathML to make sure we do it correctly.

Sorry I didn't add that. Unfortunately we have another engine-server bug here...

  • <plus/> and <plus></plus> is rendered as <plus></plus> by engine-server
  • <plus/> and <plus></plus> is rendered as <plus/> by ssr-compiler (expected output)

Shall I open another issue?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that there is literally an invisible character in there, outside of the comment. Yeah I would switch to something that's visible.

Shall I open another issue?

Yes, I would open a bug for this. All non-HTML childless components should render as self-closing (with the />).

Copy link
Contributor Author

@jhefferman-sfdc jhefferman-sfdc Dec 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sounds good, opened #5015 on engine-server, referenced it in the example and removed the invisible character example

@jhefferman-sfdc jhefferman-sfdc force-pushed the jhefferman/ssr/foreign-element-render-void-fix branch from cf45746 to 8d6d2e4 Compare December 6, 2024 16:51
@jhefferman-sfdc jhefferman-sfdc merged commit ee2edae into master Dec 10, 2024
11 checks passed
@jhefferman-sfdc jhefferman-sfdc deleted the jhefferman/ssr/foreign-element-render-void-fix branch December 10, 2024 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants