Bug Tracker

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#979 closed bug (fixed)

Metadata: class metadata breaks removeClass in Opera

Reported by: joern Owned by:
Priority: major Milestone: 1.1.2
Component: plugin Version: 1.1.1
Keywords: Cc:
Blocked by: Blocking:

Description

Opera throws this exception:

JavaScript - http://localhost/plugins/validate/demo-test/validate-demo.html
Event thread: submit
Error:
name: SyntaxError
message: Statement on line 1312: RegExp.prototype.compile: syntax error in pattern or string
Backtrace:
  Line 1312 of linked script http://localhost/plugins/validate/demo-test/js/jquery.js
    return t && new RegExp("(^|\s)" + c + "(\s|$)").test(t);
  Line 1305 of linked script http://localhost/plugins/validate/demo-test/js/jquery.js
    return ! jQuery.className.has(c, cur);
  Line 1634 of linked script http://localhost/plugins/validate/demo-test/js/jquery.js
    if (! inv && fn(elems[i], i) || inv && ! fn(elems[i], i))
        Line 1306 of linked script http://localhost/plugins/validate/demo-test/js/jquery.js
    elem.className = c ? jQuery.grep(elem.className.split(/s+/), (function (cur)
{
  return ! jQuery.className.has(c, cur);
}
)).join(" ") : "";
  Line 2103 of linked script http://localhost/plugins/validate/demo-test/js/jquery.js
    jQuery.className.remove(this, c);
  Line 1273 of linked script http://localhost/plugins/validate/demo-test/js/jquery.js
    if (fn.apply(obj[i], args || [i, obj[i]]) === false)
          Line 310 of linked script http://localhost/plugins/validate/demo-test/js/jquery.js
    return jQuery.each(this, fn, args);
  Line 2118 of linked script http://localhost/plugins/validate/demo-test/js/jquery.js
    return this.each(n, arguments);
  Line 328 of linked script http://localhost/plugins/validate/jquery.validate.js
    $(element).removeClass(this.settings.errorClass);
  Line 315 of linked script http://localhost/plugins/validate/jquery.validate.js
    instance.validateElement(this);
  Line 1273 of linked script http://localhost/plugins/validate/demo-test/js/jquery.js
    if (fn.apply(obj[i], args || [i, obj[i]]) === false)
          Line 310 of linked script http://localhost/plugins/validate/demo-test/js/jquery.js
    return jQuery.each(this, fn, args);
  Line 316 of linked script http://localhost/plugins/validate/jquery.validate.js
    this.elements.each((function ()
{
  instance.validateElement(this);
}
));
  Line 232 of linked script http://localhost/plugins/validate/jquery.validate.js
    return validator.validateForm();
  Line 2819 of linked script http://localhost/plugins/validate/demo-test/js/jquery.js
    if (c[j].apply(this, args) === false)
          At unknown location
    [statement source code not available]

A test was added to the metadata testsuite:

test("try to add and remove classes on metadata elements", function() {
	$("#two li").addClass("foobar").addClass("foo bar").removeClass("foobar");
	ok( $("#two li").is(".foo") );
	ok( $("#two li").is(".bar") );
});

Change History (4)

comment:1 Changed 13 years ago by aheimlich

Perhaps this is because metadata uses JSON syntax and parts of it (namely { : }) are also part of regular expression syntax.

You might want to try something like this:

has: function( t, c ) {
    t = t.className || t;
    t.replace(/([.\+*?[^]$(){}=!<>|:])/g, "\$1");
    return t && new RegExp("(^|\s)" + c + "(\s|$)").test( t );
}

the second line was inspired by PHP's preg_quote

Another option is to simply dump regular expressions entirely within jQuery.class.has().

comment:2 Changed 13 years ago by joern

Component: ajaxplugin

Both solutions seem valid. I wonder what would perform better, or if this has any performance impact at all.

comment:3 Changed 13 years ago by joern

This works in Opera:

has: function( t, c ) {
	t = t.className || t;
	c = c.replace(/([.\+*?[^]$(){}=!<>|:])/g, "\$1");
	return t && new RegExp("(^|\s)" + c + "(\s|$)").test( t );
}

comment:4 Changed 13 years ago by joern

Resolution: fixed
Status: newclosed

Fixed in revision [1415].

Note: See TracTickets for help on using tickets.