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

How can I remove/replace an attribute? #825

Closed
ivanjeremic opened this issue Nov 15, 2021 · 8 comments
Closed

How can I remove/replace an attribute? #825

ivanjeremic opened this issue Nov 15, 2021 · 8 comments

Comments

@ivanjeremic
Copy link

Is there any API for removing/replacing an attribute? If not how would I do it with domutils?

@fb55
Copy link
Owner

fb55 commented Apr 7, 2022

You likely want to use Cheerio for this — the relevant functions are removeAttr and attr.

@fb55 fb55 closed this as completed Apr 7, 2022
@michaelfaith
Copy link

@fb55 since Cheerio's release isn't out yet, are there any options available for this today?

@ivanjeremic
Copy link
Author

@fb55 since Cheerio's release isn't out yet, are there any options available for this today?

Just use jsdom

@michaelfaith
Copy link

michaelfaith commented May 19, 2023

@ivanjeremic I've had issues with jsdom that I don't experience with htmlparser2. Namely the ability to add/update Angular attributes like [customAttr] or (myOutputProp). It also doesn't seem to be possible to retain camel casing on attributes, which htmlParser2 preserves.

@ivanjeremic
Copy link
Author

ivanjeremic commented May 19, 2023

Maybe jsdom/jsdom#2477 (comment)

@fb55
Copy link
Owner

fb55 commented May 19, 2023

This functionality is available in Cheerio. The unavailability of new loading methods shouldn't make Cheerio less useful.

@michaelfaith
Copy link

michaelfaith commented May 19, 2023

Maybe jsdom/jsdom#2477 (comment)

Their general stance is that if it's not supported in the browser, they're not going to implement it. That's why that issue was closed without comment.

This functionality is available in Cheerio. The unavailability of new loading methods shouldn't make Cheerio less useful.

@fb55 Yes, and it works great, but ESM support is critical for us. And according to these two issues, that hasn't been released: cheeriojs/cheerio#3098, cheeriojs/cheerio#2774 Any chance the 1.0 release will make it out soon? I'd love to be able to use it.

@itmayziii
Copy link

itmayziii commented Jul 30, 2024

I'm not 100% sure this doesn't come with any caveats, but I have been removing/replacing attributes like this.

import { cloneNode } from 'domhandler'
import * as domutils from 'domutils'

const html = '<form></form>'
const dom = parseDocument(html).children

domutils.findAll(el => el.name === 'form', dom)
  .map(el => {
    const cloned = cloneNode(el, true)
    cloned.attribs.action = '/submit-form'
    cloned.attribs.method = 'post'
    domutils.replaceElement(el, cloned)
  })

EDIT:
Seems you can just edit attributes directly as well, no need to clone and replace.
This is actually what cheerio is doing with their setAttr method.

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

4 participants