Friday, November 12, 2010

jQuery 1.4.3 has a problem with apostrophe?

It seems that jQuery 1.4.3 has a problem with the apostrophe (' -- single quote) character in JSON data or any ISO Latin-1 code representation of characters.

In working on the drupal-nm project, I come across a port that has a description of Wei's phone, the port information dialog box would not display. I start up Firebug to see what's going on. Firebug seems to show that the JSON data is received and at least the debugger parsed that data correctly. However, my callback function to the jQuery's $.post() call was not invoked, at all.

Click on a different port without the apostrophe in the description, things work fine.

So I went to the server side of the function, trying to escape the apostrophe. I replaced the (') character with (') -- things stayed the same, no call back with the received JSON data, no sign of any error occurred in Firebug. In fact, any ISO Latin or HTML entity expression would cause the same problem.

This used to work fine with jQuery 1.3.2 -- which has different issues with the jQuery context menu plugin -- that needs a different blog post here.

For now, I am replacing (') with (’) -- copy and pasted the character into my code, not using the ISO Latin expression of (’), hope no one would notice.

[2010-11-13]: Posted a question on the jQuery Forum and received a pointer to jsFiddle.net -- a very nice tool for JavaScript testing, by the way. Created a test there, which seems to prove that the problem is not within jQuery 1.4.3. That means, the problem is likely a bad reaction between my own JavaScript components or other jQuery plugin modules I am using. I'll have to figure that out later.

[2010-11-16]: This is a known Drupal bug. To fix the bug in Drupal 6.20, replace lines 2500 - 2502 in includes/common.inc with the lines below:
      return '"'. str_replace(array("\r", "\n", "\t", "\\", "\/", "\"", "\'"),
array('\r', '\n', '\t', '\\', '\/', '\u0022', '\u0027'),
addslashes($var)) .'"';

[2011-03-29]: This problem still exists in the latest Drupal 6.20 release. Also, the JSON specification allows the apostrophe (') but jQuery's JSON parser does not.

[2011-05-02]: Drupal 6 may have a bigger problem with JSON conversion than this one bug. As it turns out, the above fix would also escape the double quotes surrounding member variable names and string values, which causes the entire JSON data object to be interpreted as a big string when received by a jQuery client end. Not good!

No comments:

Post a Comment