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

Stop modification of br tag #51

Open
milne-dev opened this issue May 21, 2015 · 12 comments
Open

Stop modification of br tag #51

milne-dev opened this issue May 21, 2015 · 12 comments

Comments

@milne-dev
Copy link

When I pass <br> through the sanitizeHtml() is there any way to keep it from changing to <br /> ?

Or for other tags that may be similiar..

Thanks!

@mathieumg
Copy link

<br> is not valid whereas <br /> is. What is your use case?

@simison
Copy link
Contributor

simison commented May 21, 2015

<br> is perfectly valid HTML, <br/> valid XHTML. ;-)

http://w3c.github.io/html-reference/syntax.html#void-element

Optionally, a "/" character, which may be present only if the element is a void element.

@boutell
Copy link
Member

boutell commented May 21, 2015

As long as htmlparser2 doesn't offer a way to sniff out whether the original tag had a / or not, sanitize-html will continue to use its internal list of void elements to decide whether to use /.

But even if it did, I'm not sure I would change this. <br> may be acceptable in HTML, but <br /> is always 100% valid as both HTML and XHTML. It's just cleaner and it does not change the meaning of the code in any way.

@boutell boutell closed this as completed May 21, 2015
@milne-dev
Copy link
Author

For large deployments that store a lot of user generated html, it is favorable to keep the data size as low as possible.

@omerts
Copy link

omerts commented Jul 26, 2016

@boutell I am having a similar issue with an image tag. It is totally breaking my functionality since I am working with innerHTML attribute which returns the html like it is in the browser's DOM. Chrome strips out self closing tags, which causes html that I pass through html-sanitize to be inconsistent with my innerHTML. Consistency is critical for me, since I am changing the html by selection indexes, and if the sanitize html changes it, indexes are offset.

@matiishyn
Copy link

I have same issue here
Please, Stop modification of br tag

@boutell
Copy link
Member

boutell commented Jan 2, 2019

There is no support upstream in htmlparser2 for determining which syntax was used to close tags. This is unlikely to happen at least until a new, bc break version of sanitize-html that depends on a different html parser module. Which is likely eventually, because htmlparser2 is not receiving much maintenance, but it doesn't guarantee the library we choose will support this either.

@dman777
Copy link

dman777 commented Apr 6, 2021

I am hitting this issue with quill. I don't think quill likes <br />

@gastonyte
Copy link

gastonyte commented Jul 21, 2024

Hello, Thank you for this library.

I'm also a user of Quill and I'd find it helpful to have an option to disable the addition of a slash for <br> tags.

I wouldn't say "it's just cleaner", but "this is one of the possible correct solutions".

For those interested in a solution to this particular problem, I've made it possible in my fork:

To achieve this, define the selfClosing option as an object. The keys of this object should be the tag names, and the value should be a truthy or falsy value. If the value of a specific tag has the property voidTag set to true, the library will not insert the space and the slash.

type SelfClosingOptions = Record<string, boolean | { voidTag: boolean }>

type MyIOptions = Omit<sanitizeHtml.IOptions, 'selfClosing'> & {
  selfClosing?: SelfClosingOptions | string[] | undefined
}

const sanitizationOptions: MyIOptions = {
  allowedTags: ['p', 'strong', 'em', 'u', 's', 'blockquote', 'ul', 'ol', 'li', 'br'],
  allowedAttributes: {},
  selfClosing: {
    br: {
      voidTag: true
    }
  },
}

const cleaned = sanitizeHtml(
  value,
  sanitizationOptions as sanitizeHtml.IOptions
)

@boutell
Copy link
Member

boutell commented Jul 29, 2024

These are really two separate features:

  1. "Preserve it the way it was" ("stop modification," as the title says)

We can't easily do this because of the way the upstream module gives us the information.

  1. "An option to not use the /"

Sure, that would be fine as long as the default doesn't change unexpectedly. @gastonyte would you like to create a PR for your feature?

@boutell boutell reopened this Jul 29, 2024
@gastonyte
Copy link

Hi @boutell, I didn't know if this feature was wanted or not, so I just made the modification in a fork to be able to use it in my app, but if it's something you would like to see integrated into your library, it is with pleasure that I will create the PR =)

@boutell
Copy link
Member

boutell commented Jul 29, 2024

Yes, supporting this via a flag sounds great! My objection was to the original request in which the original presence or absence of a / would be preserved, simply because I don't think we have access to that information.

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

8 participants