Skip to content

Commit 1ba33a3

Browse files
committed
Only use its:dir if rdf:version is 1.2
1 parent 1268039 commit 1ba33a3

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

lib/RdfXmlParser.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export class RdfXmlParser extends Transform implements RDF.Sink<EventEmitter, RD
162162
* @param activeTag The active tag.
163163
*/
164164
public createLiteral(value: string, activeTag: IActiveTag): RDF.Literal {
165-
return this.dataFactory.literal(value, activeTag.datatype ? activeTag.datatype : activeTag.language ? { language: activeTag.language, direction: activeTag.direction } : undefined)
165+
return this.dataFactory.literal(value, activeTag.datatype ? activeTag.datatype : activeTag.language ? { language: activeTag.language, direction: activeTag.rdfVersion ? activeTag.direction : undefined } : undefined)
166166
}
167167

168168
protected attachSaxListeners() {
@@ -217,6 +217,8 @@ export class RdfXmlParser extends Transform implements RDF.Sink<EventEmitter, RD
217217
activeTag.baseIRI = parentTag.baseIRI;
218218
// Also inherit triple term collection array
219219
activeTag.childrenTripleTerms = parentTag.childrenTripleTerms;
220+
// Also RDF version
221+
activeTag.rdfVersion = parentTag.rdfVersion;
220222
} else {
221223
activeTag.baseIRI = this.baseIRI;
222224
}
@@ -267,7 +269,7 @@ export class RdfXmlParser extends Transform implements RDF.Sink<EventEmitter, RD
267269
for (const attributeKey in tag.attributes) {
268270
const attribute = tag.attributes[attributeKey];
269271
if (attribute.uri === RdfXmlParser.RDF && attribute.local === 'version') {
270-
this.emit('version', attribute.value);
272+
this.setVersion(activeTag, attribute.value);
271273
} else if (parentTag && attribute.uri === RdfXmlParser.RDF) {
272274
switch (attribute.local) {
273275
case 'about':
@@ -443,7 +445,7 @@ while ${attribute.value} and ${activeSubjectValue} where found.`);
443445
for (const propertyAttributeKey in tag.attributes) {
444446
const propertyAttribute = tag.attributes[propertyAttributeKey];
445447
if (propertyAttribute.uri === RdfXmlParser.RDF && propertyAttribute.local === 'version') {
446-
this.emit('version', propertyAttribute.value);
448+
this.setVersion(activeTag, propertyAttribute.value);
447449
} else if (propertyAttribute.uri === RdfXmlParser.RDF) {
448450
switch (propertyAttribute.local) {
449451
case 'resource':
@@ -735,6 +737,11 @@ while ${attribute.value} and ${activeSubjectValue} where found.`);
735737
delete activeTag.direction;
736738
}
737739
}
740+
741+
private setVersion(activeTag: IActiveTag, version: string) {
742+
activeTag.rdfVersion = version;
743+
this.emit('version', version);
744+
}
738745
}
739746

