Today I experimented a weird issue while trying to update Prototype on a particular website. Code which worked correctly suddenly stopped to work with the 1.7 version.
After some investigations, I understood it was an issue with some JSON code. At first sight, this code seemed quite correct:
{
mode : "exact",
elements : "textarea_bouzi_txt_index_para_1",
theme : "advanced",
skin : "o2k7",
plugins : "",
theme_advanced_toolbar_location : "top",
theme_advanced_toolbar_align : "left",
theme_advanced_statusbar_location : "bottom",
convert_urls : false,
relative_urls : false,
forced_root_block : false,
extended_valid_elements : "div[*],iframe[*]"
}
So, why didn’t this work? Looking at this closer, we see it is not correct: properties are not between quotes. This code (I think it is provided by TinyMCE - but it is to check) should rather be:
{
"mode" : "exact",
"elements" : "textarea_bouzi_txt_index_para_1",
"theme" : "advanced",
"skin" : "o2k7",
"plugins" : "",
"theme_advanced_toolbar_location" : "top",
"theme_advanced_toolbar_align" : "left",
"theme_advanced_statusbar_location" : "bottom",
"convert_urls" : false,
"relative_urls" : false,
"forced_root_block" : false,
"extended_valid_elements" : "div[*],iframe[*]"
}
Nevertheless, it doesn’t explain why this code, as wrong as it could be, suddenly stopped to work.
The answer is that recent browsers from now on implement in a native way their own JSON parser. There is no need anymore to use a JavaScript JSON library. You simply have to do:
JSON.parse('["some JSON code"]');
And it becomes interesting because in its last version, Prototype try to rely on the the native JSON parser of the browser when it is available, in order to increase performances. But there is the problem: this parser is generally more strict. Indeed, I tried with Firefox 3.6 and Internet Explorer 9.0, and in both cases, the JSON code without quotes triggers an error.
Strictly, one can’t say it is a bug of the parsers, because the JSON code is effectively not well formed. However, a very lot of libraries which doesn’t provide JSON code with properties into quotes exist (likely the TinyMCE version I use, as it seems). All this code will crash the last Prototype version, which is problematic.
The best solution would be to provide well formed JSON code to Prototype, but it is not always possible. Remains an other simple solution, which consists to downgrade Prototype in order to avoid it to use the native parser of the browser when one is available, and to force it to use its own parser.
To do this, there is several solutions, which all require to modify the prototype.js file a little bit. I list them by a personal preference order:
Make your own choice! Even though the best solution remains to have clean JSON code at start