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

Conversation

zorkow
Copy link
Contributor
@zorkow zorkow commented Apr 3, 2023

The PR adds an implementation of level 3 method compareDocumentPosition. This is an updated version of the code suggested in jindw/xmldom#113

The code ran for many years error-free in Speech Rule Engine together with wicked-good-xpath.

@zorkow zorkow changed the title Feat: Adds compareDocumentPosition method from level 3 spec. feat: Add compareDocumentPosition method from level 3 spec. Apr 3, 2023
Copy link
Member
@karfau karfau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for picking up this topic and providing an implementation and happy to hear that it was already proven to work for a long time.

I added some comments/remarks/questions. Let me know what you think.

I would also prefer to have tests for this method, to make sure it doesn't break in the future.
Are you able and willing to extend your PR by adding those tests?

@zorkow
Copy link
Contributor Author
zorkow commented Apr 7, 2023

Thanks for the review. I'll follow up with revisions after the Easter break.

@zorkow
Copy link
Contributor Author
zorkow commented Apr 7, 2023

I would also prefer to have tests for this method, to make sure it doesn't break in the future. Are you able and willing to extend your PR by adding those tests?

Willing yes, able remains to be seen. But I know what goes wrong in SRE when compareDocumentPosition is missing, so we should be able to infer some tests from that.

@karfau
Copy link
Member
karfau commented Apr 7, 2023

Great. You can push whatever you are able to do and let me know in case I should finalize it.

@zorkow
Copy link
Contributor Author
zorkow commented May 16, 2023

Finally got round to following this up. As mentioned, this was originally not my code, so I was not really aware of some of its problems. But I've now refactored it to

  • follow the spec pretty much to the letter
  • added JSDOC
  • added the position bitmask to the Node interface
  • provide a set of tests for the single steps in the spec.

PTAL.

@@ -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.
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.
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.
Copy link
Member
@karfau karfau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such a great contribution, thx a lot.
It will be part of version 0.9.0, I will soon release a pre-release version using the next label.

@karfau karfau merged commit 7b782c3 into xmldom:master May 16, 2023
@karfau karfau added this to the 0.9.0 milestone May 30, 2023
@karfau karfau added the spec:DOM Level 3 https://www.w3.org/TR/DOM-Level-3-Core/ label May 30, 2023
@zorkow zorkow deleted the compare_doc_pos branch July 16, 2023 09:09
karfau pushed a commit that referenced this pull request Dec 16, 2024
PR contributes a more efficient coding for the `compareDocumentPosition`
method contributed in PR #488
. In detail:

* Removes the `parentChain` method and replaces it with a parent chain
computation that immediately compares target nodes, thus avoiding
unnecessary computations.
* Uses `push` and `reverse` instead of `unshift` to assemble chains as
this is considerably faster.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
spec:DOM Level 3 https://www.w3.org/TR/DOM-Level-3-Core/
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0