Skip to content

Commit 2cb9497

Browse files
committed
Fixed issue where compiler would pass in detached text node if previous markup would have removed it.
1 parent d9abfe8 commit 2cb9497

4 files changed

Lines changed: 59 additions & 16 deletions

File tree

regression/jqLite_after_NPE.html

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2+
<html xmlns:ng="http://angularjs.org">
3+
<head>
4+
<script type="text/javascript" src="../src/angular-bootstrap.js" ng:autobind></script>
5+
<script type="text/javascript">
6+
angular.markup('-'+'--', function(text, textNode, parentElement) {
7+
var compiler = this;
8+
var index = text.indexOf('-'+'--');
9+
if (index > -1) {
10+
textNode.after(text.substring(index + 3));
11+
textNode.after('<hr/>');
12+
textNode.after(compiler.text(text.substring(0, index)));
13+
textNode.remove();
14+
}
15+
});
16+
</script>
17+
</head>
18+
<body>
19+
{{1+3}}
20+
xxx
21+
---
22+
xxx
23+
<select name="something"><option selected="true">{{'a'}}</option><option value="">{{'b'}}</option><option>C</option></select></body>
24+
</html>

src/Compiler.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,17 @@ Compiler.prototype = {
158158
}
159159
if (descend){
160160
// process markup for text nodes only
161-
eachTextNode(element, function(textNode){
162-
var text = textNode.text();
163-
foreach(self.markup, function(markup){
164-
markup.call(selfApi, text, textNode, element);
165-
});
166-
});
161+
for(var i=0, child=element[0].childNodes;
162+
i<child.length; i++) {
163+
if (isTextNode(child[i])) {
164+
foreach(self.markup, function(markup){
165+
if (i<child.length) {
166+
var textNode = jqLite(child[i]);
167+
markup.call(selfApi, textNode.text(), textNode, element);
168+
}
169+
});
170+
}
171+
}
167172
}
168173

169174
if (directives) {
@@ -187,15 +192,6 @@ Compiler.prototype = {
187192
}
188193
};
189194

190-
function eachTextNode(element, fn){
191-
var i, chldNodes = element[0].childNodes || [], chld;
192-
for (i = 0; i < chldNodes.length; i++) {
193-
if(isTextNode(chld = chldNodes[i])) {
194-
fn(jqLite(chld), i);
195-
}
196-
}
197-
}
198-
199195
function eachNode(element, fn){
200196
var i, chldNodes = element[0].childNodes || [], chld;
201197
for (i = 0; i < chldNodes.length; i++) {

src/markups.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ angularTextMarkup('{{}}', function(text, textNode, parentElement) {
5252
cursor.after(newElement);
5353
cursor = newElement;
5454
});
55+
textNode.remove();
5556
}
56-
textNode.remove();
5757
}
5858
});
5959

test/CompilerSpec.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,27 @@ describe('compiler', function(){
134134
expect(scope.$element.text()).toEqual('3');
135135
});
136136

137+
it('should allow multiple markups per text element', function(){
138+
markup.push(function(text, textNode, parent){
139+
var index = text.indexOf('---');
140+
if (index > -1) {
141+
textNode.after(text.substring(index + 3));
142+
textNode.after("<hr/>");
143+
textNode.after(text.substring(0, index));
144+
textNode.remove();
145+
}
146+
});
147+
markup.push(function(text, textNode, parent){
148+
var index = text.indexOf('===');
149+
if (index > -1) {
150+
textNode.after(text.substring(index + 3));
151+
textNode.after("<p>");
152+
textNode.after(text.substring(0, index));
153+
textNode.remove();
154+
}
155+
});
156+
var scope = compile('A---B---C===D');
157+
expect(sortedHtml(scope.$element)).toEqual('<div>A<hr></hr>B<hr></hr>C<p></p>D</div>');
158+
});
159+
137160
});

0 commit comments

Comments
 (0)