mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-02 09:00:34 +08:00
refactor(jqLite): make HTML-parsing constructor more robust
Previously, the jqLite constructor was limited and would be unable to circumvent many of the HTML5 spec's "allowed content" policies for various nodes. This led to complicated and gross hacks around this in the HTML compiler. This change refactors these hacks by simplifying them, and placing them in jqLite rather than in $compile, in order to better support these things, and simplify code. While the new jqLite constructor is still not even close to as robust as jQuery, it should be more than suitable enough for the needs of the framework, while adding minimal code. Closes #6941 Closes #6958
This commit is contained in:
@@ -138,6 +138,7 @@
|
||||
"JQLitePrototype": false,
|
||||
"addEventListenerFn": false,
|
||||
"removeEventListenerFn": false,
|
||||
"jqLiteIsTextNode": false,
|
||||
|
||||
/* apis.js */
|
||||
"hashKey": false,
|
||||
|
||||
@@ -179,6 +179,81 @@ 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;
|
||||
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;
|
||||
|
||||
if (jqLiteIsTextNode(html)) {
|
||||
// Convert non-html into a text node
|
||||
nodes.push(context.createTextNode(html));
|
||||
} else {
|
||||
// Convert html into DOM nodes
|
||||
tmp = tmp || fragment.appendChild(context.createElement("div"));
|
||||
tag = (TAG_NAME_REGEXP.exec(html) || ["", ""])[1].toLowerCase();
|
||||
wrap = wrapMap[tag] || wrapMap._default;
|
||||
tmp.innerHTML = wrap[1] + html.replace(XHTML_TAG_REGEXP, "<$1></$2>") + wrap[2];
|
||||
|
||||
// Descend through wrappers to the right content
|
||||
i = wrap[0];
|
||||
while (i--) {
|
||||
tmp = tmp.lastChild;
|
||||
}
|
||||
|
||||
nodes = concat(nodes, tmp.childNodes);
|
||||
|
||||
tmp = fragment.firstChild;
|
||||
tmp.textContent = "";
|
||||
}
|
||||
|
||||
// Remove wrapper from fragment
|
||||
fragment.textContent = "";
|
||||
fragment.innerHTML = ""; // Clear inner HTML
|
||||
forEach(nodes, function(node) {
|
||||
fragment.appendChild(node);
|
||||
});
|
||||
|
||||
return fragment;
|
||||
}
|
||||
|
||||
function jqLiteParseHTML(html, context) {
|
||||
context = context || document;
|
||||
var parsed;
|
||||
|
||||
if ((parsed = SINGLE_TAG_REGEXP.exec(html))) {
|
||||
return [context.createElement(parsed[1])];
|
||||
}
|
||||
|
||||
if ((parsed = jqLiteBuildFragment(html, context))) {
|
||||
return parsed.childNodes;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////
|
||||
function JQLite(element) {
|
||||
if (element instanceof JQLite) {
|
||||
@@ -195,14 +270,7 @@ 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);
|
||||
var fragment = jqLite(document.createDocumentFragment());
|
||||
fragment.append(this); // detach the elements from the temporary DOM div.
|
||||
jqLiteAddNodes(this, jqLiteParseHTML(element));
|
||||
} else {
|
||||
jqLiteAddNodes(this, element);
|
||||
}
|
||||
|
||||
@@ -513,8 +513,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
|
||||
var hasDirectives = {},
|
||||
Suffix = 'Directive',
|
||||
COMMENT_DIRECTIVE_REGEXP = /^\s*directive\:\s*([\d\w\-_]+)\s+(.*)$/,
|
||||
CLASS_DIRECTIVE_REGEXP = /(([\d\w\-_]+)(?:\:([^;]+))?;?)/,
|
||||
TABLE_CONTENT_REGEXP = /^<\s*(tr|th|td|thead|tbody|tfoot)(\s+[^>]*)?>/i;
|
||||
CLASS_DIRECTIVE_REGEXP = /(([\d\w\-_]+)(?:\:([^;]+))?;?)/;
|
||||
|
||||
// Ref: http://developers.whatwg.org/webappapis.html#event-handler-idl-attributes
|
||||
// The assumption is that future DOM event attribute names will begin with
|
||||
@@ -1259,7 +1258,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
|
||||
|
||||
if (directive.replace) {
|
||||
replaceDirective = directive;
|
||||
$template = directiveTemplateContents(directiveValue);
|
||||
if (jqLiteIsTextNode(directiveValue)) {
|
||||
$template = [];
|
||||
} else {
|
||||
$template = jqLite(directiveValue);
|
||||
}
|
||||
compileNode = $template[0];
|
||||
|
||||
if ($template.length != 1 || compileNode.nodeType !== 1) {
|
||||
@@ -1658,27 +1661,6 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
|
||||
}
|
||||
|
||||
|
||||
function directiveTemplateContents(template) {
|
||||
var type;
|
||||
template = trim(template);
|
||||
if ((type = TABLE_CONTENT_REGEXP.exec(template))) {
|
||||
type = type[1].toLowerCase();
|
||||
var table = jqLite('<table>' + template + '</table>');
|
||||
if (/(thead|tbody|tfoot)/.test(type)) {
|
||||
return table.children(type);
|
||||
}
|
||||
table = table.children('tbody');
|
||||
if (type === 'tr') {
|
||||
return table.children('tr');
|
||||
}
|
||||
return table.children('tr').contents();
|
||||
}
|
||||
return jqLite('<div>' +
|
||||
template +
|
||||
'</div>').contents();
|
||||
}
|
||||
|
||||
|
||||
function compileTemplateUrl(directives, $compileNode, tAttrs,
|
||||
$rootElement, childTranscludeFn, preLinkFns, postLinkFns, previousCompileContext) {
|
||||
var linkQueue = [],
|
||||
@@ -1703,7 +1685,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
|
||||
content = denormalizeTemplate(content);
|
||||
|
||||
if (origAsyncDirective.replace) {
|
||||
$template = directiveTemplateContents(content);
|
||||
if (jqLiteIsTextNode(content)) {
|
||||
$template = [];
|
||||
} else {
|
||||
$template = jqLite(content);
|
||||
}
|
||||
compileNode = $template[0];
|
||||
|
||||
if ($template.length != 1 || compileNode.nodeType !== 1) {
|
||||
|
||||
@@ -95,6 +95,36 @@ describe('jqLite', function() {
|
||||
expect(fragment.length).toBe(1);
|
||||
expect(fragment[0].nodeType).toBe(11);
|
||||
});
|
||||
|
||||
|
||||
it('should allow construction of <option> elements', function() {
|
||||
var nodes = jqLite('<option>');
|
||||
expect(nodes.length).toBe(1);
|
||||
expect(nodes[0].nodeName.toLowerCase()).toBe('option');
|
||||
});
|
||||
|
||||
|
||||
// Special tests for the construction of elements which are restricted (in the HTML5 spec) to
|
||||
// being children of specific nodes.
|
||||
forEach([
|
||||
'caption',
|
||||
'colgroup',
|
||||
'col',
|
||||
'optgroup',
|
||||
'opt',
|
||||
'tbody',
|
||||
'td',
|
||||
'tfoot',
|
||||
'th',
|
||||
'thead',
|
||||
'tr'
|
||||
], function(name) {
|
||||
it('should allow construction of <$NAME$> elements'.replace('$NAME$', name), function() {
|
||||
var nodes = jqLite('<$NAME$>'.replace('$NAME$', name));
|
||||
expect(nodes.length).toBe(1);
|
||||
expect(nodes[0].nodeName.toLowerCase()).toBe(name);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('_data', function() {
|
||||
|
||||
Reference in New Issue
Block a user