Skip to content

Commit a93cd8a

Browse files
committed
Support for "@import".
1 parent 5800546 commit a93cd8a

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
@@ -4,6 +4,9 @@
44
- More support for `"@type": "@none"`.
55
- JSON literal value handling issues (`null` and `[]`).
66

7+
### Added
8+
- Support for `"@import"`.
9+
710
## 2.0.1 - 2019-12-10
811

912
### 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
@@ -119,14 +119,6 @@ const TEST_TYPES = {
119119
/remote-doc-manifest.jsonld#t0013$/,
120120
/remote-doc-manifest.jsonld#tla01$/,
121121
/remote-doc-manifest.jsonld#tla05$/,
122-
// @import
123-
/expand-manifest.jsonld#tso01$/,
124-
/expand-manifest.jsonld#tso02$/,
125-
/expand-manifest.jsonld#tso03$/,
126-
/expand-manifest.jsonld#tso05$/,
127-
/expand-manifest.jsonld#tso06$/,
128-
// @import and protected
129-
/expand-manifest.jsonld#tso07$/,
130122
// context merging
131123
/expand-manifest.jsonld#tso08$/,
132124
/expand-manifest.jsonld#tso10$/,
@@ -386,14 +378,6 @@ const TEST_TYPES = {
386378
/toRdf-manifest.jsonld#tpr28$/,
387379
// prefix
388380
/toRdf-manifest.jsonld#tpr29$/,
389-
// @import
390-
/toRdf-manifest.jsonld#tso01$/,
391-
/toRdf-manifest.jsonld#tso02$/,
392-
/toRdf-manifest.jsonld#tso03$/,
393-
/toRdf-manifest.jsonld#tso05$/,
394-
/toRdf-manifest.jsonld#tso06$/,
395-
// @import and protected
396-
/toRdf-manifest.jsonld#tso07$/,
397381
// context merging
398382
/toRdf-manifest.jsonld#tso08$/,
399383
/toRdf-manifest.jsonld#tso10$/,

0 commit comments

Comments
 (0)