@@ -17,7 +17,7 @@ const PLACEHOLDER_REGEXP_EQUAL = new RegExp(`^${PLACEHOLDER_REGEXP_TEXT}$`);
17
17
const PLACEHOLDER_REGEXP_ALL = new RegExp ( PLACEHOLDER_REGEXP_TEXT , "g" ) ;
18
18
const PLACEHOLDER_REGEXP_ONLY = / ^ [ ^ A - Z a - z ] + $ / ;
19
19
20
- function createSignature ( parts ) {
20
+ function createContent ( parts ) {
21
21
let signature = parts [ 0 ] ;
22
22
let tableMode = false ;
23
23
for ( let index = 1 ; index < parts . length ; index += 1 ) {
@@ -190,12 +190,10 @@ function updateStyleElement(target, styles) {
190
190
}
191
191
192
192
export function compileTemplate ( rawParts , isSVG , isMsg , useLayout ) {
193
- let template = globalThis . document . createElement ( "template" ) ;
194
- const parts = { } ;
195
-
196
- const signature = isMsg ? rawParts : createSignature ( rawParts ) ;
193
+ const content = isMsg ? rawParts : createContent ( rawParts ) ;
197
194
198
- template . innerHTML = isSVG ? `<svg>${ signature } </svg>` : signature ;
195
+ let template = globalThis . document . createElement ( "template" ) ;
196
+ template . innerHTML = isSVG ? `<svg>${ content } </svg>` : content ;
199
197
200
198
if ( isSVG ) {
201
199
const svgRoot = template . content . firstChild ;
@@ -235,7 +233,9 @@ export function compileTemplate(rawParts, isSVG, isMsg, useLayout) {
235
233
}
236
234
237
235
const compileWalker = createWalker ( template . content ) ;
236
+ const parts = { } ;
238
237
const notDefinedElements = [ ] ;
238
+
239
239
let compileIndex = 0 ;
240
240
let noTranslate = null ;
241
241
@@ -443,12 +443,20 @@ export function compileTemplate(rawParts, isSVG, isMsg, useLayout) {
443
443
. map ( ( e ) => `<${ e } >` )
444
444
. join ( ", " ) } element${
445
445
notDefinedElements . length > 1 ? "s" : ""
446
- } found in the template:\n${ beautifyTemplateLog ( signature , - 1 ) } `,
446
+ } found in the template:\n${ beautifyTemplateLog ( content , - 1 ) } `,
447
447
) ;
448
448
}
449
449
450
450
const partsKeys = Object . keys ( parts ) ;
451
- return function updateTemplateInstance ( host , target , args , { styleSheets } ) {
451
+ return function updateTemplateInstance ( host , target , options ) {
452
+ const { args, shadowOptions, styleSheets } = options ;
453
+
454
+ if ( shadowOptions ) {
455
+ target = host . shadowRoot || host . attachShadow ( shadowOptions ) ;
456
+ } else {
457
+ target = target || host ;
458
+ }
459
+
452
460
let meta = getMeta ( target ) ;
453
461
454
462
if ( template !== meta . template ) {
@@ -522,17 +530,20 @@ export function compileTemplate(rawParts, isSVG, isMsg, useLayout) {
522
530
523
531
for ( const marker of meta . markers ) {
524
532
const value = args [ marker . index ] ;
525
- const prevValue = meta . prevArgs && meta . prevArgs [ marker . index ] ;
533
+ let prevValue = undefined ;
526
534
527
- if ( meta . prevArgs && value === prevValue ) continue ;
535
+ if ( meta . prevArgs ) {
536
+ prevValue = meta . prevArgs [ marker . index ] ;
537
+ if ( prevValue === value ) continue ;
538
+ }
528
539
529
540
try {
530
541
marker . fn ( host , marker . node , value , prevValue , useLayout ) ;
531
542
} catch ( error ) {
532
543
console . error (
533
544
`Error while updating template expression in ${ stringifyElement (
534
545
host ,
535
- ) } :\n${ beautifyTemplateLog ( signature , marker . index ) } `,
546
+ ) } :\n${ beautifyTemplateLog ( content , marker . index ) } `,
536
547
) ;
537
548
538
549
throw error ;
0 commit comments