@@ -421,50 +421,28 @@ function $SanitizeProvider() {
421
421
}
422
422
423
423
/**
424
- * Create an inert document that contains the dirty HTML that needs sanitizing
425
- * Depending upon browser support we use one of three strategies for doing this.
426
- * Support: Safari 10.x -> XHR strategy
427
- * Support: Firefox -> DomParser strategy
424
+ * Create an inert document that contains the dirty HTML that needs sanitizing.
425
+ * We use the DOMParser API by default and fall back to createHTMLDocument if DOMParser is not
426
+ * available.
428
427
*/
429
428
var getInertBodyElement /* function(html: string): HTMLBodyElement */ = ( function ( window , document ) {
430
- var inertDocument ;
431
- if ( document && document . implementation ) {
432
- inertDocument = document . implementation . createHTMLDocument ( 'inert' ) ;
433
- } else {
434
- throw $sanitizeMinErr ( 'noinert' , 'Can\'t create an inert html document' ) ;
429
+ if ( isDOMParserAvailable ( ) ) {
430
+ return getInertBodyElement_DOMParser ;
435
431
}
436
- var inertBodyElement = ( inertDocument . documentElement || inertDocument . getDocumentElement ( ) ) . querySelector ( 'body' ) ;
437
432
438
- // Check for the Safari 10.1 bug - which allows JS to run inside the SVG G element
439
- inertBodyElement . innerHTML = '<svg><g onload="this.parentNode.remove()"></g></svg>' ;
440
- if ( ! inertBodyElement . querySelector ( 'svg' ) ) {
441
- return getInertBodyElement_XHR ;
442
- } else {
443
- // Check for the Firefox bug - which prevents the inner img JS from being sanitized
444
- inertBodyElement . innerHTML = '<svg><p><span><img src="</span><img src=x onerror=alert(1)//">' ;
445
- if ( inertBodyElement . querySelector ( 'svg img' ) ) {
446
- return getInertBodyElement_DOMParser ;
447
- } else {
448
- return getInertBodyElement_InertDocument ;
449
- }
433
+ if ( ! document || ! document . implementation ) {
434
+ throw $sanitizeMinErr ( 'noinert' , 'Can\'t create an inert html document' ) ;
450
435
}
436
+ var inertDocument = document . implementation . createHTMLDocument ( 'inert' ) ;
437
+ var inertBodyElement = ( inertDocument . documentElement || inertDocument . getDocumentElement ( ) ) . querySelector ( 'body' ) ;
438
+ return getInertBodyElement_InertDocument ;
451
439
452
- function getInertBodyElement_XHR ( html ) {
453
- // We add this dummy element to ensure that the rest of the content is parsed as expected
454
- // e.g. leading whitespace is maintained and tags like `<meta>` do not get hoisted to the `<head>` tag.
455
- html = '<remove></remove>' + html ;
440
+ function isDOMParserAvailable ( ) {
456
441
try {
457
- html = encodeURI ( html ) ;
442
+ return ! ! getInertBodyElement_DOMParser ( '' ) ;
458
443
} catch ( e ) {
459
- return undefined ;
444
+ return false ;
460
445
}
461
- var xhr = new window . XMLHttpRequest ( ) ;
462
- xhr . responseType = 'document' ;
463
- xhr . open ( 'GET' , 'data:text/html;charset=utf-8,' + html , false ) ;
464
- xhr . send ( null ) ;
465
- var body = xhr . response . body ;
466
- body . firstChild . remove ( ) ;
467
- return body ;
468
446
}
469
447
470
448
function getInertBodyElement_DOMParser ( html ) {
0 commit comments