Re: [whatwg/dom] Suggestion: new method - getNodesByType (#992)

Again, the TreeWalker already offers this possibility and more, combining multiple types at once.

The whole proposal could be basically just:

```js
const getNodesByType = function* (typeValue) {
  const type = typeof typeValue === 'number' ? typeValue : NodeFilter[
    (
      /^SHOW_/i.test(typeValue) ? typeValue : ('show_' + typeValue)
    ).toUpperCase()
  ];

  const tw = document.createTreeWalker(
    this === document ? this.documentElement : this,
    type
  );

  let currentNode = tw.nextNode();
  while (currentNode) {
    yield currentNode;
    currentNode = tw.nextNode();
  }
};
```

### Example

```js
for (const node of getNodesByType.call(document, 'text'))
  console.log(node);
```

### Differences

  * allow both numbers and strings as entry point
  * becaue it allow numbers, it's possible to specify both COMMENT and TEXT
  * explicit NodeFilter['SHOW_THING'] is preserved
  * shortcuts to SHOW are transformed
  * better ergonomics, features, less polyfill bloat, and so on

Why is TreeWalker being ignored in this discussion?

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/992#issuecomment-866094495

Received on Tuesday, 22 June 2021 15:39:05 UTC