2026-10-22 · 6 min read · Rahul Chitturi
- xml
- formatting
- debugging
The XML page parses with DOMParser in XML mode and prints XMLSerializer output. Format then inserts a newline between adjacent > and < characters and indents some of those lines by two spaces. A trailing newline is added. Minify is the serializer only, with no trailing newline.
A box that trims to nothing returns an empty string and does not parse. A box of spaces is that case. Spaces around a real document are not: the parser keeps the document, and the spaces outside it disappear.
Indent a tag only when the line is long enough
A line is indented further only when it starts with < and a letter, the character before the first > is not /, and the line does not contain </. <root> qualifies. <a> does not. It is too short. <a><b><c>d</c></b></a> therefore formats as five lines at column 0: a, b, c with its text and close, then /b, then /a.
<root><item a="1">hi</item></root> does indent. The item line is two spaces in, and the close of root returns to column 0. <r><self-closing /></r> serializes the space away, becoming <self-closing/>, and that child stays at column 0 because r never increased the indent. An empty <root></root> and an empty <b></b> both become self-closing tags.
- Expect a one-letter tag to stay flat.
- Expect an empty element to become />.
- Expect a self-closing tag not to add a level.
- Format once from minified source.
Keep the whitespace minify did not remove
Minify does not strip text nodes. A pretty document minifies with its newlines and indentation still in place. Formatting that same pretty document again indents a second time: the item line that already had two spaces is printed with four, and </root> is back at column 0.
Spaces inside text stay. <root> hi </root> keeps the two spaces on each side. A newline inside text is indented as if it were a tag. <item>line1 and then line2</item> prints the second line four spaces in, with </item> still on that line. A comment is kept and does not itself open a new indent level. Single quotes on attributes become double quotes. a > b in text becomes a > b.
Copy the parser error the browser produced
Invalid XML throws the parsererror text, and the page prefixes Error:. In Chromium that text starts with This page contains the following errors: and then the line and column, with no added space. not xml says Unexpected characters outside the root element. An unclosed root says still inside the root element. </Root> against <root> says Unexpected closing tag. A repeated attribute says Attribute 'a' is redefined. An unbound prefix says prefix is unbound.
Use the XML tool for this print, diff for a second format against the first, and the HTML entities tool when the text you care about is an escaped string rather than a document. Quote whether the input was already pretty. A second format is a different document of whitespace.