diff --git a/CHANGELOG.md b/CHANGELOG.md index ee977592..def98c9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,9 @@ renamed to `LINK_HEADER_CONTEXT`. It remains for now but will be removed in a future release. +### Added +- Support for `"@import"`. + ## 2.0.2 - 2020-01-17 ### Fixed diff --git a/lib/context.js b/lib/context.js index 0b26c043..be008050 100644 --- a/lib/context.js +++ b/lib/context.js @@ -266,6 +266,53 @@ api.process = async ({ defined.set('@propagate', true); } + // handle @import + if('@import' in ctx) { + const value = ctx['@import']; + if(activeCtx.processingMode === 'json-ld-1.0') { + throw new JsonLdError( + 'Invalid JSON-LD syntax; @import not compatible with ' + + activeCtx.processingMode, + 'jsonld.SyntaxError', + {code: 'invalid context member', context: ctx}); + } + if(!_isString(value)) { + throw new JsonLdError( + 'Invalid JSON-LD syntax; @import must be a string.', + 'jsonld.SyntaxError', + {code: 'invalid @import value', context: localCtx}); + } + + // resolve contexts + const resolvedImport = await options.contextResolver.resolve({ + context: value, + documentLoader: options.documentLoader, + base: options.base + }); + if(resolvedImport.length !== 1) { + throw new JsonLdError( + 'Invalid JSON-LD syntax; @import must reference a single context.', + 'jsonld.SyntaxError', + {code: 'invalid remote context', context: localCtx}); + } + const importCtx = resolvedImport[0].document; + if('@import' in importCtx) { + throw new JsonLdError( + 'Invalid JSON-LD syntax; imported context must not include @import.', + 'jsonld.SyntaxError', + {code: 'invalid context member', context: localCtx}); + } + + // merge ctx into importCtx and replace rval with the result + for(const key in importCtx) { + if(!ctx.hasOwnProperty(key)) { + ctx[key] = importCtx[key]; + } + } + + defined.set('@import', true); + } + // handle @protected; determine whether this sub-context is declaring // all its terms to be "protected" (exceptions can be made on a // per-definition basis) diff --git a/tests/test-common.js b/tests/test-common.js index 1c55deb5..7261fa6a 100644 --- a/tests/test-common.js +++ b/tests/test-common.js @@ -103,18 +103,6 @@ const TEST_TYPES = { /expand-manifest.jsonld#thc05$/, // remote /remote-doc-manifest.jsonld#t0013$/, // HTML - // @import - /expand-manifest.jsonld#tso01$/, - /expand-manifest.jsonld#tso02$/, - /expand-manifest.jsonld#tso03$/, - /expand-manifest.jsonld#tso05$/, - /expand-manifest.jsonld#tso06$/, - // @import and protected - /expand-manifest.jsonld#tso07$/, - // context merging - /expand-manifest.jsonld#tso08$/, - /expand-manifest.jsonld#tso10$/, - /expand-manifest.jsonld#tso11$/, // colliding keywords /expand-manifest.jsonld#t0114$/, // included @@ -341,18 +329,6 @@ const TEST_TYPES = { /toRdf-manifest.jsonld#te075$/, /toRdf-manifest.jsonld#te111$/, /toRdf-manifest.jsonld#te112$/, - // @import - /toRdf-manifest.jsonld#tso01$/, - /toRdf-manifest.jsonld#tso02$/, - /toRdf-manifest.jsonld#tso03$/, - /toRdf-manifest.jsonld#tso05$/, - /toRdf-manifest.jsonld#tso06$/, - // @import and protected - /toRdf-manifest.jsonld#tso07$/, - // context merging - /toRdf-manifest.jsonld#tso08$/, - /toRdf-manifest.jsonld#tso10$/, - /toRdf-manifest.jsonld#tso11$/, // colliding keyword /toRdf-manifest.jsonld#te114$/, // included