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

Commit bcb9d64

Browse files
committed
WIP: void elements, fixups, remove dead code, typos
1 parent f53aa3a commit bcb9d64

File tree

3 files changed

+23
-44
lines changed

3 files changed

+23
-44
lines changed

docs/content/error/$sanitize/ddns.ngdoc docs/content/error/$sanitize/noinert.ngdoc

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
@ngdoc error
2-
@name $sanitize:ddns
3-
@fullName DOMDocument not supported
2+
@name $sanitize:noinert
3+
@fullName Can't create an inert html document
44
@description
55

6-
This error occurs when `$sanitize` sanitizer determines that `DOMDocument` api is not supported by the current browser.
6+
This error occurs when `$sanitize` sanitizer determines that `document.implementation.createHTMLDocument ` api is not supported by the current browser.
77

88
This api is necessary for safe parsing of HTML strings into DOM trees and without it the sanitizer can't sanitize the input.
99

src/ngSanitize/sanitize.js

+14-35
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function sanitizeText(chars) {
149149
// Regular Expressions for parsing tags and attributes
150150
var SURROGATE_PAIR_REGEXP = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g,
151151
// Match everything outside of normal chars and " (quote character)
152-
NON_ALPHANUMERIC_REGEXP = /([^\#-~| |!])/g;
152+
NON_ALPHANUMERIC_REGEXP = /([^\#-~ |!])/g;
153153

154154

155155
// Good source of info about elements and attributes
@@ -236,26 +236,22 @@ function toMap(str, lowercaseKeys) {
236236
return obj;
237237
}
238238

239-
var baseNode;
239+
var inertBodyElement;
240240
(function(window) {
241241
var doc;
242-
if (window.DOMDocument) {
243-
doc = new window.DOMDocument();
244-
} else if (window.document && window.document.implementation) {
242+
if (window.document && window.document.implementation) {
245243
doc = window.document.implementation.createHTMLDocument("inert");
246-
} else if (window.ActiveXObject) {
247-
doc = new window.ActiveXObject("Msxml.DOMDocument");
248244
} else {
249-
throw $sanitizeMinErr('ddns', "DOMDocument not supported");
245+
throw $sanitizeMinErr('noinert', "Can't create an inert html document");
250246
}
251247
var docElement = doc.documentElement || doc.getDocumentElement();
252248
var bodyElements = docElement.getElementsByTagName('body');
253249
if (bodyElements.length === 1) {
254-
baseNode = bodyElements[0];
250+
inertBodyElement = bodyElements[0];
255251
} else {
256252
var html = doc.createElement('html');
257-
baseNode = doc.createElement('body');
258-
html.appendChild(baseNode);
253+
inertBodyElement = doc.createElement('body');
254+
html.appendChild(inertBodyElement);
259255
doc.appendChild(html);
260256
}
261257
})(window);
@@ -278,8 +274,8 @@ function htmlParser(html, handler) {
278274
} else if (typeof html !== 'string') {
279275
html = '' + html;
280276
}
281-
baseNode.innerHTML = html;
282-
var node = baseNode.firstChild;
277+
inertBodyElement.innerHTML = html;
278+
var node = inertBodyElement.firstChild;
283279
while (node) {
284280
switch (node.nodeType) {
285281
case 1: // ELEMENT_NODE
@@ -288,9 +284,6 @@ function htmlParser(html, handler) {
288284
case 3: // TEXT NODE
289285
handler.chars(node.textContent);
290286
break;
291-
case 8: // COMMENT NODE
292-
handler.comment(node.textContent);
293-
break;
294287
}
295288
var nextNode;
296289
if (!(nextNode = node.firstChild)) {
@@ -304,7 +297,7 @@ function htmlParser(html, handler) {
304297
}
305298
while (nextNode == null) {
306299
node = node.parentNode;
307-
if (node === baseNode) break;
300+
if (node === inertBodyElement) break;
308301
nextNode = node.nextSibling;
309302
if (node.nodeType == 1) {
310303
handler.end(node.nodeName.toLowerCase());
@@ -315,8 +308,8 @@ function htmlParser(html, handler) {
315308
node = nextNode;
316309
}
317310

318-
while (node = baseNode.firstChild) {
319-
baseNode.removeChild(node);
311+
while (node = inertBodyElement.firstChild) {
312+
inertBodyElement.removeChild(node);
320313
}
321314
}
322315

@@ -329,20 +322,6 @@ function attrToMap(attrs) {
329322
return map;
330323
}
331324

332-
var hiddenPre=document.createElement("pre");
333-
/**
334-
* decodes all entities into regular string
335-
* @param value
336-
* @returns {string} A string with decoded entities.
337-
*/
338-
function decodeEntities(value) {
339-
if (!value) { return ''; }
340-
341-
hiddenPre.innerHTML = value.replace(/</g,"&lt;");
342-
// innerText depends on styling as it doesn't display hidden elements.
343-
// Therefore, it's better to use textContent not to cause unnecessary reflows.
344-
return hiddenPre.textContent;
345-
}
346325

347326
/**
348327
* Escapes all potentially dangerous characters, so that the
@@ -368,7 +347,7 @@ function encodeEntities(value) {
368347

369348
/**
370349
* create an HTML/XML writer which writes to buffer
371-
* @param {Array} buf use buf.jain('') to get out sanitized html string
350+
* @param {Array} buf use buf.join('') to get out sanitized html string
372351
* @returns {object} in the form of {
373352
* start: function(tag, attrs) {},
374353
* end: function(tag) {},
@@ -405,7 +384,7 @@ function htmlSanitizeWriter(buf, uriValidator) {
405384
},
406385
end: function(tag) {
407386
tag = angular.lowercase(tag);
408-
if (!ignore && validElements[tag] === true) {
387+
if (!ignore && validElements[tag] === true && voidElements[tag] !== true) {
409388
out('</');
410389
out(tag);
411390
out('>');

test/ngSanitize/sanitizeSpec.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ describe('HTML', function() {
5050
};
5151
});
5252

53-
it('should parse comments', function() {
53+
it('should not parse comments', function() {
5454
htmlParser('<!--FOOBAR-->', handler);
55-
expect(comment).toEqual('FOOBAR');
55+
expect(comment).not.toBeDefined();
5656
});
5757

5858
it('should parse basic format', function() {
@@ -165,7 +165,7 @@ describe('HTML', function() {
165165
});
166166

167167
it('should handle self closed elements', function() {
168-
expectHTML('a<hr/>c').toEqual('a<hr></hr>c');
168+
expectHTML('a<hr/>c').toEqual('a<hr>c');
169169
});
170170

171171
it('should handle namespace', function() {
@@ -192,7 +192,7 @@ describe('HTML', function() {
192192

193193
it('should ignore back slash as escape', function() {
194194
expectHTML('<img alt="xxx\\" title="><script>....">').
195-
toEqual('<img alt="xxx\\" title="&gt;&lt;script&gt;...."></img>');
195+
toEqual('<img alt="xxx\\" title="&gt;&lt;script&gt;....">');
196196
});
197197

198198
it('should ignore object attributes', function() {
@@ -415,11 +415,11 @@ describe('HTML', function() {
415415
inject(function() {
416416
$$sanitizeUri.andReturn('someUri');
417417

418-
expectHTML('<img src="someUri"/>').toEqual('<img src="someUri"></img>');
418+
expectHTML('<img src="someUri"/>').toEqual('<img src="someUri">');
419419
expect($$sanitizeUri).toHaveBeenCalledWith('someUri', true);
420420

421421
$$sanitizeUri.andReturn('unsafe:someUri');
422-
expectHTML('<img src="someUri"/>').toEqual('<img></img>');
422+
expectHTML('<img src="someUri"/>').toEqual('<img>');
423423
});
424424
});
425425

0 commit comments

Comments
 (0)