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
- Paste the HTML source to transform. Compare the returned source or text with the input using the lexical rules below.
- Enter html using the field table and calculation rules, then run the tool.
- Read html. The explanation below defines the units, signs, ordering and any flags or placeholder values.
- 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 field | Meaning | Value type; see rules below | Requirement |
|---|---|---|---|
| html | HTML source. | string; maximum length: 1000000 | Required |
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 field | Meaning | Value type |
|---|---|---|
| html | Source with text-node ASCII whitespace collapsed. | string; maximum length: 1000000 |
{
"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
-
HTML Whitespace Collapser — tool.io
-
CSS Text Module Level 3: White Space Processing — World Wide Web Consortium
-
HTML Tag Counter — tool.io
-
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.