Skip to content

Commit 09b4c1b

Browse files
committed
Avoid stripping partial base IRIs.
Fixes CommunitySolidServer/CommunitySolidServer#1094
1 parent db3ec15 commit 09b4c1b

2 files changed

Lines changed: 24 additions & 4 deletions

File tree

src/N3Writer.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ export default class N3Writer {
5555
if (!(/triple|quad/i).test(options.format)) {
5656
this._lineMode = false;
5757
this._graph = DEFAULTGRAPH;
58-
this._baseIRI = options.baseIRI;
5958
this._prefixIRIs = Object.create(null);
6059
options.prefixes && this.addPrefixes(options.prefixes);
60+
if (options.baseIRI) {
61+
this._baseMatcher = new RegExp(`^${escapeRegex(options.baseIRI)
62+
}${options.baseIRI.endsWith('/') ? '' : '[#?]'}`);
63+
this._baseLength = options.baseIRI.length;
64+
}
6165
}
6266
else {
6367
this._lineMode = true;
@@ -148,8 +152,8 @@ export default class N3Writer {
148152
}
149153
let iri = entity.value;
150154
// Use relative IRIs if requested and possible
151-
if (this._baseIRI && iri.startsWith(this._baseIRI))
152-
iri = iri.substr(this._baseIRI.length);
155+
if (this._baseMatcher && this._baseMatcher.test(iri))
156+
iri = iri.substr(this._baseLength);
153157
// Escape special characters
154158
if (escape.test(iri))
155159
iri = iri.replace(escapeAll, characterReplacer);
@@ -290,7 +294,7 @@ export default class N3Writer {
290294
IRIlist += IRIlist ? `|${prefixIRI}` : prefixIRI;
291295
prefixList += (prefixList ? '|' : '') + this._prefixIRIs[prefixIRI];
292296
}
293-
IRIlist = IRIlist.replace(/[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
297+
IRIlist = escapeRegex(IRIlist, /[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
294298
this._prefixRegex = new RegExp(`^(?:${prefixList})[^\/]*$|` +
295299
`^(${IRIlist})([a-zA-Z][\\-_a-zA-Z0-9]*)$`);
296300
}
@@ -389,3 +393,7 @@ function characterReplacer(character) {
389393
}
390394
return result;
391395
}
396+
397+
function escapeRegex(regex) {
398+
return regex.replace(/[\]\/\(\)\*\+\?\.\\\$]/g, '\\$&');
399+
}

test/N3Writer-test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,18 @@ describe('Writer', () => {
349349
});
350350
});
351351

352+
it('does not use partially match base IRIs', done => {
353+
const writer = new Writer({ baseIRI: 'https://pod.example/profile/card' });
354+
writer.addQuad(new Quad(
355+
new NamedNode('https://pod.example/profile/card#me'),
356+
new NamedNode('http://www.w3.org/2002/07/owl#sameAs'),
357+
new NamedNode('https://pod.example/profile/card-1234.ttl')));
358+
writer.end((error, output) => {
359+
output.should.equal('<#me> <http://www.w3.org/2002/07/owl#sameAs> <https://pod.example/profile/card-1234.ttl>.\n');
360+
done(error);
361+
});
362+
});
363+
352364
it('should accept triples with separated components', done => {
353365
const writer = new Writer();
354366
writer.addQuad(new NamedNode('a'), new NamedNode('b'), new NamedNode('c'));

0 commit comments

Comments
 (0)