8000 feat: add DOM level 4 child element properties by dmitri-gb · Pull Request #652 · xmldom/xmldom · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add DOM level 4 child element properties #652

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

dmitri-gb
Copy link

Hey!

Since #398 seems to be stale and apparently no longer being worked on by the original author, I figured I'd make another attempt.

This PR adds the following properties to Element nodes:

  • childElementCount
  • firstElementChild
  • lastElementChild
  • nextElementSibling
  • previousElementSibling

These properties correspond to the attributes defined in the ParentNode and NonDocumentTypeChildNode mixins in the DOM Living Standard.

The first three of these properties are also added to Document and DocumentFragment.

Copy link
codecov bot commented Apr 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.11%. Comparing base (db21a1c) to head (28aafda).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #652      +/-   ##
==========================================
+ Coverage   95.05%   95.11%   +0.05%     
==========================================
  Files           8        8              
  Lines        2165     2191      +26     
  Branches      569      576       +7     
==========================================
+ Hits         2058     2084      +26     
  Misses        107      107              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

This adds the properties childElementCount, firstElementChild,
lastElementChild, nextElementSibling and previousElementSibling to
Element. The first three are also added to Document and
DocumentFragment.
@dmitri-gb dmitri-gb force-pushed the child-element-properties branch from 9cc3098 to 8a2671f Compare November 26, 2024 10:04
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.

Thx for picking it up and investing into the tests. Did you read all the comments in the previous PR?
I think there was an idea to implement these functions in a "getter/setter" fashion that operates on the childNodes to avoid having to maintain redundant state... But maybe you already considered that, not sure, will need more time to understand the changes.

Comment on lines -78 to +79
test('appendElement and removeElement', () => {
const dom = new DOMParser().parseFromString(`<root><A/><B/><C/></root>`, MIME_TYPE.XML_TEXT);
test('appendChild and removeChild', () => {
const dom = new DOMParser().parseFromString(`<root><A/>x<B/>y<C/></root>`, MIME_TYPE.XML_TEXT);
Copy link
Member

Choose a reason for hiding this comment

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

I might need to check out the code to understand this change. Could we keep the existing test and add a new one for the new cases?
Or does it cover all 4 funtions, in that case Iwould prefer the message to cobtain all of them.

Copy link
Author

Choose a reason for hiding this comment

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

It only covers the two functions -- appendChild and removeChild. The test simply had the names wrong.

expect(doc.type).toBe('xml');
});

test('should create a Document with root element in a named namespace', () => {
Copy link
Member

Choose a reason for hiding this comment

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

Why was this dropped?

Copy link
Author

Choose a reason for hiding this comment

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

It was an exact duplicate of the test above.

@dmitri-gb
Copy link
Author

I think there was an idea to implement these functions in a "getter/setter" fashion that operates on the childNodes to avoid having to maintain redundant state...

I didn't spot any such idea in the previous PR. I'm not sure if that's really the way to go though. Accessing something as simple as childElementCount would then need to iterate over all child nodes and count how many of them are elements. These properties would then be very slow to read for very large DOM trees. I think maintaining extra state is probably the better option, no?

@karfau
Copy link
Member
karfau commented Dec 6, 2024

I think there was an idea to implement these functions in a "getter/setter" fashion that operates on the childNodes to avoid having to maintain redundant state...

I didn't spot any such idea in the previous PR. I'm not sure if that's really the way to go though. Accessing something as simple as childElementCount would then need to iterate over all child nodes and count how many of them are elements. These properties would then be very slow to read for very large DOM trees. I think maintaining extra state is probably the better option, no?

It's always a trade between either keeping more state (in memory) and correctly updating all references when modifying the DOM (which also takes time and those things are happeing when ever a node is added during parsing), even when there might never be any access to that state, vs accessing the internal state when needed and have a (slightly) longer runtime for accessing those values.

with the amount of state maintenance in this lib I'm leaning towards doing as many things as possible using a getter/setter approach, but it has not been applied widely yet, and there might be issues with how "prototype inheritence" is implemented in this lib, which could get into the way of doing it like this, if it is implemented on prototypes that are copied.

Just thinking out loud here.

I would love to be more active on this repo, but this is only very likely to happen at the end of the year or start of the next year.
I hope this is not to big of an issue for you. Let me know when you think it's "done" so I can take a closer look whenever I have time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0