Skip to content

Commit 6f90538

Browse files
Copilotrubensworksjeswr
authored
fix: direction getter incorrectly matching -- inside literal values (#565)
* Initial plan * Fix direction getter for literals with '--' in value and add tests Co-authored-by: rubensworks <440384+rubensworks@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: rubensworks <440384+rubensworks@users.noreply.github.com> Co-authored-by: Jesse Wright <63333554+jeswr@users.noreply.github.com>
1 parent f0f2c34 commit 6f90538

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/N3DataFactory.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@ export class Literal extends Term {
9595

9696
// ### The direction of this literal
9797
get direction() {
98-
// Find the last double dash (e.g., '"abc"@en-us--ltr')
98+
// Find the last double dash after the closing quote (e.g., '"abc"@en-us--ltr')
9999
const id = this.id;
100-
const atPos = id.lastIndexOf('--') + 2;
101-
return atPos > 1 && atPos < id.length ? id.substr(atPos).toLowerCase() : '';
100+
const endPos = id.lastIndexOf('"');
101+
const dirPos = id.lastIndexOf('--');
102+
return dirPos > endPos && dirPos + 2 < id.length ? id.substr(dirPos + 2).toLowerCase() : '';
102103
}
103104

104105
// ### The datatype IRI of this literal

test/Literal-test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,48 @@ describe('Literal', () => {
621621
});
622622
});
623623

624+
describe('A Literal instance created from a string containing "--" with a language tag', () => {
625+
let literal;
626+
beforeAll(() => { literal = new Literal('"bla bla -- more bla bla"@en'); });
627+
628+
it('should have the text value as value', () => {
629+
expect(literal).toHaveProperty('value', 'bla bla -- more bla bla');
630+
});
631+
632+
it('should have the language tag as language', () => {
633+
expect(literal).toHaveProperty('language', 'en');
634+
});
635+
636+
it('should have the empty string as direction', () => {
637+
expect(literal).toHaveProperty('direction', '');
638+
});
639+
640+
it('should have rdf:langString as datatype', () => {
641+
expect(literal.datatype.value).toBe('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString');
642+
});
643+
});
644+
645+
describe('A Literal instance created from a string containing "--" with a language tag and direction', () => {
646+
let literal;
647+
beforeAll(() => { literal = new Literal('"bla bla -- more bla bla"@en--ltr'); });
648+
649+
it('should have the text value as value', () => {
650+
expect(literal).toHaveProperty('value', 'bla bla -- more bla bla');
651+
});
652+
653+
it('should have the language tag as language', () => {
654+
expect(literal).toHaveProperty('language', 'en');
655+
});
656+
657+
it('should have the direction as direction', () => {
658+
expect(literal).toHaveProperty('direction', 'ltr');
659+
});
660+
661+
it('should have rdf:dirLangString as datatype', () => {
662+
expect(literal.datatype.value).toBe('http://www.w3.org/1999/02/22-rdf-syntax-ns#dirLangString');
663+
});
664+
});
665+
624666
describe('A Literal instance created from the empty string with a datatype', () => {
625667
let literal;
626668
beforeAll(() => { literal = new Literal('""^^http://example.org/types#type'); });

test/N3Writer-test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ describe('Writer', () => {
8383
'<a> <b> "cde"@en-us--ltr.\n'),
8484
);
8585

86+
it(
87+
'should serialize a literal containing "--" with a language',
88+
shouldSerialize(['a', 'b', '"bla bla -- more bla bla"@en'],
89+
'<a> <b> "bla bla -- more bla bla"@en.\n'),
90+
);
91+
8692
// e.g. http://vocab.getty.edu/aat/300264727.ttl
8793
it(
8894
'should serialize a literal with an artificial language',

0 commit comments

Comments
 (0)