Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

html5shiv: Unroll `frag.createElement` and adjust unit tests. [jddalton]

  • Loading branch information...
commit 189e939a925c53f3c8e76ae131882937980b8539 1 parent bff47a2
@jdalton jdalton authored
View
40 src/html5shiv-printshiv.js
@@ -77,20 +77,12 @@
* @param {Document|DocumentFragment} ownerDocument The document.
*/
function shivMethods(ownerDocument) {
- var nodeName,
- cache = {},
+ var cache = {},
docCreateElement = ownerDocument.createElement,
docCreateFragment = ownerDocument.createDocumentFragment,
- elements = getElements(),
- frag = docCreateFragment(),
- index = elements.length;
+ frag = docCreateFragment();
- function createDocumentFragment() {
- var node = frag.cloneNode(false);
- return html5.shivMethods ? (shivMethods(node), node) : node;
- }
-
- function createElement(nodeName) {
+ ownerDocument.createElement = function(nodeName) {
// Avoid adding some elements to fragments in IE < 9 because
// * Attributes like `name` or `type` cannot be set/changed once an element
// is inserted into a document/fragment
@@ -98,17 +90,21 @@
// a 403 response, will cause the tab/window to crash
// * Script elements appended to fragments will execute when their `src`
// or `text` property is set
- var node = (cache[nodeName] || (cache[nodeName] = docCreateElement(nodeName))).cloneNode(false);
+ var node = (cache[nodeName] || (cache[nodeName] = docCreateElement(nodeName))).cloneNode();
return html5.shivMethods && node.canHaveChildren && !reSkip.test(nodeName) ? frag.appendChild(node) : node;
- }
-
- while (index--) {
- nodeName = elements[index];
- cache[nodeName] = docCreateElement(nodeName);
- frag.createElement(nodeName);
- }
- ownerDocument.createElement = createElement;
- ownerDocument.createDocumentFragment = createDocumentFragment;
+ };
+
+ ownerDocument.createDocumentFragment = Function('h,f', 'return function(){' +
+ 'var n=f.cloneNode(),c=n.createElement;' +
+ 'h.shivMethods&&(' +
+ // unroll the `createElement` calls
+ getElements().join().replace(/\w+/g, function(nodeName) {
+ cache[nodeName] = docCreateElement(nodeName);
+ frag.createElement(nodeName);
+ return 'c("' + nodeName + '")';
+ }) +
+ ');return n}'
+ )(html5, frag);
}
/*--------------------------------------------------------------------------*/
@@ -165,7 +161,7 @@
* @memberOf html5
* @type Array|String
*/
- 'elements': options.elements || 'abbr article aside audio bdi canvas data datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video'.split(' '),
+ 'elements': options.elements || 'abbr article aside audio bdi canvas data datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video',
/**
* A flag to indicate that the HTML5 style sheet should be inserted.
View
40 src/html5shiv.js
@@ -77,20 +77,12 @@
* @param {Document|DocumentFragment} ownerDocument The document.
*/
function shivMethods(ownerDocument) {
- var nodeName,
- cache = {},
+ var cache = {},
docCreateElement = ownerDocument.createElement,
docCreateFragment = ownerDocument.createDocumentFragment,
- elements = getElements(),
- frag = docCreateFragment(),
- index = elements.length;
+ frag = docCreateFragment();
- function createDocumentFragment() {
- var node = frag.cloneNode(false);
- return html5.shivMethods ? (shivMethods(node), node) : node;
- }
-
- function createElement(nodeName) {
+ ownerDocument.createElement = function(nodeName) {
// Avoid adding some elements to fragments in IE < 9 because
// * Attributes like `name` or `type` cannot be set/changed once an element
// is inserted into a document/fragment
@@ -98,17 +90,21 @@
// a 403 response, will cause the tab/window to crash
// * Script elements appended to fragments will execute when their `src`
// or `text` property is set
- var node = (cache[nodeName] || (cache[nodeName] = docCreateElement(nodeName))).cloneNode(false);
+ var node = (cache[nodeName] || (cache[nodeName] = docCreateElement(nodeName))).cloneNode();
return html5.shivMethods && node.canHaveChildren && !reSkip.test(nodeName) ? frag.appendChild(node) : node;
- }
-
- while (index--) {
- nodeName = elements[index];
- cache[nodeName] = docCreateElement(nodeName);
- frag.createElement(nodeName);
- }
- ownerDocument.createElement = createElement;
- ownerDocument.createDocumentFragment = createDocumentFragment;
+ };
+
+ ownerDocument.createDocumentFragment = Function('h,f', 'return function(){' +
+ 'var n=f.cloneNode(),c=n.createElement;' +
+ 'h.shivMethods&&(' +
+ // unroll the `createElement` calls
+ getElements().join().replace(/\w+/g, function(nodeName) {
+ cache[nodeName] = docCreateElement(nodeName);
+ frag.createElement(nodeName);
+ return 'c("' + nodeName + '")';
+ }) +
+ ');return n}'
+ )(html5, frag);
}
/*--------------------------------------------------------------------------*/
@@ -165,7 +161,7 @@
* @memberOf html5
* @type Array|String
*/
- 'elements': options.elements || 'abbr article aside audio bdi canvas data datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video'.split(' '),
+ 'elements': options.elements || 'abbr article aside audio bdi canvas data datalist details figcaption figure footer header hgroup mark meter nav output progress section summary time video',
/**
* A flag to indicate that the HTML5 style sheet should be inserted.
View
2  test/javascript.3.html
@@ -10,7 +10,7 @@
<section>This sentence is in a green box <mark>with these words highlighted</mark>.</section>
<script id="script">
var frag = document.createDocumentFragment();
- var fragDiv = ('createElement' in frag) ? frag.createElement('div') : frag.appendChild(document.createElement('div'));
+ var fragDiv = frag.appendChild(document.createElement('div'));
fragDiv.innerHTML = '<section>This native javascript sentence is in a green box <mark>with these words highlighted</mark>?</section>';
document.body.appendChild(fragDiv);
</script>
View
2  test/javascript.4.html
@@ -10,7 +10,7 @@
<section>This sentence is in a green box <mark>with these words highlighted</mark>.</section>
<script id="script">
var frag = document.createDocumentFragment();
- var fragDiv = ('createElement' in frag) ? frag.createElement('div') : frag.appendChild(document.createElement('div'));
+ var fragDiv = frag.appendChild(document.createElement('div'));
fragDiv.innerHTML = '<section>This native javascript sentence is in a green box <mark>with these words highlighted</mark>?</section>';
var fragDivClone = fragDiv.cloneNode(true);
document.body.appendChild(fragDivClone);
View
2  test/perf.1.html
@@ -22,7 +22,7 @@
document.body.appendChild(frag);
frag = document.createDocumentFragment();
- fragDiv = ('createElement' in frag) ? frag.createElement('div') : frag.appendChild(document.createElement('div'));
+ fragDiv = frag.appendChild(document.createElement('div'));
fragDiv.innerHTML = '<section>This native javascript sentence is in a green box <mark>with these words highlighted</mark>?</section>';
document.body.appendChild(fragDiv);
Please sign in to comment.
Something went wrong with that request. Please try again.