Skip to content

Commit dca5bf1

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 db86df3 commit dca5bf1

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: {
@@ -378,6 +387,13 @@ function addTest(manifest, test, tests) {
378387
self.skip();
379388
}
380389

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

0 commit comments

Comments
 (0)