lit / lit-element

LEGACY REPO. This repository is for maintenance of the legacy LitElement library. The LitElement base class is now part of the Lit library, which is developed in the lit monorepo.
https://lit-element.polymer-project.org
BSD 3-Clause "New" or "Revised" License
4.49k stars 318 forks source link

Question: Why use comment nodes for marking position? #1120

Closed AugustNagro closed 3 years ago

AugustNagro commented 3 years ago

lit-html currently surrounds Elements it may need to update with two <!----> comment elements.

For example, lets say you have

html`<p>hello ${name}, the count is: ${count}</p>`

Then lit-element is output two comment nodes and four text node. Whenever a data dependency changes, the respective node is re-rendered.

But why the comments? Would it be more expedient to save references to the nodes when you build, instead of using the comments to locate?

justinfagnani commented 3 years ago

We use comments because we need to insert some sigil in the HTML to find expressions, and comments don't interact with HTML parsing quirks like the adoption agency algorithm (what handles <table> etc). We insert comments to serve as the start nodes for expressions mainly to have more consistent setup for parsed parts and dynamically created parts. In lit-html 2.0 we're redicing the number of comment nodes, but this should be a transparent change.

AugustNagro commented 3 years ago

Thanks. I checked out the source over the weekend and see now why markers are needed in the context of the render function. Interesting to know about the adoption agency algorithm.

This and lit-html are cool libraries and I was surprised the source is so small and readable.