Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($compile): call normalize on nodes for cases when browser creates ... #7450

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,9 +841,37 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
// modify it.
$compileNodes = jqLite($compileNodes);
}

var isText = function(node) {
return node && node.nodeType === Node.TEXT_NODE;
};

var normalizeNode = function(node) {
var child, combined, lastSibling, _i, _len, _ref, _results;
lastSibling = null;
_ref = (node != null ? node.childNodes : void 0) || [];
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
child = _ref[_i];
if (isText(child) && isText(lastSibling)) {
combined = lastSibling.nodeValue + child.nodeValue;
child.nodeValue = combined;
node.removeChild(lastSibling);
} else {
normalizeNode(child);
}
_results.push(lastSibling = child);
}
return _results;
};

// We can not compile top level text elements since text nodes can be merged and we will
// not be able to attach scope data to them, so we will wrap them in <span>
// We also need to call `normalize` on the nodes, because in some cases
// browsers (like IE11) allocate multiple text nodes when parsing a single block of HTML.
forEach($compileNodes, function(node, index){
normalizeNode(node);

if (node.nodeType == 3 /* text node */ && node.nodeValue.match(/\S+/) /* non-empty */ ) {
$compileNodes[index] = node = jqLite(node).wrap('<span></span>').parent()[0];
}
Expand Down
14 changes: 14 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2389,6 +2389,20 @@ describe('$compile', function() {
}));


it('should support interpolating text spread across multiple text nodes', inject(function($rootScope, $compile) {
var rawElement = document.createElement("div");
var textNode1 = document.createTextNode("{{foo");
var textNode2 = document.createTextNode("bar}}");
rawElement.appendChild(textNode1);
rawElement.appendChild(textNode2);

var element = $compile(rawElement)($rootScope);
$rootScope.foobar = "spam eggs";
$rootScope.$digest();
expect(element.text()).toBe("spam eggs");
}));


it('should support custom start/end interpolation symbols in template and directive template',
function() {
module(function($interpolateProvider, $compileProvider) {
Expand Down