@mantou/gem 2.1.1 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/dist/gem.es.js +2082 -2153
  2. package/dist/gem.umd.js +25 -199
  3. package/elements/base/dialog.d.ts +1 -5
  4. package/elements/base/dialog.d.ts.map +1 -1
  5. package/elements/base/dialog.js +2 -6
  6. package/elements/base/dialog.js.map +1 -1
  7. package/elements/base/gesture.d.ts +6 -15
  8. package/elements/base/gesture.d.ts.map +1 -1
  9. package/elements/base/gesture.js +38 -34
  10. package/elements/base/gesture.js.map +1 -1
  11. package/elements/base/link.d.ts +1 -17
  12. package/elements/base/link.d.ts.map +1 -1
  13. package/elements/base/link.js +12 -37
  14. package/elements/base/link.js.map +1 -1
  15. package/elements/base/modal-factory.d.ts +48 -38
  16. package/elements/base/modal-factory.d.ts.map +1 -1
  17. package/elements/base/modal-factory.js +14 -14
  18. package/elements/base/modal-factory.js.map +1 -1
  19. package/elements/base/reflect.d.ts +1 -5
  20. package/elements/base/reflect.d.ts.map +1 -1
  21. package/elements/base/reflect.js +8 -13
  22. package/elements/base/reflect.js.map +1 -1
  23. package/elements/base/route.d.ts +17 -17
  24. package/elements/base/route.d.ts.map +1 -1
  25. package/elements/base/route.js +15 -27
  26. package/elements/base/route.js.map +1 -1
  27. package/elements/base/title.d.ts +1 -6
  28. package/elements/base/title.d.ts.map +1 -1
  29. package/elements/base/title.js +3 -8
  30. package/elements/base/title.js.map +1 -1
  31. package/elements/base/unsafe.d.ts +1 -6
  32. package/elements/base/unsafe.d.ts.map +1 -1
  33. package/elements/base/unsafe.js +3 -9
  34. package/elements/base/unsafe.js.map +1 -1
  35. package/elements/base/use.d.ts +1 -4
  36. package/elements/base/use.d.ts.map +1 -1
  37. package/elements/base/use.js +4 -11
  38. package/elements/base/use.js.map +1 -1
  39. package/elements/link.js +1 -1
  40. package/elements/link.js.map +1 -1
  41. package/elements/route.js +1 -1
  42. package/elements/route.js.map +1 -1
  43. package/helper/hmr.d.ts +11 -0
  44. package/helper/hmr.d.ts.map +1 -0
  45. package/helper/hmr.js +255 -0
  46. package/helper/hmr.js.map +1 -0
  47. package/helper/i18n.d.ts +2 -2
  48. package/helper/i18n.d.ts.map +1 -1
  49. package/helper/i18n.js +5 -4
  50. package/helper/i18n.js.map +1 -1
  51. package/helper/logger.d.ts +1 -1
  52. package/helper/logger.d.ts.map +1 -1
  53. package/helper/logger.js +8 -5
  54. package/helper/logger.js.map +1 -1
  55. package/helper/mediaquery.js +1 -1
  56. package/helper/mediaquery.js.map +1 -1
  57. package/helper/ssr-shim.d.ts.map +1 -1
  58. package/helper/ssr-shim.js +2 -0
  59. package/helper/ssr-shim.js.map +1 -1
  60. package/helper/theme.d.ts +4 -5
  61. package/helper/theme.d.ts.map +1 -1
  62. package/helper/theme.js +3 -3
  63. package/helper/theme.js.map +1 -1
  64. package/index.d.ts +1 -3
  65. package/index.d.ts.map +1 -1
  66. package/index.js +1 -4
  67. package/index.js.map +1 -1
  68. package/lib/decorators.d.ts +9 -16
  69. package/lib/decorators.d.ts.map +1 -1
  70. package/lib/decorators.js +84 -71
  71. package/lib/decorators.js.map +1 -1
  72. package/lib/directive.d.ts +65 -0
  73. package/lib/directive.d.ts.map +1 -0
  74. package/lib/directive.js +58 -0
  75. package/lib/directive.js.map +1 -0
  76. package/lib/element.d.ts +10 -153
  77. package/lib/element.d.ts.map +1 -1
  78. package/lib/element.js +19 -480
  79. package/lib/element.js.map +1 -1
  80. package/lib/history.d.ts +67 -9
  81. package/lib/history.d.ts.map +1 -1
  82. package/lib/history.js +12 -12
  83. package/lib/history.js.map +1 -1
  84. package/lib/lit-html.d.ts +232 -0
  85. package/lib/lit-html.d.ts.map +1 -0
  86. package/lib/lit-html.js +987 -0
  87. package/lib/lit-html.js.map +1 -0
  88. package/lib/reactive.d.ts +123 -0
  89. package/lib/reactive.d.ts.map +1 -0
  90. package/lib/reactive.js +478 -0
  91. package/lib/reactive.js.map +1 -0
  92. package/lib/repeat.d.ts +126 -0
  93. package/lib/repeat.d.ts.map +1 -0
  94. package/lib/repeat.js +533 -0
  95. package/lib/repeat.js.map +1 -0
  96. package/lib/store.d.ts +13 -2
  97. package/lib/store.d.ts.map +1 -1
  98. package/lib/store.js +5 -4
  99. package/lib/store.js.map +1 -1
  100. package/lib/utils.d.ts +24 -15
  101. package/lib/utils.d.ts.map +1 -1
  102. package/lib/utils.js +25 -30
  103. package/lib/utils.js.map +1 -1
  104. package/lib/version.d.ts +1 -1
  105. package/lib/version.js +1 -1
  106. package/package.json +8 -9
  107. package/src/dist.ts +6 -8
  108. package/src/elements/base/dialog.ts +2 -6
  109. package/src/elements/base/gesture.ts +35 -36
  110. package/src/elements/base/link.ts +20 -46
  111. package/src/elements/base/modal-factory.ts +14 -14
  112. package/src/elements/base/reflect.ts +2 -6
  113. package/src/elements/base/route.ts +22 -33
  114. package/src/elements/base/title.ts +4 -9
  115. package/src/elements/base/unsafe.ts +4 -9
  116. package/src/elements/base/use.ts +4 -11
  117. package/src/elements/link.ts +1 -1
  118. package/src/elements/route.ts +1 -1
  119. package/src/helper/hmr.ts +305 -0
  120. package/src/helper/i18n.ts +7 -7
  121. package/src/helper/logger.ts +9 -5
  122. package/src/helper/mediaquery.ts +1 -1
  123. package/src/helper/ssr-shim.ts +3 -0
  124. package/src/helper/theme.ts +6 -6
  125. package/src/index.ts +1 -5
  126. package/src/lib/decorators.ts +101 -100
  127. package/src/lib/directive.ts +137 -0
  128. package/src/lib/element.ts +26 -614
  129. package/src/lib/history.ts +14 -15
  130. package/src/lib/lit-html.ts +1269 -0
  131. package/src/lib/reactive.ts +596 -0
  132. package/src/lib/repeat.ts +580 -0
  133. package/src/lib/store.ts +5 -4
  134. package/src/lib/utils.ts +40 -46
  135. package/src/lib/version.ts +1 -1
  136. package/src/test/elements/link.test.ts +2 -3
  137. package/src/test/elements/reflect.test.ts +13 -3
  138. package/src/test/elements/title.test.ts +2 -3
  139. package/src/test/gem-element/advance.test.ts +22 -14
  140. package/src/test/gem-element/basic.test.ts +23 -11
  141. package/src/test/gem-element/decorators.test.ts +14 -17
  142. package/src/test/gem-element/multi.test.ts +20 -11
  143. package/src/test/history.test.ts +1 -2
  144. package/src/test/store.test.ts +3 -4
  145. package/src/test/utils.test.ts +12 -14
  146. package/src/test/utils.ts +22 -0
  147. package/lib/directives.d.ts +0 -4
  148. package/lib/directives.d.ts.map +0 -1
  149. package/lib/directives.js +0 -5
  150. package/lib/directives.js.map +0 -1
  151. package/src/lib/directives.ts +0 -6
