From 73f24e1512c79b69885bd79a4fa0e395cff2e067 Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Wed, 8 Jan 2020 16:24:00 -0800 Subject: [PATCH 1/4] Support for `"@import"`. --- CHANGELOG.md | 3 +++ lib/context.js | 47 ++++++++++++++++++++++++++++++++++++++++++++ tests/test-common.js | 16 --------------- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee977592..b61eb5d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,9 @@ ### Changed - `isKeyword()` optimization for non-keyword fast path. +### Added +- Support for `"@import"`. + ## 2.0.1 - 2019-12-10 ### Fixed diff --git a/lib/context.js b/lib/context.js index 0b26c043..79fc5f80 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(!(key in ctx)) { + 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..297803c1 100644 --- a/tests/test-common.js +++ b/tests/test-common.js @@ -103,14 +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$/, @@ -341,14 +333,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$/, From 1bedb2f00bca19d86fbb3214ca6eaf147a2abb31 Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Sun, 12 Jan 2020 14:03:28 -0800 Subject: [PATCH 2/4] Enable context merging tests. --- tests/test-common.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/tests/test-common.js b/tests/test-common.js index 297803c1..7261fa6a 100644 --- a/tests/test-common.js +++ b/tests/test-common.js @@ -103,10 +103,6 @@ const TEST_TYPES = { /expand-manifest.jsonld#thc05$/, // remote /remote-doc-manifest.jsonld#t0013$/, // HTML - // context merging - /expand-manifest.jsonld#tso08$/, - /expand-manifest.jsonld#tso10$/, - /expand-manifest.jsonld#tso11$/, // colliding keywords /expand-manifest.jsonld#t0114$/, // included @@ -333,10 +329,6 @@ const TEST_TYPES = { /toRdf-manifest.jsonld#te075$/, /toRdf-manifest.jsonld#te111$/, /toRdf-manifest.jsonld#te112$/, - // context merging - /toRdf-manifest.jsonld#tso08$/, - /toRdf-manifest.jsonld#tso10$/, - /toRdf-manifest.jsonld#tso11$/, // colliding keyword /toRdf-manifest.jsonld#te114$/, // included From 622e174636d612a8df76419d4b407e36d446af64 Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Mon, 20 Jan 2020 14:18:11 -0800 Subject: [PATCH 3/4] Move changlog entry above 2.0.2. --- CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b61eb5d4..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 @@ -29,9 +32,6 @@ ### Changed - `isKeyword()` optimization for non-keyword fast path. -### Added -- Support for `"@import"`. - ## 2.0.1 - 2019-12-10 ### Fixed From 5f5b4db9353be1e32be1b639804f612b7b81c5ec Mon Sep 17 00:00:00 2001 From: Gregg Kellogg Date: Sun, 26 Jan 2020 12:28:48 -0800 Subject: [PATCH 4/4] Update lib/context.js Co-Authored-By: David I. Lehn --- lib/context.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/context.js b/lib/context.js index 79fc5f80..be008050 100644 --- a/lib/context.js +++ b/lib/context.js @@ -305,7 +305,7 @@ api.process = async ({ // merge ctx into importCtx and replace rval with the result for(const key in importCtx) { - if(!(key in ctx)) { + if(!ctx.hasOwnProperty(key)) { ctx[key] = importCtx[key]; } }