This is an archived snapshot of W3C's public bugzilla bug tracker, decommissioned in April 2019. Please see the home page for more details.

Bug 7104 - Disagreement on handling of null value for localStorage.setItem()
Summary: Disagreement on handling of null value for localStorage.setItem()
Status: RESOLVED WONTFIX
Alias: None
Product: WebAppsWG
Classification: Unclassified
Component: Web Storage (editor: Ian Hickson) (show other bugs)
Version: unspecified
Hardware: Macintosh MacOS X
: P2 normal
Target Milestone: ---
Assignee: Ian 'Hixie' Hickson
QA Contact: public-webapps-bugzilla
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-07-14 19:41 UTC by Brett C.
Modified: 2009-08-09 23:12 UTC (History)
4 users (show)

See Also:


Attachments
test page that shows what localStorage.getItem() thinks null should be (701 bytes, text/html)
2009-07-14 19:41 UTC, Brett C.
Details

Description Brett C. 2009-07-14 19:41:46 UTC
Created attachment 718 [details]
test page that shows what localStorage.getItem() thinks null should be

In Safari 4, if you use null as a value when setting a new key it will end up returning the string "null". But in Firefox 3.5, you get back null itself. I have attached a dead-simple HTML file that shows the differences when run in the two browsers. Not sure who is right since the spec says a DOMString is expected and I am not sure if null is considered a valid DOMString.

And in both browsers you end up with a key being set. While in Safari that makes sense since you are getting back a string, but in Firefox that's confusing as the value for the key is the same as the value used to signify that no value exists for the key. That means in Firefox the only way to know  that the key is actually set explicitly to null is to iterate through all the keys using localStorage.key() to try to see if the key has actually been given a value.
Comment 1 Jeremy Orlow 2009-07-15 02:03:13 UTC
I brought this up on WhatWG a while ago.  I believe it was agreed that Firefox's implementation goes against the spec.
Comment 2 Brett C. 2009-07-15 04:36:07 UTC
(In reply to comment #1)
> I brought this up on WhatWG a while ago.  I believe it was agreed that
> Firefox's implementation goes against the spec.
> 

Best I could find was http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2008-April/014383.html which ended with someone believing that Firefox is wrong and Safari is right, but no definitive answer.
Comment 3 Jeremy Orlow 2009-07-15 07:21:19 UTC
I apologize...I must have been remembering incorrectly.  I couldn't even find any mentions of any such message in my email archive, which further confounds me.

So yes, I suppose it is good to double check and perhaps have specific language in the spec to settle the issue once and for all.  :-)

From my reading, the Safari way of doing things is definitely the correct one.  The rational is essentially what you already mentioned, Brett.

If not, a .contains method should be added...but writing code for such an interface seems cumbersome.
Comment 4 Brett C. 2009-07-15 18:34:26 UTC
Although my opinion counts for squat, I do agree with Safari's behavior as well. Coercion of an object to a string is common enough in JS that it is reasonable to have null coerced. It did catch me by surprise that it did that, but I chalk that up to lack of JS experience on my part.

And once an official decision is made on this I will report the proper behavior to the right browser folks.
Comment 5 Ian 'Hixie' Hickson 2009-08-09 22:04:35 UTC
I propose to make the spec just use the default behaviour specified by Web IDL.
Comment 6 Brett C. 2009-08-09 22:57:55 UTC
Could you tell me what that means, Ian? I am writing a localStorage wrapper to deal with browser incompatibilities and add some helper functions and I would like to keep the wrapper agreeing with the spec.
Comment 7 Ian 'Hixie' Hickson 2009-08-09 23:05:30 UTC
I'm not 100% sure, as this has changed a few times, but it appears:
   http://dev.w3.org/2006/webapi/WebIDL/#TreatNullAs
...that it means that null and undefined, when passed to methods accepting DOMStrings, treat them as the strings "null" and "undefined" respectively.
Comment 8 Brett C. 2009-08-09 23:12:35 UTC
Thanks for the clarification, Ian! That's what I figured; just pass all key/value arguments through String. That leaves Firefox 3.5 incompatible in this instance and (I believe) a bug report has been sent in.

And as an aside for anyone curious how FF 3.5 and Safari 4 are incompatible with the spec so far, both do not implement ``localStorage.key(localStorage.length)`` properly as they both throw an exception instead of returning null. I reported the appropriate bugs. Otherwise they seem to be following the spec.