URI.js icon indicating copy to clipboard operation
URI.js copied to clipboard

Always add an equal sign for empty values in the query string

Open tjconcept opened this issue 7 years ago • 5 comments

https://github.com/medialize/URI.js/blob/gh-pages/src/URI.js#L739

I read this comment about having = for null values in the source, then read the referenced spec, but got to the opposite conclusion: you should output a = for all values including nulls.

Am I reading the spec wrong or missing something here?

Append name, followed by U+003D (=), followed by value, to output.

https://url.spec.whatwg.org/#urlencoded-serializing

tjconcept avatar Apr 08 '17 11:04 tjconcept

This comment suggests that at some point the URL spec said something different than it does now. These days it looks as if the value should be the empty string regardless of = being there or not. HTML5 URL-encoded form data agrees with the URL spec.

Are you up for a PR to tackle this?

rodneyrehm avatar Apr 08 '17 19:04 rodneyrehm

I'm not really concluded on whether I would, personally, like the = or not. I would like to do some research into whether parsers (and the spec for it) are fine with not having it for empty values, and if so, I would probably prefer it to be omitted.

Regarding a pull request, I am not a fan of the approach of this library (doing everything itself - no dependencies), so I've built https://github.com/srcagency/http-querystring-stringify and filed the issue here during research for it. I thought it would be of use here. So I'm not really motivated for the work to be honest.

tjconcept avatar Apr 10 '17 10:04 tjconcept

fair enough! thank you for the heads up :)

What sets your module apart from querystring (browser version of node querystring) and qs?

rodneyrehm avatar Apr 10 '17 12:04 rodneyrehm

A lot, I'm afraid. Unfortunately there are so many ways to do this (mostly the part of going from JS types to strings), that I doubt you can have one universal.

First of all I wanted to split encoding and decoding as my projects tend to use one or the other, and they are very different to implement.

From the README of https://github.com/srcagency/http-querystring-stringify:

  • toJSON is respected (like JSON.stringify does)
  • true and false are converted to y or n respectively
  • null is represented by an empty string
  • undefined values will be skipped completely
  • arrays will be numbered only if they contain arrays or objects themselves

Depending on my research on =, the next difference may be that empty strings do not carry the = (I think that's cleaner).

tjconcept avatar Apr 10 '17 12:04 tjconcept

This inspired me to add a "compatibility" section with a list of parsers: https://github.com/srcagency/http-querystring-stringify#compatibility-with-parsers

tjconcept avatar Apr 10 '17 21:04 tjconcept