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

Discard inner text of tag containing custom data attribute #689

Open
pjebs opened this issue Jan 13, 2025 · 5 comments
Open

Discard inner text of tag containing custom data attribute #689

pjebs opened this issue Jan 13, 2025 · 5 comments

Comments

@pjebs
Copy link

pjebs commented Jan 13, 2025

Feature request:
If a tag contains a custom data attribute

Eg

<div data-danger>...</div>

Configure to discard the inner contents.

@boutell
Copy link
Member

boutell commented Jan 13, 2025

You should be able to do this with the existing transformTags option.

@boutell boutell closed this as completed Jan 13, 2025
@pjebs
Copy link
Author

pjebs commented Jan 14, 2025

@boutell

I meant something more like this:

If it encounters a custom data attribute: data-keep, it keeps the inner text.
If it doesn't encounter it, it removes the text inside the tag.

const html = "<div data-keep>...sadgasg<div>ABCDEF</div></div>";

Should produce:

"<div data-keep>...sadgasg<div></div></div>" or maybe even "<div>...sadgasg<div></div></div>"

@pjebs
Copy link
Author

pjebs commented Jan 14, 2025

text: '' doesn't seem to work

sanitizeHtml(html,  {
  transformTags: {
    '*': function(tagName, attribs) {
      if (attribs.hasOwnProperty("data-keep")) {
        return {
          tagName: tagName,
        }
      } else {
        return {
          tagName: tagName,
          text: ''
        }
      }
    },
  }
})

@pjebs
Copy link
Author

pjebs commented Jan 14, 2025

The bug seems to be with *.

@boutell boutell reopened this Jan 14, 2025
@boutell
Copy link
Member

boutell commented Jan 14, 2025

Sounds like you're right. PRs welcome...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants