UNPKG

@polymer/polymer

Version:

The Polymer library makes it easy to create your own web components. Give your element some markup and properties, and then use it on a site. Polymer provides features like dynamic templates and data binding to reduce the amount of boilerplate you need to

142 lines (133 loc) 4.89 kB
<!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>the \`price\` is \${{foo}}\n</span>`; assert.equal(t.innerHTML, '<span>the `price` is ${{foo}}\n</span>'); }); test('interpolation of non-templates throw', function() { assert.throws(() => Polymer.html`<div>${'a'}</div>`); }); test('interpolation of templates 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>'); }); test('interpolation of literal values', function() { let s1 = Polymer.htmlLiteral`hello`; assert.equal(s1.value, `hello`); let s2 = Polymer.htmlLiteral`hello${Polymer.htmlLiteral` ${Polymer.htmlLiteral`world`}!`}!`; assert.equal(s2.value, 'hello world!!'); }); test('interpolation of literal values within html values', function() { let s1 = Polymer.htmlLiteral`foo`; let s2 = Polymer.htmlLiteral`display: block;`; let t = Polymer.html`<style>:host {${s2}}</style><div ${s1}></div>`; assert.equal(t.innerHTML, `<style>:host {display: block;}</style><div foo=""></div>`); }); }); 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>