a post-component library for building user-interfaces on the web.

add a closing delimiter to the internal template syntax (#36)

otherwise ${...}1 would come out as dyn-$01, breaking
the index matching in /dyn-\$(\d+)/

authored by tombl.dev and committed by

GitHub b176bbfb f4eead8f

+22 -4
+3 -3
src/html.js
··· 219 219 } 220 220 } 221 221 222 - const DYNAMIC_WHOLE = /^dyn-\$(\d+)$/i 223 - const DYNAMIC_GLOBAL = /dyn-\$(\d+)/gi 222 + const DYNAMIC_WHOLE = /^dyn-\$(\d+)\$$/i 223 + const DYNAMIC_GLOBAL = /dyn-\$(\d+)\$/gi 224 224 225 225 /** @type {Map<TemplateStringsArray, CompiledTemplate>} */ 226 226 const templates = new Map() ··· 230 230 if (cached) return cached 231 231 232 232 const templateElement = document.createElement('template') 233 - templateElement.innerHTML = statics.reduce((a, v, i) => a + v + (i === statics.length - 1 ? '' : `dyn-$${i}`), '') 233 + templateElement.innerHTML = statics.reduce((a, v, i) => a + v + (i === statics.length - 1 ? '' : `dyn-$${i}$`), '') 234 234 235 235 let nextPart = 0 236 236 /** @type {CompiledTemplate} */
+19 -1
test/basic.test.ts
··· 163 163 const { root, el } = setup() 164 164 165 165 root.render(html`<!-- ${'text'} -->`) 166 - expect(el.innerHTML).toMatchInlineSnapshot(`"<!-- dyn-$0 -->"`) 166 + expect(el.innerHTML).toMatchInlineSnapshot(`"<!-- dyn-$0$ -->"`) 167 + }) 168 + 169 + it('throws when manually specifying internal template syntax', { skip: import.meta.env.PROD }, () => { 170 + const { root, el } = setup() 171 + 172 + expect(() => { 173 + root.render(html`${1} dyn-$0$`) 174 + }).toThrowErrorMatchingInlineSnapshot(`[Error: got more parts than expected]`) 175 + 176 + expect(el.innerHTML).toMatchInlineSnapshot(`""`) 177 + }) 178 + 179 + it('does not throw when syntax is close but not exact', () => { 180 + const { root, el } = setup() 181 + 182 + root.render(html`dyn-$${0}1$`) 183 + 184 + expect(el.innerHTML).toMatchInlineSnapshot(`"dyn-$01$"`) 167 185 }) 168 186 })