milesj / interweave

🌀 React library to safely render HTML, filter attributes, autowrap text with matchers, render emoji characters, and much more.
https://interweave.dev
MIT License
1.1k stars 38 forks source link

why interweave transform attributes to lowercase? #260

Open Intaria opened 1 year ago

Intaria commented 1 year ago

Good afternoon. Can you please tell me why interweave transforms all attributes to lowercase?

I am trying to do something like this, for not to describe each attribute by hand:

const onTransform = (node: HTMLElement, children: Node[]): ReactNode => {
    const attributes = {}

    node.attributes.map((item) => {
        attributes[item['name']] = item['value']
    })

    switch (node.tagName) {
        case 'a':
            return (
                <Link {...attributes}>{children}</Link>
            )

        case 'img':
            return (
                <Image {...attributes} />
            )

        default:
            return undefined
    }
}

And got this: ​ Warning: Invalid DOM property `classname`. Did you mean `className`?

milesj commented 1 year ago

Attributes are HTML attributes, which are lowercase.

If you are converting an HTML element into a React component, you'll need to remap it to props manually.

Intaria commented 1 year ago

Then is it possible to pass raw attributes too in HTMLElement node?

milesj commented 1 year ago

Not sure I follow? Just do this:

const onTransform = (node: HTMLElement, children: Node[]): ReactNode => {
    const attributes = {}

    node.attributes.map((item) => {
        attributes[item['name']] = item['value']
    })

    // this 
    attributes.className = attributes.classname;

    switch (node.tagName) {
        case 'a':
            return (
                <Link {...attributes}>{children}</Link>
            )

        case 'img':
            return (
                <Image {...attributes} />
            )

        default:
            return undefined
    }
}
Intaria commented 1 year ago

I am trying to create an HOC component. For it i need the ability to forward any attributes to the tag.

The problem is that there are a lot of options in react, which can be different: className, tabIndex, defaultChecked, itemScope, itemType, etc. And listing them all by hand seems like a crutch

milesj commented 1 year ago

You only need to write this once, it's only a couple of lines. This won't be changing.