forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate-impl.js
339 lines (300 loc) · 9.75 KB
/
template-impl.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {childElementByTag} from '../dom';
import {fromClass} from '../service';
import {dev, user} from '../log';
/**
* @fileoverview
* For the set of decisions made on templating see:
* {@link https://docs.google.com/document/d/1q-5MPQHnOHLF_uL7lQsGZdzuBgrPTkCy2PdRP-YCbOw/edit#}
*/
/**
* @typedef {function(new:BaseTemplate, !Element)}
*/
let TemplateClassDef;
/** @private @const {string} */
const PROP_ = '__AMP_IMPL_';
/** @private @const {string} */
const PROP_PROMISE_ = '__AMP_WAIT_';
/**
* The interface that is implemented by all templates.
*/
export class BaseTemplate {
/** @param {!Element} element */
constructor(element) {
/** @public @const */
this.element = element;
/** @public @const {!Window} */
this.win = element.ownerDocument.defaultView;
this.compileCallback();
}
/**
* Override in subclass if the element needs to compile the template.
* @protected
*/
compileCallback() {
// Subclasses may override.
}
/**
* To be implemented by subclasses.
* @param {!JSONType} unusedData
* @return {!Element}
*/
render(unusedData) {
throw new Error('Not implemented');
}
/**
* Helps the template implementation to unwrap the root element. The root
* element can be unwrapped only when it contains a single element or a
* single element surrounded by empty text nodes.
* @param {!Element} root
* @return {!Element}
* @protected @final
*/
unwrap(root) {
let singleElement = null;
for (let n = root.firstChild; n != null; n = n.nextSibling) {
if (n.nodeType == /* TEXT */ 3) {
if (n.textContent.trim()) {
// Non-empty text node - can't unwrap.
singleElement = null;
break;
}
} else if (n.nodeType == /* COMMENT */ 8) {
// Ignore comments.
} else if (n.nodeType == /* ELEMENT */ 1) {
if (!singleElement) {
singleElement = dev().assertElement(n);
} else {
// This is not the first element - can't unwrap.
singleElement = null;
break;
}
} else {
singleElement = null;
}
}
return singleElement || root;
}
}
/**
*/
export class Templates {
/** @param {!Window} win */
constructor(win) {
/** @private @const {!Window} */
this.win_ = win;
/**
* A map from template type to template's class promise.
* @private @const {!Object<string, !Promise<!TemplateClassDef>>}
*/
this.templateClassMap_ = {};
/**
* A map from template type to template's class promise. This is a transient
* storage. As soon as the template class loaded, the entry is removed.
* @private @const {!Object<string, function(!TemplateClassDef)>}
*/
this.templateClassResolvers_ = {};
/** @type {!Object<string, boolean>|undefined} */
this.declaredTemplates_ = undefined;
}
/**
* Renders the specified template element using the supplied data.
* @param {!Element} templateElement
* @param {!JSONType} data
* @return {!Promise<!Element>}
*/
renderTemplate(templateElement, data) {
return this.getImplementation_(templateElement).then(impl => {
return this.render_(impl, data);
});
}
/**
* Renders the specified template element using the supplied array of data
* and returns an array of resulting elements.
* @param {!Element} templateElement
* @param {!Array<!JSONType>} array
* @return {!Promise<!Array<!Element>>}
*/
renderTemplateArray(templateElement, array) {
if (array.length == 0) {
return Promise.resolve([]);
}
return this.getImplementation_(templateElement).then(impl => {
return array.map(item => {
return this.render_(impl, item);
});
});
}
/**
* Discovers the template for the specified parent and renders it using the
* supplied data. The template can be specified either via "template"
* attribute or as a child "template" element. When specified via "template"
* attribute, the value indicates the ID of the template element.
* @param {!Element} parent
* @param {!JSONType} data
* @return {!Promise<!Element>}
*/
findAndRenderTemplate(parent, data) {
return this.renderTemplate(this.findTemplate_(parent), data);
}
/**
* Discovers the template for the specified parent and renders it using the
* supplied array of data. The template can be specified either via "template"
* attribute or as a child "template" element. When specified via "template"
* attribute, the value indicates the ID of the template element. Returns
* the array of the rendered elements.
* @param {!Element} parent
* @param {!Array<!JSONType>} array
* @return {!Promise<!Array<!Element>>}
*/
findAndRenderTemplateArray(parent, array) {
return this.renderTemplateArray(this.findTemplate_(parent), array);
}
/**
* The template can be specified either via "template" attribute or as a
* child "template" element. When specified via "template" attribute,
* the value indicates the ID of the template element.
* @param {!Element} parent
* @return {!Element}
* @private
*/
findTemplate_(parent) {
let templateElement = null;
const templateId = parent.getAttribute('template');
if (templateId) {
templateElement = parent.ownerDocument.getElementById(templateId);
} else {
templateElement = childElementByTag(parent, 'template');
}
user().assert(templateElement, 'Template not found for %s', parent);
user().assert(templateElement.tagName == 'TEMPLATE',
'Template element must be a "template" tag %s', templateElement);
return templateElement;
}
/**
* Returns the promise that will eventually yield the template implementation
* for the specified template element.
* @param {!Element} element
* @return {!Promise<!BaseTemplate>}
* @private
*/
getImplementation_(element) {
/** @const {!BaseTemplate} */
const impl = element[PROP_];
if (impl) {
return Promise.resolve(impl);
}
const type = user().assert(element.getAttribute('type'),
'Type must be specified: %s', element);
let promise = element[PROP_PROMISE_];
if (promise) {
return promise;
}
promise = this.waitForTemplateClass_(element, type).then(templateClass => {
const impl = element[PROP_] = new templateClass(element);
delete element[PROP_PROMISE_];
return impl;
});
element[PROP_PROMISE_] = promise;
return promise;
}
/**
* Returns the promise that will eventually yield the template class. This will
* wait until the actual template script has been downloaded and parsed.
* @param {!Element} element
* @param {string} type
* @return {!Promise<!TemplateClassDef>}
* @private
*/
waitForTemplateClass_(element, type) {
if (this.templateClassMap_[type]) {
return this.templateClassMap_[type];
}
this.checkTemplateDeclared_(element, type);
let aResolve;
const promise = new Promise((resolve, unusedReject) => {
aResolve = resolve;
});
this.templateClassMap_[type] = promise;
this.templateClassResolvers_[type] = aResolve;
return promise;
}
/**
* Checks that the template type has actually been declared by a
* `<script custom-template=$type>` tag in the head.
* @param {!Element} element
* @param {string} type
* @private
*/
checkTemplateDeclared_(element, type) {
if (!this.declaredTemplates_) {
this.declaredTemplates_ = this.win_.Object.create(null);
const scriptTags = this.win_.document.querySelectorAll(
'script[custom-template]');
for (let i = 0; i < scriptTags.length; i++) {
this.declaredTemplates_[scriptTags[i].getAttribute(
'custom-template')] = true;
}
}
user().assert(this.declaredTemplates_[type],
'Template must be declared for %s as <script custom-template=%s>',
element, type);
}
/**
* Registers an extended template. This function should typically be called
* through the registerTemplate method on the AMP runtime.
* @param {string} type
* @param {!TemplateClassDef} templateClass
* @private
*/
registerTemplate_(type, templateClass) {
if (!this.templateClassMap_[type]) {
this.templateClassMap_[type] = Promise.resolve(templateClass);
} else {
const resolver = this.templateClassResolvers_[type];
user().assert(resolver, 'Duplicate template type: %s', type);
delete this.templateClassResolvers_[type];
resolver(templateClass);
}
}
/**
* @param {!BaseTemplate} impl
* @param {!JSONType} data
* @private
*/
render_(impl, data) {
return impl.render(data);
}
}
/**
* Registers an extended template. This function should typically be called
* through the registerTemplate method on the AMP runtime.
* @param {!Window} win
* @param {string} type
* @param {!TemplateClassDef} templateClass
* @package
*/
export function registerExtendedTemplate(win, type, templateClass) {
return installTemplatesService(win).registerTemplate_(type, templateClass);
}
/**
* @param {!Window} window
* @return {!Templates}
*/
export function installTemplatesService(window) {
return fromClass(window, 'templates', Templates);
};