@lblod/graph-rdfa-processor 2.1.1 → 2.1.3

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/src/rdfa-graph.js CHANGED
@@ -4,10 +4,10 @@ export class RDFaGraph {
4
4
  constructor() {
5
5
  var dataContext = this;
6
6
  this.curieParser = {
7
- trim: function(str) {
7
+ trim: function (str) {
8
8
  return str.replace(/^\s\s*/, "").replace(/\s\s*$/, "");
9
9
  },
10
- parse: function(value, resolve) {
10
+ parse: function (value, resolve) {
11
11
  value = this.trim(value);
12
12
  if (value.charAt(0) == "[" && value.charAt(value.length - 1) == "]") {
13
13
  value = value.substring(1, value.length - 1);
@@ -34,19 +34,19 @@ export class RDFaGraph {
34
34
  },
35
35
  };
36
36
  this.base = null;
37
- this.toString = function(requestOptions) {
37
+ this.toString = function (requestOptions) {
38
38
  var options =
39
39
  requestOptions && requestOptions.shorten
40
40
  ? { graph: this, shorten: true, prefixesUsed: {} }
41
41
  : null;
42
42
  if (requestOptions && requestOptions.blankNodePrefix) {
43
- options.filterBlankNode = function(id) {
43
+ options.filterBlankNode = function (id) {
44
44
  return "_:" + requestOptions.blankNodePrefix + id.substring(2);
45
45
  };
46
46
  }
47
47
  if (requestOptions && requestOptions.numericalBlankNodePrefix) {
48
48
  var onlyNumbers = /^[0-9]+$/;
49
- options.filterBlankNode = function(id) {
49
+ options.filterBlankNode = function (id) {
50
50
  var label = id.substring(2);
51
51
  return onlyNumbers.test(label)
52
52
  ? "_:" + requestOptions.numericalBlankNodePrefix + label
@@ -72,7 +72,7 @@ export class RDFaGraph {
72
72
  return prolog.length == 0 ? s : prolog + "\n" + s;
73
73
  };
74
74
  this.blankNodeCounter = 0;
75
- this.clear = function() {
75
+ this.clear = function () {
76
76
  this.subjects = {};
77
77
  this.prefixes = {};
78
78
  this.terms = {};
@@ -130,7 +130,7 @@ export class RDFaGraph {
130
130
  Object.defineProperty(this, "tripleCount", {
131
131
  enumerable: true,
132
132
  configurable: false,
133
- get: function() {
133
+ get: function () {
134
134
  var count = 0;
135
135
  for (var s in this.subjects) {
136
136
  var snode = this.subjects[s];
@@ -459,7 +459,9 @@ export class RDFaPredicate {
459
459
  }
460
460
  s +=
461
461
  '"""' +
462
- value.replace(/"""/g, '\\"\\"\\"') +
462
+ value
463
+ .replace(/([^\\]|^)(\\)(?!\\)/g, "$1\\\\")
464
+ .replace(/"""/g, '\\"\\"\\"') +
463
465
  '"""^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>';
464
466
  } else if (
465
467
  this.objects[i].type ==
@@ -471,18 +473,25 @@ export class RDFaPredicate {
471
473
  } else {
472
474
  s +=
473
475
  '"""' +
474
- this.objects[i].value[0].parentNode.innerHTML.replace(
475
- /"""/g,
476
- '\\"\\"\\"',
477
- ) +
476
+ this.objects[i].value[0].parentNode.innerHTML
477
+ .replace(/([^\\]|^)(\\)(?!\\)/g, "$1\\\\")
478
+ .replace(/"""/g, '\\"\\"\\"') +
478
479
  '"""^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#HTML>';
479
480
  }
480
481
  } else {
481
482
  var l = this.objects[i].value.toString();
482
483
  if (l.indexOf("\n") >= 0 || l.indexOf("\r") >= 0) {
483
- s += '"""' + l.replace(/"""/g, '\\"\\"\\"') + '"""';
484
+ s +=
485
+ '"""' +
486
+ l
487
+ .replace(/([^\\]|^)(\\)(?!\\)/g, "$1\\\\")
488
+ .replace(/"""/g, '\\"\\"\\"') +
489
+ '"""';
484
490
  } else {
485
- s += '"' + l.replace(/"/g, '\\"') + '"';
491
+ s +=
492
+ '"' +
493
+ l.replace(/([^\\]|^)(\\)(?!\\)/g, "$1\\\\").replace(/"/g, '\\"') +
494
+ '"';
486
495
  }
487
496
  if (
488
497
  this.objects[i].type !=
@@ -498,7 +507,7 @@ export class RDFaPredicate {
498
507
  }
499
508
  }
500
509
 
501
- RDFaPredicate.getPrefixMap = function(e) {
510
+ RDFaPredicate.getPrefixMap = function (e) {
502
511
  var prefixMap = {};
503
512
  while (e.attributes) {
504
513
  for (var i = 0; i < e.attributes.length; i++) {
@@ -913,9 +913,7 @@ export default class RDFaProcessor extends URIResolver {
913
913
  content = typedResource;
914
914
  } else {
915
915
  content = current.textContent;
916
- if (content.includes("\\")) {
917
- content = content.replaceAll("\\", "\\\\");
918
- }
916
+
919
917
  if (this.inHTMLMode && current.localName == "time") {
920
918
  datatype = RDFaProcessor.deriveDateTimeType(content);
921
919
  }
package/test/test.js CHANGED
@@ -23,7 +23,7 @@ describe("getRDFaGraph", function () {
23
23
  assert.equal(graph.toString(), expected);
24
24
  });
25
25
 
26
- it("whatever", () => {
26
+ it.only("whatever", () => {
27
27
  let ht = readFileSync("./bug.html");
28
28
  let { document } = jsdom(ht).defaultView.window;
29
29
 
@@ -37,7 +37,7 @@ describe("getRDFaGraph", function () {
37
37
  },
38
38
  ],
39
39
  });
40
- writeFileSync("/tmp/x.out", graph.toString(), "utf8");
40
+ writeFileSync("/tmp/x.ttl", graph.toString(), "utf8");
41
41
  });
42
42
 
43
43
  it("should getRDFaGraph from a node", function () {