forked from webcomponents/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
port template.js from webcomponentsjs
- Loading branch information
1 parent
6692522
commit 47b57a7
Showing
9 changed files
with
627 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
dist |
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,20 @@ | ||
language: node_js | ||
sudo: required | ||
dist: trusty | ||
node_js: 6 | ||
addons: | ||
firefox: latest | ||
apt: | ||
sources: | ||
- google-chrome | ||
packages: | ||
- google-chrome-stable | ||
script: | ||
- xvfb-run wct | ||
- if [ "${TRAVIS_PULL_REQUEST}" = "false" ]; then wct -s 'windows 10/microsoftedge@14' | ||
-s 'windows 8.1/internet explorer@11' -s 'os x 10.11/safari@10' -s 'os x 10.11/safari@9'; | ||
fi | ||
env: | ||
global: | ||
- secure: HJqGZudd8jAegvRbU8MYMtGiLbfu8C1hbz+occ0ML5vfKgggsv2JrHSgdo1cF9gRkZu5zp389v00za+P0Fm6dFs+VA4Xp0/kC/gDUloRfp01Mnkdz171GoO0NpZo6iQWgN98ih72DrVUyhGi3Enj0y8JVPmIwMkosW/EGEJURIegl9EKW8VE3Hvo79zwXi3FN2JzNja20MmQUWH8LXypfNJfx9LKlwzXnvD+tYGZHV80lM7qadTwfw9uJIucd5cSevuZeuA0vZIVv0eLy09Hk/pQedZo8KOTrbjXezlJGHUDvkcYpUjUtQu7so71aVkdEoMUlV0CGBavreS17GTWn5KP2IV9Pu4QmnxMmjkIhBnuF/LftRDTAE/JqzenTi2ep0MsPlFiK963uuT/A/y4hD3JDaFTFUtY7XCScgEnyTMPpLJKDBgnUejLJOIiC6ETgRkCXM/kTq3tbWbNYMbcuMN6jcRvIQ0OezoAsYElDDqBzpa0Ou9vul/k5+gTz0A2bvALKfOQuacGXchUU2U9gFqvz6MJx+lSZK7SPZgQS+k51WEHJjCHCfHMOZ0R21DtAMigijou8PL8Mqkw/blczoSY/21qjNYxlLpIblRIWTVWdsHzJbmTHuDDwuoYQdre62uRW3GhO6gINYAQy5ymE8GF8eUyTQLxvnL/H7Uo1EU= | ||
- secure: pF2Y4srYs2GBrNj0ZoTcI+BKtIePMCU6BLP9QcVSJRWN/B2Lo4f41awsMpIV2c5QtFy4wMJyaAXEhgqHDCxedWYsICS4+/gS6fgZaCndF5xNTrVFy/nQmSaqZxJf/MppbDDZKbzX0fofQPxZ+IKP4LMzNvN8xoIkxXiqbVk0/qbKqkJqs7NkB5n7pexIXMYNH9xX4jK6p8aHWHAAYGqz2X8sQ24TyS9EOQh+gjABPnzjGEi1MhpY5faz1xrjcs6Vjv60Xt/6aXOTJGLvn0Ojbu5gHh/w6XsX2RQYPS0ZIOiraxosgPFejhrzLoaRZW/1+Fb9YGiEkXgjRPFv1/V/6p+GjZLH9bEZdYxGNGjAPNQa80EsMOoztMlk2JNbfTK0PF+GfjHi/XQvR0xc/z1Cpjw2j01iFVF8TXsaxY3Ag2DcFPJpD1NNUtwD/moa+1XSgY+lgw1LQbFhuSuZWFsXBbkjW55VYOiP6zGMzHvEgmvnBuqJxjFocwFfKU3RPemek7wqxloTX+lJMOBmOh5zjhMzTpHpbW7CeAfOzmvv0Od1OfN+icPOyJbtMoPNkbAIEAOCXVgbFYPxkwjYgkbaWRnGy3QHyoiQETv9VFMbRm+JudKlcXj7AJblE6zwk/xx6bhpBeWJFRpNG07JjHN8uDg7QLRF2T/qlLou6K2FNt0= |
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,19 @@ | ||
# License | ||
|
||
Everything in this repo is BSD style license unless otherwise specified. | ||
|
||
Copyright (c) 2016 The Polymer Authors. All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following disclaimer | ||
in the documentation and/or other materials provided with the | ||
distribution. | ||
* Neither the name of Google Inc. nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
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 |
---|---|---|
@@ -1,2 +1,8 @@ | ||
# template | ||
Minimal polyfill for <template> | ||
A minimal polyfill for <template>. | ||
|
||
## License | ||
|
||
Everything in this repository is BSD style license unless otherwise specified. | ||
|
||
Copyright (c) 2016 The Polymer Authors. All rights reserved. |
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,25 @@ | ||
{ | ||
"name": "template", | ||
"version": "0.0.1", | ||
"description": "Minimal template polyfills needed by webcomponents.js", | ||
"main": "template.js", | ||
"directories": { | ||
"test": "tests" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/webcomponents/template.git" | ||
}, | ||
"author": "The Polymer Authors", | ||
"license": "BSD-3-Clause", | ||
"bugs": { | ||
"url": "https://github.com/webcomponents/template/issues" | ||
}, | ||
"scripts": { | ||
"test": "wct" | ||
}, | ||
"homepage": "http://webcomponents.org", | ||
"devDependencies": { | ||
"web-component-tester": "^5.0.0" | ||
} | ||
} |
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,253 @@ | ||
/** | ||
* @license | ||
* Copyright (c) 2016 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 | ||
*/ | ||
|
||
// minimal template polyfill | ||
(function() { | ||
var needsTemplate = (typeof HTMLTemplateElement === 'undefined'); | ||
// NOTE: Patch document.importNode to work around IE11 bug that | ||
// casues children of a document fragment imported while | ||
// there is a mutation observer to not have a parentNode (!?!) | ||
// This needs to happen *after* patching importNode to fix template cloning | ||
if (/Trident/.test(navigator.userAgent)) { | ||
(function() { | ||
var importNode = document.importNode; | ||
document.importNode = function() { | ||
var n = importNode.apply(document, arguments); | ||
// Copy all children to a new document fragment since | ||
// this one may be broken | ||
if (n.nodeType === Node.DOCUMENT_FRAGMENT_NODE) { | ||
var f = document.createDocumentFragment(); | ||
f.appendChild(n); | ||
return f; | ||
} else { | ||
return n; | ||
} | ||
}; | ||
})(); | ||
} | ||
|
||
// returns true if nested templates cannot be cloned (they cannot be on | ||
// some impl's like Safari 8) | ||
var needsCloning = (function() { | ||
if (!needsTemplate) { | ||
var t = document.createElement('template'); | ||
var t2 = document.createElement('template'); | ||
t2.content.appendChild(document.createElement('div')); | ||
t.content.appendChild(t2); | ||
var clone = t.cloneNode(true); | ||
return (clone.content.childNodes.length === 0 || clone.content.firstChild.content.childNodes.length === 0); | ||
} | ||
})(); | ||
|
||
var TEMPLATE_TAG = 'template'; | ||
var TemplateImpl = function() {}; | ||
|
||
if (needsTemplate) { | ||
|
||
var contentDoc = document.implementation.createHTMLDocument('template'); | ||
var canDecorate = true; | ||
|
||
var templateStyle = document.createElement('style'); | ||
templateStyle.textContent = TEMPLATE_TAG + '{display:none;}'; | ||
|
||
var head = document.head; | ||
head.insertBefore(templateStyle, head.firstElementChild); | ||
|
||
/** | ||
Provides a minimal shim for the <template> element. | ||
*/ | ||
TemplateImpl.prototype = Object.create(HTMLElement.prototype); | ||
|
||
/** | ||
The `decorate` method moves element children to the template's `content`. | ||
NOTE: there is no support for dynamically adding elements to templates. | ||
*/ | ||
TemplateImpl.decorate = function(template) { | ||
// if the template is decorated, return fast | ||
if (template.content) { | ||
return; | ||
} | ||
template.content = contentDoc.createDocumentFragment(); | ||
var child; | ||
while (child = template.firstChild) { | ||
template.content.appendChild(child); | ||
} | ||
|
||
template.cloneNode = function(deep) { | ||
return TemplateImpl.cloneNode(this, deep); | ||
}; | ||
|
||
// add innerHTML to template, if possible | ||
// Note: this throws on Safari 7 | ||
if (canDecorate) { | ||
try { | ||
Object.defineProperty(template, 'innerHTML', { | ||
get: function() { | ||
var o = ''; | ||
for (var e = this.content.firstChild; e; e = e.nextSibling) { | ||
o += e.outerHTML || escapeData(e.data); | ||
} | ||
return o; | ||
}, | ||
set: function(text) { | ||
contentDoc.body.innerHTML = text; | ||
TemplateImpl.bootstrap(contentDoc); | ||
while (this.content.firstChild) { | ||
this.content.removeChild(this.content.firstChild); | ||
} | ||
while (contentDoc.body.firstChild) { | ||
this.content.appendChild(contentDoc.body.firstChild); | ||
} | ||
}, | ||
configurable: true | ||
}); | ||
|
||
} catch (err) { | ||
canDecorate = false; | ||
} | ||
} | ||
// bootstrap recursively | ||
TemplateImpl.bootstrap(template.content); | ||
}; | ||
|
||
/** | ||
The `bootstrap` method is called automatically and "fixes" all | ||
<template> elements in the document referenced by the `doc` argument. | ||
*/ | ||
TemplateImpl.bootstrap = function(doc) { | ||
var templates = doc.querySelectorAll(TEMPLATE_TAG); | ||
for (var i=0, l=templates.length, t; (i<l) && (t=templates[i]); i++) { | ||
TemplateImpl.decorate(t); | ||
} | ||
}; | ||
|
||
// auto-bootstrapping for main document | ||
document.addEventListener('DOMContentLoaded', function() { | ||
TemplateImpl.bootstrap(document); | ||
}); | ||
|
||
// Patch document.createElement to ensure newly created templates have content | ||
var createElement = document.createElement; | ||
document.createElement = function() { | ||
'use strict'; | ||
var el = createElement.apply(document, arguments); | ||
if (el.localName === 'template') { | ||
TemplateImpl.decorate(el); | ||
} | ||
return el; | ||
}; | ||
|
||
var escapeDataRegExp = /[&\u00A0<>]/g; | ||
|
||
function escapeReplace(c) { | ||
switch (c) { | ||
case '&': | ||
return '&'; | ||
case '<': | ||
return '<'; | ||
case '>': | ||
return '>'; | ||
case '\u00A0': | ||
return ' '; | ||
} | ||
} | ||
|
||
function escapeData(s) { | ||
return s.replace(escapeDataRegExp, escapeReplace); | ||
} | ||
} | ||
|
||
// make cloning/importing work! | ||
if (needsTemplate || needsCloning) { | ||
// NOTE: we rely on this cloneNode not causing element upgrade. | ||
// This means this polyfill must load before the CE polyfill and | ||
// this would need to be re-worked if a browser supports native CE | ||
// but not <template>. | ||
var nativeCloneNode = Node.prototype.cloneNode; | ||
|
||
TemplateImpl.cloneNode = function(template, deep) { | ||
var clone = nativeCloneNode.call(template, false); | ||
// NOTE: decorate doesn't auto-fix children because they are already | ||
// decorated so they need special clone fixup. | ||
if (this.decorate) { | ||
this.decorate(clone); | ||
} | ||
if (deep) { | ||
// NOTE: use native clone node to make sure CE's wrapped | ||
// cloneNode does not cause elements to upgrade. | ||
clone.content.appendChild( | ||
nativeCloneNode.call(template.content, true)); | ||
// now ensure nested templates are cloned correctly. | ||
this.fixClonedDom(clone.content, template.content); | ||
} | ||
return clone; | ||
}; | ||
|
||
// Given a source and cloned subtree, find <template>'s in the cloned | ||
// subtree and replace them with cloned <template>'s from source. | ||
// We must do this because only the source templates have proper .content. | ||
TemplateImpl.fixClonedDom = function(clone, source) { | ||
// do nothing if cloned node is not an element | ||
if (!source.querySelectorAll) return; | ||
// these two lists should be coincident | ||
var s$ = source.querySelectorAll(TEMPLATE_TAG); | ||
var t$ = clone.querySelectorAll(TEMPLATE_TAG); | ||
for (var i=0, l=t$.length, t, s; i<l; i++) { | ||
s = s$[i]; | ||
t = t$[i]; | ||
if (this.decorate) { | ||
this.decorate(s); | ||
} | ||
t.parentNode.replaceChild(s.cloneNode(true), t); | ||
} | ||
}; | ||
|
||
var originalImportNode = document.importNode; | ||
|
||
// override all cloning to fix the cloned subtree to contain properly | ||
// cloned templates. | ||
Node.prototype.cloneNode = function(deep) { | ||
var dom = nativeCloneNode.call(this, deep); | ||
// template.content is cloned iff `deep`. | ||
if (deep) { | ||
TemplateImpl.fixClonedDom(dom, this); | ||
} | ||
return dom; | ||
}; | ||
|
||
// NOTE: we are cloning instead of importing <template>'s. | ||
// However, the ownerDocument of the cloned template will be correct! | ||
// This is because the native import node creates the right document owned | ||
// subtree and `fixClonedDom` inserts cloned templates into this subtree, | ||
// thus updating the owner doc. | ||
document.importNode = function(element, deep) { | ||
if (element.localName === TEMPLATE_TAG) { | ||
return TemplateImpl.cloneNode(element, deep); | ||
} else { | ||
var dom = originalImportNode.call(document, element, deep); | ||
if (deep) { | ||
TemplateImpl.fixClonedDom(dom, element); | ||
} | ||
return dom; | ||
} | ||
}; | ||
|
||
if (needsCloning) { | ||
HTMLTemplateElement.prototype.cloneNode = function(deep) { | ||
return TemplateImpl.cloneNode(this, deep); | ||
}; | ||
} | ||
} | ||
|
||
if (needsTemplate) { | ||
window.HTMLTemplateElement = TemplateImpl; | ||
} | ||
|
||
})(); |
Oops, something went wrong.