Skip to content

Commit e4faee2

Browse files
committed
Test for DOMParser slightly more complicated.
Skip HTML tests if there is no DOMParser, or loading the module raises an exception. Allows Karma tests to pass.
1 parent bd0a40c commit e4faee2

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/jsonld.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,14 @@ jsonld.documentLoaders.xhr = require('./documentLoaders/xhr');
10051005

10061006
// Optional DOM parser
10071007
try {
1008-
jsonld.domParser = require('xmldom').DOMParser;
1008+
jsonld.domParser = require('xmldom').DOMParser || class NoDOMParser {
1009+
parseFromString() {
1010+
throw new JsonLdError(
1011+
'Could not parse HTML document. ' +
1012+
'HTML parsing not implemented.', 'jsonld.LoadDocumentError',
1013+
{code: 'loading document failed'});
1014+
}
1015+
};
10091016
} catch(e) {
10101017
jsonld.domParser = class NoDOMParser {
10111018
parseFromString() {

tests/test-common.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ const manifest = options.manifest || {
2525
filename: '/'
2626
};
2727

28+
let htmlSupport;
29+
try {
30+
// xmldom may load but not have a DOMParser
31+
htmlSupport = !!require('xmldom').DOMParser;
32+
} catch(e) {
33+
htmlSupport = false;
34+
}
35+
console.log("HTML Support: " + htmlSupport);
36+
2837
const TEST_TYPES = {
2938
'jld:CompactTest': {
3039
skip: {
@@ -379,6 +388,13 @@ function addTest(manifest, test, tests) {
379388
self.skip();
380389
}
381390

391+
// if xmldom not loaded, skip HTML tests
392+
if(isJsonLdType(test, 'jld:HtmlTest') && !htmlSupport) {
393+
console.log('Skipping test due to lack of HTML support:',
394+
{id: test['@id'], name: test.name});
395+
self.skip();
396+
}
397+
382398
// skip based on test type
383399
if(isJsonLdType(test, SKIP_TESTS)) {
384400
if(options.verboseSkip) {

0 commit comments

Comments
 (0)