TobiasNickel / tXml

:zap:very small and fast xml-parser in pure javascript:zap:
MIT License
217 stars 28 forks source link

Inconsistent interface for utility methods #11

Closed cortopy closed 4 years ago

cortopy commented 4 years ago

I'm a bit confused about how to use the utility methods and I'm getting inconsistent outputs.

With the following HTML string

<head>
    <style>
        p { color: "red" }
    </style>
</head>

<body>
    <p>hello</p>
</body>

I get different results depending on how I use filter.

Following the README and the typescript types this example seems to be the correct one:

    xml(x).filter(element => {
      return element.tagName.toLowerCase() == 'style';
    })

However, it returns an empty array.

After digging into the unit tests, it appears that using filter as an option renders the desire output.

   xml(x, {
      filter: function (element: txml.INode) {
        return element.tagName.toLowerCase() == 'style';
      }
    } as any)

But this feels weird because it's not documented (I don't know if it will break in certain circumstances) and the typescript types don't reflect it

TobiasNickel commented 4 years ago

In: xml('<text>').filter(...). the filter method is the native filter of an array. It only can find head and body here. I was mostly using the filter the following way:

const elements = xml(`<head>
<style>
    p { color: "red" }
</style>
</head>

<body>
<p>hello</p>
</body>`);

xml.filter(elements, element => {
    return element.tagName.toLowerCase() == 'style';
});

Thanks for sharing this experience ! ! I will definitely fix the type definition and see how I can improve the documentation around this.

cortopy commented 4 years ago

thank you!