-
Notifications
You must be signed in to change notification settings - Fork 27.4k
refactor(jqLite): make HTML-parsing constructor more robust #6958
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,6 +179,75 @@ function jqLitePatchJQueryRemove(name, dispatchThis, filterElems, getterIfNoArgu | |
} | ||
} | ||
|
||
var SINGLE_TAG_REGEXP = /^<(\w+)\s*\/?>(?:<\/\1>|)$/; | ||
var HTML_REGEXP = /<|&#?\w+;/; | ||
var TAG_NAME_REGEXP = /<([\w:]+)/; | ||
var XHTML_TAG_REGEXP = /<(?!area|br|col|embed|hr|img|input|link|meta|param)(([\w:]+)[^>]*)\/>/gi; | ||
|
||
var wrapMap = { | ||
'option': [1, '<select multiple="multiple">', '</select>'], | ||
|
||
'thead': [1, '<table>', '</table>'], | ||
'col': [2, '<table><colgroup>', '</colgroup></table>'], | ||
'tr': [2, '<table><tbody>', '</tbody></table>'], | ||
'td': [3, '<table><tbody><tr>', '</tr></tbody></table>'], | ||
'_default': [0, "", ""] | ||
}; | ||
|
||
wrapMap.optgroup = wrapMap.option; | ||
wrapMap.tbody = wrapMap.tfoot = wrapMap.colgroup = wrapMap.caption = wrapMap.thead; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing this stuff does potentially break IE8, unfortunately :( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, it will. this means that we should not port this to 1.2.x There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any further insight from jQuery on how to get this working in IE8? ♿ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's probably possible, I'm working on cherry-picking this into another branch to test against v1.2.x right now, so I can try to make it work there --- Testing for IE8 is at #6963, although I expect the first build to fail. |
||
wrapMap.th = wrapMap.td; | ||
|
||
function jqLiteIsTextNode(html) { | ||
return !HTML_REGEXP.test(html); | ||
} | ||
|
||
function jqLiteBuildFragment(html, context) { | ||
var elem, tmp, tag, wrap, | ||
fragment = context.createDocumentFragment(), | ||
nodes = [], i, j, jj; | ||
|
||
if (jqLiteIsTextNode(html)) { | ||
// Convert non-html into a text node | ||
nodes.push(context.createTextNode(html)); | ||
} else { | ||
tmp = fragment.appendChild(context.createElement('div')); | ||
// Convert html into DOM nodes | ||
tag = (TAG_NAME_REGEXP.exec(html) || ["", ""])[1].toLowerCase(); | ||
wrap = wrapMap[tag] || wrapMap._default; | ||
tmp.innerHTML = '<div> </div>' + | ||
wrap[1] + html.replace(XHTML_TAG_REGEXP, "<$1></$2>") + wrap[2]; | ||
tmp.removeChild(tmp.firstChild); | ||
|
||
// Descend through wrappers to the right content | ||
i = wrap[0]; | ||
while (i--) { | ||
tmp = tmp.lastChild; | ||
} | ||
|
||
for (j=0, jj=tmp.childNodes.length; j<jj; ++j) nodes.push(tmp.childNodes[j]); | ||
|
||
tmp = fragment.firstChild; | ||
tmp.textContent = ""; | ||
} | ||
|
||
// Remove wrapper from fragment | ||
fragment.textContent = ""; | ||
fragment.innerHTML = ""; // Clear inner HTML | ||
return nodes; | ||
} | ||
|
||
function jqLiteParseHTML(html, context) { | ||
context = context || document; | ||
var parsed; | ||
|
||
if ((parsed = SINGLE_TAG_REGEXP.exec(html))) { | ||
return [context.createElement(parsed[1])]; | ||
} | ||
|
||
return jqLiteBuildFragment(html, context); | ||
} | ||
|
||
///////////////////////////////////////////// | ||
function JQLite(element) { | ||
if (element instanceof JQLite) { | ||
|
@@ -195,14 +264,9 @@ function JQLite(element) { | |
} | ||
|
||
if (isString(element)) { | ||
var div = document.createElement('div'); | ||
// Read about the NoScope elements here: | ||
// http://msdn.microsoft.com/en-us/library/ms533897(VS.85).aspx | ||
div.innerHTML = '<div> </div>' + element; // IE insanity to make NoScope elements work! | ||
div.removeChild(div.firstChild); // remove the superfluous div | ||
jqLiteAddNodes(this, div.childNodes); | ||
jqLiteAddNodes(this, jqLiteParseHTML(element)); | ||
var fragment = jqLite(document.createDocumentFragment()); | ||
fragment.append(this); // detach the elements from the temporary DOM div. | ||
fragment.append(this); | ||
} else { | ||
jqLiteAddNodes(this, element); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a bigger change than it really is. The
wrapMap
(essentially stolen from jQuery, credit where credit is due) greatly simplifies the hacks that previously lived in compile.js, and makes it much easier to extend in the future if needed. This is a big deal, IMO.