Behaviors are initialized with this function Drupal.attachBehaviors = function(context, settings) {}. And we call it on load with those parameters:
Drupal.attachBehaviors(document, drupalSettings);
The idea is to have a DOM element as the context parameter so that attach scripts can use DOM methods on this context directly. Including doing
$(context).once('myonce');
The issue here is that document is not an actual DOM element, it's a Document and does not have all the methods and properties of an Element.
This is an issue with the new once script that expects an Element (so it can use setAttribute or getAttribute). The change here would pass the <html> element as the context for all behaviors. and make it possible to do
once([context], 'myonce');
Comments
Comment #2
nod_Comment #3
nod_The change shouldn't break anything because if you use context as document it would cause issues on ajax calls where the context is an actual dom element.
Comment #4
droplet commentedThis change is harmful.
Since the new Once has been handled BC problems for old scripts, I will close the door.
No matter the Once support that or not, I also say the below usages are bad practice:
A. If your script has a target element, registered the once on that element.
B. If your script has no element, and `window|document` is your target (It's not common. If you have a good example, please share to me)
First,
do whatever you do to prevent the script from running twice outside the Drupal before the quick tool:
DO
DON'T
Second,
Do NOT put the script inside `Drupal.behaviors.*.attach`, that meant never re-execute the script again. (, if Drupal CodeStandard allowed)
** Including the same script and has no intended to execute the script twice is a bug. Once is not help to prevent bug happens.
Comment #5
nod_To be clear, I'm not encouraging people to do
once([context], 'myonce'). I'm just saying that it should be possible. There is no reason it should be broken. Code that works on ajax request and break on the page load is simply bad and should be fixed. People can't assume context will bedocumentbecause it's true only on page load, not during ajax requests. I'd be surprised if anybody notices the change.I agree about the bad practices you're mentioning. And yes, everyone should do A. I dont have a good use-case for B.
Having a behavior run only once could be useful (That's what I understood from hardcoding the 'html' in the selector) and we should open an issue for that because it could get rid of most behaviors on ajax calls, so performance would be better. It's not in this issue that we should.
It's a lot of text to say:
I say nobody will see it because both Document and Element inherit from Node, so they share the same features also querySelector and querySelectorAll methods comes from the ParentNode mixin that is applied to both Document and Element.
Comment #6
nod_Comment #7
avpadernoThe documentation doesn't say to use
$(context), but (for example)$('input.myCustomBehavior', context).contextis the same type of context$(selector, context)gets as second parameter.This issue doesn't aim to remove
contextor suggest thatcontextshould never be used. It aims to simplify what objectattach()gets.Maybe few modules would get an error because they try to access a method that
contextdoesn't have because they getDocumentin that parameter, but I think it's still a change that avoids issues, at least in some cases.Moving away from jQuery, it's probable that more modules will get errors because what they get as
contextis reallyDocument.Comment #8
avpadernoAre there cases where converting
$(selector, context)to plain JavaScript I need to check what exactly iscontext? If the answer is yes, that would be a reason more to make the change proposed here.Comment #14
andypostquick re-roll, btw I think few other comments needs to be improved as well
Comment #15
longwaveThis also needs a change record, it is not impossible that someone is relying on the existing behaviour somehow.
Comment #16
lauriiiWouldn't this change be disruptive for any behaviors that are relying on the context being
Documentobject? I understand that depending onDocumentis wrong because the type definition allows it to be eitherDocumentorElement. However, since attaching a behavior on a site tends to happen relatively consistently, it is possible for code that only expects aDocumentto continue working without adverse effects.That said, I'm in favor of this change because it's making the API more consistent, and makes it even more explicit that behaviors should not expect the context to be solely the
Document. It also simplifies the use of the Once library because the type of the context is consistent.Comment #17
nod_updated the bigpipe call, and changed the default argument for detachBehaviors as well.
Comment #18
nod_missed a few spots
Comment #19
smustgrave commentedMoving to NW for the change record.
This doesn't require any kind of test?
Comment #20
longwaveI think this could have a test that relies on the context being the
<html>element, this will avoid us breaking this functionality again in the future.Comment #21
alexpottShould we also change the observer to attach to document.documentElement for consistency?
Below this code we do...
Comment #22
longwaveRe #21 will anything get added to
<html>but outside of<body>?Comment #23
Vidushi Mehta commented#18 was not applying on big_pipe.js with the latest pull so rerolled the patch with #21 comment.
Comment #24
Vidushi Mehta commentedAdding interdiff for the same.