@@ -0,0 +1,987 @@
1
+ /**
2
+ * @license
3
+ * Copyright 2017 Google LLC
4
+ * SPDX-License-Identifier: BSD-3-Clause
5
+ */
6
+ const global = globalThis;
7
+ const trustedTypes = global.trustedTypes;
8
+ const policy = trustedTypes?.createPolicy('lit-html', { createHTML: (s) => s });
9
+ // Added to an attribute name to mark the attribute as bound so we can find
10
+ // it easily.
11
+ const boundAttributeSuffix = '$lit$';
12
+ // This marker is used in many syntactic positions in HTML, so it must be
13
+ // a valid element name and attribute name. We don't support dynamic names (yet)
14
+ // but this at least ensures that the parse tree is closer to the template
15
+ // intention.
16
+ const marker = `lit$${Math.random().toFixed(9).slice(2)}$`;
17
+ // String used to tell if a comment is a marker comment
18
+ const markerMatch = '?' + marker;
19
+ // Text used to insert a comment marker node. We use processing instruction
20
+ // syntax because it's slightly smaller, but parses as a comment node.
21
+ const nodeMarker = `<${markerMatch}>`;
22
+ const d = document;
23
+ // Creates a dynamic marker. We never have to search for these in the DOM.
24
+ const createMarker = () => d.createComment('');
25
+ const isPrimitive = (value) => value === null || (typeof value !== 'object' && typeof value !== 'function');
26
+ const isNullish = (value) => value === null || value === undefined;
27
+ const isIterable = (value) => Symbol.iterator in value;
28
+ const SPACE_CHAR = `[ \t\n\f\r]`;
29
+ const ATTR_VALUE_CHAR = `[^ \t\n\f\r"'\`<>=]`;
30
+ const NAME_CHAR = `[^\\s"'>=/]`;
31
+ // These regexes represent the five parsing states that we care about in the
32
+ // Template's HTML scanner. They match the *end* of the state they're named
33
+ // after.
34
+ // Depending on the match, we transition to a new state. If there's no match,
35
+ // we stay in the same state.
36
+ // Note that the regexes are stateful. We utilize lastIndex and sync it
37
+ // across the multiple regexes used. In addition to the five regexes below
38
+ // we also dynamically create a regex to find the matching end tags for raw
39
+ // text elements.
40
+ /**
41
+ * End of text is: `<` followed by:
42
+ * (comment start) or (tag) or (dynamic tag binding)
43
+ */
44
+ const textEndRegex = /<(?:(!--|\/[^a-zA-Z])|(\/?[a-zA-Z][^>\s]*)|(\/?$))/g;
45
+ const COMMENT_START = 1;
46
+ const TAG_NAME = 2;
47
+ const DYNAMIC_TAG_NAME = 3;
48
+ const commentEndRegex = /-->/g;
49
+ /**
50
+ * Comments not started with <!--, like </{, can be ended by a single `>`
51
+ */
52
+ const comment2EndRegex = />/g;
53
+ /**
54
+ * The tagEnd regex matches the end of the "inside an opening" tag syntax
55
+ * position. It either matches a `>`, an attribute-like sequence, or the end
56
+ * of the string after a space (attribute-name position ending).
57
+ *
58
+ * See attributes in the HTML spec:
59
+ * https://www.w3.org/TR/html5/syntax.html#elements-attributes
60
+ *
61
+ * " \t\n\f\r" are HTML space characters:
62
+ * https://infra.spec.whatwg.org/#ascii-whitespace
63
+ *
64
+ * So an attribute is:
65
+ * * The name: any character except a whitespace character, ("), ('), ">",
66
+ * "=", or "/". Note: this is different from the HTML spec which also excludes control characters.
67
+ * * Followed by zero or more space characters
68
+ * * Followed by "="
69
+ * * Followed by zero or more space characters
70
+ * * Followed by:
71
+ * * Any character except space, ('), ("), "<", ">", "=", (`), or
72
+ * * (") then any non-("), or
73
+ * * (') then any non-(')
74
+ */
75
+ const tagEndRegex = new RegExp(`>|${SPACE_CHAR}(?:(${NAME_CHAR}+)(${SPACE_CHAR}*=${SPACE_CHAR}*(?:${ATTR_VALUE_CHAR}|("|')|))|$)`, 'g');
76
+ const ENTIRE_MATCH = 0;
77
+ const ATTRIBUTE_NAME = 1;
78
+ const SPACES_AND_EQUALS = 2;
79
+ const QUOTE_CHAR = 3;
80
+ const singleQuoteAttrEndRegex = /'/g;
81
+ const doubleQuoteAttrEndRegex = /"/g;
82
+ /**
83
+ * Matches the raw text elements.
84
+ *
85
+ * Comments are not parsed within raw text elements, so we need to search their
86
+ * text content for marker strings.
87
+ */
88
+ const rawTextElement = /^(?:script|style|textarea|title)$/i;
89
+ /** TemplateResult types */
90
+ const HTML_RESULT = 1;
91
+ const SVG_RESULT = 2;
92
+ const MATHML_RESULT = 3;
93
+ // TemplatePart types
94
+ // IMPORTANT: these must match the values in PartType
95
+ const ATTRIBUTE_PART = 1;
96
+ const CHILD_PART = 2;
97
+ const PROPERTY_PART = 3;
98
+ const BOOLEAN_ATTRIBUTE_PART = 4;
99
+ const EVENT_PART = 5;
100
+ const ELEMENT_PART = 6;
101
+ const COMMENT_PART = 7;
102
+ const V_IF = 'v-if';
103
+ const V_ELSE_IF = 'v-else-if';
104
+ const V_ELSE = 'v-else';
105
+ export class TemplateResult {
106
+ type;
107
+ strings;
108
+ values;
109
+ constructor(type, strings, values) {
110
+ this.type = type;
111
+ this.strings = strings;
112
+ this.values = values;
113
+ }
114
+ }
115
+ const tag = (type) => (strings, ...values) => {
116
+ return new TemplateResult(type, strings, values);
117
+ };
118
+ export const html = tag(HTML_RESULT);
119
+ export const svg = tag(SVG_RESULT);
120
+ export const mathml = tag(MATHML_RESULT);
121
+ // for directive
122
+ export const noChange = Symbol.for('lit-noChange');
123
+ const templateCache = new WeakMap();
124
+ const walker = d.createTreeWalker(d, 129 /* NodeFilter.SHOW_{ELEMENT|COMMENT} */);
125
+ function trustFromTemplateString(tsa, stringFromTSA) {
126
+ if (!Array.isArray(tsa))
127
+ throw new Error('invalid template');
128
+ return policy !== undefined ? policy.createHTML(stringFromTSA) : stringFromTSA;
129
+ }
130
+ /**
131
+ * Returns an HTML string for the given TemplateStringsArray and result type
132
+ * (HTML or SVG), along with the case-sensitive bound attribute names in
133
+ * template order. The HTML contains comment markers denoting the `ChildPart`s
134
+ * and suffixes on bound attributes denoting the `AttributeParts`.
135
+ *
136
+ * @param strings template strings array
137
+ * @param type HTML or SVG
138
+ * @return Array containing `[html, attrNames]` (array returned for terseness,
139
+ * to avoid object fields since this code is shared with non-minified SSR
140
+ * code)
141
+ */
142
+ const getTemplateHtml = (strings, type) => {
143
+ // Insert makers into the template HTML to represent the position of
144
+ // bindings. The following code scans the template strings to determine the
145
+ // syntactic position of the bindings. They can be in text position, where
146
+ // we insert an HTML comment, attribute value position, where we insert a
147
+ // sentinel string and re-write the attribute name, or inside a tag where
148
+ // we insert the sentinel string.
149
+ const l = strings.length - 1;
150
+ // Stores the case-sensitive bound attribute names in the order of their
151
+ // parts. ElementParts are also reflected in this array as undefined
152
+ // rather than a string, to disambiguate from attribute bindings.
153
+ const attrNames = [];
154
+ let htmlStr = type === SVG_RESULT ? '<svg>' : type === MATHML_RESULT ? '<math>' : '';
155
+ // When we're inside a raw text tag (not it's text content), the regex
156
+ // will still be tagRegex so we can find attributes, but will switch to
157
+ // this regex when the tag ends.
158
+ let rawTextEndRegex;
159
+ // The current parsing state, represented as a reference to one of the
160
+ // regexes
161
+ let regex = textEndRegex;
162
+ for (let i = 0; i < l; i++) {
163
+ const s = strings[i];
164
+ // The index of the end of the last attribute name. When this is
165
+ // positive at end of a string, it means we're in an attribute value
166
+ // position and need to rewrite the attribute name.
167
+ // We also use a special value of -2 to indicate that we encountered
168
+ // the end of a string in attribute name position.
169
+ let attrNameEndIndex = -1;
170
+ let attrName;
171
+ let lastIndex = 0;
172
+ let match;
173
+ // The conditions in this loop handle the current parse state, and the
174
+ // assignments to the `regex` variable are the state transitions.
175
+ while (lastIndex < s.length) {
176
+ // Make sure we start searching from where we previously left off
177
+ regex.lastIndex = lastIndex;
178
+ match = regex.exec(s);
179
+ if (match === null) {
180
+ break;
181
+ }
182
+ lastIndex = regex.lastIndex;
183
+ if (regex === textEndRegex) {
184
+ if (match[COMMENT_START] === '!--') {
185
+ regex = commentEndRegex;
186
+ }
187
+ else if (match[COMMENT_START] !== undefined) {
188
+ // We started a weird comment, like </{
189
+ regex = comment2EndRegex;
190
+ }
191
+ else if (match[TAG_NAME] !== undefined) {
192
+ if (rawTextElement.test(match[TAG_NAME])) {
193
+ // Record if we encounter a raw-text element. We'll switch to
194
+ // this regex at the end of the tag.
195
+ rawTextEndRegex = new RegExp(`</${match[TAG_NAME]}`, 'g');
196
+ }
197
+ regex = tagEndRegex;
198
+ }
199
+ else if (match[DYNAMIC_TAG_NAME] !== undefined) {
200
+ regex = tagEndRegex;
201
+ }
202
+ }
203
+ else if (regex === tagEndRegex) {
204
+ if (match[ENTIRE_MATCH] === '>') {
205
+ // End of a tag. If we had started a raw-text element, use that
206
+ // regex
207
+ regex = rawTextEndRegex ?? textEndRegex;
208
+ // We may be ending an unquoted attribute value, so make sure we
209
+ // clear any pending attrNameEndIndex
210
+ attrNameEndIndex = -1;
211
+ }
212
+ else if (match[ATTRIBUTE_NAME] === undefined) {
213
+ // Attribute name position
214
+ attrNameEndIndex = -2;
215
+ }
216
+ else {
217
+ attrNameEndIndex = regex.lastIndex - match[SPACES_AND_EQUALS].length;
218
+ attrName = match[ATTRIBUTE_NAME];
219
+ regex =
220
+ match[QUOTE_CHAR] === undefined
221
+ ? tagEndRegex
222
+ : match[QUOTE_CHAR] === '"'
223
+ ? doubleQuoteAttrEndRegex
224
+ : singleQuoteAttrEndRegex;
225
+ }
226
+ }
227
+ else if (regex === doubleQuoteAttrEndRegex || regex === singleQuoteAttrEndRegex) {
228
+ regex = tagEndRegex;
229
+ }
230
+ else if (regex === commentEndRegex || regex === comment2EndRegex) {
231
+ regex = textEndRegex;
232
+ }
233
+ else {
234
+ // Not one of the five state regexes, so it must be the dynamically
235
+ // created raw text regex and we're at the close of that element.
236
+ regex = tagEndRegex;
237
+ rawTextEndRegex = undefined;
238
+ }
239
+ }
240
+ // We have four cases:
241
+ // 1. We're in text position, and not in a raw text element
242
+ // (regex === textEndRegex): insert a comment marker.
243
+ // 2. We have a non-negative attrNameEndIndex which means we need to
244
+ // rewrite the attribute name to add a bound attribute suffix.
245
+ // 3. We're at the non-first binding in a multi-binding attribute, use a
246
+ // plain marker.
247
+ // 4. We're somewhere else inside the tag. If we're in attribute name
248
+ // position (attrNameEndIndex === -2), add a sequential suffix to
249
+ // generate a unique attribute name.
250
+ // Detect a binding next to self-closing tag end and insert a space to
251
+ // separate the marker from the tag end:
252
+ const end = regex === tagEndRegex && strings[i + 1].startsWith('/>') ? ' ' : '';
253
+ htmlStr +=
254
+ regex === textEndRegex
255
+ ? s + nodeMarker
256
+ : attrNameEndIndex >= 0
257
+ ? (attrNames.push(attrName),
258
+ s.slice(0, attrNameEndIndex) + boundAttributeSuffix + s.slice(attrNameEndIndex)) +
259
+ marker +
260
+ end
261
+ : s + marker + (attrNameEndIndex === -2 ? i : end);
262
+ }
263
+ const htmlResult = htmlStr + (strings[l] || '<?>') + (type === SVG_RESULT ? '</svg>' : type === MATHML_RESULT ? '</math>' : '');
264
+ // Returned as an array for terseness
265
+ return [trustFromTemplateString(strings, htmlResult), attrNames];
266
+ };
267
+ class Template {
268
+ /** @internal */
269
+ el;
270
+ parts = [];
271
+ constructor({ strings, type }, options) {
272
+ let node;
273
+ let nodeIndex = 0;
274
+ let attrNameIndex = 0;
275
+ const partCount = strings.length - 1;
276
+ const parts = this.parts;
277
+ // Create template element
278
+ const [trustedHtml, attrNames] = getTemplateHtml(strings, type);
279
+ this.el = Template.createElement(trustedHtml, options);
280
+ walker.currentNode = this.el.content;
281
+ // Re-parent SVG or MathML nodes into template root
282
+ if (type === SVG_RESULT || type === MATHML_RESULT) {
283
+ const wrapper = this.el.content.firstChild;
284
+ wrapper.replaceWith(...wrapper.childNodes);
285
+ }
286
+ // Walk the template to find binding markers and create TemplateParts
287
+ while ((node = walker.nextNode()) !== null && parts.length < partCount) {
288
+ if (node.nodeType === 1) {
289
+ // TODO (justinfagnani): for attempted dynamic tag names, we don't
290
+ // increment the bindingIndex, and it'll be off by 1 in the element
291
+ // and off by two after it.
292
+ if (node.hasAttributes()) {
293
+ for (const name of node.getAttributeNames()) {
294
+ if (name.endsWith(boundAttributeSuffix)) {
295
+ const realName = attrNames[attrNameIndex++];
296
+ const value = node.getAttribute(name);
297
+ const statics = value.split(marker);
298
+ const m = /([.?@])?(.*)/.exec(realName);
299
+ parts.push({
300
+ type: ATTRIBUTE_PART,
301
+ index: nodeIndex,
302
+ name: m[2],
303
+ strings: statics,
304
+ ctor: m[1] === '.'
305
+ ? PropertyPart
306
+ : m[1] === '?'
307
+ ? BooleanAttributePart
308
+ : m[1] === '@'
309
+ ? EventPart
310
+ : AttributePart,
311
+ });
312
+ node.removeAttribute(name);
313
+ }
314
+ else if (name.startsWith(marker)) {
315
+ parts.push({
316
+ type: ELEMENT_PART,
317
+ index: nodeIndex,
318
+ });
319
+ node.removeAttribute(name);
320
+ }
321
+ }
322
+ }
323
+ // TODO (justinfagnani): benchmark the regex against testing for each
324
+ // of the 3 raw text element names.
325
+ if (rawTextElement.test(node.tagName)) {
326
+ // For raw text elements we need to split the text content on
327
+ // markers, create a Text node for each segment, and create
328
+ // a TemplatePart for each marker.
329
+ const stringList = node.textContent.split(marker);
330
+ const lastIndex = stringList.length - 1;
331
+ if (lastIndex > 0) {
332
+ node.textContent = trustedTypes ? trustedTypes.emptyScript : '';
333
+ // Generate a new text node for each literal section
334
+ // These nodes are also used as the markers for node parts
335
+ // We can't use empty text nodes as markers because they're
336
+ // normalized when cloning in IE (could simplify when
337
+ // IE is no longer supported)
338
+ for (let i = 0; i < lastIndex; i++) {
339
+ node.append(stringList[i], createMarker());
340
+ // Walk past the marker node we just added
341
+ walker.nextNode();
342
+ parts.push({ type: CHILD_PART, index: ++nodeIndex });
343
+ }
344
+ // Note because this marker is added after the walker's current
345
+ // node, it will be walked to in the outer loop (and ignored), so
346
+ // we don't need to adjust nodeIndex here
347
+ node.append(stringList[lastIndex], createMarker());
348
+ }
349
+ }
350
+ }
351
+ else if (node.nodeType === 8) {
352
+ const data = node.data;
353
+ if (data === markerMatch) {
354
+ parts.push({ type: CHILD_PART, index: nodeIndex });
355
+ }
356
+ else {
357
+ let i = -1;
358
+ while ((i = node.data.indexOf(marker, i + 1)) !== -1) {
359
+ // Comment node has a binding marker inside, make an inactive part
360
+ // The binding won't work, but subsequent bindings will
361
+ parts.push({ type: COMMENT_PART, index: nodeIndex });
362
+ // Move to the end of the match
363
+ i += marker.length - 1;
364
+ }
365
+ }
366
+ }
367
+ nodeIndex++;
368
+ }
369
+ }
370
+ // Overridden via `litHtmlPolyfillSupport` to provide platform support.
371
+ /** @nocollapse */
372
+ static createElement(trustedHtml, _options) {
373
+ const el = d.createElement('template');
374
+ el.innerHTML = trustedHtml;
375
+ return el;
376
+ }
377
+ }
378
+ function resolveDirective(part, value, parent = part, attributeIndex) {
379
+ // Bail early if the value is explicitly noChange. Note, this means any
380
+ // nested directive is still attached and is not run.
381
+ if (value === noChange) {
382
+ return value;
383
+ }
384
+ let currentDirective = attributeIndex !== undefined
385
+ ? parent.__directives?.[attributeIndex]
386
+ : parent.__directive;
387
+ const nextDirectiveConstructor = isPrimitive(value)
388
+ ? undefined
389
+ : // This property needs to remain unminified.
390
+ value._$litDirective$;
391
+ if (currentDirective?.constructor !== nextDirectiveConstructor) {
392
+ // This property needs to remain unminified.
393
+ currentDirective?._$notifyDirectiveConnectionChanged?.(false);
394
+ if (nextDirectiveConstructor === undefined) {
395
+ currentDirective = undefined;
396
+ }
397
+ else {
398
+ currentDirective = new nextDirectiveConstructor(part);
399
+ currentDirective._$initialize(part, parent, attributeIndex);
400
+ }
401
+ if (attributeIndex !== undefined) {
402
+ (parent.__directives ??= [])[attributeIndex] = currentDirective;
403
+ }
404
+ else {
405
+ parent.__directive = currentDirective;
406
+ }
407
+ }
408
+ if (currentDirective !== undefined) {
409
+ value = resolveDirective(part, currentDirective._$resolve(part, value.values), currentDirective, attributeIndex);
410
+ }
411
+ return value;
412
+ }
413
+ /**
414
+ * An updateable instance of a Template. Holds references to the Parts used to
415
+ * update the template instance.
416
+ */
417
+ class TemplateInstance {
418
+ _$template;
419
+ _$parts = [];
420
+ /** @internal */
421
+ _$conditions = [];
422
+ /** @internal */
423
+ _$parent;
424
+ /** @internal */
425
+ _$disconnectableChildren = undefined;
426
+ constructor(template, parent) {
427
+ this._$template = template;
428
+ this._$parent = parent;
429
+ }
430
+ // Called by ChildPart parentNode getter
431
+ get parentNode() {
432
+ return this._$parent.parentNode;
433
+ }
434
+ // See comment in Disconnectable interface for why this is a getter
435
+ get _$isConnected() {
436
+ return this._$parent._$isConnected;
437
+ }
438
+ // This method is separate from the constructor because we need to return a
439
+ // DocumentFragment and we don't want to hold onto it with an instance field.
440
+ _clone(options) {
441
+ const { el: { content }, parts, } = this._$template;
442
+ const ifMap = new Map();
443
+ const elseIfMap = new Map();
444
+ const fragment = (options?.creationScope ?? d).importNode(content, true);
445
+ walker.currentNode = fragment;
446
+ let node = walker.nextNode();
447
+ let nodeIndex = 0;
448
+ let partIndex = 0;
449
+ let templatePart = parts[0];
450
+ while (templatePart !== undefined) {
451
+ if (nodeIndex === templatePart.index) {
452
+ let part;
453
+ if (templatePart.type === CHILD_PART) {
454
+ part = new ChildPart(node, node.nextSibling, this, options);
455
+ }
456
+ else if (templatePart.type === ATTRIBUTE_PART) {
457
+ templatePart.name === V_IF && ifMap.set(node, partIndex);
458
+ templatePart.name === V_ELSE_IF && elseIfMap.set(node, partIndex);
459
+ part = new templatePart.ctor(node, templatePart.name, templatePart.strings, this, options);
460
+ }
461
+ else if (templatePart.type === ELEMENT_PART) {
462
+ part = new ElementPart(node, this, options);
463
+ }
464
+ this._$parts.push(part);
465
+ templatePart = parts[++partIndex];
466
+ }
467
+ if (nodeIndex !== templatePart?.index) {
468
+ node = walker.nextNode();
469
+ nodeIndex++;
470
+ }
471
+ }
472
+ ifMap.forEach((idx, element) => {
473
+ const chain = new ConditionChain(element, idx);
474
+ this._$conditions.push(chain);
475
+ let ele = element;
476
+ while ((ele = ele.nextElementSibling)) {
477
+ const pos = elseIfMap.get(ele);
478
+ if (pos === undefined)
479
+ break;
480
+ chain.push(ele, pos);
481
+ }
482
+ });
483
+ // We need to set the currentNode away from the cloned tree so that we
484
+ // don't hold onto the tree even if the tree is detached and should be
485
+ // freed.
486
+ walker.currentNode = d;
487
+ return fragment;
488
+ }
489
+ _update(values) {
490
+ let i = 0;
491
+ for (const part of this._$parts) {
492
+ if (part !== undefined) {
493
+ if (part.strings !== undefined) {
494
+ part._$setValue(values, part, i);
495
+ // The number of values the part consumes is part.strings.length - 1
496
+ // since values are in between template spans. We increment i by 1
497
+ // later in the loop, so increment it by part.strings.length - 2 here
498
+ i += part.strings.length - 2;
499
+ }
500
+ else if (part.name === V_IF || part.name === V_ELSE_IF) {
501
+ }
502
+ else {
503
+ part._$setValue(values[i]);
504
+ }
505
+ }
506
+ i++;
507
+ }
508
+ this._$conditions.forEach((e) => e.update(values));
509
+ }
510
+ }
511
+ class ConditionChain {
512
+ mark;
513
+ list;
514
+ constructor(ele, idx) {
515
+ const mark = createMarker();
516
+ ele.before(mark);
517
+ this.mark = mark;
518
+ this.list = [{ ele, idx }];
519
+ this.check();
520
+ }
521
+ get next() {
522
+ return this.list.at(-1).ele.nextElementSibling;
523
+ }
524
+ push(ele, idx) {
525
+ this.list.push({ ele, idx });
526
+ this.check();
527
+ }
528
+ check() {
529
+ const next = this.next;
530
+ if (next?.attributes.getNamedItem(V_ELSE)) {
531
+ this.list.push({ ele: next });
532
+ next.removeAttribute(V_ELSE);
533
+ }
534
+ }
535
+ update(values) {
536
+ const { mark, list } = this;
537
+ const show = list.find(({ idx }) => idx === undefined || !!values[idx]);
538
+ if (show && !show.ele.parentNode)
539
+ mark.after(show.ele);
540
+ list.forEach(({ ele }) => (!show || ele !== show.ele) && ele.remove());
541
+ }
542
+ }
543
+ export class ChildPart {
544
+ type = CHILD_PART;
545
+ options;
546
+ _$committedValue;
547
+ /** @internal */
548
+ __directive;
549
+ /** @internal */
550
+ _$startNode;
551
+ /** @internal */
552
+ _$endNode;
553
+ /** @internal */
554
+ _$parent;
555
+ /**
556
+ * Connection state for RootParts only (i.e. ChildPart without _$parent
557
+ * returned from top-level `render`). This field is unused otherwise. The
558
+ * intention would be clearer if we made `RootPart` a subclass of `ChildPart`
559
+ * with this field (and a different _$isConnected getter), but the subclass
560
+ * caused a perf regression, possibly due to making call sites polymorphic.
561
+ * @internal
562
+ */
563
+ __isConnected;
564
+ // See comment in Disconnectable interface for why this is a getter
565
+ get _$isConnected() {
566
+ // ChildParts that are not at the root should always be created with a
567
+ // parent; only RootChildNode's won't, so they return the local isConnected
568
+ // state
569
+ return this._$parent?._$isConnected ?? this.__isConnected;
570
+ }
571
+ // The following fields will be patched onto ChildParts when required by
572
+ // AsyncDirective
573
+ /** @internal */
574
+ _$disconnectableChildren = undefined;
575
+ constructor(startNode, endNode, parent, options) {
576
+ this._$startNode = startNode;
577
+ this._$endNode = endNode;
578
+ this._$parent = parent;
579
+ this.options = options;
580
+ // Note __isConnected is only ever accessed on RootParts (i.e. when there is
581
+ // no _$parent); the value on a non-root-part is "don't care", but checking
582
+ // for parent would be more code
583
+ this.__isConnected = options?.isConnected ?? true;
584
+ }
585
+ /**
586
+ * The parent node into which the part renders its content.
587
+ *
588
+ * A ChildPart's content consists of a range of adjacent child nodes of
589
+ * `.parentNode`, possibly bordered by 'marker nodes' (`.startNode` and
590
+ * `.endNode`).
591
+ *
592
+ * - If both `.startNode` and `.endNode` are non-null, then the part's content
593
+ * consists of all siblings between `.startNode` and `.endNode`, exclusively.
594
+ *
595
+ * - If `.startNode` is non-null but `.endNode` is null, then the part's
596
+ * content consists of all siblings following `.startNode`, up to and
597
+ * including the last child of `.parentNode`. If `.endNode` is non-null, then
598
+ * `.startNode` will always be non-null.
599
+ *
600
+ * - If both `.endNode` and `.startNode` are null, then the part's content
601
+ * consists of all child nodes of `.parentNode`.
602
+ */
603
+ get parentNode() {
604
+ let parentNode = this._$startNode.parentNode;
605
+ const parent = this._$parent;
606
+ if (parent !== undefined && parentNode?.nodeType === 11 /* Node.DOCUMENT_FRAGMENT */) {
607
+ // If the parentNode is a DocumentFragment, it may be because the DOM is
608
+ // still in the cloned fragment during initial render; if so, get the real
609
+ // parentNode the part will be committed into by asking the parent.
610
+ parentNode = parent.parentNode;
611
+ }
612
+ return parentNode;
613
+ }
614
+ /**
615
+ * The part's leading marker node, if any. See `.parentNode` for more
616
+ * information.
617
+ */
618
+ get startNode() {
619
+ return this._$startNode;
620
+ }
621
+ /**
622
+ * The part's trailing marker node, if any. See `.parentNode` for more
623
+ * information.
624
+ */
625
+ get endNode() {
626
+ return this._$endNode;
627
+ }
628
+ _$setValue(value, directiveParent = this) {
629
+ // This `ChildPart` has no `parentNode` and therefore cannot accept a value.
630
+ // This likely means the element containing the part was manipulated in an
631
+ // unsupported way outside of Lit's control such that the part's marker
632
+ // nodes were ejected from DOM. For example, setting the element's
633
+ // `innerHTML` or `textContent` can do this.
634
+ if (!this.parentNode)
635
+ return;
636
+ value = resolveDirective(this, value, directiveParent);
637
+ if (isPrimitive(value)) {
638
+ // Non-rendering child values. It's important that these do not render
639
+ // empty text nodes to avoid issues with preventing default <slot>
640
+ // fallback content.
641
+ if (isNullish(value) || value === '') {
642
+ if (!isNullish(this._$committedValue)) {
643
+ this._$clear();
644
+ }
645
+ this._$committedValue = undefined;
646
+ }
647
+ else if (value !== this._$committedValue && value !== noChange) {
648
+ this._commitText(value);
649
+ }
650
+ }
651
+ else if (value instanceof TemplateResult) {
652
+ this._commitTemplateResult(value);
653
+ }
654
+ else if (value.nodeType !== undefined) {
655
+ this._commitNode(value);
656
+ }
657
+ else if (isIterable(value)) {
658
+ this._commitIterable(value);
659
+ }
660
+ else {
661
+ // Fallback, will render the string representation
662
+ this._commitText(value);
663
+ }
664
+ }
665
+ _insert(node) {
666
+ return this._$startNode.parentNode.insertBefore(node, this._$endNode);
667
+ }
668
+ _commitNode(value) {
669
+ if (this._$committedValue !== value) {
670
+ this._$clear();
671
+ this._$committedValue = this._insert(value);
672
+ }
673
+ }
674
+ _commitText(value) {
675
+ // If the committed value is a primitive it means we called _commitText on
676
+ // the previous render, and we know that this._$startNode.nextSibling is a
677
+ // Text node. We can now just replace the text content (.data) of the node.
678
+ if (!isNullish(this._$committedValue) && isPrimitive(this._$committedValue)) {
679
+ const node = this._$startNode.nextSibling;
680
+ node.data = value;
681
+ }
682
+ else {
683
+ this._commitNode(d.createTextNode(value));
684
+ }
685
+ this._$committedValue = value;
686
+ }
687
+ _commitTemplateResult(result) {
688
+ const { values } = result;
689
+ const template = this._$getTemplate(result);
690
+ if (this._$committedValue?._$template === template) {
691
+ this._$committedValue._update(values);
692
+ }
693
+ else {
694
+ const instance = new TemplateInstance(template, this);
695
+ const fragment = instance._clone(this.options);
696
+ instance._update(values);
697
+ this._commitNode(fragment);
698
+ this._$committedValue = instance;
699
+ }
700
+ }
701
+ // Overridden via `litHtmlPolyfillSupport` to provide platform support.
702
+ /** @internal */
703
+ _$getTemplate(result) {
704
+ let template = templateCache.get(result.strings);
705
+ if (template === undefined) {
706
+ templateCache.set(result.strings, (template = new Template(result)));
707
+ }
708
+ return template;
709
+ }
710
+ _commitIterable(value) {
711
+ // For an Iterable, we create a new InstancePart per item, then set its
712
+ // value to the item. This is a little bit of overhead for every item in
713
+ // an Iterable, but it lets us recurse easily and efficiently update Arrays
714
+ // of TemplateResults that will be commonly returned from expressions like:
715
+ // array.map((i) => html`${i}`), by reusing existing TemplateInstances.
716
+ // If value is an array, then the previous render was of an
717
+ // iterable and value will contain the ChildParts from the previous
718
+ // render. If value is not an array, clear this part and make a new
719
+ // array for ChildParts.
720
+ if (!Array.isArray(this._$committedValue)) {
721
+ this._$committedValue = [];
722
+ this._$clear();
723
+ }
724
+ // Lets us keep track of how many items we stamped so we can clear leftover
725
+ // items from a previous render
726
+ const itemParts = this._$committedValue;
727
+ let partIndex = 0;
728
+ let itemPart;
729
+ for (const item of value) {
730
+ if (partIndex === itemParts.length) {
731
+ // If no existing part, create a new one
732
+ // TODO (justinfagnani): test perf impact of always creating two parts
733
+ // instead of sharing parts between nodes
734
+ // https://github.com/lit/lit/issues/1266
735
+ itemParts.push((itemPart = new ChildPart(this._insert(createMarker()), this._insert(createMarker()), this, this.options)));
736
+ }
737
+ else {
738
+ // Reuse an existing part
739
+ itemPart = itemParts[partIndex];
740
+ }
741
+ itemPart._$setValue(item);
742
+ partIndex++;
743
+ }
744
+ if (partIndex < itemParts.length) {
745
+ // itemParts always have end nodes
746
+ this._$clear(itemPart && itemPart._$endNode.nextSibling, partIndex);
747
+ // Truncate the parts array so _value reflects the current state
748
+ itemParts.length = partIndex;
749
+ }
750
+ }
751
+ /**
752
+ * Removes the nodes contained within this Part from the DOM.
753
+ *
754
+ * @param start Start node to clear from, for clearing a subset of the part's
755
+ * DOM (used when truncating iterables)
756
+ * @param from When `start` is specified, the index within the iterable from
757
+ * which ChildParts are being removed, used for disconnecting directives in
758
+ * those Parts.
759
+ *
760
+ * @internal
761
+ */
762
+ _$clear(start = this._$startNode.nextSibling, from) {
763
+ this._$notifyConnectionChanged?.(false, true, from);
764
+ while (start && start !== this._$endNode) {
765
+ const n = start.nextSibling;
766
+ start.remove();
767
+ start = n;
768
+ }
769
+ }
770
+ /**
771
+ * Implementation of RootPart's `isConnected`. Note that this method
772
+ * should only be called on `RootPart`s (the `ChildPart` returned from a
773
+ * top-level `render()` call). It has no effect on non-root ChildParts.
774
+ * @param isConnected Whether to set
775
+ * @internal
776
+ */
777
+ setConnected(isConnected) {
778
+ if (this._$parent === undefined) {
779
+ this.__isConnected = isConnected;
780
+ this._$notifyConnectionChanged?.(isConnected);
781
+ }
782
+ }
783
+ }
784
+ class AttributePart {
785
+ type = ATTRIBUTE_PART;
786
+ element;
787
+ name;
788
+ options;
789
+ /**
790
+ * If this attribute part represents an interpolation, this contains the
791
+ * static strings of the interpolation. For single-value, complete bindings,
792
+ * this is undefined.
793
+ */
794
+ strings;
795
+ /** @internal */
796
+ _$committedValue;
797
+ /** @internal */
798
+ __directives;
799
+ /** @internal */
800
+ _$parent;
801
+ /** @internal */
802
+ _$disconnectableChildren = undefined;
803
+ get tagName() {
804
+ return this.element.tagName;
805
+ }
806
+ // See comment in Disconnectable interface for why this is a getter
807
+ get _$isConnected() {
808
+ return this._$parent._$isConnected;
809
+ }
810
+ constructor(element, name, strings, parent, options) {
811
+ this.element = element;
812
+ this.name = name;
813
+ this._$parent = parent;
814
+ this.options = options;
815
+ if (strings.length > 2 || strings[0] !== '' || strings[1] !== '') {
816
+ this._$committedValue = new Array(strings.length - 1).fill(new String());
817
+ this.strings = strings;
818
+ }
819
+ else {
820
+ this._$committedValue = undefined;
821
+ }
822
+ }
823
+ /**
824
+ * Sets the value of this part by resolving the value from possibly multiple
825
+ * values and static strings and committing it to the DOM.
826
+ * If this part is single-valued, `this._strings` will be undefined, and the
827
+ * method will be called with a single value argument. If this part is
828
+ * multi-value, `this._strings` will be defined, and the method is called
829
+ * with the value array of the part's owning TemplateInstance, and an offset
830
+ * into the value array from which the values should be read.
831
+ * This method is overloaded this way to eliminate short-lived array slices
832
+ * of the template instance values, and allow a fast-path for single-valued
833
+ * parts.
834
+ *
835
+ * @param value The part value, or an array of values for multi-valued parts
836
+ * @param valueIndex the index to start reading values from. `undefined` for
837
+ * single-valued parts
838
+ * @param noCommit causes the part to not commit its value to the DOM. Used
839
+ * in hydration to prime attribute parts with their first-rendered value,
840
+ * but not set the attribute, and in SSR to no-op the DOM operation and
841
+ * capture the value for serialization.
842
+ *
843
+ * @internal
844
+ */
845
+ _$setValue(value, directiveParent = this, valueIndex, noCommit) {
846
+ const strings = this.strings;
847
+ // Whether any of the values has changed, for dirty-checking
848
+ let change = false;
849
+ if (strings === undefined) {
850
+ // Single-value binding case
851
+ value = resolveDirective(this, value, directiveParent, 0);
852
+ change = !isPrimitive(value) || value !== this._$committedValue;
853
+ if (change) {
854
+ this._$committedValue = value;
855
+ }
856
+ }
857
+ else {
858
+ // Interpolation case
859
+ const values = value;
860
+ value = strings[0];
861
+ let i, v;
862
+ for (i = 0; i < strings.length - 1; i++) {
863
+ v = resolveDirective(this, values[valueIndex + i], directiveParent, i);
864
+ change ||= !isPrimitive(v) || v !== this._$committedValue[i];
865
+ if (isNullish(v)) {
866
+ value = undefined;
867
+ }
868
+ else if (!isNullish(value)) {
869
+ value += v + strings[i + 1];
870
+ }
871
+ this._$committedValue[i] = v;
872
+ }
873
+ }
874
+ if (change && !noCommit) {
875
+ this._commitValue(value);
876
+ }
877
+ }
878
+ /** @internal */
879
+ _commitValue(value) {
880
+ if (isNullish(value)) {
881
+ this.element.removeAttribute(this.name);
882
+ }
883
+ else {
884
+ this.element.setAttribute(this.name, value);
885
+ }
886
+ }
887
+ }
888
+ class PropertyPart extends AttributePart {
889
+ type = PROPERTY_PART;
890
+ /** @internal */
891
+ _commitValue(value) {
892
+ this.element[this.name] = value;
893
+ }
894
+ }
895
+ class BooleanAttributePart extends AttributePart {
896
+ type = BOOLEAN_ATTRIBUTE_PART;
897
+ /** @internal */
898
+ _commitValue(value) {
899
+ this.element.toggleAttribute(this.name, !!value);
900
+ }
901
+ }
902
+ class EventPart extends AttributePart {
903
+ type = EVENT_PART;
904
+ /** @internal */
905
+ _$setValue(newListener, directiveParent = this) {
906
+ newListener = resolveDirective(this, newListener, directiveParent, 0);
907
+ const oldListener = this._$committedValue;
908
+ const shouldRemoveListener = oldListener &&
909
+ (!newListener ||
910
+ newListener.capture !== oldListener.capture ||
911
+ newListener.once !== oldListener.once ||
912
+ newListener.passive !== oldListener.passive);
913
+ const shouldAddListener = newListener && (!oldListener || shouldRemoveListener);
914
+ if (shouldRemoveListener) {
915
+ this.element.removeEventListener(this.name, this, oldListener);
916
+ }
917
+ if (shouldAddListener) {
918
+ this.element.addEventListener(this.name, this, newListener);
919
+ }
920
+ this._$committedValue = newListener;
921
+ }
922
+ handleEvent(event) {
923
+ if (typeof this._$committedValue === 'function') {
924
+ this._$committedValue.call(this.options?.host ?? this.element, event);
925
+ }
926
+ else {
927
+ this._$committedValue.handleEvent(event);
928
+ }
929
+ }
930
+ }
931
+ class ElementPart {
932
+ element;
933
+ type = ELEMENT_PART;
934
+ /** @internal */
935
+ __directive;
936
+ // This is to ensure that every Part has a _$committedValue
937
+ _$committedValue;
938
+ /** @internal */
939
+ _$parent;
940
+ /** @internal */
941
+ _$disconnectableChildren = undefined;
942
+ _$props;
943
+ options;
944
+ constructor(element, parent, options) {
945
+ this.element = element;
946
+ this._$parent = parent;
947
+ this.options = options;
948
+ }
949
+ // See comment in Disconnectable interface for why this is a getter
950
+ get _$isConnected() {
951
+ return this._$parent._$isConnected;
952
+ }
953
+ _$setValue(value) {
954
+ if (!value)
955
+ return;
956
+ if (value instanceof Ref) {
957
+ value.value = this.element;
958
+ }
959
+ else if (value._$litDirective$) {
960
+ resolveDirective(this, value);
961
+ }
962
+ else {
963
+ if (this._$props) {
964
+ for (const prop in this._$props) {
965
+ Reflect.set(this.element, prop, undefined);
966
+ }
967
+ }
968
+ Object.assign(this.element, value);
969
+ this._$props = value;
970
+ }
971
+ }
972
+ }
973
+ export const createRef = () => new Ref();
974
+ class Ref {
975
+ value;
976
+ }
977
+ export const render = (value, container, options) => {
978
+ const partOwnerNode = (options?.renderBefore ?? container);
979
+ let part = partOwnerNode._$litPart$;
980
+ if (part === undefined) {
981
+ const endNode = options?.renderBefore ?? null;
982
+ partOwnerNode._$litPart$ = part = new ChildPart(container.insertBefore(createMarker(), endNode), endNode, undefined, options ?? {});
983
+ }
984
+ part._$setValue(value);
985
+ return part;
986
+ };
987
+ //# sourceMappingURL=lit-html.js.map