This project is archived and is in readonly mode.

#867 ✓resolved

Element.match does not return false when passing in an unmatching element.

Reported by onassar | March 15th, 2010 @ 02:33 PM | in 1.3.0 rc2 (closed)

For example:

var one = $('photos');

Running on FF, 1.2.4
Let me know if I can provide anything else. That throws "selector.match is not a function".
I've traced the problem down to a 'match' function/method calling being made against a RegExp object, rather than a string or HTMLElement object. Funny enough, it works when the passed in object is a match, such as:

var one = $('photos');


Comments and changes to this ticket

  • onassar

    onassar March 15th, 2010 @ 02:34 PM

    • Tag changed from match element recursion error to element, error, match, recursion
  • onassar

    onassar March 15th, 2010 @ 02:40 PM

    Followed up: it's valid and returning true for itself because the match function/method's first line is:
    @@@if (!selector || (selector == this)) return true;@@@

  • JacobThornton

    JacobThornton March 16th, 2010 @ 12:50 AM

    I don't know why !selector would return true -- strange... but I suspect match is expecting selector to be a string ... if you absolutely need it to match an element ... you might want to consider implementing your own match function which does stuff like detecting an element and adapting:

    tagid = Selectors.Utils.parseTagAndID(selector)


    var tagid = $type(selector) == 'element' ? [selector.get('tag'), selector.get('id')] : Selectors.Utils.parseTagAndID(selector);


  • onassar

    onassar March 16th, 2010 @ 12:54 AM

    Thanks for the ping Jacob.
    I've found a way around this problem. I think it's mainly a problem because the documentation says specifically that you can pass in a node/el, and it'll return true/false accordingly. I found an ugly way around it, but it's usable until this is fixed I suppose. Or nixed; either or :)

  • JacobThornton

    JacobThornton March 16th, 2010 @ 01:07 AM

    oh you're right -- easy to fix:

    An element to match; returns true if this is the actual element passed in (else false...)


    match: function(selector){
        if (!selector || (selector == this)) return true;
                else if($type(selector) == 'element') return false;
        var tagid = Selectors.Utils.parseTagAndID(selector);
        var tag = tagid[0], id = tagid[1];
        if (!Selectors.Filters.byID(this, id) || !Selectors.Filters.byTag(this, tag)) return false;
        var parsed = Selectors.Utils.parseSelector(selector);
        return (parsed) ? Selectors.Utils.filter(this, parsed, {}) : true;


  • onassar

    onassar March 16th, 2010 @ 07:44 PM

    Even easier to fix it is to wrap your code in a $try call :D:D
    Anyway, whats the process for pushing bug fixes to the repo?

  • JacobThornton
  • Fábio M. Costa

    Fábio M. Costa March 21st, 2010 @ 11:46 PM

    Hi guys, the new mootools version, 1.3, will come with a new Selector Engine (codename Slick) that will have a safer match function.

    That being said... the match function should be used with a string, not an element (you can do element === other_element and it will work). It tests if the current element matches the passed css selector. On 1.3 it will work with any selector. Currently it just works with simple selectors.

  • Fábio M. Costa

    Fábio M. Costa March 27th, 2010 @ 07:48 PM

    The match function will work fine if you pass an element too, just checked the code.

  • Fábio M. Costa

    Fábio M. Costa March 27th, 2010 @ 07:53 PM

    • State changed from “new” to “resolved”
    • Assigned user set to “Fábio M. Costa”
    • Milestone set to 1.3.0 rc2

    There are specs for this on 1.3 (will use Slick).

  • Tim Wienk

    Tim Wienk April 8th, 2010 @ 09:18 PM

    ++ on the 1.3 thing mentioned by Fabio, but it's not safe to pass in an Element right now, unless you use the fix mentioned by JacobThornton.

    - (selector != this): not returning true
    - Selectors.Utils.parseTagAndID(yourElement)
    --- calls yourElement.match(/some_regex/)
    ----- (selector != this): not returning true
    ----- Selectors.Utils.parseTagAndID(/some_regex/)
    ------- calls /some_regex/.match
    --------- throws: /some_regex/ has no method match

    JacobThornton's fix (checking whether selector is an Element beforing going on) will work around this.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

People watching this ticket