Open danschumann opened 9 years ago
I think to do this properly you'll need a list of block tags (or a list of inline tags). You want to add a space wherever a block tag ends (actually it should probably be a newline character and let the browser convert it to space), but not where an inline tag ends (e.g. <div>foo</div><div>bar</div>
should convert to foo bar
, but <span>foo</span><span>bar</span>
should convert to foobar
).
I agree, and there should be an option to override that list.
I'd take a pull request for this one.
On Fri, Nov 27, 2015 at 12:42 PM, Daniel Grad notifications@github.com wrote:
I think to do this properly you'll need a list of block tags (or a list of inline tags). You want to add a space wherever a block tag ends (actually it should probably be a newline character and let the browser convert it to space), but not where an inline tag ends (e.g.
foobarshould convert to foo bar, but foobar should convert to foobar). — Reply to this email directly or view it on GitHub https://github.com/punkave/sanitize-html/issues/49#issuecomment-160181276 .
THOMAS BOUTELL, DEV & OPS P'UNK AVENUE | (215) 755-1330 | punkave.com
Until this gets implemented, a messy hack would be:
text = text.replace(/>/g, '> ');
text = sanitizeHtml(text, {allowedTags:[]});
up!!
Is this still on?
@r-custodio As mentioned, I'd take a PR for this. Unfortunately as maintainer I can't necessarily implement every feature.
@abea this is labeled as seeking contributions
but closed. Is it still something you'd accept a PR for?
@greghub Sure. It had been sitting idle for years, so there didn't seem much reason to keep it open. I'll reopen it if you want to work on it for 2.x and let the stalebot close it if nothing happens.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Until this gets implemented, a messy hack would be:
text = text.replace(/>/g, '> '); text = sanitizeHtml(text, {allowedTags:[]});
it's 2023 now. thank you for your solution =)
@rusakovic Contributions are welcome!
it's 2024 now. :)
Yes, it's 2024 now, and as always, community contributions are welcome. 😄 This isn't a feature that matters for our use cases, although I appreciate it would be nice for developers reading the resulting markup.
Reopening for potential community PRs.
Is anyone aware of a reliable way to get a list of block-tags? If so, this shouldn't be terribly difficult to implement, right? I'm not sure.
Somewhat dubious source, but chatGPT says Sure! Here is a list of HTML block-level tags:
<address>
<article>
<aside>
<blockquote>
<canvas>
<dd>
<div>
<dl>
<dt>
<fieldset>
<figcaption>
<figure>
<footer>
<form>
<h1>
to <h6>
<header>
<hr>
<li>
<main>
<nav>
<ol>
<p>
<pre>
<section>
<table>
<ul>
These tags are generally used to structure the main content of an HTML document.
It might be easier to exclude inline elements. There are fewer(?) and they're generally easier to identify. The list of phrasing content elements minus inputs, media, br
, and super randos (e.g., ruby
) looks to be a pretty good start.
This is what I got from ChatGPT:
Here is a comprehensive list of block-level HTML tags/elements available in the latest implementation(s) of HTML, including both standard and experimental/non-standard elements:
<address>
- Represents contact information for the author/owner of a document.<article>
- Represents a self-contained composition in a document.<aside>
- Represents a section of content indirectly related to the content around it.<blockquote>
- Represents a section that is quoted from another source.<canvas>
- Used for rendering graphics via scripting (usually JavaScript).<dd>
- Describes a term in a description list.<div>
- A generic container for flow content.<dl>
- Represents a description list.<dt>
- Represents a term/name in a description list.<fieldset>
- Groups related elements in a form.<figcaption>
- Represents a caption or legend for the content of its parent <figure>
.<figure>
- Represents self-contained content, like illustrations, diagrams, photos, etc.<footer>
- Represents a footer for its nearest sectioning content or sectioning root element.<form>
- Represents a document section containing interactive controls for submitting information.<h1>
to <h6>
- Represent headings, with <h1>
being the highest (or most important) level and <h6>
the lowest.<header>
- Represents introductory content, typically a group of introductory or navigational aids.<hgroup>
- Groups a set of <h1>
-<h6>
elements when a heading has multiple levels.<hr>
- Represents a thematic break between paragraph-level elements.<li>
- Represents a list item in an ordered or unordered list.<main>
- Represents the main content of the <body>
.<nav>
- Represents a section of a page that links to other pages or to parts within the page.<ol>
- Represents an ordered list of items.<p>
- Represents a paragraph.<pre>
- Represents preformatted text.<section>
- Represents a standalone section — which doesn't have a more specific semantic element to represent it.<table>
- Represents tabular data.<ul>
- Represents an unordered list of items.<center>
- Used to center-align text (use CSS instead).<dir>
- Used for directory lists (use <ul>
instead).<menu>
- Initially intended for context menus, lists of commands, or form controls. It has been repurposed and is less common in modern usage for block-level content.<dialog>
- Represents a dialog box or other interactive component, such as an inspector or window.<main>
- Although now part of the HTML5 specification, it was experimental before being standardized. It represents the dominant content of the <body>
of a document.<details>
- Represents a disclosure widget from which the user can obtain additional information or controls.<summary>
- Represents a summary, caption, or legend for a <details>
element's disclosure box.These elements are considered block-level because they typically start on a new line and take up the full width available (unless otherwise styled with CSS).
For the most up-to-date list, always refer to the latest HTML specification and browser documentation, as new elements and updates can be introduced.
Here is a comprehensive list of inline HTML tags/elements available in the latest implementation(s) of HTML, including both standard and experimental/non-standard elements:
<a>
- Defines a hyperlink.<abbr>
- Represents an abbreviation or acronym.<b>
- Represents a span of text stylistically different from normal text, without conveying any extra importance or emphasis.<bdi>
- Isolates a span of text that might be formatted in a different direction from other text outside it.<bdo>
- Overrides the current text direction.<br>
- Produces a line break in text.<cite>
- Represents the title of a work.<code>
- Displays a fragment of computer code.<data>
- Links a given content with a machine-readable translation.<dfn>
- Indicates the term being defined within the context of a definition phrase or sentence.<em>
- Marks text that has stress emphasis.<i>
- Represents a span of text in an alternate voice or mood, or otherwise offset from the normal prose in a manner indicating a different quality of text.<img>
- Embeds an image into the document.<input>
- Allows the user to enter data.<kbd>
- Represents user input from a keyboard, voice input, or any other text entry device.<label>
- Represents a caption for an item in a user interface.<mark>
- Represents text that has been highlighted for reference or notation purposes.<meter>
- Represents either a scalar value within a known range or a fractional value.<noscript>
- Defines a section of text to be displayed if a script type on the page is unsupported or if scripting is currently turned off in the browser.<object>
- Represents an external resource, which can be treated as an image, a nested browsing context, or a resource to be handled by a plugin.<output>
- Represents the result of a calculation or user action.<picture>
- Contains zero or more <source>
elements and one <img>
element to offer alternative versions of an image for different display/device scenarios.<progress>
- Represents the completion progress of a task.<q>
- Indicates that the enclosed text is a short inline quotation.<s>
- Represents text that is no longer accurate or relevant.<samp>
- Represents sample output from a program or computing system.<script>
- Contains scripting statements, or points to an external script file through the src
attribute.<select>
- Represents a control that provides a menu of options.<small>
- Makes the text font size one size smaller (for example, from large to medium, or from small to x-small).<span>
- Generic inline container for phrasing content, which does not inherently represent anything.<strong>
- Indicates that its contents have strong importance, seriousness, or urgency.<sub>
- Specifies inline text which should be displayed as subscript.<sup>
- Specifies inline text which should be displayed as superscript.<template>
- Holds client-side content that will not be rendered when the page loads but can be instantiated later using JavaScript.<textarea>
- Represents a multi-line plain-text editing control.<time>
- Represents either a time on a 24-hour clock or a precise date in the Gregorian calendar.<u>
- Represents a span of inline text which should be rendered in a way that indicates that it has a non-textual annotation.<var>
- Represents the name of a variable in a mathematical expression or a programming context.<wbr>
- Represents a word break opportunity.<acronym>
- Represents an acronym; use <abbr>
instead.<big>
- Makes the text font size one size larger.<tt>
- Represents text in a fixed-pitch font; use CSS instead.<font>
- Defines font, color, and size for text; use CSS instead.<slot>
- Part of the Web Components technology suite, it is a placeholder inside a web component that you can fill with your own markup, similar to a content placeholder in other templating systems.These elements are considered inline because they do not start on a new line and only take up as much width as necessary. For the most accurate and up-to-date list, always refer to the latest HTML specification and browser documentation.
In HTML, custom or undefined tags are treated as inline elements by default. This means that if you define a custom tag that is not recognized by the HTML specification, it will behave like an inline element unless you explicitly style it using CSS.
For example, if you create a custom tag <my-custom-element>
, it will be treated as an inline element:
<my-custom-element>This is a custom element.</my-custom-element>
To change its behavior to a block-level element, you need to use CSS:
my-custom-element {
display: block;
}
This CSS rule will make the custom element behave as a block-level element:
<my-custom-element>This is a custom element.</my-custom-element>
With the CSS applied, <my-custom-element>
will now start on a new line and take up the full width available, like standard block-level elements.
I'm wondering if there is a way to create a space wherever tags were.
Picture this:
<div>Some sentence.</div><div>Some other Sentence</div>
It converts to
Some sentence.Some other Sentence
when I runtext = sanitizeHtml(text, {allowedTags:[], allowedAttribute: {}});
Is there an option to add whitespace so the output is better:
Some sentence. Some other Sentence