1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 * This module creates a new API for accessing and modifying RDF graphs. The
7 * goal is to be able to serialise the graph in a human readable form. Also
8 * if the graph was originally loaded from an RDF/XML the serialisation should
9 * closely match the original with any new data closely following the existing
10 * layout. The output should always be compatible with Mozilla's RDF parser.
12 * This is all achieved by using a DOM Document to hold the current state of the
13 * graph in XML form. This can be initially loaded and parsed from disk or
14 * a blank document used for an empty graph. As assertions are added to the
15 * graph, appropriate DOM nodes are added to the document to represent them
16 * along with any necessary whitespace to properly layout the XML.
18 * In general the order of adding assertions to the graph will impact the form
19 * the serialisation takes. If a resource is first added as the object of an
20 * assertion then it will eventually be serialised inside the assertion's
21 * property element. If a resource is first added as the subject of an assertion
22 * then it will be serialised at the top level of the XML.
25 const NS_XML = "http://www.w3.org/XML/1998/namespace";
26 const NS_XMLNS = "http://www.w3.org/2000/xmlns/";
27 const NS_RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
28 const NS_NC = "http://home.netscape.com/NC-rdf#";
30 /* eslint prefer-template: 1 */
32 function raw(strings) {
33 return strings.raw[0].replace(/\s+/, "");
36 // Copied from http://www.w3.org/TR/2000/REC-xml-20001006#CharClasses
37 const XML_LETTER = raw`
38 \u0041-\u005A\u0061-\u007A\u00C0-\u00D6\u00D8-\u00F6
39 \u00F8-\u00FF\u0100-\u0131\u0134-\u013E\u0141-\u0148
40 \u014A-\u017E\u0180-\u01C3\u01CD-\u01F0\u01F4-\u01F5
41 \u01FA-\u0217\u0250-\u02A8\u02BB-\u02C1\u0386\u0388-\u038A
42 \u038C\u038E-\u03A1\u03A3-\u03CE\u03D0-\u03D6\u03DA\u03DC
43 \u03DE\u03E0\u03E2-\u03F3\u0401-\u040C\u040E-\u044F
44 \u0451-\u045C\u045E-\u0481\u0490-\u04C4\u04C7-\u04C8
45 \u04CB-\u04CC\u04D0-\u04EB\u04EE-\u04F5\u04F8-\u04F9
46 \u0531-\u0556\u0559\u0561-\u0586\u05D0-\u05EA\u05F0-\u05F2
47 \u0621-\u063A\u0641-\u064A\u0671-\u06B7\u06BA-\u06BE
48 \u06C0-\u06CE\u06D0-\u06D3\u06D5\u06E5-\u06E6\u0905-\u0939
49 \u093D\u0958-\u0961\u0985-\u098C\u098F-\u0990\u0993-\u09A8
50 \u09AA-\u09B0\u09B2\u09B6-\u09B9\u09DC-\u09DD\u09DF-\u09E1
51 \u09F0-\u09F1\u0A05-\u0A0A\u0A0F-\u0A10\u0A13-\u0A28
52 \u0A2A-\u0A30\u0A32-\u0A33\u0A35-\u0A36\u0A38-\u0A39
53 \u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8B\u0A8D
54 \u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2-\u0AB3
55 \u0AB5-\u0AB9\u0ABD\u0AE0\u0B05-\u0B0C\u0B0F-\u0B10
56 \u0B13-\u0B28\u0B2A-\u0B30\u0B32-\u0B33\u0B36-\u0B39
57 \u0B3D\u0B5C-\u0B5D\u0B5F-\u0B61\u0B85-\u0B8A\u0B8E-\u0B90
58 \u0B92-\u0B95\u0B99-\u0B9A\u0B9C\u0B9E-\u0B9F\u0BA3-\u0BA4
59 \u0BA8-\u0BAA\u0BAE-\u0BB5\u0BB7-\u0BB9\u0C05-\u0C0C
60 \u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C33\u0C35-\u0C39
61 \u0C60-\u0C61\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8
62 \u0CAA-\u0CB3\u0CB5-\u0CB9\u0CDE\u0CE0-\u0CE1\u0D05-\u0D0C
63 \u0D0E-\u0D10\u0D12-\u0D28\u0D2A-\u0D39\u0D60-\u0D61
64 \u0E01-\u0E2E\u0E30\u0E32-\u0E33\u0E40-\u0E45\u0E81-\u0E82
65 \u0E84\u0E87-\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F
66 \u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA-\u0EAB\u0EAD-\u0EAE\u0EB0
67 \u0EB2-\u0EB3\u0EBD\u0EC0-\u0EC4\u0F40-\u0F47\u0F49-\u0F69
68 \u10A0-\u10C5\u10D0-\u10F6\u1100\u1102-\u1103\u1105-\u1107
69 \u1109\u110B-\u110C\u110E-\u1112\u113C\u113E\u1140\u114C
70 \u114E\u1150\u1154-\u1155\u1159\u115F-\u1161\u1163\u1165
71 \u1167\u1169\u116D-\u116E\u1172-\u1173\u1175\u119E\u11A8
72 \u11AB\u11AE-\u11AF\u11B7-\u11B8\u11BA\u11BC-\u11C2\u11EB
73 \u11F0\u11F9\u1E00-\u1E9B\u1EA0-\u1EF9\u1F00-\u1F15
74 \u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57
75 \u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC
76 \u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB
77 \u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2126\u212A-\u212B
78 \u212E\u2180-\u2182\u3041-\u3094\u30A1-\u30FA\u3105-\u312C
79 \uAC00-\uD7A3\u4E00-\u9FA5\u3007\u3021-\u3029
81 const XML_DIGIT = raw`
82 \u0030-\u0039\u0660-\u0669\u06F0-\u06F9\u0966-\u096F
83 \u09E6-\u09EF\u0A66-\u0A6F\u0AE6-\u0AEF\u0B66-\u0B6F
84 \u0BE7-\u0BEF\u0C66-\u0C6F\u0CE6-\u0CEF\u0D66-\u0D6F
85 \u0E50-\u0E59\u0ED0-\u0ED9\u0F20-\u0F29
87 const XML_COMBINING = raw`
88 \u0300-\u0345\u0360-\u0361\u0483-\u0486\u0591-\u05A1
89 \u05A3-\u05B9\u05BB-\u05BD\u05BF\u05C1-\u05C2\u05C4
90 \u064B-\u0652\u0670\u06D6-\u06DC\u06DD-\u06DF\u06E0-\u06E4
91 \u06E7-\u06E8\u06EA-\u06ED\u0901-\u0903\u093C\u093E-\u094C
92 \u094D\u0951-\u0954\u0962-\u0963\u0981-\u0983\u09BC\u09BE
93 \u09BF\u09C0-\u09C4\u09C7-\u09C8\u09CB-\u09CD\u09D7
94 \u09E2-\u09E3\u0A02\u0A3C\u0A3E\u0A3F\u0A40-\u0A42
95 \u0A47-\u0A48\u0A4B-\u0A4D\u0A70-\u0A71\u0A81-\u0A83
96 \u0ABC\u0ABE-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0B01-\u0B03
97 \u0B3C\u0B3E-\u0B43\u0B47-\u0B48\u0B4B-\u0B4D\u0B56-\u0B57
98 \u0B82-\u0B83\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD7
99 \u0C01-\u0C03\u0C3E-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D
100 \u0C55-\u0C56\u0C82-\u0C83\u0CBE-\u0CC4\u0CC6-\u0CC8
101 \u0CCA-\u0CCD\u0CD5-\u0CD6\u0D02-\u0D03\u0D3E-\u0D43
102 \u0D46-\u0D48\u0D4A-\u0D4D\u0D57\u0E31\u0E34-\u0E3A
103 \u0E47-\u0E4E\u0EB1\u0EB4-\u0EB9\u0EBB-\u0EBC\u0EC8-\u0ECD
104 \u0F18-\u0F19\u0F35\u0F37\u0F39\u0F3E\u0F3F\u0F71-\u0F84
105 \u0F86-\u0F8B\u0F90-\u0F95\u0F97\u0F99-\u0FAD\u0FB1-\u0FB7
106 \u0FB9\u20D0-\u20DC\u20E1\u302A-\u302F\u3099\u309A
108 const XML_EXTENDER = raw`
109 \u00B7\u02D0\u02D1\u0387\u0640\u0E46\u0EC6\u3005
110 \u3031-\u3035\u309D-\u309E\u30FC-\u30FE
112 const XML_NCNAMECHAR = String.raw`${XML_LETTER}${XML_DIGIT}\.\-_${XML_COMBINING}${XML_EXTENDER}`;
113 const XML_NCNAME = new RegExp(`^[${XML_LETTER}_][${XML_NCNAMECHAR}]*$`);
115 const URI_SUFFIX = /[A-Za-z_][0-9A-Za-z\.\-_]*$/;
116 const INDENT = /\n([ \t]*)$/;
117 const RDF_LISTITEM = /^http:\/\/www.w3.org\/1999\/02\/22-rdf-syntax-ns#_\d+$/;
119 const RDF_NODE_INVALID_TYPES =
120 ["RDF", "ID", "about", "bagID", "parseType", "resource", "nodeID",
121 "li", "aboutEach", "aboutEachPrefix"];
122 const RDF_PROPERTY_INVALID_TYPES =
123 ["Description", "RDF", "ID", "about", "bagID", "parseType", "resource",
124 "nodeID", "aboutEach", "aboutEachPrefix"];
127 * Whether to use properly namespaces attributes for rdf:about etc...
128 * When on this produces poor output in the event that the rdf namespace is the
129 * default namespace, and the parser recognises unnamespaced attributes and
130 * most of our rdf examples are unnamespaced so leaving off for the time being.
132 const USE_RDFNS_ATTR = false;
134 var EXPORTED_SYMBOLS = ["RDFLiteral", "RDFIntLiteral", "RDFDateLiteral",
135 "RDFBlankNode", "RDFResource", "RDFDataSource"];
137 const {XPCOMUtils} = ChromeUtils.import("resource://gre/modules/XPCOMUtils.jsm");
139 XPCOMUtils.defineLazyGlobalGetters(this, ["DOMParser", "Element", "XMLSerializer", "fetch"]);
141 const Services = globalThis.Services || ChromeUtils.import("resource://gre/modules/Services.jsm").Services;
143 function isAttr(obj) {
144 return obj && typeof obj == "object" && ChromeUtils.getClassName(obj) == "Attr";
146 function isDocument(obj) {
147 return obj && typeof obj == "object" && obj.nodeType == Element.DOCUMENT_NODE;
149 function isElement(obj) {
150 return Element.isInstance(obj);
152 function isText(obj) {
153 return obj && typeof obj == "object" && ChromeUtils.getClassName(obj) == "Text";
157 * Logs an error message to the error console
159 function ERROR(str) {
163 function RDF_R(name) {
164 return NS_RDF + name;
167 function renameNode(domnode, namespaceURI, qname) {
168 if (isElement(domnode)) {
169 var newdomnode = domnode.ownerDocument.createElementNS(namespaceURI, qname);
170 if ("listCounter" in domnode)
171 newdomnode.listCounter = domnode.listCounter;
172 domnode.replaceWith(newdomnode);
173 while (domnode.firstChild)
174 newdomnode.appendChild(domnode.firstChild);
175 for (let attr of domnode.attributes) {
176 domnode.removeAttributeNode(attr);
177 newdomnode.setAttributeNode(attr);
180 } else if (isAttr(domnode)) {
181 if (domnode.ownerElement.hasAttribute(namespaceURI, qname))
182 throw new Error("attribute already exists");
183 var attr = domnode.ownerDocument.createAttributeNS(namespaceURI, qname);
184 attr.value = domnode.value;
185 domnode.ownerElement.setAttributeNode(attr);
186 domnode.ownerElement.removeAttributeNode(domnode);
189 throw new Error("cannot rename node of this type");
192 function predicateOrder(a, b) {
193 return a.getPredicate().localeCompare(b.getPredicate());
197 * Returns either an rdf namespaced attribute or an un-namespaced attribute
198 * value. Returns null if neither exists,
200 function getRDFAttribute(element, name) {
201 if (element.hasAttributeNS(NS_RDF, name))
202 return element.getAttributeNS(NS_RDF, name);
203 if (element.hasAttribute(name))
204 return element.getAttribute(name);
209 * Represents an assertion in the datasource
212 constructor(subject, predicate, object) {
213 if (!(subject instanceof RDFSubject))
214 throw new Error("subject must be an RDFSubject");
216 if (typeof(predicate) != "string")
217 throw new Error("predicate must be a string URI");
219 if (!(object instanceof RDFLiteral) && !(object instanceof RDFSubject))
220 throw new Error("object must be a concrete RDFNode");
222 if (object instanceof RDFSubject && object._ds != subject._ds)
223 throw new Error("object must be from the same datasource as subject");
225 // The subject on this assertion, an RDFSubject
226 this._subject = subject;
227 // The predicate, a string
228 this._predicate = predicate;
229 // The object, an RDFNode
230 this._object = object;
231 // The datasource this assertion exists in
232 this._ds = this._subject._ds;
233 // Marks that _DOMnode is the subject's element
234 this._isSubjectElement = false;
235 // The DOM node that represents this assertion. Could be a property element,
236 // a property attribute or the subject's element for rdf:type
237 this._DOMNode = null;
241 * Adds content to _DOMnode to store this assertion in the DOM document.
244 if (this._object instanceof RDFLiteral)
245 this._object._applyToDOMNode(this._ds, this._DOMnode);
247 this._object._addReferenceToElement(this._DOMnode);
251 * Returns the DOM Element linked to the subject that this assertion is
254 _getSubjectElement() {
255 if (isAttr(this._DOMnode))
256 return this._DOMnode.ownerElement;
257 if (this._isSubjectElement)
258 return this._DOMnode;
259 return this._DOMnode.parentNode;
263 return this._subject;
267 return this._predicate;
277 return (rdfnode.constructor === this.constructor &&
278 rdfnode._value == this._value);
283 * A simple literal value
285 class RDFLiteral extends RDFNode {
292 * This stores the value of the literal in the given DOM node
294 _applyToDOMNode(ds, domnode) {
295 if (isElement(domnode))
296 domnode.textContent = this._value;
297 else if (isAttr(domnode))
298 domnode.value = this._value;
300 throw new Error("cannot use this node for a literal");
309 * A literal that is integer typed.
311 class RDFIntLiteral extends RDFLiteral {
313 super(parseInt(value));
317 * This stores the value of the literal in the given DOM node
319 _applyToDOMNode(ds, domnode) {
320 if (!isElement(domnode))
321 throw new Error("cannot use this node for a literal");
323 RDFLiteral.prototype._applyToDOMNode.call(this, ds, domnode);
324 var prefix = ds._resolvePrefix(domnode, `${NS_NC}parseType`);
325 domnode.setAttributeNS(prefix.namespaceURI, prefix.qname, "Integer");
330 * A literal that represents a date.
332 class RDFDateLiteral extends RDFLiteral {
334 if (!(value instanceof Date))
335 throw new Error("RDFDateLiteral must be constructed with a Date object");
341 * This stores the value of the literal in the given DOM node
343 _applyToDOMNode(ds, domnode) {
344 if (!isElement(domnode))
345 throw new Error("cannot use this node for a literal");
347 domnode.textContent = this._value.getTime();
348 var prefix = ds._resolvePrefix(domnode, `${NS_NC}parseType`);
349 domnode.setAttributeNS(prefix.namespaceURI, prefix.qname, "Date");
354 * This is an RDF node that can be a subject so a resource or a blank node
356 class RDFSubject extends RDFNode {
359 // A lookup of the assertions with this as the subject. Keyed on predicate
360 this._assertions = {};
361 // A lookup of the assertions with this as the object. Keyed on predicate
362 this._backwards = {};
363 // The datasource this subject belongs to
365 // The DOM elements in the document that represent this subject. Array of Element
370 * Creates a new Element in the document for holding assertions about this
371 * subject. The URI controls what tagname to use.
373 _createElement(uri) {
374 // Seek an appropriate reference to this node to add this node under
376 for (var p in this._backwards) {
377 for (let back of this._backwards[p]) {
378 // Don't add under an rdf:type
379 if (back.getPredicate() == RDF_R("type"))
381 // The assertion already has a child node, probably one of ours
382 if (back._DOMnode.firstChild)
384 parent = back._DOMnode;
385 var element = this._ds._addElement(parent, uri);
386 this._removeReferenceFromElement(parent);
393 // No back assertions that are sensible to use
395 element = this._ds._addElement(this._ds._document.documentElement, uri);
397 element.listCounter = 1;
398 this._applyToElement(element);
399 this._elements.push(element);
404 * When a DOM node representing this subject is removed from the document
405 * we must remove the node and recreate any child assertions elsewhere.
407 _removeElement(element) {
408 var pos = this._elements.indexOf(element);
410 throw new Error("invalid element");
411 this._elements.splice(pos, 1);
412 if (element.parentNode != element.ownerDocument.documentElement)
413 this._addReferenceToElement(element.parentNode);
414 this._ds._removeElement(element);
416 // Find all the assertions that are represented here and create new
418 for (var predicate in this._assertions) {
419 for (let assertion of this._assertions[predicate]) {
420 if (assertion._getSubjectElement() == element)
421 this._createDOMNodeForAssertion(assertion);
427 * Creates a DOM node to represent the assertion in the document. If the
428 * assertion has rdf:type as the predicate then an attempt will be made to
429 * create a typed subject Element, otherwise a new property Element is
430 * created. For list items an attempt is made to find an appropriate container
431 * that an rdf:li element can be added to.
433 _createDOMNodeForAssertion(assertion) {
435 if (RDF_LISTITEM.test(assertion.getPredicate())) {
436 // Find all the containers
437 elements = this._elements.filter(function(element) {
438 return (element.namespaceURI == NS_RDF && (element.localName == "Seq" ||
439 element.localName == "Bag" ||
440 element.localName == "Alt"));
442 if (elements.length > 0) {
443 // Look for one whose listCounter matches the item we want to add
444 var item = parseInt(assertion.getPredicate().substring(NS_RDF.length + 1));
445 for (let element of elements) {
446 if (element.listCounter == item) {
447 assertion._DOMnode = this._ds._addElement(element, RDF_R("li"));
448 assertion._applyToDOMNode();
449 element.listCounter++;
453 // No good container to add to, shove in the first real container
454 assertion._DOMnode = this._ds._addElement(elements[0], assertion.getPredicate());
455 assertion._applyToDOMNode();
458 // TODO No containers, this will end up in a non-container for now
459 } else if (assertion.getPredicate() == RDF_R("type")) {
460 // Try renaming an existing rdf:Description
461 for (let element of this.elements) {
462 if (element.namespaceURI == NS_RDF &&
463 element.localName == "Description") {
465 var prefix = this._ds._resolvePrefix(element.parentNode, assertion.getObject().getURI());
466 element = renameNode(element, prefix.namespaceURI, prefix.qname);
467 assertion._DOMnode = element;
468 assertion._isSubjectElement = true;
471 // If the type cannot be sensibly turned into a prefix then just set
472 // as a regular property
478 // Filter out all the containers
479 elements = this._elements.filter(function(element) {
480 return (element.namespaceURI != NS_RDF || (element.localName != "Seq" &&
481 element.localName != "Bag" &&
482 element.localName != "Alt"));
484 if (elements.length == 0) {
485 // Create a new node of the right type
486 if (assertion.getPredicate() == RDF_R("type")) {
488 assertion._DOMnode = this._createElement(assertion.getObject().getURI());
489 assertion._isSubjectElement = true;
492 // If the type cannot be sensibly turned into a prefix then just set
493 // as a regular property
496 elements[0] = this._createElement(RDF_R("Description"));
498 assertion._DOMnode = this._ds._addElement(elements[0], assertion.getPredicate());
499 assertion._applyToDOMNode();
503 * Removes the DOM node representing the assertion.
505 _removeDOMNodeForAssertion(assertion) {
506 if (isAttr(assertion._DOMnode)) {
507 var parent = assertion._DOMnode.ownerElement;
508 parent.removeAttributeNode(assertion._DOMnode);
509 } else if (assertion._isSubjectElement) {
510 var domnode = renameNode(assertion._DOMnode, NS_RDF, "Description");
511 if (domnode != assertion._DOMnode) {
512 var pos = this._elements.indexOf(assertion._DOMnode);
513 this._elements.splice(pos, 1, domnode);
517 var object = assertion.getObject();
518 if (object instanceof RDFSubject && assertion._DOMnode.firstChild) {
519 // Object is a subject that has an Element inside this assertion's node.
520 for (let element of object._elements) {
521 if (element.parentNode == assertion._DOMnode) {
522 object._removeElement(element);
527 parent = assertion._DOMnode.parentNode;
528 if (assertion._DOMnode.namespaceURI == NS_RDF &&
529 assertion._DOMnode.localName == "li")
530 parent.listCounter--;
531 this._ds._removeElement(assertion._DOMnode);
534 // If there are no assertions left using the assertion's containing dom node
535 // then remove it from the document.
536 // TODO could do with a quick lookup list for assertions attached to a node
537 for (var p in this._assertions) {
538 for (let assertion of this._assertions[p]) {
539 if (assertion._getSubjectElement() == parent)
543 // No assertions left in this element.
544 this._removeElement(parent);
548 * Parses the given Element from the DOM document
550 /* eslint-disable complexity */
551 _parseElement(element) {
552 this._elements.push(element);
554 // There might be an inferred rdf:type assertion in the element name
555 if (element.namespaceURI != NS_RDF ||
556 element.localName != "Description") {
557 if (element.namespaceURI == NS_RDF && element.localName == "li")
558 throw new Error("rdf:li is not a valid type for a subject node");
559 var assertion = new RDFAssertion(this, RDF_R("type"),
560 this._ds.getResource(element.namespaceURI + element.localName));
561 assertion._DOMnode = element;
562 assertion._isSubjectElement = true;
563 this._addAssertion(assertion);
566 // Certain attributes can be literal properties
567 for (let attr of element.attributes) {
568 if (attr.namespaceURI == NS_XML || attr.namespaceURI == NS_XMLNS ||
569 attr.nodeName == "xmlns")
571 if ((attr.namespaceURI == NS_RDF || !attr.namespaceURI) &&
572 (["nodeID", "about", "resource", "ID", "parseType"].includes(attr.localName)))
575 if (attr.namespaceURI == NS_RDF) {
576 if (attr.localName == "type")
577 object = this._ds.getResource(attr.nodeValue);
578 else if (attr.localName == "li")
579 throw new Error("rdf:li is not allowed as a property attribute");
580 else if (attr.localName == "aboutEach")
581 throw new Error("rdf:aboutEach is deprecated");
582 else if (attr.localName == "aboutEachPrefix")
583 throw new Error("rdf:aboutEachPrefix is deprecated");
584 else if (attr.localName == "aboutEach")
585 throw new Error("rdf:aboutEach is deprecated");
586 else if (attr.localName == "bagID")
587 throw new Error("rdf:bagID is deprecated");
590 object = new RDFLiteral(attr.nodeValue);
591 assertion = new RDFAssertion(this, attr.namespaceURI + attr.localName, object);
592 assertion._DOMnode = attr;
593 this._addAssertion(assertion);
596 var child = element.firstChild;
597 element.listCounter = 1;
599 if (isText(child) && /\S/.test(child.nodeValue)) {
600 ERROR(`Text ${child.nodeValue} is not allowed in a subject node`);
601 throw new Error("subject nodes cannot contain text content");
602 } else if (isElement(child)) {
604 var predicate = child.namespaceURI + child.localName;
605 if (child.namespaceURI == NS_RDF) {
606 if (RDF_PROPERTY_INVALID_TYPES.includes(child.localName) &&
607 !child.localName.match(/^_\d+$/))
608 throw new Error(`${child.nodeName} is an invalid property`);
609 if (child.localName == "li") {
610 predicate = RDF_R(`_${element.listCounter}`);
611 element.listCounter++;
615 // Check for and bail out on unknown attributes on the property element
616 for (let attr of child.attributes) {
617 // Ignore XML namespaced attributes
618 if (attr.namespaceURI == NS_XML)
620 // These are reserved by XML for future use
621 if (attr.localName.substring(0, 3).toLowerCase() == "xml")
623 // We can handle these RDF attributes
624 if ((!attr.namespaceURI || attr.namespaceURI == NS_RDF) &&
625 ["resource", "nodeID"].includes(attr.localName))
627 // This is a special attribute we handle for compatibility with Mozilla RDF
628 if (attr.namespaceURI == NS_NC &&
629 attr.localName == "parseType")
631 throw new Error(`Attribute ${attr.nodeName} is not supported`);
634 var parseType = child.getAttributeNS(NS_NC, "parseType");
635 if (parseType && parseType != "Date" && parseType != "Integer") {
636 ERROR(`parseType ${parseType} is not supported`);
637 throw new Error("unsupported parseType");
640 var resource = getRDFAttribute(child, "resource");
641 var nodeID = getRDFAttribute(child, "nodeID");
642 if ((resource && (nodeID || parseType)) ||
643 (nodeID && (resource || parseType))) {
644 ERROR("Cannot use more than one of parseType, resource and nodeID on a single node");
645 throw new Error("Invalid rdf assertion");
648 if (resource !== undefined) {
649 var base = Services.io.newURI(element.baseURI);
650 object = this._ds.getResource(base.resolve(resource));
651 } else if (nodeID !== undefined) {
652 if (!nodeID.match(XML_NCNAME))
653 throw new Error("rdf:nodeID must be a valid XML name");
654 object = this._ds.getBlankNode(nodeID);
657 var childElement = null;
658 var subchild = child.firstChild;
660 if (isText(subchild) && /\S/.test(subchild.nodeValue)) {
662 } else if (isElement(subchild)) {
664 new Error(`Multiple object elements found in ${child.nodeName}`);
666 childElement = subchild;
668 subchild = subchild.nextSibling;
671 if ((resource || nodeID) && (hasText || childElement)) {
672 ERROR("Assertion references a resource so should not contain additional contents");
673 throw new Error("assertion cannot contain multiple objects");
676 if (hasText && childElement) {
677 ERROR(`Both literal and resource objects found in ${child.nodeName}`);
678 throw new Error("assertion cannot contain multiple objects");
683 ERROR("Cannot specify a parseType for an assertion with resource object");
684 throw new Error("parseType is not valid in this context");
686 object = this._ds._getSubjectForElement(childElement);
687 object._parseElement(childElement);
688 } else if (parseType == "Integer") {
689 object = new RDFIntLiteral(child.textContent);
690 } else if (parseType == "Date") {
691 object = new RDFDateLiteral(new Date(child.textContent));
693 object = new RDFLiteral(child.textContent);
697 assertion = new RDFAssertion(this, predicate, object);
698 this._addAssertion(assertion);
699 assertion._DOMnode = child;
701 child = child.nextSibling;
704 /* eslint-enable complexity */
707 * Adds a new assertion to the internal hashes. Should be called for every
708 * new assertion parsed or created programmatically.
710 _addAssertion(assertion) {
711 var predicate = assertion.getPredicate();
712 if (predicate in this._assertions)
713 this._assertions[predicate].push(assertion);
715 this._assertions[predicate] = [ assertion ];
717 var object = assertion.getObject();
718 if (object instanceof RDFSubject) {
719 // Create reverse assertion
720 if (predicate in object._backwards)
721 object._backwards[predicate].push(assertion);
723 object._backwards[predicate] = [ assertion ];
728 * Removes an assertion from the internal hashes. Should be called for all
729 * assertions that are programmatically deleted.
731 _removeAssertion(assertion) {
732 var predicate = assertion.getPredicate();
733 if (predicate in this._assertions) {
734 var pos = this._assertions[predicate].indexOf(assertion);
736 this._assertions[predicate].splice(pos, 1);
737 if (this._assertions[predicate].length == 0)
738 delete this._assertions[predicate];
741 var object = assertion.getObject();
742 if (object instanceof RDFSubject) {
743 // Delete reverse assertion
744 if (predicate in object._backwards) {
745 pos = object._backwards[predicate].indexOf(assertion);
747 object._backwards[predicate].splice(pos, 1);
748 if (object._backwards[predicate].length == 0)
749 delete object._backwards[predicate];
755 * Returns the ordinal assertions from this subject in order.
757 _getChildAssertions() {
759 for (var i in this._assertions) {
760 if (RDF_LISTITEM.test(i))
761 assertions.push(...this._assertions[i]);
763 assertions.sort(predicateOrder);
768 * Compares this to another rdf node
771 // subjects are created by the datasource so no two objects ever correspond
773 return this === rdfnode;
777 * Adds a new assertion with this as the subject
779 assert(predicate, object) {
780 if (predicate == RDF_R("type") && !(object instanceof RDFResource))
781 throw new Error("rdf:type must be an RDFResource");
783 var assertion = new RDFAssertion(this, predicate, object);
784 this._createDOMNodeForAssertion(assertion);
785 this._addAssertion(assertion);
789 * Removes an assertion matching the predicate and node given, if such an
792 unassert(predicate, object) {
793 if (!(predicate in this._assertions))
796 for (let assertion of this._assertions[predicate]) {
797 if (assertion.getObject().equals(object)) {
798 this._removeAssertion(assertion);
799 this._removeDOMNodeForAssertion(assertion);
806 * Returns an array of all the predicates that exist in assertions from this
810 return Object.keys(this._assertions);
814 * Returns all objects in assertions with this subject and the given predicate.
816 getObjects(predicate) {
817 if (predicate in this._assertions)
818 return Array.from(this._assertions[predicate],
825 * Returns all of the ordinal children of this subject in order.
828 return Array.from(this._getChildAssertions(),
833 * Removes the child at the given index. This is the index based on the
834 * children returned from getChildren. Forces a reordering of the later
839 throw new Error("no such child");
840 var assertions = this._getChildAssertions();
841 if (pos >= assertions.length)
842 throw new Error("no such child");
843 for (var i = pos; i < assertions.length; i++) {
844 this._removeAssertion(assertions[i]);
845 this._removeDOMNodeForAssertion(assertions[i]);
849 index = parseInt(assertions[pos - 1].getPredicate().substring(NS_RDF.length + 1)) + 1;
850 for (let i = pos + 1; i < assertions.length; i++) {
851 assertions[i]._predicate = RDF_R(`_${index}`);
852 this._addAssertion(assertions[i]);
853 this._createDOMNodeForAssertion(assertions[i]);
859 * Removes the child with the given object. It is unspecified which child is
860 * removed if the object features more than once.
862 removeChild(object) {
863 var assertions = this._getChildAssertions();
864 for (var pos = 0; pos < assertions.length; pos++) {
865 if (assertions[pos].getObject().equals(object)) {
866 for (var i = pos; i < assertions.length; i++) {
867 this._removeAssertion(assertions[i]);
868 this._removeDOMNodeForAssertion(assertions[i]);
872 index = parseInt(assertions[pos - 1].getPredicate().substring(NS_RDF.length + 1)) + 1;
873 for (let i = pos + 1; i < assertions.length; i++) {
874 assertions[i]._predicate = RDF_R(`_${index}`);
875 this._addAssertion(assertions[i]);
876 this._createDOMNodeForAssertion(assertions[i]);
882 throw new Error("no such child");
886 * Adds a new ordinal child to this subject.
890 for (var i in this._assertions) {
891 if (RDF_LISTITEM.test(i))
892 max = Math.max(max, parseInt(i.substring(NS_RDF.length + 1)));
895 this.assert(RDF_R(`_${max}`), object);
899 * This reorders the child assertions to remove duplicates and gaps in the
900 * sequence. Generally this will move all children to be under the same
901 * container element and all represented as an rdf:li
904 var assertions = this._getChildAssertions();
905 for (let assertion of assertions) {
906 this._removeAssertion(assertion);
907 this._removeDOMNodeForAssertion(assertion);
910 for (let assertion of assertions) {
911 assertion._predicate = RDF_R(`_${index}`);
912 this._addAssertion(assertion);
913 this._createDOMNodeForAssertion(assertion);
919 * Returns the type of this subject or null if there is no specified type.
922 var type = this.getProperty(RDF_R("type"));
923 if (type && type instanceof RDFResource)
924 return type.getURI();
929 * Tests if a property exists for the given predicate.
931 hasProperty(predicate) {
932 return (predicate in this._assertions);
936 * Retrieves the first property value for the given predicate.
938 getProperty(predicate) {
939 if (predicate in this._assertions)
940 return this._assertions[predicate][0].getObject();
945 * Sets the property value for the given predicate, clearing any existing
948 setProperty(predicate, object) {
949 // TODO optimise by replacing the first assertion and clearing the rest
950 this.clearProperty(predicate);
951 this.assert(predicate, object);
955 * Clears any existing properties for the given predicate.
957 clearProperty(predicate) {
958 if (!(predicate in this._assertions))
961 var assertions = this._assertions[predicate];
962 while (assertions.length > 0) {
963 var assertion = assertions[0];
964 this._removeAssertion(assertion);
965 this._removeDOMNodeForAssertion(assertion);
971 * Creates a new RDFResource for the datasource. Private.
973 class RDFResource extends RDFSubject {
974 constructor(ds, uri) {
975 if (!(ds instanceof RDFDataSource))
976 throw new Error("datasource must be an RDFDataSource");
979 throw new Error("An RDFResource requires a non-null uri");
982 // This is the uri that the resource represents.
987 * Sets attributes on the DOM element to mark it as representing this resource
989 _applyToElement(element) {
990 if (USE_RDFNS_ATTR) {
991 var prefix = this._ds._resolvePrefix(element, RDF_R("about"));
992 element.setAttributeNS(prefix.namespaceURI, prefix.qname, this._uri);
994 element.setAttribute("about", this._uri);
999 * Adds a reference to this resource to the given property Element.
1001 _addReferenceToElement(element) {
1002 if (USE_RDFNS_ATTR) {
1003 var prefix = this._ds._resolvePrefix(element, RDF_R("resource"));
1004 element.setAttributeNS(prefix.namespaceURI, prefix.qname, this._uri);
1006 element.setAttribute("resource", this._uri);
1011 * Removes any reference to this resource from the given property Element.
1013 _removeReferenceFromElement(element) {
1014 if (element.hasAttributeNS(NS_RDF, "resource"))
1015 element.removeAttributeNS(NS_RDF, "resource");
1016 if (element.hasAttribute("resource"))
1017 element.removeAttribute("resource");
1026 * Creates a new blank node. Private.
1028 class RDFBlankNode extends RDFSubject {
1029 constructor(ds, nodeID) {
1030 if (!(ds instanceof RDFDataSource))
1031 throw new Error("datasource must be an RDFDataSource");
1034 // The nodeID of this node. May be null if there is no ID.
1035 this._nodeID = nodeID;
1039 * Sets attributes on the DOM element to mark it as representing this node
1041 _applyToElement(element) {
1044 if (USE_RDFNS_ATTR) {
1045 var prefix = this._ds._resolvePrefix(element, RDF_R("nodeID"));
1046 element.setAttributeNS(prefix.namespaceURI, prefix.qname, this._nodeID);
1048 element.setAttribute("nodeID", this._nodeID);
1053 * Creates a new Element in the document for holding assertions about this
1054 * subject. The URI controls what tagname to use.
1056 _createNewElement(uri) {
1057 // If there are already nodes representing this in the document then we need
1058 // a nodeID to match them
1059 if (!this._nodeID && this._elements.length > 0) {
1060 this._ds._createNodeID(this);
1061 for (let element of this._elements)
1062 this._applyToElement(element);
1065 return super._createNewElement.call(uri);
1069 * Adds a reference to this node to the given property Element.
1071 _addReferenceToElement(element) {
1072 if (this._elements.length > 0 && !this._nodeID) {
1073 // In document elsewhere already
1074 // Create a node ID and update the other nodes referencing
1075 this._ds._createNodeID(this);
1076 for (let element of this._elements)
1077 this._applyToElement(element);
1081 if (USE_RDFNS_ATTR) {
1082 let prefix = this._ds._resolvePrefix(element, RDF_R("nodeID"));
1083 element.setAttributeNS(prefix.namespaceURI, prefix.qname, this._nodeID);
1085 element.setAttribute("nodeID", this._nodeID);
1088 // Add the empty blank node, this is generally right since further
1089 // assertions will be added to fill this out
1090 var newelement = this._ds._addElement(element, RDF_R("Description"));
1091 newelement.listCounter = 1;
1092 this._elements.push(newelement);
1097 * Removes any reference to this node from the given property Element.
1099 _removeReferenceFromElement(element) {
1100 if (element.hasAttributeNS(NS_RDF, "nodeID"))
1101 element.removeAttributeNS(NS_RDF, "nodeID");
1102 if (element.hasAttribute("nodeID"))
1103 element.removeAttribute("nodeID");
1107 return this._nodeID;
1112 * Creates a new RDFDataSource from the given document. The document will be
1113 * changed as assertions are added and removed to the RDF. Pass a null document
1114 * to start with an empty graph.
1116 class RDFDataSource {
1117 constructor(document) {
1118 // All known resources, indexed on URI
1119 this._resources = {};
1121 this._allBlankNodes = [];
1122 // All blank nodes with IDs, indexed on ID
1123 this._blankNodes = {};
1124 // Suggested prefixes to use for namespaces, index is prefix, value is namespaceURI.
1131 // Creating a document through xpcom leaves out the xml prolog so just parse
1133 var parser = Cc["@mozilla.org/xmlextras/domparser;1"].
1134 createInstance(Ci.nsIDOMParser);
1135 var doctext = `<?xml version="1.0"?>\n<rdf:RDF xmlns:rdf="${NS_RDF}"/>\n`;
1136 document = parser.parseFromString(doctext, "text/xml");
1138 // The underlying DOM document for this datasource
1139 this._document = document;
1140 this._parseDocument();
1143 static loadFromString(text) {
1144 let parser = new DOMParser();
1145 let document = parser.parseFromString(text, "application/xml");
1147 return new this(document);
1150 static loadFromBuffer(buffer) {
1151 let parser = new DOMParser();
1152 let document = parser.parseFromBuffer(new Uint8Array(buffer), "application/xml");
1154 return new this(document);
1157 static async loadFromFile(uri) {
1158 if (uri instanceof Ci.nsIFile)
1159 uri = Services.io.newFileURI(uri);
1160 else if (typeof(uri) == "string")
1161 uri = Services.io.newURI(uri);
1163 let resp = await fetch(uri.spec);
1164 return this.loadFromBuffer(await resp.arrayBuffer());
1168 return this._document.documentURI;
1172 * Creates a new nodeID for an unnamed blank node. Just node<number>.
1174 _createNodeID(blanknode) {
1176 while (`node${i}` in this._blankNodes)
1178 blanknode._nodeID = `node${i}`;
1179 this._blankNodes[blanknode._nodeID] = blanknode;
1183 * Returns an rdf subject for the given DOM Element. If the subject has not
1184 * been seen before a new one is created.
1186 _getSubjectForElement(element) {
1187 if (element.namespaceURI == NS_RDF &&
1188 RDF_NODE_INVALID_TYPES.includes(element.localName))
1189 throw new Error(`${element.nodeName} is not a valid class for a subject node`);
1191 var about = getRDFAttribute(element, "about");
1192 var id = getRDFAttribute(element, "ID");
1193 var nodeID = getRDFAttribute(element, "nodeID");
1195 if ((about && (id || nodeID)) ||
1196 (nodeID && (id || about))) {
1197 ERROR("More than one of about, ID and nodeID present on the same subject");
1198 throw new Error("invalid subject in rdf");
1201 if (about !== undefined) {
1202 let base = Services.io.newURI(element.baseURI);
1203 return this.getResource(base.resolve(about));
1205 if (id !== undefined) {
1206 if (!id.match(XML_NCNAME))
1207 throw new Error("rdf:ID must be a valid XML name");
1208 let base = Services.io.newURI(element.baseURI);
1209 return this.getResource(base.resolve(`#${id}`));
1211 if (nodeID !== undefined)
1212 return this.getBlankNode(nodeID);
1213 return this.getBlankNode(null);
1217 * Parses the document for subjects at the top level.
1220 if (!this._document.documentElement) {
1221 ERROR("No document element in document");
1222 throw new Error("document contains no root element");
1225 if (this._document.documentElement.namespaceURI != NS_RDF ||
1226 this._document.documentElement.localName != "RDF") {
1227 ERROR(`${this._document.documentElement.nodeName} is not rdf:RDF`);
1228 throw new Error("document does not appear to be RDF");
1231 var domnode = this._document.documentElement.firstChild;
1233 if (isText(domnode) && /\S/.test(domnode.nodeValue)) {
1234 ERROR("RDF does not allow for text in the root of the document");
1235 throw new Error("invalid markup in document");
1236 } else if (isElement(domnode)) {
1237 var subject = this._getSubjectForElement(domnode);
1238 subject._parseElement(domnode);
1240 domnode = domnode.nextSibling;
1245 * Works out a sensible namespace prefix to use for the given uri. node should
1246 * be the parent of where the element is to be inserted, or the node that an
1247 * attribute is to be added to. This will recursively walk to the top of the
1248 * document finding an already registered prefix that matches for the uri.
1249 * If none is found a new prefix is registered.
1250 * This returns an object with keys namespaceURI, prefix, localName and qname.
1251 * Pass null or undefined for badPrefixes for the first call.
1253 _resolvePrefix(domnode, uri, badPrefixes) {
1257 // No known prefix, try to create one from the lookup list
1258 if (!domnode || isDocument(domnode)) {
1259 for (let i in this._prefixes) {
1260 if (badPrefixes.includes(i))
1262 if (this._prefixes[i] == uri.substring(0, this._prefixes[i].length)) {
1263 var local = uri.substring(this._prefixes[i].length);
1264 var test = URI_SUFFIX.exec(local);
1265 // Remaining part of uri is a good XML Name
1266 if (test && test[0] == local) {
1267 this._document.documentElement.setAttributeNS(NS_XMLNS, `xmlns:${i}`, this._prefixes[i]);
1269 namespaceURI: this._prefixes[i],
1272 qname: i ? `${i}:${local}` : local,
1278 // No match, make something up
1279 test = URI_SUFFIX.exec(uri);
1281 var namespaceURI = uri.substring(0, uri.length - test[0].length);
1284 while (badPrefixes.includes(`NS${i}`))
1286 this._document.documentElement.setAttributeNS(NS_XMLNS, `xmlns:NS${i}`, namespaceURI);
1291 qname: `NS${i}:${local}`,
1294 // There is no end part of this URI that is an XML Name
1295 throw new Error(`invalid node name: ${uri}`);
1298 for (let attr of domnode.attributes) {
1299 // Not a namespace declaration, ignore this attribute
1300 if (attr.namespaceURI != NS_XMLNS && attr.nodeName != "xmlns")
1303 var prefix = attr.prefix ? attr.localName : "";
1304 // Seen this prefix before, cannot use it
1305 if (badPrefixes.includes(prefix))
1308 // Namespace matches the start of the uri
1309 if (attr.value == uri.substring(0, attr.value.length)) {
1310 local = uri.substring(attr.value.length);
1311 test = URI_SUFFIX.exec(local);
1312 // Remaining part of uri is a good XML Name
1313 if (test && test[0] == local) {
1315 namespaceURI: attr.value,
1318 qname: prefix ? `${prefix}:${local}` : local,
1323 badPrefixes.push(prefix);
1326 // No prefix found here, move up the document
1327 return this._resolvePrefix(domnode.parentNode, uri, badPrefixes);
1331 * Guess the indent level within the given Element. The method looks for
1332 * elements that are preceded by whitespace including a newline. The
1333 * whitespace following the newline is presumed to be the indentation for the
1335 * If the indentation cannot be guessed then it recurses up the document
1336 * hierarchy until it can guess the indent or until the Document is reached.
1338 _guessIndent(element) {
1339 // The indent at document level is 0
1340 if (!element || isDocument(element))
1343 // Check the text immediately preceding each child node. One could be
1346 var child = element.firstChild;
1348 if (isText(child)) {
1349 pretext += child.nodeValue;
1350 } else if (isElement(child)) {
1351 var result = INDENT.exec(pretext);
1356 child = child.nextSibling;
1359 // pretext now contains any trailing text in the element. This can be
1360 // the indent of the end tag. If so add a little to it.
1361 result = INDENT.exec(pretext);
1363 return `${result[1]} `;
1365 // Check the text immediately before this node
1367 var sibling = element.previousSibling;
1368 while (sibling && isText(sibling)) {
1369 pretext += sibling.nodeValue;
1370 sibling = sibling.previousSibling;
1373 // If there is a sensible indent then just add to it.
1374 result = INDENT.exec(pretext);
1376 return `${result[1]} `;
1378 // Last chance, get the indent level for the tag above and add to it
1379 return `${this._guessIndent(element.parentNode)} `;
1382 _addElement(parent, uri) {
1383 var prefix = this._resolvePrefix(parent, uri);
1384 var element = this._document.createElementNS(prefix.namespaceURI, prefix.qname);
1386 if (parent.lastChild) {
1387 // We want to insert immediately after the last child element
1388 var last = parent.lastChild;
1389 while (last && isText(last))
1390 last = last.previousSibling;
1391 // No child elements so insert at the start
1393 last = parent.firstChild;
1395 last = last.nextSibling;
1397 let indent = this._guessIndent(parent);
1398 parent.insertBefore(this._document.createTextNode(`\n${indent}`), last);
1399 parent.insertBefore(element, last);
1401 // No children, must indent our element and the end tag
1402 let indent = this._guessIndent(parent.parentNode);
1403 parent.append(`\n${indent} `, element, `\n${indent}`);
1409 * Removes the element from its parent. Should also remove surrounding
1410 * white space as appropriate.
1412 _removeElement(element) {
1413 var parent = element.parentNode;
1414 var sibling = element.previousSibling;
1415 // Drop any text nodes immediately preceding the element
1416 while (sibling && isText(sibling)) {
1418 sibling = sibling.previousSibling;
1419 parent.removeChild(temp);
1422 sibling = element.nextSibling;
1424 parent.removeChild(element);
1426 // If the next node after element is now the first child then element was
1427 // the first child. If there are no other child elements then remove the
1428 // remaining child nodes.
1429 if (parent.firstChild == sibling) {
1430 while (sibling && isText(sibling))
1431 sibling = sibling.nextSibling;
1433 // No other child elements
1434 while (parent.lastChild)
1435 parent.removeChild(parent.lastChild);
1441 * Requests that a given prefix be used for the namespace where possible.
1442 * This must be called before any assertions are made using the namespace
1443 * and the registration will not override any existing prefix used in the
1446 registerPrefix(prefix, namespaceURI) {
1447 this._prefixes[prefix] = namespaceURI;
1451 * Gets a blank node. nodeID may be null and if so a new blank node is created.
1452 * If a nodeID is given then the blank node with that ID is returned or created.
1454 getBlankNode(nodeID) {
1455 if (nodeID && nodeID in this._blankNodes)
1456 return this._blankNodes[nodeID];
1458 if (nodeID && !nodeID.match(XML_NCNAME))
1459 throw new Error("rdf:nodeID must be a valid XML name");
1461 var rdfnode = new RDFBlankNode(this, nodeID);
1462 this._allBlankNodes.push(rdfnode);
1464 this._blankNodes[nodeID] = rdfnode;
1469 * Gets all blank nodes
1471 getAllBlankNodes() {
1472 return this._allBlankNodes.slice();
1476 * Gets the resource for the URI. The resource is created if it has not been
1480 if (uri in this._resources)
1481 return this._resources[uri];
1483 var resource = new RDFResource(this, uri);
1484 this._resources[uri] = resource;
1489 * Gets all resources that have been used.
1492 return Object.values(this._resources);
1496 * Returns all blank nodes and resources
1499 return [...Object.values(this._resources),
1500 ...this._allBlankNodes];
1504 * Saves the RDF/XML to a string.
1506 serializeToString() {
1507 var serializer = new XMLSerializer();
1508 return serializer.serializeToString(this._document);
1512 * Saves the RDF/XML to a file.
1514 async saveToFile(file) {
1515 return IOUtils.writeUTF8(file, this.serializeToString());