Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
- Only support HTMLTemplateElement and strings
- Use raw strings for the constants to handle single `\`
  • Loading branch information
dfreedm committed Dec 5, 2017
1 parent 1bba3ab commit d5070bb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
20 changes: 11 additions & 9 deletions lib/utils/html-fn.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,18 @@
* @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:
Expand All @@ -44,15 +44,17 @@
*
* @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;
Expand Down
1 change: 1 addition & 0 deletions test/smoke/html-fn.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ <h3 id="name">${this.is}</h3>
return html`
<style>.frame {font-style: italic}</style>
<div class="frame">${super.template}</div>
<div>\</div>
`;
}
constructor() {
Expand Down

0 comments on commit d5070bb

Please sign in to comment.