Skip to content
Open
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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# jsonld ChangeLog

## 9.0.1 - xxxx-xx-xx

### Fixed
- Use a term as the prefix of a compact IRI only if its name contains neither
`:` nor `/`, as JSON-LD 1.1 API Create Term Definition step 14.2.5 does.
With a term such as `/a/`, `expand` read `/a/:b` as a compact IRI instead of
as a relative IRI, and `compact` wrote such compact IRIs. With a term such as
`:a/`, `compact` wrote `:a/:b`, which does not expand back to the IRI.
- **NOTE**: This changes the output of `expand`, `compact`, `toRDF` and
`canonize` for contexts with such terms. A context with a term such as
`/a/:b` can now fail, or newly pass, the check that the term expands to
its own IRI.

## 9.0.0 - 2025-11-20

### Added
Expand Down
6 changes: 4 additions & 2 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,11 @@ api.createTermDefinition = ({
}

mapping['@id'] = id;
// indicate if this term may be used as a compact IRI prefix
// indicate if this term may be used as a compact IRI prefix: only a
// simple term without ':' or '/' may (the term name condition of
// Create Term Definition, step 14.2.5)
mapping._prefix = (simpleTerm &&
!mapping._termHasColon &&
!term.match(/:|\//) &&
id.match(/[:\/\?#\[\]@]$/) !== null);
}
}
Expand Down
63 changes: 63 additions & 0 deletions tests/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,69 @@ describe('url tests', () => {
});
});

// Create Term Definition, step 14.2.5: only a simple term without ':' or '/'
// may be a prefix
describe('terms with a colon or slash', () => {
it('should not expand a value with a term with a slash as prefix',
async () => {
const output = await jsonld.expand({
'@context': {
'@base': 'http://example.org/ns/doc',
'@vocab': 'http://other.org',
'/a/': 'http://other.org/a/'
},
'@id': '/a/:b',
'/a/:b': {'@id': '/a/:b'}
});
assert.deepStrictEqual(output, [{
'@id': 'http://example.org/a/:b',
'http://other.org/a/:b': [{'@id': 'http://example.org/a/:b'}]
}]);
});

it('should give a term name with a slash the IRI of its prefix term',
async () => {
// step 15 doesn't depend on the prefix flag
const output = await jsonld.expand({
'@context': {
'@vocab': 'http://other.org',
'/a/': 'http://other.org/a/',
'/a/:b': {'@type': '@id'}
},
'/a/:b': 'http://example.org/o'
});
assert.deepStrictEqual(output, [{
'http://other.org/a/b': [{'@id': 'http://example.org/o'}]
}]);
});

it('should not compact to a term with a colon or slash as prefix',
async () => {
const cases = [
[{
'@vocab': 'http://example.org/x',
':a/': 'http://example.org/x:a/'
}, 'http://example.org/x:a/b', 'http://example.org/p'],
[{
'@vocab': 'http://example.org',
'/a/': 'http://example.org/a/'
}, 'http://example.org/a/b', '/p']
];
for(const [context, iri, property] of cases) {
const output = await jsonld.compact({
'@id': 'http://example.org/s',
'http://example.org/p': {'@id': iri}
}, context);
assert.deepStrictEqual(output[property], {'@id': iri});
const expanded = await jsonld.expand(output);
assert.deepStrictEqual(expanded, [{
'@id': 'http://example.org/s',
'http://example.org/p': [{'@id': iri}]
}]);
}
});
});

describe('js keywords', () => {
it('expand js valueOf/toString keywords (top ctx)', async () => {
const d =
Expand Down