rdfjs / N3.js

Lightning fast, spec-compatible, streaming RDF for JavaScript
http://rdf.js.org/N3.js/
Other
676 stars 127 forks source link

Cannot use alternative factories in n3 mode #334

Closed tpluscode closed 1 year ago

tpluscode commented 1 year ago

I have never had such an issue until I tried parsing some rules with format: 'text/n3'. When a factory is used this error is thrown

Cannot read properties of undefined (reading 'substr') at N3Parser._saveContext (/node_modules/n3/lib/N3Parser.js:102:58)

I find that on that line internally the this._graph is accessed with properties which are not defined in the RDF/JS spec. Thus I found that no common factory implementations to work. rdf-ext, @rdfjs/data-model, nor rdf-data-factory

Here's an example: https://runkit.com/embed/k5l3tf4fmvpq

tpluscode commented 1 year ago

Hm, there would probably be other (edge) cases but this seems to work

diff --git a/node_modules/n3/lib/N3Parser.js b/node_modules/n3/lib/N3Parser.js
index 8f16bab..28d4514 100644
--- a/node_modules/n3/lib/N3Parser.js
+++ b/node_modules/n3/lib/N3Parser.js
@@ -99,7 +99,7 @@ class N3Parser {
       this._inversePredicate = false; // In N3, blank nodes are scoped to a formula
       // (using a dot as separator, as a blank node label cannot start with it)

-      this._prefixes._ = this._graph ? `${this._graph.id.substr(2)}.` : '.'; // Quantifiers are scoped to a formula
+      this._prefixes._ = this._graph ? `${this._graph.value}.` : '.'; // Quantifiers are scoped to a formula

       this._quantified = Object.create(this._quantified);
     }
jeswr commented 1 year ago

Fixed in 1.16.4