W3C home > Mailing lists > Public > whatwg@whatwg.org > September 2012

Re: [whatwg] New URL Standard

From: Glenn Maynard <glenn@zewt.org>
Date: Tue, 25 Sep 2012 16:13:57 -0500
Message-ID: <CABirCh_J8_d-nmUMRo=Vk03DJhHY-5oeMw4k1p-D_ys_aSWdhQ@mail.gmail.com>
To: David Sheets <kosmo.zb@gmail.com>
Cc: Alexandre Morgaut <Alexandre.Morgaut@4d.com>, whatwg <whatwg@whatwg.org>, "Tab Atkins Jr." <jackalmage@gmail.com>
On Mon, Sep 24, 2012 at 7:18 PM, David Sheets <kosmo.zb@gmail.com> wrote:

>  Always. The appropriate interface is (string * string?) list. Id est,
an association list of keys and nullable values (null is
> key-without-value and empty string is empty-value). If you prefer to
> not use a nullable value and don't like tuple representations in JS,
> you could use type: string list list
> i.e.
> [["key_without_value"],[""],["key","value"],[],["numbers",1,2,3,4],["",""],["","",""]]

This isn't an appropriate interface.  It's terrible for 99.9% of use cases,
where you really want dictionary-like access.

The right approach is probably to expose the results in an object-like
form, as Tab suggests, but to store the state internally in a list-like
format, with modifications defined in terms of mutations to the list.

That is, parsing "a=1&b=2&a=3" would result in an internal representation
like [('a', '1'), ('b', '2'), ('a', '3')].  When viewed from script, you
see {a: ['1', '3'], 'b': ['2']}.  If you serialize it right back to a URL
the internal representation is unchanged, so the original order is
preserved.  The mutation algorithms can then do their best to preserve the
list as reasonably as they can (eg. assigning query.a = ['5', '6'] would
remove all 'a' keys, then insert items at the location of the first removed
item, or append if there were none).

 Is this not already supported by creating a new URL which contains
> only a relative query part?
> Like: query = new URL("?a=b&c=d"); query.query["a"] = "x";
> query.toString() == "?a=x&c=d";
> Why is a new interface necessary?

That won't work, since "?a=b&c=d" isn't a valid URL.  The invalid flag will
be set, so the change to .query will be a no-op, and .href (presumably what
toString will invoke) would return the original URL, "?a=b&c=d", not
"?a=x&c=d".  You'd need to do something like:

var query = new URL("http://example.com?" + url.hash);
query.query.a = "x";
url.hash = query.search.slice(1); // remove the leading "?"

That's awkward, but maybe it's good enough.

Glenn Maynard
Received on Tuesday, 25 September 2012 21:14:26 UTC

This archive was generated by hypermail 2.4.0 : Wednesday, 22 January 2020 16:59:45 UTC