Skip to content

Commit 8781c72

Browse files
sylvain-hamelcaitp
authored andcommitted
fix: text that looks like an html tag but is not causes [$sanitize:badparse] error
1 parent 840e889 commit 8781c72

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/ngSanitize/sanitize.js

+4
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ function htmlParser( html, handler ) {
277277
html = html.substring( match[0].length );
278278
match[0].replace( START_TAG_REGEXP, parseStartTag );
279279
chars = false;
280+
} else {
281+
// no ending tag found
282+
if (handler.chars) handler.chars( '<' );
283+
html = html.substring(1);
280284
}
281285
}
282286

test/ngSanitize/sanitizeSpec.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('HTML', function() {
2121

2222
var handler, start, text, comment;
2323
beforeEach(function() {
24+
text = "";
2425
handler = {
2526
start: function(tag, attrs, unary){
2627
start = {
@@ -35,7 +36,7 @@ describe('HTML', function() {
3536
});
3637
},
3738
chars: function(text_){
38-
text = text_;
39+
text += text_;
3940
},
4041
end:function(tag) {
4142
expect(tag).toEqual(start.tag);
@@ -81,6 +82,16 @@ describe('HTML', function() {
8182
expect(text).toEqual('text');
8283
});
8384

85+
it('should parse unterminated tags as regular content', function() {
86+
htmlParser('<a text1 text2 <a text1 text2', handler);
87+
expect(text).toEqual('<a text1 text2 <a text1 text2');
88+
});
89+
90+
it('should accept tag delimiters such as "<" inside real tags', function() {
91+
htmlParser('<p> 10 < 100 </p>', handler);
92+
expect(text).toEqual(' 10 < 100 ');
93+
});
94+
8495
it('should parse newlines in tags', function() {
8596
htmlParser('<\ntag\n attr="value"\n>text<\n/\ntag\n>', handler);
8697
expect(start).toEqual({tag:'tag', attrs:{attr:'value'}, unary:false});
@@ -195,6 +206,12 @@ describe('HTML', function() {
195206
expectHTML('\na\n').toEqual('&#10;a&#10;');
196207
});
197208

209+
it('should accept tag delimiters such as "<" inside real tags (with nesting)', function() {
210+
//this is an integrated version of the 'should accept tag delimiters such as "<" inside real tags' test
211+
expectHTML('<p> 10 < <span>100</span> </p>')
212+
.toEqual('<p> 10 &lt; <span>100</span> </p>');
213+
});
214+
198215
describe('htmlSanitizerWriter', function() {
199216
/* global htmlSanitizeWriter: false */
200217
if (angular.isUndefined(window.htmlSanitizeWriter)) return;

0 commit comments

Comments
 (0)