Format SQL, minify CSS and read bytes without breaking anything

Why formatters and minifiers break working code, how strings, comments and UTF-8 bytes trip them up, and how to tidy code without pasting it into a server.

Updated . How guides are checked

Primary sources checked

Formatters and minifiers break code when they treat it as characters instead of tokens, changing text inside strings, comments or url() values. Use tools that read tokens first. Remember that UTF-8 characters can take up to four bytes, and avoid pasting private schemas or stylesheets into sites that upload them.

Tidying code should be the safest thing you can do to it. You are not changing what it does, only how it looks. Yet the classic bug reports for formatters and minifiers are all the same shape: the output looked fine and behaved differently. A colon vanished from inside a URL. A keyword inside a string was capitalised. A comment swallowed the rule after it.

Every one of those comes from the same mistake, which is treating code as a run of characters rather than a sequence of tokens. This guide explains the tokens that matter in CSS and SQL, why text turns into more bytes than you expect, and how to clean up code without handing it to someone else’s server.

Code is tokens, not characters

A language’s grammar is written in terms of tokens: strings, names, numbers, punctuation and comments. The W3C’s CSS Syntax specification defines how a stylesheet is split into those tokens before anything else happens, including what a string token is, what a comment is, and how url() is read. The browser does that tokenising first and only then looks at rules and declarations.

A tool that rewrites CSS should do the same. If it removes “unnecessary” spaces before it knows where the strings are, then the space inside content: "a ; b" and the spaces in a quoted font family name look exactly like spaces between declarations. If it strips comments with a simple pattern, a /* inside a string ends up treated as the start of a comment.

SQL has the same structure. PostgreSQL’s documentation on lexical structure describes string constants in single quotes, with a doubled quote standing for a quote inside, quoted identifiers in double quotes, and comments that start with two dashes or sit between /* and */. Each of those is data or a name as far as the database is concerned. A formatter that capitalises select wherever it appears will change a value of 'select from', and on a case-sensitive quoted identifier it can change which column you mean.

What a safe CSS minifier removes

Once the stylesheet is in tokens, the safe changes are few and dull:

  • comments, apart from those starting /*!, the convention libraries use to keep their licence text;
  • line breaks and indentation;
  • spaces next to braces, colons, semicolons, commas and the combinators >, + and ~;
  • the last semicolon in a block.

Spaces that separate two values stay: margin: 0 auto cannot become margin:0auto. So does the space after and in a media query, since and( is not valid where and ( is.

What a safe minifier does not do is rewrite values or merge rules. Shortening #ffffff to #fff is harmless. Merging two rules with the same declarations, or moving a rule, can change which declaration wins the cascade, and only a tool that sees every stylesheet in the project can judge that. That is a job for your build, not for a paste box.

The CSS minifier works this way, tokens first, and reports the size before and after so you can see whether the saving was worth having. Its Readable setting does the reverse for a minified file, one declaration per line.

Formatting SQL so it can be read

A long query is hard to read because the structure is invisible: which part is the selection, which the filter, where a subquery starts. Good formatting puts each clause on its own line, lists selected columns one per line, and gives each AND and OR condition its own line under WHERE.

Parentheses need judgement. A subquery is a query and reads best laid out like one, indented under the line that opens it. Function arguments and IN lists are not, and splitting COUNT(o.id) over three lines makes it worse. A formatter has to tell the two apart, which it can do by looking at whether the parenthesis opens with SELECT or WITH.

The SQL formatter does this and leaves strings, quoted names and comments as written. It formats text and does not run it, so it will lay out a query with a mistake in it. That is often useful in itself: a stray comma is much easier to see once every column is on its own line.

Why text is more bytes than characters

When a database column, a form field or an API limits length in bytes, text that fits on screen can still be rejected. RFC 3629, which defines UTF-8, explains why: each character is written as one to four bytes. The characters of plain English take one byte each and match ASCII exactly. Accented letters and other alphabets take two or three. Many emoji take four.

So “café” is four characters and five bytes, and a short name in Devanagari can be several times its visible length in bytes. The text to binary converter shows the exact bytes of any text in binary, hex, octal or decimal, and reads byte codes back into text. When codes do not form valid UTF-8, it says so instead of printing question marks, which usually means the bytes are part of a file in another encoding. For characters that look identical but are not, use the Unicode inspector.

Keep private code on your machine

Queries carry table and column names, customer identifiers and the shape of a schema. Stylesheets carry class names for features that have not launched. An online formatter that sends text to a server has seen all of it, whatever its privacy policy says.

The tools above run in your browser and do not upload what you paste. The same is true of the JSON formatter and the number base converter. If you need a check you cannot see in the code, open the browser’s network panel while you use them: there is no request carrying your text.

Tools for this guide

Frequently asked questions

Why did a CSS minifier break my styles?

Usually because it removed or changed something inside a string, a url() or a comment it did not recognise as one. A minifier that reads the stylesheet as tokens first, and only then removes whitespace between them, cannot do that.

Will formatting SQL change what the query does?

Not if the formatter only changes whitespace and the case of keywords. Text inside quotes, quoted names and comments must pass through exactly, because the database treats them as data or names rather than as SQL.

Why does my text have more bytes than characters?

UTF-8 writes plain English letters as one byte each and other characters as two, three or four. A name with an accent, a word in Hindi or an emoji is longer in bytes than it looks, which is why byte-limited fields cut it short.

Is it safe to paste code into an online formatter?

It depends where the code goes. A query or stylesheet can reveal table names, internal hosts and unreleased features. A tool that runs in your browser and never uploads the text removes that question.

Primary references