Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
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.
  • Loading branch information
gkellogg committed Apr 9, 2020
commit 03c4834467f335ebdedb92b469bdb26e4fa678b2
9 changes: 8 additions & 1 deletion lib/jsonld.js
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,14 @@ jsonld.documentLoaders.xhr = require('./documentLoaders/xhr');

// Optional DOM parser
try {
jsonld.domParser = require('xmldom').DOMParser;
jsonld.domParser = require('xmldom').DOMParser || class NoDOMParser {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidlehn -- can you comment here? I don't think we can easily support this pattern with webpack. Can you suggest an alternative path forward? Instead of a require here, the user may need to have installed another package themselves that registered a DOM parser with jsonld in a similar way we do with RDF parsers. If so -- we should copy that pattern since it's already used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: PR #341 removes xmldom as a dependency.

This PR here (#347) should be updated to take that into account (specifically line 1022 here).

parseFromString() {
throw new JsonLdError(
'Could not parse HTML document. ' +
'HTML parsing not implemented.', 'jsonld.LoadDocumentError',
{code: 'loading document failed'});
}
};
} catch(e) {
jsonld.domParser = class NoDOMParser {
parseFromString() {
Expand Down
16 changes: 16 additions & 0 deletions tests/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ const manifest = options.manifest || {
filename: '/'
};

let htmlSupport;
try {
// xmldom may load but not have a DOMParser
htmlSupport = !!require('xmldom').DOMParser;
} catch(e) {
htmlSupport = false;
}
console.log("HTML Support: " + htmlSupport);

const TEST_TYPES = {
'jld:CompactTest': {
skip: {
Expand Down Expand Up @@ -385,6 +394,13 @@ function addTest(manifest, test, tests) {
self.skip();
}

// if xmldom not loaded, skip HTML tests
if(isJsonLdType(test, 'jld:HtmlTest') && !htmlSupport) {
console.log('Skipping test due to lack of HTML support:',
{id: test['@id'], name: test.name});
self.skip();
}

// skip based on test type
if(isJsonLdType(test, SKIP_TESTS)) {
if(options.verboseSkip) {
Expand Down