diff --git a/CHANGELOG.md b/CHANGELOG.md index 82a69cfe..f62a3c29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/context.js b/lib/context.js index f9fdcc84..d73dad25 100644 --- a/lib/context.js +++ b/lib/context.js @@ -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); } } diff --git a/tests/misc.js b/tests/misc.js index 2d8c8cf7..cf390ba0 100644 --- a/tests/misc.js +++ b/tests/misc.js @@ -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 =