-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4971 from Polymer/html-template-fn
`html` tag function for generating templates
- Loading branch information
Showing
7 changed files
with
276 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<!-- | ||
@license | ||
Copyright (c) 2017 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
<link rel="import" href="boot.html"> | ||
<script> | ||
(function() { | ||
'use strict'; | ||
|
||
/** | ||
* @param {*} value Object to stringify into HTML | ||
* @return {string} HTML stringified form of `obj` | ||
*/ | ||
function htmlValue(value) { | ||
if (value instanceof HTMLTemplateElement) { | ||
return /** @type {!HTMLTemplateElement} */(value).innerHTML; | ||
} else { | ||
return String(value); | ||
} | ||
} | ||
|
||
/** | ||
* A template literal tag that creates an HTML <template> element from the contents of the string. | ||
* | ||
* This allows you to write a Polymer Template in JavaScript. | ||
* | ||
* Interpolated values are converted to strings when the template is created, | ||
* they are not intended as a replacement for Polymer data-binding. | ||
* | ||
* There is special support for HTMLTemplateElement values, | ||
* allowing for easy composition of superclass or partial templates. | ||
* | ||
* Example: | ||
* | ||
* static get template() { | ||
* return Polymer.html` | ||
* <style>:host{ content:"..." }</style> | ||
* <div class="shadowed">${this.partialTemplate}</div> | ||
* ${super.template} | ||
* `; | ||
* } | ||
* static get partialTemplate() { return Polymer.html`<span>Partial!</span>`; } | ||
* | ||
* @memberof Polymer | ||
* @param {!Array<string>} strings Constant 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; | ||
const template = /** @type {!HTMLTemplateElement} */(document.createElement('template')); | ||
template.innerHTML = values.reduce((acc, v, idx) => | ||
acc + htmlValue(v) + rawStrings[idx + 1], rawStrings[0]); | ||
return template; | ||
}; | ||
})(); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<!DOCTYPE html> | ||
<!-- | ||
@license | ||
Copyright (c) 2017 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
<html> | ||
<head> | ||
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script> | ||
<link rel="import" href="../../polymer-element.html"> | ||
</head> | ||
<body> | ||
<script> | ||
const html = Polymer.html; | ||
class SuperClass extends Polymer.Element { | ||
static get is() {return 'super-class';} | ||
static get template() { | ||
return html` | ||
<style>#name {color: red}</style> | ||
<h3 id="name">${this.is}</h3> | ||
<div>${this.headerTemplate}</div> | ||
[[myProp.stuffThatGoesHere]] | ||
<div>${this.footerTemplate}</div> | ||
`; | ||
} | ||
static get headerTemplate() { return html`<h1>Header</h1>`; } | ||
static get footerTemplate() { return html`<h1>Footer</h1>`; } | ||
} | ||
customElements.define(SuperClass.is, SuperClass); | ||
class SubClass extends SuperClass { | ||
static get is() {return 'sub-class';} | ||
static get template() { | ||
return html` | ||
<style>.frame {font-style: italic}</style> | ||
<div class="frame">${super.template}</div> | ||
<div>\</div> | ||
`; | ||
} | ||
constructor() { | ||
super(); | ||
this.myProp = {stuffThatGoesHere: '!stuff that goes here!'}; | ||
} | ||
static get headerTemplate() { return html`<h2>Sub-header</h2>`; } | ||
static get footerTemplate() { return html`<h2>Sub-footer</h2>`; } | ||
} | ||
customElements.define(SubClass.is, SubClass); | ||
</script> | ||
<script> | ||
class XString extends Polymer.Element { | ||
static get is() {return 'x-string'}; | ||
static get template() { | ||
return 'string template!'; | ||
} | ||
} | ||
customElements.define(XString.is, XString); | ||
</script> | ||
<super-class></super-class> | ||
<sub-class></sub-class> | ||
<x-string></x-string> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<!doctype html> | ||
<!-- | ||
@license | ||
Copyright (c) 2017 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script> | ||
<script src="../../../web-component-tester/browser.js"></script> | ||
<link rel="import" href="../../polymer.html"> | ||
</head> | ||
<body> | ||
<script> | ||
suite('html function', function() { | ||
test('html makes an HTML template', function() { | ||
let t = Polymer.html``; | ||
assert.instanceOf(t, HTMLTemplateElement); | ||
}); | ||
test('template output has elements', function() { | ||
let t = Polymer.html`<div><span></span></div><!-- hi -->`; | ||
assert(t.content.querySelector('div')); | ||
assert(t.content.querySelector('span')); | ||
assert.instanceOf(t.content.lastChild, Comment); | ||
}); | ||
test('escaping works as expected', function() { | ||
let t = Polymer.html`<span>\f\o\o</span>`; | ||
assert.equal(t.innerHTML, '<span>\\f\\o\\o</span>'); | ||
}); | ||
test('variables work', function() { | ||
let a = 3; | ||
let b = 'foo'; | ||
let t = Polymer.html`<div>${a} ${b}</div>`; | ||
let inst = document.createElement('div'); | ||
inst.appendChild(t.content.cloneNode(true)); | ||
assert.equal(inst.textContent, `${a} ${b}`); | ||
}); | ||
test('template variables only include the template contents', function() { | ||
let t1 = Polymer.html`<div></div>`; | ||
let t2 = Polymer.html`<span>${t1}</span>`; | ||
assert.equal(t2.innerHTML, '<span><div></div></span>'); | ||
}); | ||
}); | ||
suite('Polymer + html fn', function() { | ||
suiteSetup(function() { | ||
|
||
class HtmlFn extends Polymer.Element { | ||
static get is() {return 'html-fn';} | ||
static get template() { | ||
return Polymer.html` | ||
<style> | ||
:host { | ||
display: block; | ||
} | ||
</style> | ||
<div id="child"> | ||
[[databoundProperty]] | ||
</div> | ||
`; | ||
} | ||
static get properties() { | ||
return { | ||
databoundProperty: String | ||
}; | ||
} | ||
ready() { | ||
super.ready(); | ||
this.databoundProperty = 'first'; | ||
} | ||
} | ||
|
||
customElements.define(HtmlFn.is, HtmlFn); | ||
|
||
class HtmlFnSub extends HtmlFn { | ||
static get is() {return 'html-fn-sub';} | ||
static get template() { | ||
return Polymer.html` | ||
<div id="super"> | ||
${super.template} | ||
</div> | ||
`; | ||
} | ||
ready() { | ||
super.ready(); | ||
this.databoundProperty = 'second'; | ||
} | ||
} | ||
|
||
customElements.define(HtmlFnSub.is, HtmlFnSub); | ||
|
||
Polymer({ | ||
is: 'html-legacy', | ||
_template: Polymer.html` | ||
<div id="child">[[databoundProperty]]</div> | ||
`, | ||
properties: { | ||
databoundProperty: { | ||
type: String, | ||
value: 'legacy' | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
test('Polymer elements can use html fn', function () { | ||
let el = document.createElement('html-fn'); | ||
document.body.appendChild(el); | ||
assert(el.shadowRoot); | ||
assert.equal(el.$.child.textContent.trim(), 'first'); | ||
}); | ||
|
||
test('subclass elements can embed the superclass template', function() { | ||
let el = document.createElement('html-fn-sub'); | ||
document.body.appendChild(el); | ||
assert(el.$.super); | ||
assert(el.$.child); | ||
assert.equal(el.$.child.textContent.trim(), 'second'); | ||
}); | ||
|
||
test('legacy element works with html fn', function() { | ||
let el = document.createElement('html-legacy'); | ||
document.body.appendChild(el); | ||
assert(el.shadowRoot); | ||
assert.equal(el.$.child.textContent.trim(), 'legacy'); | ||
}); | ||
}); | ||
</script> | ||
</body> | ||
</html> |