Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

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

Seems like this would need to be more involved to catch @import in scoped contexts too. There are not test for this.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Catching it was going to require adding more state to be passed down, and even though it isn't the use case, it doesn't really cause any harm if its (mis)used this way. But, that would be an issue on the spec.

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)
Expand Down
24 changes: 0 additions & 24 deletions tests/test-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down