From db494f5656a7213c6af2b9b7f214e668e779f781 Mon Sep 17 00:00:00 2001 From: jdsika Date: Wed, 25 Nov 2026 16:57:47 +0100 Subject: [PATCH 1/3] fix: use a term as a prefix only if its name has no colon or slash Signed-off-by: jdsika --- CHANGELOG.md | 11 +++++++++ lib/context.js | 6 +++-- tests/misc.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 82a69cfe..8e1389a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,16 @@ # 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. + ## 9.0.0 - 2025-11-20 ### Added diff --git a/lib/context.js b/lib/context.js index f9fdcc84..2fa9f9ed 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 (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..bd632fd8 100644 --- a/tests/misc.js +++ b/tests/misc.js @@ -292,6 +292,69 @@ describe('loading multiple levels of contexts', () => { }); }); +// 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('url tests', () => { it('should detect absolute IRIs', done => { // absolute IRIs From 28c8654accbbf817b4be8134fffd9910b541335c Mon Sep 17 00:00:00 2001 From: jdsika Date: Wed, 25 Nov 2026 16:59:50 +0100 Subject: [PATCH 2/3] test: move the prefix term tests after the url tests Signed-off-by: jdsika --- tests/misc.js | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/tests/misc.js b/tests/misc.js index bd632fd8..cf390ba0 100644 --- a/tests/misc.js +++ b/tests/misc.js @@ -292,6 +292,28 @@ describe('loading multiple levels of contexts', () => { }); }); +describe('url tests', () => { + it('should detect absolute IRIs', done => { + // absolute IRIs + assert(jsonld.url.isAbsolute('a:')); + assert(jsonld.url.isAbsolute('a:b')); + assert(jsonld.url.isAbsolute('a:b:c')); + // blank nodes + assert(jsonld.url.isAbsolute('_:')); + assert(jsonld.url.isAbsolute('_:a')); + assert(jsonld.url.isAbsolute('_:a:b')); + + // not absolute or blank node + assert(!jsonld.url.isAbsolute(':')); + assert(!jsonld.url.isAbsolute('a')); + assert(!jsonld.url.isAbsolute('/:')); + assert(!jsonld.url.isAbsolute('/a:')); + assert(!jsonld.url.isAbsolute('/a:b')); + assert(!jsonld.url.isAbsolute('_')); + done(); + }); +}); + // Create Term Definition, step 14.2.5: only a simple term without ':' or '/' // may be a prefix describe('terms with a colon or slash', () => { @@ -355,28 +377,6 @@ describe('terms with a colon or slash', () => { }); }); -describe('url tests', () => { - it('should detect absolute IRIs', done => { - // absolute IRIs - assert(jsonld.url.isAbsolute('a:')); - assert(jsonld.url.isAbsolute('a:b')); - assert(jsonld.url.isAbsolute('a:b:c')); - // blank nodes - assert(jsonld.url.isAbsolute('_:')); - assert(jsonld.url.isAbsolute('_:a')); - assert(jsonld.url.isAbsolute('_:a:b')); - - // not absolute or blank node - assert(!jsonld.url.isAbsolute(':')); - assert(!jsonld.url.isAbsolute('a')); - assert(!jsonld.url.isAbsolute('/:')); - assert(!jsonld.url.isAbsolute('/a:')); - assert(!jsonld.url.isAbsolute('/a:b')); - assert(!jsonld.url.isAbsolute('_')); - done(); - }); -}); - describe('js keywords', () => { it('expand js valueOf/toString keywords (top ctx)', async () => { const d = From 12af8485d697547a6e2969ef45887cd80117fb6a Mon Sep 17 00:00:00 2001 From: jdsika Date: Wed, 25 Nov 2026 17:15:24 +0100 Subject: [PATCH 3/3] docs: narrow the prefix comment and note the context check change Signed-off-by: jdsika --- CHANGELOG.md | 4 +++- lib/context.js | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e1389a2..f62a3c29 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,9 @@ 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. + `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 diff --git a/lib/context.js b/lib/context.js index 2fa9f9ed..d73dad25 100644 --- a/lib/context.js +++ b/lib/context.js @@ -706,8 +706,8 @@ api.createTermDefinition = ({ mapping['@id'] = id; // indicate if this term may be used as a compact IRI prefix: only a - // simple term without ':' or '/' may (Create Term Definition, step - // 14.2.5) + // simple term without ':' or '/' may (the term name condition of + // Create Term Definition, step 14.2.5) mapping._prefix = (simpleTerm && !term.match(/:|\//) && id.match(/[:\/\?#\[\]@]$/) !== null);