@@ -21,8 +21,8 @@ const templates = new Map<TemplateStringsArray, Template>();
21
21
* Interprets a template literal as an HTML template that can efficiently
22
22
* render to and update a container.
23
23
*/
24
- export function html ( strings : TemplateStringsArray ,
25
- ...values : any [ ] ) : TemplateResult {
24
+ export function html (
25
+ strings : TemplateStringsArray , ...values : any [ ] ) : TemplateResult {
26
26
let template = templates . get ( strings ) ;
27
27
if ( template === undefined ) {
28
28
template = new Template ( strings ) ;
@@ -51,9 +51,10 @@ export class TemplateResult {
51
51
* To update a container with new values, reevaluate the template literal and
52
52
* call `render` with the new result.
53
53
*/
54
- export function render ( result : TemplateResult ,
55
- container : Element | DocumentFragment ,
56
- partCallback : PartCallback = defaultPartCallback ) {
54
+ export function render (
55
+ result : TemplateResult ,
56
+ container : Element | DocumentFragment ,
57
+ partCallback : PartCallback = defaultPartCallback ) {
57
58
let instance = ( container as any ) . __templateInstance as any ;
58
59
59
60
// Repeat render, just call update()
@@ -99,8 +100,10 @@ const exprMarker = `{{lit-${Math.random()}}}`;
99
100
* to Part.update().
100
101
*/
101
102
export class TemplatePart {
102
- constructor ( public type : string , public index : number , public name ?: string ,
103
- public rawName ?: string , public strings ?: string [ ] ) { }
103
+ constructor (
104
+ public type : string , public index : number , public name ?: string ,
105
+ public rawName ?: string , public strings ?: string [ ] ) {
106
+ }
104
107
}
105
108
106
109
export class Template {
@@ -110,8 +113,8 @@ export class Template {
110
113
constructor ( strings : TemplateStringsArray ) {
111
114
this . element = document . createElement ( 'template' ) ;
112
115
this . element . innerHTML = strings . join ( exprMarker ) ;
113
- const walker = document . createTreeWalker ( this . element . content ,
114
- 5 /* elements & text */ ) ;
116
+ const walker = document . createTreeWalker (
117
+ this . element . content , 5 /* elements & text */ ) ;
115
118
let index = - 1 ;
116
119
let partIndex = 0 ;
117
120
const nodesToRemove = [ ] ;
@@ -135,8 +138,8 @@ export class Template {
135
138
0 , attributeString . length - attributeStrings [ 0 ] . length ) ;
136
139
// Find the attribute name
137
140
const rawName = rawNameString . match ( / ( (?: \w | [ . \- _ $ ] ) + ) = [ " ' ] ? $ / ) ! [ 1 ] ;
138
- this . parts . push ( new TemplatePart ( 'attribute' , index , attribute . name ,
139
- rawName , attributeStrings ) ) ;
141
+ this . parts . push ( new TemplatePart (
142
+ 'attribute' , index , attribute . name , rawName , attributeStrings ) ) ;
140
143
node . removeAttribute ( attribute . name ) ;
141
144
partIndex += attributeStrings . length - 1 ;
142
145
i -- ;
@@ -176,15 +179,14 @@ export class Template {
176
179
}
177
180
}
178
181
179
- export const getValue =
180
- ( part : Part , value : any ) => {
181
- // `null` as the value of a Text node will render the string 'null'
182
- // so we convert it to undefined
183
- if ( value != null && value . __litDirective === true ) {
184
- value = value ( part ) ;
185
- }
186
- return value === null ? undefined : value ;
187
- } ;
182
+ export const getValue = ( part : Part , value : any ) => {
183
+ // `null` as the value of a Text node will render the string 'null'
184
+ // so we convert it to undefined
185
+ if ( value != null && value . __litDirective === true ) {
186
+ value = value ( part ) ;
187
+ }
188
+ return value === null ? undefined : value ;
189
+ } ;
188
190
189
191
export type DirectiveFn = ( part : Part ) => any ;
190
192
@@ -215,8 +217,9 @@ export class AttributePart implements MultiPart {
215
217
strings : string [ ] ;
216
218
size : number ;
217
219
218
- constructor ( instance : TemplateInstance , element : Element , name : string ,
219
- strings : string [ ] ) {
220
+ constructor (
221
+ instance : TemplateInstance , element : Element , name : string ,
222
+ strings : string [ ] ) {
220
223
this . instance = instance ;
221
224
this . element = element ;
222
225
this . name = name ;
@@ -395,20 +398,23 @@ export class NodePart implements SinglePart {
395
398
}
396
399
}
397
400
398
- export type PartCallback = ( instance : TemplateInstance ,
399
- templatePart : TemplatePart , node : Node ) => Part ;
401
+ export type PartCallback =
402
+ ( instance : TemplateInstance , templatePart : TemplatePart , node : Node ) =>
403
+ Part ;
400
404
401
405
export const defaultPartCallback =
402
- ( instance : TemplateInstance , templatePart : TemplatePart , node : Node ) :
403
- Part => {
404
- if ( templatePart . type === 'attribute' ) {
405
- return new AttributePart ( instance , node as Element ,
406
- templatePart . name ! , templatePart . strings ! ) ;
407
- } else if ( templatePart . type === 'node' ) {
408
- return new NodePart ( instance , node , node . nextSibling ! ) ;
409
- }
410
- throw new Error ( `Unknown part type ${ templatePart . type } ` ) ;
411
- } ;
406
+ ( instance : TemplateInstance ,
407
+ templatePart : TemplatePart ,
408
+ node : Node ) : Part => {
409
+ if ( templatePart . type === 'attribute' ) {
410
+ return new AttributePart (
411
+ instance , node as Element , templatePart . name ! , templatePart . strings !
412
+ ) ;
413
+ } else if ( templatePart . type === 'node' ) {
414
+ return new NodePart ( instance , node , node . nextSibling ! ) ;
415
+ }
416
+ throw new Error ( `Unknown part type ${ templatePart . type } ` ) ;
417
+ } ;
412
418
413
419
/**
414
420
* An instance of a `Template` that can be attached to the DOM and updated
@@ -419,8 +425,8 @@ export class TemplateInstance {
419
425
_partCallback : PartCallback ;
420
426
template : Template ;
421
427
422
- constructor ( template : Template ,
423
- partCallback : PartCallback = defaultPartCallback ) {
428
+ constructor (
429
+ template : Template , partCallback : PartCallback = defaultPartCallback ) {
424
430
this . template = template ;
425
431
this . _partCallback = partCallback ;
426
432
}
0 commit comments