Skip to content

Commit a43720e

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 8df0eef commit a43720e

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
@@ -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: {
@@ -557,6 +566,13 @@ function addTest(manifest, test, tests) {
557566
self.skip();
558567
}
559568

569+
// if xmldom not loaded, skip HTML tests
570+
if(isJsonLdType(test, 'jld:HtmlTest') && !htmlSupport) {
571+
console.log('Skipping test due to lack of HTML support:',
572+
{id: test['@id'], name: test.name});
573+
self.skip();
574+
}
575+
560576
// skip based on test type
561577
if(isJsonLdType(test, SKIP_TESTS)) {
562578
if(options.verboseSkip) {

0 commit comments

Comments
 (0)