8000 feat: Add compareDocumentPosition method from level 3 spec. by zorkow · Pull Request #488 · xmldom/xmldom · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: Add compareDocumentPosition method from level 3 spec. #488

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions lib/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,58 @@ var INVALID_MODIFICATION_ERR = (ExceptionCode.INVALID_MODIFICATION_ERR = ((Excep
var NAMESPACE_ERR = (ExceptionCode.NAMESPACE_ERR = ((ExceptionMessage[14] = 'Invalid namespace'), 14));
var INVALID_ACCESS_ERR = (ExceptionCode.INVALID_ACCESS_ERR = ((ExceptionMessage[15] = 'Invalid access'), 15));

// compareDocumentPosition bitmask results
var DocumentPosition = {};
var DOCUMENT_POSITION_DISCONNECTED = (DocumentPosition.DOCUMENT_POSITION_DISCONNECTED = 1);
var DOCUMENT_POSITION_PRECEDING = (DocumentPosition.DOCUMENT_POSITION_PRECEDING = 2);
var DOCUMENT_POSITION_FOLLOWING = (DocumentPosition.DOCUMENT_POSITION_FOLLOWING = 4);
var DOCUMENT_POSITION_CONTAINS = (DocumentPosition.DOCUMENT_POSITION_CONTAINS = 8);
var DOCUMENT_POSITION_CONTAINED_BY = (DocumentPosition.DOCUMENT_POSITION_CONTAINED_BY = 16);
var DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = (DocumentPosition.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 32);

//helper functions for compareDocumentPosition
/**
* Construct a parent chain for a node.
* @param {Node} node The start node.
* @return {Node[]} The parent chain.
*/
function parentChain(node) {
var chain = [];
while (node.parentNode || node.ownerElement) {
node = node.parentNode || node.ownerElement;
chain.unshift(node);
}
return chain;
}

/**
* Find the common ancestor in two parent chains.
* @param {Node[]} a A parent chain.
< 8000 /td> * @param {Node[]} b A parent chain.
* @return {Node} The common ancestor if it exists.
*/
function commonAncestor(a, b) {
if (b.length < a.length) return commonAncestor(b, a);
var c = null;
for (var n in a) {
if (a[n] !== b[n]) return c;
c = a[n];
}
return c;
}

/**
* Comparing unrelated nodes must be consistent, so we assign a guid to the
* compared docs for further reference.
* @param {Document} doc The document.
* @return {string} The document's guid.
*/
function docGUID(doc) {
if (!doc.guid) doc.guid = Math.random();
return doc.guid;
}
//-- end of helper functions

/**
* DOM Level 2
* Object DOMException
Expand Down Expand Up @@ -603,6 +655,62 @@ Node.prototype = {
var prefix = this.lookupPrefix(namespaceURI);
return prefix == null;
},
// Introduced in DOM Level 3:
/**
* Compares the reference node with a node with regard to their position
* in the document and according to the document order.
*
* @param {Node} other The node to compare the reference node to.
* @return {number} Returns how the node is positioned relatively to the
* reference node according to the bitmask. 0 if reference node and
* given node are the same.
* @see https://www.w3.org/TR/2004/REC-DOM-Level-3-Core-20040407/core.html#Node3-compareDocumentPosition
*/
compareDocumentPosition: function (other) {
if (this === other) return 0;
var node1 = other;
var node2 = this;
var attr1 = null;
var attr2 = null;
if (node1 instanceof Attr) {
attr1 = node1;
node1 = attr1.ownerElement;
}
if (node2 instanceof Attr) {
attr2 = node2;
node2 = attr2.ownerElement;
if (attr1 && node1 && node2 === node1) {
for (var i = 0, attr; (attr = node2.attributes[i]); i++) {
if (attr === attr1) return DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC + DOCUMENT_POSITION_PRECEDING;
if (attr === attr2) return DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC + DOCUMENT_POSITION_FOLLOWING;
}
}
}
if (!node1 || !node2 || node2.ownerDocument !== node1.ownerDocument) {
return (
DOCUMENT_POSITION_DISCONNECTED +
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC +
(docGUID(node2.ownerDocument) > docGUID(node1.ownerDocument) ? DOCUMENT_POSITION_FOLLOWING : DOCUMENT_POSITION_PRECEDING)
);
}
var chain1 = parentChain(node1);
var chain2 = parentChain(node2);
if ((!attr1 && chain2.indexOf(node1) >= 0) || (attr2 && node1 === node2)) {
return DOCUMENT_POSITION_CONTAINS + DOCUMENT_POSITION_PRECEDING;
}
if ((!attr2 && chain1.indexOf(node2) >= 0) || (attr1 && node1 === node2)) {
return DOCUMENT_POSITION_CONTAINED_BY + DOCUMENT_POSITION_FOLLOWING;
}
var ca = commonAncestor(chain2, chain1);
for (var n in ca.childNodes) {
var child = ca.childNodes[n];
if (child === node2) return DOCUMENT_POSITION_FOLLOWING;
if (child === node1) return DOCUMENT_POSITION_PRECEDING;
if (chain2.indexOf(child) >= 0) return DOCUMENT_POSITION_FOLLOWING;
if (chain1.indexOf(child) >= 0) return DOCUMENT_POSITION_PRECEDING;
}
return 0;
},
};

function _xmlEncoder(c) {
Expand All @@ -613,6 +721,8 @@ function _xmlEncoder(c) {

copy(NodeType, Node);
copy(NodeType, Node.prototype);
copy(DocumentPosition, Node);
copy(DocumentPosition, Node.prototype);

/**
* @param callback return true for continue,false for break
Expand Down
92 changes: 92 additions & 0 deletions test/dom/dom-comparison.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
'use strict';
const { DOMParser } = require('../../lib');
const { DOMException } = require('../../lib/dom');

Check notice

Code scanning / CodeQL

Unused variable, import, function or class

Unused variable DOMException.

// Tests following for steps in the specification
// https://dom.spec.whatwg.org/#dom-node-comparedocumentposition
describe('DOM position comparison', () => {
const dp = new DOMParser();
const x0 = dp.parseFromString(
'<x0 foo="a" bar="b">' +
'<x1 foo="a" bar="b">' +
'<x2 foo="a" bar="b">c</x2>' +
'<y2 foo="a" bar="b">d</y2>' +
'</x1>' +
'<y1 foo="a" bar="b">' +
'<z2 foo="a" bar="b">e</z2>' +
'</y1></x0>',
'text/xml'
).documentElement;
const x1 = x0.childNodes[0];
const y1 = x0.childNodes[1];
const x2 = x1.childNodes[0];
const y2 = x1.childNodes[1];

Check notice

Code scanning / CodeQL

Unused variable, import, function or class

Unused variable y2.
const z2 = y1.childNodes[0];
const foo = x0.attributes[0];
const bar = x0.attributes[1];
let text = x2.childNodes[0];
it('Step 1', () => {
expect(x0.compareDocumentPosition(x0)).toBe(0);
expect(x1.compareDocumentPosition(x1)).toBe(0);
expect(foo.compareDocumentPosition(foo)).toBe(0);
expect(text.compareDocumentPosition(text));
});
it('Step 5 2 1 1', async () => {
let result = x0.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC + x0.DOCUMENT_POSITION_PRECEDING;
expect(bar.compareDocumentPosition(foo)).toBe(result);
});
it('Step 5 2 1 2', async () => {
let result = x0.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC + x0.DOCUMENT_POSITION_FOLLOWING;
expect(foo.compareDocumentPosition(bar)).toBe(result);
});
it('Step 6', () => {
let result = x0.DOCUMENT_POSITION_DISCONNECTED + x0.DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC;
let resPre = result + x0.DOCUMENT_POSITION_PRECEDING;
let resFol = result + x0.DOCUMENT_POSITION_FOLLOWING;
const root = dp.parseFromString('<xml><baz abaz="y"></baz></xml>', 'text/xml').documentElement;
const baz = root.childNodes[0];
const abaz = baz.attributes[0];
// This ensures the comparison is stable.
let comp = x0.compareDocumentPosition(root) === resPre;
expect(x0.compareDocumentPosition(root)).toBe(comp ? resPre : resFol);
expect(root.compareDocumentPosition(x0)).toBe(comp ? resFol : resPre);
expect(x1.compareDocumentPosition(baz)).toBe(comp ? resPre : resFol);
expect(baz.compareDocumentPosition(x1)).toBe(comp ? resFol : resPre);
expect(foo.compareDocumentPosition(abaz)).toBe(comp ? resPre : resFol);
expect(abaz.compareDocumentPosition(foo)).toBe(comp ? resFol : resPre);
});
it('Step 7', () => {
let result = x0.DOCUMENT_POSITION_CONTAINS + x0.DOCUMENT_POSITION_PRECEDING;
expect(x1.compareDocumentPosition(x0)).toBe(result);
expect(x2.compareDocumentPosition(x0)).toBe(result);
expect(x2.compareDocumentPosition(x1)).toBe(result);
expect(foo.compareDocumentPosition(x0)).toBe(result);
});
it('Step 8', () => {
let result = x0.DOCUMENT_POSITION_CONTAINED_BY + x0.DOCUMENT_POSITION_FOLLOWING;

Check notice

Code scanning / CodeQL

Unused variable, import, function or class

Unused variable result.
expect(x0.compareDocumentPosition(x1)).toBe(20);
expect(x0.compareDocumentPosition(x2)).toBe(20);
expect(x1.compareDocumentPosition(x2)).toBe(20);
expect(x0.compareDocumentPosition(foo)).toBe(20);
});
it('Step 9', () => {
let result = x0.DOCUMENT_POSITION_PRECEDING;
expect(y1.compareDocumentPosition(x1)).toBe(result);
expect(y1.compareDocumentPosition(x2)).toBe(result);
expect(z2.compareDocumentPosition(x2)).toBe(result);
expect(z2.compareDocumentPosition(x1)).toBe(result);
expect(y1.compareDocumentPosition(x1.attributes[0])).toBe(result);
expect(x1.attributes[0].compareDocumentPosition(foo)).toBe(result);
expect(y1.attributes[0].compareDocumentPosition(foo)).toBe(result);
});
it('Step 10', () => {
let result = x0.DOCUMENT_POSITION_FOLLOWING;
expect(x1.compareDocumentPosition(y1)).toBe(result);
expect(x2.compareDocumentPosition(y1)).toBe(result);
expect(x2.compareDocumentPosition(z2)).toBe(result);
expect(x1.compareDocumentPosition(z2)).toBe(result);
expect(x1.attributes[0].compareDocumentPosition(y1)).toBe(result);
expect(foo.compareDocumentPosition(x1.attributes[0])).toBe(result);
expect(foo.compareDocumentPosition(y1.attributes[0])).toBe(result);
});
});
0