jQuery: The Write Less, Do More JavaScript Library

Old json.js / Object.prototype compatibility problem, new fix for jquery-1.3.2

View: New views
5 Messages — Rating Filter:   Alert me  

Old json.js / Object.prototype compatibility problem, new fix for jquery-1.3.2

by nothize :: Rate this Message:

| View Threaded | Show Only this Message


I've found an old thread talking about the compatibility issue with
json.js and jquery :

[ jQuery + Json library = broken jQuery ]
http://groups.google.com/group/jquery-dev/browse_thread/thread/5aecf614042d5a20/5eacf4619d19a8a7?lnk=gst&
q=json#

Whenever json.js, which mess with Object.prototype, is included in a
web page with jquery, two javascript errors are potentially awakened.

As I think it is awful to workaround this problem by modifying all
JSON related function calls or any other things that might have
injected into Object, I decided to let jquery to be more fault
tolerant to a given environment. Like how it did for cross browser
support.


The unified diff between jquery 1.3.2 and the changes I have made to
avoid the errors:

==============================================
--- jquery-1.3.2.js 2009-03-12 14:09:14.000000000 +0800
+++ jquery-1.3.2_fix.js 2009-03-12 14:15:38.000000000 +0800
@@ -1582,7 +1582,7 @@

  while ( expr && set.length ) {
  for ( var type in Expr.filter ) {
- if ( (match = Expr.match[ type ].exec( expr )) != null ) {
+ if ( Expr.match[ type ].exec && (match = Expr.match[ type ].exec
( expr )) != null ) {
  var filter = Expr.filter[ type ], found, item;
  anyFound = false;

@@ -2683,6 +2683,9 @@
  for ( var j in handlers ) {
  var handler = handlers[j];

+ if ( !handler.guid ) {
+ continue;
+ }
  // Filter the functions by class
  if ( all || namespace.test(handler.type) ) {
  // Pass in a reference to the handler function itself
==============================================

The first change avoided a problem in IE 5 that saying blahblahbalh
method not found.
The second change avoided an infinite recursion loop, though checking
the existence of guid may still be vulnerable to coincidence. Perhaps
adding a hopefully unique identifier(maybe the jQuery instance?) when
an event is being added via jQuery.event.add() will be safer?

I love jquery as it helped me much in not reinventing the wheels.
Hopefully the fixes will be incorporated as part of the jquery so that
the fixes can be shared by more people.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Old json.js / Object.prototype compatibility problem, new fix for jquery-1.3.2

by John Resig :: Rate this Message:

| View Threaded | Show Only this Message

This is something that we're looking in to for a future release, in the meantime, you should definitely be using json2.js:
http://json.org/json2.js

--John


On Thu, Mar 12, 2009 at 2:27 AM, nothize <nothize@...> wrote:

I've found an old thread talking about the compatibility issue with
json.js and jquery :

[ jQuery + Json library = broken jQuery ]
http://groups.google.com/group/jquery-dev/browse_thread/thread/5aecf614042d5a20/5eacf4619d19a8a7?lnk=gst&
q=json#


Whenever json.js, which mess with Object.prototype, is included in a
web page with jquery, two javascript errors are potentially awakened.

As I think it is awful to workaround this problem by modifying all
JSON related function calls or any other things that might have
injected into Object, I decided to let jquery to be more fault
tolerant to a given environment. Like how it did for cross browser
support.


The unified diff between jquery 1.3.2 and the changes I have made to
avoid the errors:

==============================================
--- jquery-1.3.2.js     2009-03-12 14:09:14.000000000 +0800
+++ jquery-1.3.2_fix.js 2009-03-12 14:15:38.000000000 +0800
@@ -1582,7 +1582,7 @@

       while ( expr && set.length ) {
               for ( var type in Expr.filter ) {
-                       if ( (match = Expr.match[ type ].exec( expr )) != null ) {
+                       if ( Expr.match[ type ].exec && (match = Expr.match[ type ].exec
( expr )) != null ) {
                               var filter = Expr.filter[ type ], found, item;
                               anyFound = false;

@@ -2683,6 +2683,9 @@
               for ( var j in handlers ) {
                       var handler = handlers[j];

+                       if ( !handler.guid ) {
+                               continue;
+                       }
                       // Filter the functions by class
                       if ( all || namespace.test(handler.type) ) {
                               // Pass in a reference to the handler function itself
==============================================

The first change avoided a problem in IE 5 that saying blahblahbalh
method not found.
The second change avoided an infinite recursion loop, though checking
the existence of guid may still be vulnerable to coincidence. Perhaps
adding a hopefully unique identifier(maybe the jQuery instance?) when
an event is being added via jQuery.event.add() will be safer?

I love jquery as it helped me much in not reinventing the wheels.
Hopefully the fixes will be incorporated as part of the jquery so that
the fixes can be shared by more people.




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Old json.js / Object.prototype compatibility problem, new fix for jquery-1.3.2

by nothize :: Rate this Message:

| View Threaded | Show Only this Message


Oops, I could not catch your meaning.

Is it the jQuery development team has some plan or design already for
resolving this issue, just not yet implement on the latest source?

Since I fetched from the trunk (r6274) and the 2 issues are
repeatable.


Someone on the IRC development channel has suggested me to use
json2.js too.

However, the project to introduce jQuery has been into a stable stage,
when jQuery is the newly added component, it's considered more
favorite to modify jQuery than the old stuff, that the migration from
json.js to json2.js may not be as simple as modifying jQuery.

And I concern the way I modified jQuery is whether the usual approach
and recognized by the jQuery development team since they may have
their own approach or design that I am not aware of.

Sorry for the previous diff that it wasn't generated from a svn diff.
Below is the updated code and svn diff of the local copy against the
revision 6274.

Index: selector.js
===================================================================
--- selector.js (revision 6274)
+++ selector.js (working copy)
@@ -189,6 +189,9 @@

  while ( expr && set.length ) {
  for ( var type in Expr.filter ) {
+ if ( !Expr.match[ type ].exec ) {
+ continue;
+ }
  if ( (match = Expr.match[ type ].exec( expr )) != null ) {
  var filter = Expr.filter[ type ], found, item;
  anyFound = false;
Index: event.js
===================================================================
--- event.js (revision 6274)
+++ event.js (working copy)
@@ -256,6 +256,9 @@
  for ( var j in handlers ) {
  var handler = handlers[j];

+ if ( !handler.guid ) {
+ continue;
+ }
  // Filter the functions by class
  if ( all || namespace.test(handler.type) ) {
  // Pass in a reference to the handler function itself
===================================================================

--
Nothize


On Mar 13, 12:54 am, John Resig <jere...@...> wrote:
> This is something that we're looking in to for a future release, in the
> meantime, you should definitely be using json2.js:http://json.org/json2.js
>
> --John
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Old json.js / Object.prototype compatibility problem, new fix for jquery-1.3.2

by John-David Dalton :: Rate this Message:

| View Threaded | Show Only this Message


Hello Nothize,

Modifying Object.prototype will cause more than problems with jQuery.
Anything that for-in iterates over an object will be affected.

json2 follows the ECMA 3.1 draft, and uses native JSON api where
available, so its a pretty win win solution.
Trying to back in a patch for this issue seems very wrong.

- JDD
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---


Re: Old json.js / Object.prototype compatibility problem, new fix for jquery-1.3.2

by nothize :: Rate this Message:

| View Threaded | Show Only this Message


Hi jdalton,

Thanks for the info!! Now I understand more to the problem and found
some workaround others have done years ago. And the problem appeared
in other js lib like prototype......

As with the old post said, the hasOwnProperty check is good enuf for
this problem.

It seemed that the workaround was rejected due to the performance
degradation(well I have not done any test) as said by John Resig
"Especially since the hasOwnProperty
work around will add a performance hit to all object property
iterations (which is unacceptable)".

Now I recall that my intention to post the new fix is to minimize the
performance hit to hope that the fix can get accepted, if the only
reason for rejecting any backward-compatibility issues are just
because of performance.

Nothize

On Mar 17, 10:25 pm, jdalton <John.David.Dal...@...> wrote:

> Hello Nothize,
>
> Modifying Object.prototype will cause more than problems with jQuery.
> Anything that for-in iterates over an object will be affected.
>
> json2 follows the ECMA 3.1 draft, and uses native JSON api where
> available, so its a pretty win win solution.
> Trying to back in a patch for this issue seems very wrong.
>
> - JDD
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "jQuery Development" group.
To post to this group, send email to jquery-dev@...
To unsubscribe from this group, send email to jquery-dev+unsubscribe@...
For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en
-~----------~----~----~----~------~----~------~--~---