Skip to content

Commit 9ff3a2b

Browse files
fix: validate length of language subtags rather than full tag (#566)
* Fix validation on language tags * Tidy up formatting * Fix false positive issue in tests --------- Co-authored-by: Jesse Wright <63333554+jeswr@users.noreply.github.com>
1 parent 6f90538 commit 9ff3a2b

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/N3Parser.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,8 @@ export default class N3Parser {
585585
break;
586586
// Create a language-tagged string
587587
case 'langcode':
588-
if (token.value.length > 8)
589-
return this._error('Detected language tag of length larger than 8', token);
588+
if (token.value.split('-').some(t => t.length > 8))
589+
return this._error('Detected language tag with subtag longer than 8 characters', token);
590590
literal = this._factory.literal(this._literalValue, token.value);
591591
this._literalLanguage = token.value;
592592
token = null;

test/N3Parser-test.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,17 @@ describe('Parser', () => {
170170
}),
171171
);
172172

173+
const validLanguageTags = ['en', 'en-US', 'be-tarask'];
174+
it.each(validLanguageTags)(
175+
'should parse a triple with a valid language tag (%s)', (tag, done) => {
176+
return shouldParse(`<a> <b> "Hello"@${tag}.`,
177+
['a', 'b', `"Hello"@${tag}`])(done);
178+
});
179+
173180
it(
174-
'should error on a triple with a literal with language tag of length > 8',
175-
shouldNotParse('<a> <b> "Hello"@cantbethislong.',
176-
'Detected language tag of length larger than 8 on line 1.', {
181+
'should error on a triple where a literal has a language subtag longer than 8 characters (single subtag)',
182+
shouldNotParse('<a> <b> "Hello"@cantbethislong.',
183+
'Detected language tag with subtag longer than 8 characters on line 1.', {
177184
line: 1,
178185
previousToken: {
179186
line: 1,
@@ -3128,7 +3135,7 @@ describe('Parser', () => {
31283135
@prefix : <http://example.com/> .
31293136
[ :friend [
31303137
:name "Thomas" ;
3131-
]
3138+
]
31323139
] .
31333140
`);
31343141

0 commit comments

Comments
 (0)