Form Mode Is Two-Way: Why So Many URL Encoders Break on a Literal +
WHATWG form encoding maps space to + and + to %2B. Encoders that skip the second rule corrupt any literal plus, such as C++ or a phone country code.
Type c++ into a typical online URL encoder, flip the "form mode" switch on, and most tools return c++ unchanged. Round-trip the result through a form decoder and you get c — two spaces. The literal pluses survived encoding but were eaten on the way back, and nothing reported an error.
WHATWG's application/x-www-form-urlencoded serializer is explicit about this: the byte 0x20 (space) maps to +, and the byte 0x2B (+ itself) maps to %2B. Both directions of the substitution have to ship together. A form-mode encoder that does only space → + is a one-way street; round-trip is broken the moment a user types a literal +, a phone number with a country code, or the C++ language name.
The fix is six lines, and most ad-supported URL tools never wrote them.
The Bytes Are 0x20 and 0x2B
Here is what the inner loop of the Mode 1 codec looks like — the entire form-mode behaviour collapses into two continue branches:
const plus = spaceMode === "plus";
for (let i = 0; i < bytes.length; i++) {
const b = bytes[i];
if (plus && b === 0x20) { out += "+"; continue; } // space → +
if (plus && b === 0x2b) { out += "%2B"; continue; } // + → %2B
if (table[b] === 1) out += String.fromCharCode(b);
else out += `%${HEX[(b >> 4) & 0xf]}${HEX[b & 0xf]}`;
}
The 0x2B line is the one nobody writes. Without it, c++ Hello World round-trips to c Hello World. With it, the same input encodes to c%2B%2B+Hello+World and decodes back exactly. Paste 1 + 1 = 2 into FreeFormatter's "URL Encode" with form mode and the output keeps the bare +; paste the same input into the URL Encoder & Decoder with space mode = + and the output is 1+%2B+1+%3D+2. The first one is wrong; the second one round-trips.
On the decode side the same rule applies symmetrically: in form mode, + is first mapped back to space, then %XX sequences are decoded. Reverse the order and %2B is silently corrupted (it becomes a literal +, which then becomes a space).
encodeURI and encodeURIComponent Are Not Interchangeable
Every URL encoder has to pick a passthrough set, and the standard library gives JavaScript three different answers.
| Function | Passes through |
|---|---|
encodeURI | unreserved + reserved (; / ? : @ & = + $ , #) |
encodeURIComponent | unreserved + ! * ' ( ) |
| RFC 3986 §2.3 unreserved | only A–Z a–z 0–9 - _ . ~ |
The eleven characters that encodeURI leaves alone but encodeURIComponent percent-encodes are exactly the structural delimiters of a URL. Run a whole URL through encodeURIComponent and you reproduce the failure mode shipped by half the encoders on the first page of Google:
encodeURI('https://example.com/?q=1') → https://example.com/?q=1
encodeURIComponent('https://example.com/?q=1') → https%3A%2F%2Fexample.com%2F%3Fq%3D1
The second form is what you want for embedding a URL inside a query value (?redirect=...). The first is what you want for the URL itself. The QR code generator uses the second for email codes: subject and body go into the mailto: query through encodeURIComponent. Picking the wrong one — or worse, hiding the choice from the user — turns a q=1 query into a q%3D1 query, and your backend stops finding the parameter. The codec exposes all three strategies as a segmented control so the eleven-character gap is visible.
Punycode in Under 80 Lines
The IDN converter is the part most "URL encoder" tools split off into a separate page (see punycoder.com, browserling). The whole RFC 3492 Bootstring algorithm — encode plus decode — fits in well under 80 lines once you accept five magic numbers from the spec:
base = 36 tmin = 1 tmax = 26
skew = 38 damp = 700
They tune the adapt() bias function:
function adapt(delta: number, numPoints: number, firstTime: boolean) {
let d = firstTime ? Math.floor(delta / 700) : delta >> 1;
d += Math.floor(d / numPoints);
let k = 0;
while (d > ((36 - 1) * 26) >> 1) {
d = Math.floor(d / (36 - 1));
k += 36;
}
return k + Math.floor(((36 - 1 + 1) * d) / (d + 38));
}
damp = 700 is the first-iteration brake: it stops münchen from blowing past German's typical codepoint spread on the very first delta. skew = 38 keeps the output digit length under control once you've moved into the high BMP — drop it and 中国 expands to a much longer ACE form than the canonical fiqs8s. The other three values define the generalised variable-length integer's digit range.
Two test vectors anchor the implementation: RFC 3492 §7.1 sample B (他们为什么不说中文 → ihqwcrb4cv8a8dqg056pqjye) and the Wikipedia case münchen → mnchen-3ya. Both pass against URL.hostname's native output, which is the cleanest oracle available.
The reason we did not just delegate everything to new URL(input).hostname is that the WHATWG URL parser does not give you the label-by-label breakdown — it returns one ACE string for the whole hostname, and you cannot ask it "encode just this label." The dedicated Punycode mode in the tool lets you paste münchen (a single label, no dots) and see xn--mnchen-3ya, which is what you need when registering a subdomain or auditing a TLS cert SAN list.
Try It
Paste 1 + 1 = 2 into the URL Encoder & Decoder under Codec with strategy = encodeURIComponent. With space mode %20 the encoded pane reads 1%20%2B%201%20%3D%202; flip space mode to + and it reads 1+%2B+1+%3D+2. Toggle back and forth — the byte for + stays %2B either way; only the byte for space changes. That's the bidirectional fix in one switch.
For the URL-shaped failure, paste https://中国.cn/中文?q=北京 into the Full URL tab. The output is https://xn--fiqs8s.cn/%E4%B8%AD%E6%96%87?q=%E5%8C%97%E4%BA%AC — host through Punycode, path and query through UTF-8 percent-encoding, in one pass.