HTML Whitespace Collapser

Collapse ASCII whitespace in lexical text nodes while preserving tags, attributes, comments, and preformatted or raw-text element contents.

Description

Whitespace processing can reduce source text spacing while leaving tags and protected element contents intact.

Whitespace processing can reduce source text spacing while leaving tags and protected element contents intact. This transform operates on lexical tokens rather than browser layout.1, 2

Collapse ASCII whitespace in lexical text nodes while preserving tags, attributes, comments, and preformatted or raw-text element contents.

When to use HTML Whitespace Collapser

Use this tool to collapse ASCII whitespace in lexical text nodes while preserving tags, attributes, comments, and preformatted or raw-text element contents.

Paste the HTML source to transform. Compare the returned source or text with the input using the lexical rules below.

How to use HTML Whitespace Collapser and understand its results

  1. Paste the HTML source to transform. Compare the returned source or text with the input using the lexical rules below.
  2. Enter html using the field table and calculation rules, then run the tool.
  3. Read html. The explanation below defines the units, signs, ordering and any flags or placeholder values.
  4. If the tool reports an input issue, correct the values and run it again. Apply the stated assumptions when interpreting a successfully returned result.
Input fieldMeaningValue type; see rules belowRequirement
htmlHTML source.string; maximum length: 1000000Required

Paste HTML source. Scan tokens and collapse tabs, line breaks, form feeds, carriage returns and spaces only in text outside pre, textarea, script, style, title, xmp, iframe, noembed and noframes. Preserve tag/attribute/comment source and protected contents; return joined HTML without outer trimming.3, 4

ordinary text: ASCII whitespace runs→one space

Units, indexing, boundary conditions and exceptional cases are defined in the calculation rules above.

Output fieldMeaningValue type
htmlSource with text-node ASCII whitespace collapsed.string; maximum length: 1000000
json
{
  "input": {
    "html": "<p title=\"a  b\">hello\n   world</p><pre>a  b\n c</pre>"
  },
  "output": {
    "html": "<p title=\"a  b\">hello world</p><pre>a  b\n c</pre>"
  }
}

Limitations and assumptions

Nonbreaking spaces and character references remain unchanged. No DOM construction, CSS white-space rules, browser-equivalent minification or sanitization is provided; malformed source follows scanner behavior.

Numeric inputs must be finite. Follow the calculation-specific rules for overflow, underflow and exceptional cases. Extremely large text inputs are rejected. Missing required fields, extra fields, wrong value types and violations of the calculation rules are input errors.

Alternative or Complementary methods

Use HTML Tag Stripper: Remove lexical HTML tags, comments, and declarations while retaining raw text and character references; the result is not sanitized markup. For a complementary task, use HTML Comment Remover: Remove lexical HTML comments while preserving comment-like text inside script, style, title, and textarea elements.

Further background: CSS Text Module Level 3: White Space Processing.

References

  1. HTML Whitespace Collapser — tool.io

  2. CSS Text Module Level 3: White Space Processing — World Wide Web Consortium

  3. HTML Tag Counter — tool.io

  4. HTML Attribute Parser — tool.io

Similar or alternative tools

  • HTML Tag Stripper

    Remove lexical HTML tags, comments, and declarations while retaining raw text and character references; the result is not sanitized markup.

  • HTML Comment Remover

    Remove lexical HTML comments while preserving comment-like text inside script, style, title, and textarea elements.

Don't forget to set a bookmark for tool.io!
Privacy | Imprint | Cookies