Skip to content

Commit 06380fa

Browse files
gkelloggdavidlehn
authored andcommitted
Support for "@import".
1 parent 35cc893 commit 06380fa

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
### Changed
3131
- `isKeyword()` optimization for non-keyword fast path.
3232

33+
### Added
34+
- Support for `"@import"`.
35+
3336
## 2.0.1 - 2019-12-10
3437

3538
### Fixed

lib/context.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,53 @@ api.process = async ({
266266
defined.set('@propagate', true);
267267
}
268268

269+
// handle @import
270+
if('@import' in ctx) {
271+
const value = ctx['@import'];
272+
if(activeCtx.processingMode === 'json-ld-1.0') {
273+
throw new JsonLdError(
274+
'Invalid JSON-LD syntax; @import not compatible with ' +
275+
activeCtx.processingMode,
276+
'jsonld.SyntaxError',
277+
{code: 'invalid context member', context: ctx});
278+
}
279+
if(!_isString(value)) {
280+
throw new JsonLdError(
281+
'Invalid JSON-LD syntax; @import must be a string.',
282+
'jsonld.SyntaxError',
283+
{code: 'invalid @import value', context: localCtx});
284+
}
285+
286+
// resolve contexts
287+
const resolvedImport = await options.contextResolver.resolve({
288+
context: value,
289+
documentLoader: options.documentLoader,
290+
base: options.base
291+
});
292+
if(resolvedImport.length !== 1) {
293+
throw new JsonLdError(
294+
'Invalid JSON-LD syntax; @import must reference a single context.',
295+
'jsonld.SyntaxError',
296+
{code: 'invalid remote context', context: localCtx});
297+
}
298+
const importCtx = resolvedImport[0].document;
299+
if('@import' in importCtx) {
300+
throw new JsonLdError(
301+
'Invalid JSON-LD syntax; imported context must not include @import.',
302+
'jsonld.SyntaxError',
303+
{code: 'invalid context member', context: localCtx});
304+
}
305+
306+
// merge ctx into importCtx and replace rval with the result
307+
for(const key in importCtx) {
308+
if(!(key in ctx)) {
309+
ctx[key] = importCtx[key];
310+
}
311+
}
312+
313+
defined.set('@import', true);
314+
}
315+
269316
// handle @protected; determine whether this sub-context is declaring
270317
// all its terms to be "protected" (exceptions can be made on a
271318
// per-definition basis)

tests/test-common.js

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,6 @@ const TEST_TYPES = {
103103
/expand-manifest.jsonld#thc05$/,
104104
// remote
105105
/remote-doc-manifest.jsonld#t0013$/, // HTML
106-
// @import
107-
/expand-manifest.jsonld#tso01$/,
108-
/expand-manifest.jsonld#tso02$/,
109-
/expand-manifest.jsonld#tso03$/,
110-
/expand-manifest.jsonld#tso05$/,
111-
/expand-manifest.jsonld#tso06$/,
112-
// @import and protected
113-
/expand-manifest.jsonld#tso07$/,
114106
// context merging
115107
/expand-manifest.jsonld#tso08$/,
116108
/expand-manifest.jsonld#tso10$/,
@@ -341,14 +333,6 @@ const TEST_TYPES = {
341333
/toRdf-manifest.jsonld#te075$/,
342334
/toRdf-manifest.jsonld#te111$/,
343335
/toRdf-manifest.jsonld#te112$/,
344-
// @import
345-
/toRdf-manifest.jsonld#tso01$/,
346-
/toRdf-manifest.jsonld#tso02$/,
347-
/toRdf-manifest.jsonld#tso03$/,
348-
/toRdf-manifest.jsonld#tso05$/,
349-
/toRdf-manifest.jsonld#tso06$/,
350-
// @import and protected
351-
/toRdf-manifest.jsonld#tso07$/,
352336
// context merging
353337
/toRdf-manifest.jsonld#tso08$/,
354338
/toRdf-manifest.jsonld#tso10$/,

0 commit comments

Comments
 (0)