Skip to content

Commit a98355f

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 b2a959f commit a98355f

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
@@ -997,7 +997,14 @@ jsonld.documentLoaders.xhr = require('./documentLoaders/xhr');
997997

998998
// Optional DOM parser
999999
try {
1000-
jsonld.domParser = require('xmldom').DOMParser;
1000+
jsonld.domParser = require('xmldom').DOMParser || class NoDOMParser {
1001+
parseFromString() {
1002+
throw new JsonLdError(
1003+
'Could not parse HTML document. ' +
1004+
'HTML parsing not implemented.', 'jsonld.LoadDocumentError',
1005+
{code: 'loading document failed'});
1006+
}
1007+
};
10011008
} catch(e) {
10021009
jsonld.domParser = class NoDOMParser {
10031010
parseFromString() {

tests/test-common.js

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

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

613+
// if xmldom not loaded, skip HTML tests
614+
if(isJsonLdType(test, 'jld:HtmlTest') && !htmlSupport) {
615+
console.log('Skipping test due to lack of HTML support:',
616+
{id: test['@id'], name: test.name});
617+
self.skip();
618+
}
619+
604620
// skip based on test type
605621
if(isJsonLdType(test, SKIP_TESTS)) {
606622
if(options.verboseSkip) {

0 commit comments

Comments
 (0)