740747
export interface IRdfXmlParserArgs {
@@ -796,6 +803,7 @@ export interface IActiveTag {
796803
childrenTagsToTripleTerms?: boolean;
797804
childrenTripleTerms?: RDF.Quad[];
798805
reifier?: RDF.NamedNode | RDF.BlankNode;
806+
rdfVersion?: string;
799807
}
800808

801809
export enum ParseType {

test/RdfXmlParser-test.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,8 @@ abc`)).rejects.toBeTruthy();
851851
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
852852
xmlns:dc="http://purl.org/dc/elements/1.1/"
853853
xmlns:ex="http://example.org/stuff/1.0/"
854-
xmlns:its="http://www.w3.org/2005/11/its">
854+
xmlns:its="http://www.w3.org/2005/11/its"
855+
rdf:version="1.2">
855856
<rdf:Description rdf:about="http://www.w3.org/TR/rdf-syntax-grammar" xml:lang="en-us" its:dir="abc">
856857
<dc:title>RDF 1.1 XML Syntax</dc:title>
857858
</rdf:Description>
@@ -1470,7 +1471,8 @@ abc`)).rejects.toBeTruthy();
14701471
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
14711472
xmlns:dc="http://purl.org/dc/elements/1.1/"
14721473
xmlns:ex="http://example.org/stuff/1.0/"
1473-
xmlns:its="http://www.w3.org/2005/11/its">
1474+
xmlns:its="http://www.w3.org/2005/11/its"
1475+
rdf:version="1.2">
14741476
<rdf:Description rdf:about="http://www.w3.org/TR/rdf-syntax-grammar" xml:lang="en-us" its:dir="ltr">
14751477
<dc:title>RDF 1.1 XML Syntax</dc:title>
14761478
</rdf:Description>
@@ -1509,7 +1511,8 @@ abc`)).rejects.toBeTruthy();
15091511
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
15101512
xmlns:dc="http://purl.org/dc/elements/1.1/"
15111513
xmlns:ex="http://example.org/stuff/1.0/"
1512-
xmlns:its="http://www.w3.org/2005/11/its">
1514+
xmlns:its="http://www.w3.org/2005/11/its"
1515+
rdf:version="1.2">
15131516
<rdf:Description rdf:about="http://www.w3.org/TR/rdf-syntax-grammar" xml:lang="en-us" its:dir="ltr">
15141517
<ex:editor>
15151518
<rdf:Description>
@@ -1591,7 +1594,8 @@ abc`)).rejects.toBeTruthy();
15911594
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
15921595
xmlns:dc="http://purl.org/dc/elements/1.1/"
15931596
xmlns:ex="http://example.org/stuff/1.0/"
1594-
xmlns:its="http://www.w3.org/2005/11/its">
1597+
xmlns:its="http://www.w3.org/2005/11/its"
1598+
rdf:version="1.2">
15951599
<rdf:Description rdf:about="http://www.w3.org/TR/rdf-syntax-grammar">
15961600
<dc:title xml:lang="en-us" its:dir="rtl">RDF 1.1 XML Syntax</dc:title>
15971601
</rdf:Description>
@@ -1687,7 +1691,8 @@ abc`)).rejects.toBeTruthy();
16871691
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
16881692
xmlns:dc="http://purl.org/dc/elements/1.1/"
16891693
xmlns:ex="http://example.org/stuff/1.0/"
1690-
xmlns:its="http://www.w3.org/2005/11/its">
1694+
xmlns:its="http://www.w3.org/2005/11/its"
1695+
rdf:version="1.2">
16911696
<rdf:Description rdf:about="http://www.w3.org/TR/rdf-syntax-grammar">
16921697
<dc:title>RDF 1.1 XML Syntax</dc:title>
16931698
<dc:title xml:lang="en" its:dir="ltr">RDF 1.1 XML Syntax</dc:title>
@@ -1717,6 +1722,24 @@ abc`)).rejects.toBeTruthy();
17171722
]);
17181723
});
17191724

1725+
// 2.8
1726+
it('its:dir without rdf:version', async () => {
1727+
const array = await parse(parser, `<?xml version="1.0" ?>
1728+
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
1729+
xmlns:ex="http://example.org/"
1730+
xmlns:its="http://www.w3.org/2005/11/its"
1731+
its:version="2.0"
1732+
its:dir="ltr"
1733+
xml:lang="en">
1734+
<rdf:Description rdf:about="http://example.org/joe" ex:name="bar" />
1735+
</rdf:RDF>`);
1736+
return expect(array)
1737+
.toBeRdfIsomorphic([
1738+
quad('http://example.org/joe', 'http://example.org/name',
1739+
'"bar"@en'),
1740+
]);
1741+
});
1742+
17201743
// 2.9
17211744
it('rdf:datatype on property elements', async () => {
17221745
const array = await parse(parser, `<?xml version="1.0"?>

0 commit comments

Comments
 (0)