Skip to content
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

problems with typescript #783

Closed
0x0a0d opened this issue Sep 28, 2021 · 4 comments
Closed

problems with typescript #783

0x0a0d opened this issue Sep 28, 2021 · 4 comments

Comments

@0x0a0d
Copy link

0x0a0d commented Sep 28, 2021

I got multiple problems when using DomUtils with typescript

DomUtils.findAll(el => el.name === 'div' && el.parent?.name === 'body', dom.children')

TS2339: Property 'name' does not exist on type 'NodeWithChildren'.


DomUtils.getElements({name: 'div'}, dom.children).map(elment => element.children)

TS2339: Property 'children' does not exist on type 'Node'.


find(test: (elem: Node) => boolean, nodes: Node[], recurse: boolean, limit: number): Node[]
findAll(test: (elem: Element) => boolean, nodes: Node[]): Element[]

Why typeof test's params of find is Node but findAll is Element? and their return too?


There are cases like these but I can't remember now

How to be clear

Why getElements not return Element[] ?

When will method return Element, Node, NodeWithChildren, ... ?

Thanks

@0x0a0d 0x0a0d closed this as completed Apr 2, 2022
@fb55 fb55 reopened this Apr 2, 2022
@fb55
Copy link
Owner

fb55 commented Apr 2, 2022

Hi @0x0a0d, thanks for opening this, and sorry for never responding. I agree that the current DOM structure isn't ideal, and I am thinking about how to improve it. You point at real issues with the current types, and I'd like to keep the issue open — hope that's okay!

fb55 added a commit that referenced this issue Apr 22, 2022
@fb55
Copy link
Owner

fb55 commented Apr 22, 2022

Okay, let's address this:

  1. The issue here is el.parent?.name === 'body', as the parent isn't guaranteed to be a tag (it could also be the document). You can work around this with an additional check: el.parent?.type === 'tag' && el.parent.name === 'body'.
  2. getElements is inherited from the original node-htmlparser project and provides filters for any kind of node. For a simple name filter, you can use getElementsByTagName; this will only return elements.
  3. find similarly allows you to get any kind of node, while findAll is limited to elements. Agreed that this is confusing. I have added a documentation comment for now.

@0x0a0d
Copy link
Author

0x0a0d commented Nov 7, 2022

I found the correct way to do it

DomUtils.find(el=> el instanceof Element /* && any conditions */, children, false, 10) as Element[]

@fb55
Copy link
Owner

fb55 commented Apr 29, 2023

I think most of this has been resolved, even though there are still plenty of casts and type checks that are necessary.

@fb55 fb55 closed this as completed Apr 29, 2023
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

No branches or pull requests

2 participants