rdflib 2.3.0 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -0
- package/dist/rdflib.min.js +1 -1
- package/dist/rdflib.min.js.map +1 -1
- package/esm/n3parser.js +45 -9
- package/esm/serialize.js +4 -2
- package/esm/serializer.js +42 -10
- package/lib/n3parser.js +45 -9
- package/lib/serialize.js +4 -2
- package/lib/serializer.d.ts +7 -0
- package/lib/serializer.js +42 -10
- package/package.json +2 -1
- package/src/n3parser.js +46 -17
- package/src/serialize.ts +4 -2
- package/src/serializer.js +41 -10
package/esm/n3parser.js
CHANGED
|
@@ -183,9 +183,19 @@ var ws = new RegExp("^[ \\t]*", 'g');
|
|
|
183
183
|
var signed_integer = new RegExp("^[-+]?[0-9]+", 'g');
|
|
184
184
|
var number_syntax = new RegExp("^([-+]?[0-9]+)(\\.[0-9]+)?([eE][-+]?[0-9]+)?", 'g');
|
|
185
185
|
var datetime_syntax = new RegExp('^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9](T[0-9][0-9]:[0-9][0-9](:[0-9][0-9](\\.[0-9]*)?)?)?Z?');
|
|
186
|
+
|
|
187
|
+
// Reused in tight loops to detect whitespace or comment after a dot
|
|
188
|
+
var wsOrHash = new RegExp("[\\s#]");
|
|
186
189
|
var digitstring = new RegExp("^[0-9]+", 'g');
|
|
187
190
|
var interesting = new RegExp("[\\\\\\r\\n\\\"]", 'g');
|
|
188
191
|
var langcode = new RegExp("^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*", 'g');
|
|
192
|
+
|
|
193
|
+
// Returns true when a dot at position i should terminate a name,
|
|
194
|
+
// i.e., when the next character is whitespace, a comment start, or EOF
|
|
195
|
+
function dotTerminatesName(str, i) {
|
|
196
|
+
var next = str.charAt(i + 1);
|
|
197
|
+
return next === '' || wsOrHash.test(next);
|
|
198
|
+
}
|
|
189
199
|
function createSinkParser(store, openFormula, thisDoc, baseURI, genPrefix, metaURI, flags, why) {
|
|
190
200
|
return new SinkParser(store, openFormula, thisDoc, baseURI, genPrefix, metaURI, flags, why);
|
|
191
201
|
}
|
|
@@ -674,6 +684,16 @@ export class SinkParser {
|
|
|
674
684
|
if (j < 0) {
|
|
675
685
|
throw BadSyntax(this._thisDoc, this.lines, str, i, "EOF when ']' expected after [ <propertyList>");
|
|
676
686
|
}
|
|
687
|
+
if (str.slice(j, j + 1) == ".") {
|
|
688
|
+
// If a dot is found after a blank node, treat it as a statement terminator.
|
|
689
|
+
// Do NOT consume the '.' here: statement terminators are handled centrally by
|
|
690
|
+
// checkDot() (called by directiveOrStatement after statement()). Consuming the dot
|
|
691
|
+
// locally would bypass that unified logic and could cause inconsistencies.
|
|
692
|
+
// We do consume ']' below because it is a structural closer of the blank node,
|
|
693
|
+
// not a statement terminator.
|
|
694
|
+
res.push(subj);
|
|
695
|
+
return j; // leave '.' for checkDot()
|
|
696
|
+
}
|
|
677
697
|
if (str.slice(j, j + 1) != "]") {
|
|
678
698
|
throw BadSyntax(this._thisDoc, this.lines, str, j, "']' expected");
|
|
679
699
|
}
|
|
@@ -1122,7 +1142,17 @@ export class SinkParser {
|
|
|
1122
1142
|
return -1;
|
|
1123
1143
|
}
|
|
1124
1144
|
var i = j;
|
|
1125
|
-
while (i < pyjslib_len(str)
|
|
1145
|
+
while (i < pyjslib_len(str)) {
|
|
1146
|
+
var c = str.charAt(i);
|
|
1147
|
+
if (c === '.') {
|
|
1148
|
+
if (dotTerminatesName(str, i)) {
|
|
1149
|
+
break; // treat as statement terminator, not part of name
|
|
1150
|
+
}
|
|
1151
|
+
// else: accept '.' as part of name
|
|
1152
|
+
} else if (_notNameChars.indexOf(c) >= 0) {
|
|
1153
|
+
// Other invalid characters terminate the name
|
|
1154
|
+
break;
|
|
1155
|
+
}
|
|
1126
1156
|
var i = i + 1;
|
|
1127
1157
|
}
|
|
1128
1158
|
res.push(str.slice(j, i));
|
|
@@ -1148,12 +1178,15 @@ export class SinkParser {
|
|
|
1148
1178
|
var i = i + 1;
|
|
1149
1179
|
while (i < pyjslib_len(str)) {
|
|
1150
1180
|
var c = str.charAt(i);
|
|
1151
|
-
if (
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1181
|
+
if (c === '.') {
|
|
1182
|
+
if (dotTerminatesName(str, i)) {
|
|
1183
|
+
break; // dot ends the name here
|
|
1184
|
+
}
|
|
1185
|
+
} else if (_notNameChars.indexOf(c) >= 0) {
|
|
1155
1186
|
break;
|
|
1156
1187
|
}
|
|
1188
|
+
var ln = ln + c;
|
|
1189
|
+
var i = i + 1;
|
|
1157
1190
|
}
|
|
1158
1191
|
} else {
|
|
1159
1192
|
var ln = "";
|
|
@@ -1164,12 +1197,15 @@ export class SinkParser {
|
|
|
1164
1197
|
var ln = "";
|
|
1165
1198
|
while (i < pyjslib_len(str)) {
|
|
1166
1199
|
var c = str.charAt(i);
|
|
1167
|
-
if (
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1200
|
+
if (c === '.') {
|
|
1201
|
+
if (dotTerminatesName(str, i)) {
|
|
1202
|
+
break; // dot ends the name here
|
|
1203
|
+
}
|
|
1204
|
+
} else if (_notNameChars.indexOf(c) >= 0) {
|
|
1171
1205
|
break;
|
|
1172
1206
|
}
|
|
1207
|
+
var ln = ln + c;
|
|
1208
|
+
var i = i + 1;
|
|
1173
1209
|
}
|
|
1174
1210
|
res.push(new pyjslib_Tuple([pfx, ln]));
|
|
1175
1211
|
return i;
|
package/esm/serialize.js
CHANGED
|
@@ -40,7 +40,8 @@ contentType, callback, options) {
|
|
|
40
40
|
return executeCallback(null, documentString);
|
|
41
41
|
case TurtleContentType:
|
|
42
42
|
case TurtleLegacyContentType:
|
|
43
|
-
|
|
43
|
+
// Suppress = for sameAs and => for implies; preserve any user-specified flags (e.g., 'o')
|
|
44
|
+
sz.setFlags('si' + (opts.flags ? ' ' + opts.flags : ''));
|
|
44
45
|
documentString = sz.statementsToN3(newSts);
|
|
45
46
|
return executeCallback(null, documentString);
|
|
46
47
|
case NTriplesContentType:
|
|
@@ -48,7 +49,8 @@ contentType, callback, options) {
|
|
|
48
49
|
documentString = sz.statementsToNTriples(newSts);
|
|
49
50
|
return executeCallback(null, documentString);
|
|
50
51
|
case JSONLDContentType:
|
|
51
|
-
|
|
52
|
+
// turtle + dr (means no default, no relative prefix); preserve user flags
|
|
53
|
+
sz.setFlags('si dr' + (opts.flags ? ' ' + opts.flags : ''));
|
|
52
54
|
documentString = sz.statementsToJsonld(newSts); // convert via turtle
|
|
53
55
|
return executeCallback(null, documentString);
|
|
54
56
|
case NQuadsContentType:
|
package/esm/serializer.js
CHANGED
|
@@ -20,7 +20,8 @@ export default function createSerializer(store) {
|
|
|
20
20
|
;
|
|
21
21
|
export class Serializer {
|
|
22
22
|
constructor(store) {
|
|
23
|
-
_defineProperty(this, "_notQNameChars", '\t\r\n !"#$%&\'()
|
|
23
|
+
_defineProperty(this, "_notQNameChars", '\t\r\n !"#$%&\'()*,+/;<=>?@[\\]^`{|}~');
|
|
24
|
+
// issue#228
|
|
24
25
|
_defineProperty(this, "_notNameChars", this._notQNameChars + ':');
|
|
25
26
|
// stringToN3: String escaping for N3
|
|
26
27
|
_defineProperty(this, "validPrefix", new RegExp(/^[a-zA-Z][a-zA-Z0-9]*$/));
|
|
@@ -53,6 +54,13 @@ export class Serializer {
|
|
|
53
54
|
this.base = base;
|
|
54
55
|
return this;
|
|
55
56
|
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Set serializer behavior flags. Letters can be combined with spaces.
|
|
60
|
+
* Examples: 'si', 'deinprstux', 'si dr', 'o'.
|
|
61
|
+
* Notable flags:
|
|
62
|
+
* - 'o': do not abbreviate to a prefixed name when the local part contains a dot
|
|
63
|
+
*/
|
|
56
64
|
setFlags(flags) {
|
|
57
65
|
this.flags = flags || '';
|
|
58
66
|
return this;
|
|
@@ -243,6 +251,28 @@ export class Serializer {
|
|
|
243
251
|
toN3(f) {
|
|
244
252
|
return this.statementsToN3(f.statements);
|
|
245
253
|
}
|
|
254
|
+
// Validate if a string is a valid PN_LOCAL per Turtle 1.1 spec
|
|
255
|
+
// Allows dots inside the local name but not as trailing character
|
|
256
|
+
// Also allows empty local names (for URIs ending in / or #)
|
|
257
|
+
isValidPNLocal(local) {
|
|
258
|
+
// Empty local name is valid (e.g., ex: for http://example.com/)
|
|
259
|
+
if (local.length === 0) return true;
|
|
260
|
+
|
|
261
|
+
// Cannot end with a dot
|
|
262
|
+
if (local[local.length - 1] === '.') return false;
|
|
263
|
+
|
|
264
|
+
// Check each character (allow dots mid-string)
|
|
265
|
+
for (var i = 0; i < local.length; i++) {
|
|
266
|
+
var ch = local[i];
|
|
267
|
+
// Dot is allowed unless it's the last character (checked above)
|
|
268
|
+
if (ch === '.') continue;
|
|
269
|
+
// Other characters must not be in the blacklist
|
|
270
|
+
if (this._notNameChars.indexOf(ch) >= 0) {
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return true;
|
|
275
|
+
}
|
|
246
276
|
explicitURI(uri) {
|
|
247
277
|
if (this.flags.indexOf('r') < 0 && this.base) {
|
|
248
278
|
uri = Uri.refTo(this.base, uri);
|
|
@@ -605,13 +635,17 @@ export class Serializer {
|
|
|
605
635
|
if (j >= 0 && this.flags.indexOf('p') < 0 && (
|
|
606
636
|
// Can split at namespace but only if http[s]: URI or file: or ws[s] (why not others?)
|
|
607
637
|
uri.indexOf('http') === 0 || uri.indexOf('ws') === 0 || uri.indexOf('file') === 0)) {
|
|
608
|
-
var
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
638
|
+
var localid = uri.slice(j + 1);
|
|
639
|
+
var namesp = uri.slice(0, j + 1);
|
|
640
|
+
// Don't split if namespace is just the protocol (e.g., https://)
|
|
641
|
+
// A valid namespace should have content after the protocol
|
|
642
|
+
var minNamespaceLength = uri.indexOf('://') + 4; // e.g., "http://x" minimum
|
|
643
|
+
// Also don't split if namespace is the base directory (would serialize as relative URI)
|
|
644
|
+
var baseDir = this.base ? this.base.slice(0, Math.max(this.base.lastIndexOf('/'), this.base.lastIndexOf('#')) + 1) : null;
|
|
645
|
+
var namespaceIsBaseDir = baseDir && namesp === baseDir;
|
|
646
|
+
// If flag 'o' is present, forbid dots in local part when abbreviating
|
|
647
|
+
var forbidDotLocal = this.flags.indexOf('o') >= 0 && localid.indexOf('.') >= 0;
|
|
648
|
+
var canSplit = !namespaceIsBaseDir && !forbidDotLocal && namesp.length > minNamespaceLength && this.isValidPNLocal(localid);
|
|
615
649
|
/*
|
|
616
650
|
if (uri.slice(0, j + 1) === this.base + '#') { // base-relative
|
|
617
651
|
if (canSplit) {
|
|
@@ -622,8 +656,6 @@ export class Serializer {
|
|
|
622
656
|
}
|
|
623
657
|
*/
|
|
624
658
|
if (canSplit) {
|
|
625
|
-
var localid = uri.slice(j + 1);
|
|
626
|
-
var namesp = uri.slice(0, j + 1);
|
|
627
659
|
if (this.defaultNamespace && this.defaultNamespace === namesp && this.flags.indexOf('d') < 0) {
|
|
628
660
|
// d -> suppress default
|
|
629
661
|
if (this.flags.indexOf('k') >= 0 && this.keyords.indexOf(localid) < 0) {
|
package/lib/n3parser.js
CHANGED
|
@@ -191,9 +191,19 @@ var ws = new RegExp("^[ \\t]*", 'g');
|
|
|
191
191
|
var signed_integer = new RegExp("^[-+]?[0-9]+", 'g');
|
|
192
192
|
var number_syntax = new RegExp("^([-+]?[0-9]+)(\\.[0-9]+)?([eE][-+]?[0-9]+)?", 'g');
|
|
193
193
|
var datetime_syntax = new RegExp('^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9](T[0-9][0-9]:[0-9][0-9](:[0-9][0-9](\\.[0-9]*)?)?)?Z?');
|
|
194
|
+
|
|
195
|
+
// Reused in tight loops to detect whitespace or comment after a dot
|
|
196
|
+
var wsOrHash = new RegExp("[\\s#]");
|
|
194
197
|
var digitstring = new RegExp("^[0-9]+", 'g');
|
|
195
198
|
var interesting = new RegExp("[\\\\\\r\\n\\\"]", 'g');
|
|
196
199
|
var langcode = new RegExp("^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*", 'g');
|
|
200
|
+
|
|
201
|
+
// Returns true when a dot at position i should terminate a name,
|
|
202
|
+
// i.e., when the next character is whitespace, a comment start, or EOF
|
|
203
|
+
function dotTerminatesName(str, i) {
|
|
204
|
+
var next = str.charAt(i + 1);
|
|
205
|
+
return next === '' || wsOrHash.test(next);
|
|
206
|
+
}
|
|
197
207
|
function createSinkParser(store, openFormula, thisDoc, baseURI, genPrefix, metaURI, flags, why) {
|
|
198
208
|
return new SinkParser(store, openFormula, thisDoc, baseURI, genPrefix, metaURI, flags, why);
|
|
199
209
|
}
|
|
@@ -682,6 +692,16 @@ class SinkParser {
|
|
|
682
692
|
if (j < 0) {
|
|
683
693
|
throw BadSyntax(this._thisDoc, this.lines, str, i, "EOF when ']' expected after [ <propertyList>");
|
|
684
694
|
}
|
|
695
|
+
if (str.slice(j, j + 1) == ".") {
|
|
696
|
+
// If a dot is found after a blank node, treat it as a statement terminator.
|
|
697
|
+
// Do NOT consume the '.' here: statement terminators are handled centrally by
|
|
698
|
+
// checkDot() (called by directiveOrStatement after statement()). Consuming the dot
|
|
699
|
+
// locally would bypass that unified logic and could cause inconsistencies.
|
|
700
|
+
// We do consume ']' below because it is a structural closer of the blank node,
|
|
701
|
+
// not a statement terminator.
|
|
702
|
+
res.push(subj);
|
|
703
|
+
return j; // leave '.' for checkDot()
|
|
704
|
+
}
|
|
685
705
|
if (str.slice(j, j + 1) != "]") {
|
|
686
706
|
throw BadSyntax(this._thisDoc, this.lines, str, j, "']' expected");
|
|
687
707
|
}
|
|
@@ -1130,7 +1150,17 @@ class SinkParser {
|
|
|
1130
1150
|
return -1;
|
|
1131
1151
|
}
|
|
1132
1152
|
var i = j;
|
|
1133
|
-
while (i < pyjslib_len(str)
|
|
1153
|
+
while (i < pyjslib_len(str)) {
|
|
1154
|
+
var c = str.charAt(i);
|
|
1155
|
+
if (c === '.') {
|
|
1156
|
+
if (dotTerminatesName(str, i)) {
|
|
1157
|
+
break; // treat as statement terminator, not part of name
|
|
1158
|
+
}
|
|
1159
|
+
// else: accept '.' as part of name
|
|
1160
|
+
} else if (_notNameChars.indexOf(c) >= 0) {
|
|
1161
|
+
// Other invalid characters terminate the name
|
|
1162
|
+
break;
|
|
1163
|
+
}
|
|
1134
1164
|
var i = i + 1;
|
|
1135
1165
|
}
|
|
1136
1166
|
res.push(str.slice(j, i));
|
|
@@ -1156,12 +1186,15 @@ class SinkParser {
|
|
|
1156
1186
|
var i = i + 1;
|
|
1157
1187
|
while (i < pyjslib_len(str)) {
|
|
1158
1188
|
var c = str.charAt(i);
|
|
1159
|
-
if (
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1189
|
+
if (c === '.') {
|
|
1190
|
+
if (dotTerminatesName(str, i)) {
|
|
1191
|
+
break; // dot ends the name here
|
|
1192
|
+
}
|
|
1193
|
+
} else if (_notNameChars.indexOf(c) >= 0) {
|
|
1163
1194
|
break;
|
|
1164
1195
|
}
|
|
1196
|
+
var ln = ln + c;
|
|
1197
|
+
var i = i + 1;
|
|
1165
1198
|
}
|
|
1166
1199
|
} else {
|
|
1167
1200
|
var ln = "";
|
|
@@ -1172,12 +1205,15 @@ class SinkParser {
|
|
|
1172
1205
|
var ln = "";
|
|
1173
1206
|
while (i < pyjslib_len(str)) {
|
|
1174
1207
|
var c = str.charAt(i);
|
|
1175
|
-
if (
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1208
|
+
if (c === '.') {
|
|
1209
|
+
if (dotTerminatesName(str, i)) {
|
|
1210
|
+
break; // dot ends the name here
|
|
1211
|
+
}
|
|
1212
|
+
} else if (_notNameChars.indexOf(c) >= 0) {
|
|
1179
1213
|
break;
|
|
1180
1214
|
}
|
|
1215
|
+
var ln = ln + c;
|
|
1216
|
+
var i = i + 1;
|
|
1181
1217
|
}
|
|
1182
1218
|
res.push(new pyjslib_Tuple([pfx, ln]));
|
|
1183
1219
|
return i;
|
package/lib/serialize.js
CHANGED
|
@@ -47,7 +47,8 @@ contentType, callback, options) {
|
|
|
47
47
|
return executeCallback(null, documentString);
|
|
48
48
|
case _types.TurtleContentType:
|
|
49
49
|
case _types.TurtleLegacyContentType:
|
|
50
|
-
|
|
50
|
+
// Suppress = for sameAs and => for implies; preserve any user-specified flags (e.g., 'o')
|
|
51
|
+
sz.setFlags('si' + (opts.flags ? ' ' + opts.flags : ''));
|
|
51
52
|
documentString = sz.statementsToN3(newSts);
|
|
52
53
|
return executeCallback(null, documentString);
|
|
53
54
|
case _types.NTriplesContentType:
|
|
@@ -55,7 +56,8 @@ contentType, callback, options) {
|
|
|
55
56
|
documentString = sz.statementsToNTriples(newSts);
|
|
56
57
|
return executeCallback(null, documentString);
|
|
57
58
|
case _types.JSONLDContentType:
|
|
58
|
-
|
|
59
|
+
// turtle + dr (means no default, no relative prefix); preserve user flags
|
|
60
|
+
sz.setFlags('si dr' + (opts.flags ? ' ' + opts.flags : ''));
|
|
59
61
|
documentString = sz.statementsToJsonld(newSts); // convert via turtle
|
|
60
62
|
return executeCallback(null, documentString);
|
|
61
63
|
case _types.NQuadsContentType:
|
package/lib/serializer.d.ts
CHANGED
|
@@ -22,6 +22,12 @@ export class Serializer {
|
|
|
22
22
|
string: NamedNode;
|
|
23
23
|
};
|
|
24
24
|
setBase(base: any): Serializer;
|
|
25
|
+
/**
|
|
26
|
+
* Set serializer behavior flags. Letters can be combined with spaces.
|
|
27
|
+
* Examples: 'si', 'deinprstux', 'si dr', 'o'.
|
|
28
|
+
* Notable flags:
|
|
29
|
+
* - 'o': do not abbreviate to a prefixed name when the local part contains a dot
|
|
30
|
+
*/
|
|
25
31
|
setFlags(flags: any): Serializer;
|
|
26
32
|
toStr(x: any): any;
|
|
27
33
|
fromStr(s: any): any;
|
|
@@ -51,6 +57,7 @@ export class Serializer {
|
|
|
51
57
|
toN3(f: any): string;
|
|
52
58
|
_notQNameChars: string;
|
|
53
59
|
_notNameChars: string;
|
|
60
|
+
isValidPNLocal(local: any): boolean;
|
|
54
61
|
explicitURI(uri: any): string;
|
|
55
62
|
statementsToNTriples(sts: any): string;
|
|
56
63
|
statementsToN3(sts: any): string;
|
package/lib/serializer.js
CHANGED
|
@@ -30,7 +30,8 @@ function createSerializer(store) {
|
|
|
30
30
|
;
|
|
31
31
|
class Serializer {
|
|
32
32
|
constructor(store) {
|
|
33
|
-
(0, _defineProperty2.default)(this, "_notQNameChars", '\t\r\n !"#$%&\'()
|
|
33
|
+
(0, _defineProperty2.default)(this, "_notQNameChars", '\t\r\n !"#$%&\'()*,+/;<=>?@[\\]^`{|}~');
|
|
34
|
+
// issue#228
|
|
34
35
|
(0, _defineProperty2.default)(this, "_notNameChars", this._notQNameChars + ':');
|
|
35
36
|
// stringToN3: String escaping for N3
|
|
36
37
|
(0, _defineProperty2.default)(this, "validPrefix", new RegExp(/^[a-zA-Z][a-zA-Z0-9]*$/));
|
|
@@ -63,6 +64,13 @@ class Serializer {
|
|
|
63
64
|
this.base = base;
|
|
64
65
|
return this;
|
|
65
66
|
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Set serializer behavior flags. Letters can be combined with spaces.
|
|
70
|
+
* Examples: 'si', 'deinprstux', 'si dr', 'o'.
|
|
71
|
+
* Notable flags:
|
|
72
|
+
* - 'o': do not abbreviate to a prefixed name when the local part contains a dot
|
|
73
|
+
*/
|
|
66
74
|
setFlags(flags) {
|
|
67
75
|
this.flags = flags || '';
|
|
68
76
|
return this;
|
|
@@ -253,6 +261,28 @@ class Serializer {
|
|
|
253
261
|
toN3(f) {
|
|
254
262
|
return this.statementsToN3(f.statements);
|
|
255
263
|
}
|
|
264
|
+
// Validate if a string is a valid PN_LOCAL per Turtle 1.1 spec
|
|
265
|
+
// Allows dots inside the local name but not as trailing character
|
|
266
|
+
// Also allows empty local names (for URIs ending in / or #)
|
|
267
|
+
isValidPNLocal(local) {
|
|
268
|
+
// Empty local name is valid (e.g., ex: for http://example.com/)
|
|
269
|
+
if (local.length === 0) return true;
|
|
270
|
+
|
|
271
|
+
// Cannot end with a dot
|
|
272
|
+
if (local[local.length - 1] === '.') return false;
|
|
273
|
+
|
|
274
|
+
// Check each character (allow dots mid-string)
|
|
275
|
+
for (var i = 0; i < local.length; i++) {
|
|
276
|
+
var ch = local[i];
|
|
277
|
+
// Dot is allowed unless it's the last character (checked above)
|
|
278
|
+
if (ch === '.') continue;
|
|
279
|
+
// Other characters must not be in the blacklist
|
|
280
|
+
if (this._notNameChars.indexOf(ch) >= 0) {
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
256
286
|
explicitURI(uri) {
|
|
257
287
|
if (this.flags.indexOf('r') < 0 && this.base) {
|
|
258
288
|
uri = Uri.refTo(this.base, uri);
|
|
@@ -615,13 +645,17 @@ class Serializer {
|
|
|
615
645
|
if (j >= 0 && this.flags.indexOf('p') < 0 && (
|
|
616
646
|
// Can split at namespace but only if http[s]: URI or file: or ws[s] (why not others?)
|
|
617
647
|
uri.indexOf('http') === 0 || uri.indexOf('ws') === 0 || uri.indexOf('file') === 0)) {
|
|
618
|
-
var
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
648
|
+
var localid = uri.slice(j + 1);
|
|
649
|
+
var namesp = uri.slice(0, j + 1);
|
|
650
|
+
// Don't split if namespace is just the protocol (e.g., https://)
|
|
651
|
+
// A valid namespace should have content after the protocol
|
|
652
|
+
var minNamespaceLength = uri.indexOf('://') + 4; // e.g., "http://x" minimum
|
|
653
|
+
// Also don't split if namespace is the base directory (would serialize as relative URI)
|
|
654
|
+
var baseDir = this.base ? this.base.slice(0, Math.max(this.base.lastIndexOf('/'), this.base.lastIndexOf('#')) + 1) : null;
|
|
655
|
+
var namespaceIsBaseDir = baseDir && namesp === baseDir;
|
|
656
|
+
// If flag 'o' is present, forbid dots in local part when abbreviating
|
|
657
|
+
var forbidDotLocal = this.flags.indexOf('o') >= 0 && localid.indexOf('.') >= 0;
|
|
658
|
+
var canSplit = !namespaceIsBaseDir && !forbidDotLocal && namesp.length > minNamespaceLength && this.isValidPNLocal(localid);
|
|
625
659
|
/*
|
|
626
660
|
if (uri.slice(0, j + 1) === this.base + '#') { // base-relative
|
|
627
661
|
if (canSplit) {
|
|
@@ -632,8 +666,6 @@ class Serializer {
|
|
|
632
666
|
}
|
|
633
667
|
*/
|
|
634
668
|
if (canSplit) {
|
|
635
|
-
var localid = uri.slice(j + 1);
|
|
636
|
-
var namesp = uri.slice(0, j + 1);
|
|
637
669
|
if (this.defaultNamespace && this.defaultNamespace === namesp && this.flags.indexOf('d') < 0) {
|
|
638
670
|
// d -> suppress default
|
|
639
671
|
if (this.flags.indexOf('k') >= 0 && this.keyords.indexOf(localid) < 0) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rdflib",
|
|
3
3
|
"description": "an RDF library for node.js. Suitable for client and server side.",
|
|
4
|
-
"version": "2.3.
|
|
4
|
+
"version": "2.3.1",
|
|
5
5
|
"private": false,
|
|
6
6
|
"browserslist": [
|
|
7
7
|
"> 0.5%"
|
|
@@ -117,6 +117,7 @@
|
|
|
117
117
|
"test:serialize:14": "cd ./tests/serialize && node ./data.js -in=t14.html -format=text/turtle -out=,t14.ttl && node diff ,t14.ttl t14-ref.ttl",
|
|
118
118
|
"test:serialize:15": "cd ./tests/serialize && node ./data.js -in=t15.html -format=text/turtle -out=,t15.ttl && node diff ,t15.ttl t15-ref.ttl",
|
|
119
119
|
"test:serialize:16": "cd ./tests/serialize && node ./data.js -in=t1.ttl -format=application/ld+json -out=,t1.jsonld && node diff ,t1.jsonld t16-ref.jsonld",
|
|
120
|
+
"test:serialize:17": "cd ./tests/serialize && node ./data.js -in=t17.ttl -format=application/rdf+xml -out=,t17.xml && node diff ,t17.xml t17-ref.xml",
|
|
120
121
|
"test:types": "tsc --noEmit --target es2019 --moduleResolution node tests/types/*.ts",
|
|
121
122
|
"test:unit": "mocha --growl --require ./tests/babel-register.js tests/unit/**-test.*",
|
|
122
123
|
"test:unit:egp": "mocha --require ./tests/babel-register.js tests/unit/fetcher-egp-test.js",
|
package/src/n3parser.js
CHANGED
|
@@ -212,10 +212,20 @@ var signed_integer = new RegExp("^[-+]?[0-9]+", 'g');
|
|
|
212
212
|
var number_syntax = new RegExp("^([-+]?[0-9]+)(\\.[0-9]+)?([eE][-+]?[0-9]+)?", 'g');
|
|
213
213
|
var datetime_syntax = new RegExp('^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9](T[0-9][0-9]:[0-9][0-9](:[0-9][0-9](\\.[0-9]*)?)?)?Z?');
|
|
214
214
|
|
|
215
|
+
// Reused in tight loops to detect whitespace or comment after a dot
|
|
216
|
+
var wsOrHash = new RegExp("[\\s#]");
|
|
217
|
+
|
|
215
218
|
var digitstring = new RegExp("^[0-9]+", 'g');
|
|
216
219
|
var interesting = new RegExp("[\\\\\\r\\n\\\"]", 'g');
|
|
217
220
|
var langcode = new RegExp("^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*", 'g');
|
|
218
221
|
|
|
222
|
+
// Returns true when a dot at position i should terminate a name,
|
|
223
|
+
// i.e., when the next character is whitespace, a comment start, or EOF
|
|
224
|
+
function dotTerminatesName(str, i) {
|
|
225
|
+
var next = str.charAt(i + 1);
|
|
226
|
+
return next === '' || wsOrHash.test(next);
|
|
227
|
+
}
|
|
228
|
+
|
|
219
229
|
function createSinkParser(store, openFormula, thisDoc, baseURI, genPrefix, metaURI, flags, why) {
|
|
220
230
|
return new SinkParser(store, openFormula, thisDoc, baseURI, genPrefix, metaURI, flags, why);
|
|
221
231
|
}
|
|
@@ -554,7 +564,7 @@ export class SinkParser {
|
|
|
554
564
|
<- prop -<
|
|
555
565
|
_operator_*/
|
|
556
566
|
|
|
557
|
-
|
|
567
|
+
var j = this.skipSpace(str, i);
|
|
558
568
|
if ((j < 0)) {
|
|
559
569
|
return j;
|
|
560
570
|
}
|
|
@@ -703,22 +713,17 @@ export class SinkParser {
|
|
|
703
713
|
if ((j >= 0)) {
|
|
704
714
|
var subj = objs[0];
|
|
705
715
|
if ((pyjslib_len(objs) > 1)) {
|
|
706
|
-
|
|
707
716
|
var __obj = new pyjslib_Iterator(objs);
|
|
708
717
|
try {
|
|
709
718
|
while (true) {
|
|
710
719
|
var obj = __obj.next();
|
|
711
|
-
|
|
712
|
-
|
|
713
720
|
this.makeStatement(new pyjslib_Tuple([this._context, this._store.sym(DAML_sameAs_URI), subj, obj]));
|
|
714
|
-
|
|
715
721
|
}
|
|
716
722
|
} catch (e) {
|
|
717
723
|
if (e != StopIteration) {
|
|
718
724
|
throw e;
|
|
719
725
|
}
|
|
720
726
|
}
|
|
721
|
-
|
|
722
727
|
}
|
|
723
728
|
var j = this.skipSpace(str, j);
|
|
724
729
|
if ((j < 0)) {
|
|
@@ -743,6 +748,16 @@ export class SinkParser {
|
|
|
743
748
|
if ((j < 0)) {
|
|
744
749
|
throw BadSyntax(this._thisDoc, this.lines, str, i, "EOF when ']' expected after [ <propertyList>");
|
|
745
750
|
}
|
|
751
|
+
if ((str.slice( j, ( j + 1 ) ) == ".")) {
|
|
752
|
+
// If a dot is found after a blank node, treat it as a statement terminator.
|
|
753
|
+
// Do NOT consume the '.' here: statement terminators are handled centrally by
|
|
754
|
+
// checkDot() (called by directiveOrStatement after statement()). Consuming the dot
|
|
755
|
+
// locally would bypass that unified logic and could cause inconsistencies.
|
|
756
|
+
// We do consume ']' below because it is a structural closer of the blank node,
|
|
757
|
+
// not a statement terminator.
|
|
758
|
+
res.push(subj);
|
|
759
|
+
return j; // leave '.' for checkDot()
|
|
760
|
+
}
|
|
746
761
|
if ((str.slice( j, ( j + 1 ) ) != "]")) {
|
|
747
762
|
throw BadSyntax(this._thisDoc, this.lines, str, j, "']' expected");
|
|
748
763
|
}
|
|
@@ -1208,7 +1223,17 @@ export class SinkParser {
|
|
|
1208
1223
|
return -1;
|
|
1209
1224
|
}
|
|
1210
1225
|
var i = j;
|
|
1211
|
-
while ((i < pyjslib_len(str))
|
|
1226
|
+
while ((i < pyjslib_len(str))) {
|
|
1227
|
+
var c = str.charAt(i);
|
|
1228
|
+
if (c === '.') {
|
|
1229
|
+
if (dotTerminatesName(str, i)) {
|
|
1230
|
+
break; // treat as statement terminator, not part of name
|
|
1231
|
+
}
|
|
1232
|
+
// else: accept '.' as part of name
|
|
1233
|
+
} else if (_notNameChars.indexOf(c) >= 0) {
|
|
1234
|
+
// Other invalid characters terminate the name
|
|
1235
|
+
break;
|
|
1236
|
+
}
|
|
1212
1237
|
var i = ( i + 1 ) ;
|
|
1213
1238
|
}
|
|
1214
1239
|
res.push(str.slice( j, i));
|
|
@@ -1235,13 +1260,15 @@ export class SinkParser {
|
|
|
1235
1260
|
var i = ( i + 1 ) ;
|
|
1236
1261
|
while ((i < pyjslib_len(str))) {
|
|
1237
1262
|
var c = str.charAt(i);
|
|
1238
|
-
if (
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
else {
|
|
1263
|
+
if (c === '.') {
|
|
1264
|
+
if (dotTerminatesName(str, i)) {
|
|
1265
|
+
break; // dot ends the name here
|
|
1266
|
+
}
|
|
1267
|
+
} else if (_notNameChars.indexOf(c) >= 0) {
|
|
1243
1268
|
break;
|
|
1244
1269
|
}
|
|
1270
|
+
var ln = ( ln + c ) ;
|
|
1271
|
+
var i = ( i + 1 ) ;
|
|
1245
1272
|
}
|
|
1246
1273
|
}
|
|
1247
1274
|
else {
|
|
@@ -1253,13 +1280,15 @@ export class SinkParser {
|
|
|
1253
1280
|
var ln = "";
|
|
1254
1281
|
while ((i < pyjslib_len(str))) {
|
|
1255
1282
|
var c = str.charAt(i);
|
|
1256
|
-
if (
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
else {
|
|
1283
|
+
if (c === '.') {
|
|
1284
|
+
if (dotTerminatesName(str, i)) {
|
|
1285
|
+
break; // dot ends the name here
|
|
1286
|
+
}
|
|
1287
|
+
} else if (_notNameChars.indexOf(c) >= 0) {
|
|
1261
1288
|
break;
|
|
1262
1289
|
}
|
|
1290
|
+
var ln = ( ln + c ) ;
|
|
1291
|
+
var i = ( i + 1 ) ;
|
|
1263
1292
|
}
|
|
1264
1293
|
res.push(new pyjslib_Tuple([pfx, ln]));
|
|
1265
1294
|
return i;
|
package/src/serialize.ts
CHANGED
|
@@ -72,7 +72,8 @@ export default function serialize (
|
|
|
72
72
|
return executeCallback(null, documentString)
|
|
73
73
|
case TurtleContentType:
|
|
74
74
|
case TurtleLegacyContentType:
|
|
75
|
-
|
|
75
|
+
// Suppress = for sameAs and => for implies; preserve any user-specified flags (e.g., 'o')
|
|
76
|
+
sz.setFlags('si' + (opts.flags ? (' ' + opts.flags) : ''))
|
|
76
77
|
documentString = sz.statementsToN3(newSts)
|
|
77
78
|
return executeCallback(null, documentString)
|
|
78
79
|
case NTriplesContentType:
|
|
@@ -80,7 +81,8 @@ export default function serialize (
|
|
|
80
81
|
documentString = sz.statementsToNTriples(newSts)
|
|
81
82
|
return executeCallback(null, documentString)
|
|
82
83
|
case JSONLDContentType:
|
|
83
|
-
|
|
84
|
+
// turtle + dr (means no default, no relative prefix); preserve user flags
|
|
85
|
+
sz.setFlags('si dr' + (opts.flags ? (' ' + opts.flags) : ''))
|
|
84
86
|
documentString = sz.statementsToJsonld(newSts) // convert via turtle
|
|
85
87
|
return executeCallback(null, documentString)
|
|
86
88
|
case NQuadsContentType:
|