Skip to content

Commit 03c4834

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 a41f081 commit 03c4834

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: {
@@ -385,6 +394,13 @@ function addTest(manifest, test, tests) {
385394
self.skip();
386395
}
387396

397+
// if xmldom not loaded, skip HTML tests
398+
if(isJsonLdType(test, 'jld:HtmlTest') && !htmlSupport) {
399+
console.log('Skipping test due to lack of HTML support:',
400+
{id: test['@id'], name: test.name});
401+
self.skip();
402+
}
403+
388404
// skip based on test type
389405
if(isJsonLdType(test, SKIP_TESTS)) {
390406
if(options.verboseSkip) {

0 commit comments

Comments
 (0)