Address feedback

- Only support HTMLTemplateElement and strings
- Use raw strings for the constants to handle single `\`
This commit is contained in:
Daniel Freedman
2017-12-04 16:07:45 -08:00
parent 1bba3abb81
commit d5070bbee3
2 changed files with 12 additions and 9 deletions
+11 -9
View File
@@ -17,18 +17,18 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
* @return {string} HTML stringified form of `obj`
*/
function htmlValue(value) {
if (value instanceof HTMLElement) {
return /** @type {!HTMLElement} */(value).innerHTML;
} else if (value instanceof Node) {
return /** @type {!Node} */(value).textContent;
if (value === null || value === undefined) {
return '';
} else if (value instanceof HTMLTemplateElement) {
return /** @type {!HTMLTemplateElement} */(value).innerHTML;
} else {
return value.toString();
return String(value);
}
}
/**
* Small tagged template string function to facilitate building a `<template>` element.
* Polymer.html will automatically compose HTML elements in `${}` blocks, allowing for easy
* Polymer.html will automatically compose `<template>` elements in `${}` blocks, allowing for easy
* composition of superclass templates, or partial templates.
*
* Example:
@@ -44,15 +44,17 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
*
* @memberof Polymer
* @param {!Array<string>} strings Constant parts of tagged template literal
* @param {!Array<*>} values Variable parts of tagged template literal
* @param {...*} values Variable parts of tagged template literal
* @return {!HTMLTemplateElement} Constructed HTMLTemplateElement
*/
Polymer.html = function html(strings, ...values) {
// use raw strings to preserve literal escapes in strings
const rawStrings = strings.raw;
/** @type {!Array<string>} */
const parts = [strings[0]];
const parts = [rawStrings[0]];
const template = /** @type {!HTMLTemplateElement} */(document.createElement('template'));
for (let i = 0; i < values.length; i++) {
parts.push(htmlValue(values[i]), strings[i + 1]);
parts.push(htmlValue(values[i]), rawStrings[i + 1]);
}
template.innerHTML = parts.join('');
return template;
+1
View File
@@ -38,6 +38,7 @@ subject to an additional IP rights grant found at http://polymer.github.io/PATEN
return html`
<style>.frame {font-style: italic}</style>
<div class="frame">${super.template}</div>
<div>\</div>
`;
}
constructor() {