Skip to content

Commit a3cfb26

Browse files
committed
Fix domparser on phantomjs
Signed-off-by: Julius Härtl <jus@bitgrid.net>
1 parent 0b3f2c0 commit a3cfb26

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

core/js/tests/html-domparser.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* DOMParser HTML extension
3+
* 2012-09-04
4+
*
5+
* By Eli Grey, http://eligrey.com
6+
* Public domain.
7+
* NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
8+
*/
9+
10+
/*! @source https://gist.github.com/1129031 */
11+
/*global document, DOMParser*/
12+
13+
(function(DOMParser) {
14+
"use strict";
15+
16+
var
17+
DOMParser_proto = DOMParser.prototype
18+
, real_parseFromString = DOMParser_proto.parseFromString
19+
;
20+
21+
// Firefox/Opera/IE throw errors on unsupported types
22+
try {
23+
// WebKit returns null on unsupported types
24+
if ((new DOMParser).parseFromString("", "text/html")) {
25+
// text/html parsing is natively supported
26+
return;
27+
}
28+
} catch (ex) {}
29+
30+
DOMParser_proto.parseFromString = function(markup, type) {
31+
if (/^\s*text\/html\s*(?:;|$)/i.test(type)) {
32+
var
33+
doc = document.implementation.createHTMLDocument("")
34+
;
35+
if (markup.toLowerCase().indexOf('<!doctype') > -1) {
36+
doc.documentElement.innerHTML = markup;
37+
}
38+
else {
39+
doc.body.innerHTML = markup;
40+
}
41+
return doc;
42+
} else {
43+
return real_parseFromString.apply(this, arguments);
44+
}
45+
};
46+
}(DOMParser));

tests/karma.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ module.exports = function(config) {
127127
testCore = true;
128128
}
129129

130+
files.push(corePath + 'tests/html-domparser.js');
130131
files.push('core/js/dist/main.js');
131132
// core mocks
132133
files.push(corePath + 'tests/specHelper.js');

0 commit comments

Comments
 (0)