Skip to content

Commit 10f08f7

Browse files
jodarovepmdartus
andauthored
feat: HTML comments for LWC templates (#47)
* feat: HTML comments for LWC templates * Update text/0000-template-comments.md Co-authored-by: Pierre-Marie Dartus <p.dartus@salesforce.com> * wip: review comments * wip: latest from the review * Update text/0000-template-comments.md Co-authored-by: Pierre-Marie Dartus <p.dartus@salesforce.com> Co-authored-by: Pierre-Marie Dartus <p.dartus@salesforce.com>
1 parent ee84f43 commit 10f08f7

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

text/0000-template-comments.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
title: HTML comments for LWC templates
3+
status: DRAFTED
4+
created_at: 2021-03-31
5+
updated_at: 2021-03-31
6+
pr: https://github.com/salesforce/lwc/pull/2263
7+
---
8+
9+
# HTML comments for LWC templates
10+
11+
## Summary
12+
13+
In most cases, HTML comments in templates only add information for other developers. There are some use cases where HTML comments convey some runtime semantic (e.g., MSO comments). As of today, the LWC template compiler strips HTML comments. This RFC proposes a way to preserve HTML comments in LWC templates.
14+
15+
### Basic example
16+
17+
```html
18+
<template lwc:preserve-comments>
19+
<!-- Greeter container -->
20+
<div>
21+
Hello <b>{name}</b>!
22+
</div>
23+
<!-- eof: Greeter container -->
24+
</template>
25+
```
26+
```html
27+
<x-example>
28+
# shadow-root
29+
| <!-- Greeter container -->
30+
| <div>
31+
| Hello <b>world</b>
32+
| </div>
33+
| <!-- eof: Greeter container -->
34+
</x-example>
35+
```
36+
37+
## Motivation
38+
39+
Microsoft Outlook's desktop email client leverage MSO (Microsoft Office) to render their HTML content. Because of this, the support for common HTML and CSS properties is limited, eg: `<div>`s are not supported.
40+
41+
Email developers rely on [MSO conditionals](https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/compatibility/ms537512(v=vs.85)?redirectedfrom=MSDN) to improve the UX of emails opened on Microsoft Outlook.
42+
43+
This RFC introduces a new opt-in mechanism at compile time to preserve HTML comments.
44+
45+
## Proposal
46+
47+
### Compile-time behavior
48+
49+
LWC compilation strips HTML comments because the browsers do not use them at runtime. The bundle's size is smaller by stripping the HTML comments, and the rehydration process does not need to take into account comments nodes.
50+
51+
To preserve the performance of the existing components, we propose to introduce two options to enable comments:
52+
53+
1. A new boolean attribute (`lwc:preserve-comments`) to the root template tag in the component template; `false` by default.
54+
55+
2. A new compile option (`preserveHTMLComments`) in the template compiler; `false` by default.
56+
57+
Both options will make the compiler invoke a runtime API adding the HTML comments in the component template to the output code that runs in the browser/server. Modifications in each environment's renderers will ensure that the comment node/text is present on the resulting HTML.
58+
59+
#### Invariants
60+
61+
* When option 1 is present, HTML comments will be enabled regardless of the value of option 2.
62+
63+
* When option 2 is set to true while compiling a LWC template, HTML comments will be enabled, regardless of the presence of option 1.
64+
65+
### Runtime behavior
66+
67+
A new API "`[co]`mment" will be added to the engine/core. The `co(string commentText): VNode` will receive the comment text and return a Comment vnode.
68+
69+
During rendering cycles, the LWC engine diffing algorithm will show/hide the comment based on the directive containing it (if:true/false, for:each, etc.)
70+
71+
#### Invariants
72+
73+
* HTML comments will be enabled based on the opt-in mechanism, either [option 1 or 2](#compile-time-behavior).
74+
* HTML comments will not support bindings (`{foo}`). Note: if you need to render some dynamic content in your comment, you may use the [lwc:inner-html directive](https://github.com/salesforce/lwc-rfcs/pull/15/files?short_path=e9f0a56#diff-e9f0a56e79f3840c76e53b10b430f4cd21be3e4bda91f2d41856fd151e525e93).
75+
* When enabled, comments must be rendered in engine/dom and engine/server.
76+
* HTML comments will behave the same way as other nodes with respect to slots and directives (`if:true`, `for:each`, etc.).
77+
78+
### Backwards Compatible
79+
80+
This feature is backward compatible because is gated by the new opt-in mechanism.
81+
82+
### Prior art
83+
84+
* React: [dangerouslySetInnerHTML attribute](https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml).
85+
* VueJS: [comments option](https://vuejs.org/v2/api/#comments).
86+
* Svelte: [preserveComments compile option](https://svelte.dev/docs#svelte_compile)
87+
88+
### How we teach this
89+
90+
We must update the documentation to reflect this opt-in mechanism.

0 commit comments

Comments
 (0)