|
7 | 7 | // Any new exports need to be added to the export statement in |
8 | 8 | // `packages/lit/src/index.all.ts`. |
9 | 9 |
|
10 | | -import { html as coreHtml, svg as coreSvg, TemplateResult } from './lit-html.js'; |
| 10 | +import {html as coreHtml, svg as coreSvg, TemplateResult} from './lit-html.js'; |
11 | 11 |
|
12 | 12 | interface StaticValue { |
13 | 13 | /** The value to interpolate as-is into the template. */ |
@@ -108,57 +108,57 @@ const stringsCache = new Map<string, TemplateStringsArray>(); |
108 | 108 | */ |
109 | 109 | export const withStatic = |
110 | 110 | (coreTag: typeof coreHtml | typeof coreSvg) => |
111 | | - (strings: TemplateStringsArray, ...values: unknown[]): TemplateResult => { |
112 | | - const l = values.length; |
113 | | - let staticValue: string | undefined; |
114 | | - let dynamicValue: unknown; |
115 | | - const staticStrings: Array<string> = []; |
116 | | - const dynamicValues: Array<unknown> = []; |
117 | | - let i = 0; |
118 | | - let hasStatics = false; |
119 | | - let s: string; |
| 111 | + (strings: TemplateStringsArray, ...values: unknown[]): TemplateResult => { |
| 112 | + const l = values.length; |
| 113 | + let staticValue: string | undefined; |
| 114 | + let dynamicValue: unknown; |
| 115 | + const staticStrings: Array<string> = []; |
| 116 | + const dynamicValues: Array<unknown> = []; |
| 117 | + let i = 0; |
| 118 | + let hasStatics = false; |
| 119 | + let s: string; |
120 | 120 |
|
121 | | - while (i < l) { |
122 | | - s = strings[i]; |
123 | | - // Collect any unsafeStatic values, and their following template strings |
124 | | - // so that we treat a run of template strings and unsafe static values as |
125 | | - // a single template string. |
126 | | - while ( |
127 | | - i < l && |
128 | | - ((dynamicValue = values[i]), |
129 | | - (staticValue = unwrapStaticValue(dynamicValue))) !== undefined |
130 | | - ) { |
131 | | - s += staticValue + strings[++i]; |
132 | | - hasStatics = true; |
133 | | - } |
134 | | - dynamicValues.push(dynamicValue); |
135 | | - staticStrings.push(s); |
136 | | - i++; |
137 | | - } |
138 | | - // If the last value isn't static (which would have consumed the last |
139 | | - // string), then we need to add the last string. |
140 | | - if (i === l) { |
141 | | - staticStrings.push(strings[l]); |
| 121 | + while (i < l) { |
| 122 | + s = strings[i]; |
| 123 | + // Collect any unsafeStatic values, and their following template strings |
| 124 | + // so that we treat a run of template strings and unsafe static values as |
| 125 | + // a single template string. |
| 126 | + while ( |
| 127 | + i < l && |
| 128 | + ((dynamicValue = values[i]), |
| 129 | + (staticValue = unwrapStaticValue(dynamicValue))) !== undefined |
| 130 | + ) { |
| 131 | + s += staticValue + strings[++i]; |
| 132 | + hasStatics = true; |
142 | 133 | } |
| 134 | + dynamicValues.push(dynamicValue); |
| 135 | + staticStrings.push(s); |
| 136 | + i++; |
| 137 | + } |
| 138 | + // If the last value isn't static (which would have consumed the last |
| 139 | + // string), then we need to add the last string. |
| 140 | + if (i === l) { |
| 141 | + staticStrings.push(strings[l]); |
| 142 | + } |
143 | 143 |
|
144 | | - if (hasStatics) { |
145 | | - const key = staticStrings.join('$$lit$$'); |
146 | | - strings = stringsCache.get(key)!; |
147 | | - if (strings === undefined) { |
148 | | - // Beware: in general this pattern is unsafe, and doing so may bypass |
149 | | - // lit's security checks and allow an attacker to execute arbitrary |
150 | | - // code and inject arbitrary content. |
151 | | - // eslint-disable-next-line @typescript-eslint/no-explicit-any |
152 | | - (staticStrings as any).raw = staticStrings; |
153 | | - stringsCache.set( |
154 | | - key, |
155 | | - (strings = staticStrings as unknown as TemplateStringsArray) |
156 | | - ); |
157 | | - } |
158 | | - values = dynamicValues; |
| 144 | + if (hasStatics) { |
| 145 | + const key = staticStrings.join('$$lit$$'); |
| 146 | + strings = stringsCache.get(key)!; |
| 147 | + if (strings === undefined) { |
| 148 | + // Beware: in general this pattern is unsafe, and doing so may bypass |
| 149 | + // lit's security checks and allow an attacker to execute arbitrary |
| 150 | + // code and inject arbitrary content. |
| 151 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 152 | + (staticStrings as any).raw = staticStrings; |
| 153 | + stringsCache.set( |
| 154 | + key, |
| 155 | + (strings = staticStrings as unknown as TemplateStringsArray) |
| 156 | + ); |
159 | 157 | } |
160 | | - return coreTag(strings, ...values); |
161 | | - }; |
| 158 | + values = dynamicValues; |
| 159 | + } |
| 160 | + return coreTag(strings, ...values); |
| 161 | + }; |
162 | 162 |
|
163 | 163 | /** |
164 | 164 | * Interprets a template literal as an HTML template that can efficiently |
|
0 commit comments