There is selector without context parameter in Drupal.behaviors.states function . So when some scripts calls Drupal.attachBehaviors(context), states behavior attaches to whole page and creates duplicate event handlers.

Example - using http://drupal.org/project/active_tags and Drupal states functionallity on the same page.

Code from states.js.

Drupal.behaviors.states = {
  attach: function (context, settings) {
    for (var selector in settings.states) {
      for (var state in settings.states[selector]) {
        new states.Dependent({
          element: $(selector),
          state: states.State.sanitize(state),
          dependees: settings.states[selector][state]
        });
      }
    }
    // Execute all postponed functions now.
    while (states.postponed.length) {
      (states.postponed.shift())();
    }
  }
};

Replacement of element: $(selector) with element: $(selector,context) solved this problem for me.

Files: 
CommentFileSizeAuthor
#12 core-js-states-selector-1507954-12.patch939 bytesboombatower
PASSED: [[SimpleTest]]: [MySQL] 39,153 pass(es).
[ View ]
#2 core-js-states-selector-1507954-2.patch623 bytesnod_
PASSED: [[SimpleTest]]: [MySQL] 35,916 pass(es).
[ View ]
#2 core-js-states-selector-1507954-2-D7.patch603 bytesnod_
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch core-js-states-selector-1507954-2-D7.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]

Comments

Version:7.12» 8.x-dev

So, this will land soon #1419968: Replace $('selector', domelement) with $(domelement).find('selector') in 8.x
and it will be backported, so let's just change it to $context.find(selector); to make it easier on us later.

Status:Active» Needs review
StatusFileSize
new603 bytes
FAILED: [[SimpleTest]]: [MySQL] Unable to apply patch core-js-states-selector-1507954-2-D7.patch. Unable to apply patch. See the log in the details link for more information.
[ View ]
new623 bytes
PASSED: [[SimpleTest]]: [MySQL] 35,916 pass(es).
[ View ]

Status:Needs review» Needs work

The last submitted patch, core-js-states-selector-1507954-2-D7.patch, failed testing.

Status:Needs work» Needs review

forgot how to name the thing so that bot ignores it.

Need review from someone other than me.

I tested patch on Drupal 7. When "Hierarchical Select" send function as context it didn`t work properly.

Status:Needs review» Reviewed & tested by the community

Status:Reviewed & tested by the community» Needs review
Issue tags:+Needs manual testing, +needs backport to D7

silinor's post in #6 suggests this breaks hierarchical select, if this is intended for backport it'd be good to confirm there's no regression either way.

When hierarchical select called attachBehaviors it sended function as context. States.js is not worked correctly with function as context and attach behavior to whole page. For example required state after 2 selections with HS clone '*' and it`s become '***'
I found no other regressions. Only with hierarchical select. I think this issue will appear if some module will send function as context.

Status:Needs review» Reviewed & tested by the community

This looks like a bug in hierarchical select. It's context variable is a function and is used as a jQuery element everywhere. This is unrelated to this patch. In the case of hierarchical select the context should be cached.

Also it's sending wrong data to attachbehaviors, it's a raw DOM element that should be passed to it, neither a function nor a jQuery object.

Version:8.x-dev» 7.x-dev
Status:Reviewed & tested by the community» Patch (to be ported)

OK that makes sense. Committed/pushed to 8.x, will probably need a bug report for hierarchical select before it can be fixed in 7.x though I think.

Status:Patch (to be ported)» Needs review
StatusFileSize
new939 bytes
PASSED: [[SimpleTest]]: [MySQL] 39,153 pass(es).
[ View ]

Status:Needs review» Reviewed & tested by the community

Filled the patch over in the HS queue #1655100: Wong data sent to Drupal.attachBehaviors

Thanks for the bug report! I'll commit the fix today and issue a new release.

Doesn't look like that was actually done?

Status:Reviewed & tested by the community» Fixed

I'm not sure the Hierarchical Select issue was fixed yet (looks like probably not), but the module only has an alpha release so I don't think we have to wait forever for it. Also, if I understand the issue right, it's just that the bug fix doesn't work when Hierarchical Select is being used, not that anything else horrible breaks.

So... committed to 7.x. Thanks! http://drupalcode.org/project/drupal.git/commit/349ea19

Automatically closed -- issue fixed for 2 weeks with no activity.