-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Allow for unattached elements during inject call #7149
Conversation
Change-Id: I616cb30b8a3292b3be9f58536d7bfb9444e529d4
if (!document.contains(containerElement)) { | ||
if ( | ||
!document.contains(containerElement) && | ||
document !== containerElement?.ownerDocument |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the first check returns true, won't the second check always return true as well? In this case, can we just remove the first check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically yes that should be the case, but I haven't fully tested that theory with iframes, e.g., if we create containerElement in an iframe will document.contains(containerElement) pass if document is the document for top
rather than the frame. I think it's less risky to add an additional allowance rather than wholesale replace the original logic in the short term.
@maribethb any opinion on this? The check goes back to the beginning of time, but App Inventor has been monkey-patching it out for forever, and they haven't had problems. So I think it is safe to remove. |
I'm fine with the general idea. This has also been requested by folks using web components, e.g. #1114 and I don't know if the new check would work for that use case. I wonder if all we really need is to check that the containerElement exists (i.e. the getElementById call did not fail)? |
Makes sense. Since this probably affects several different use cases and would require some manual testing, I'm going to punt on that for now, and just merge this change to unblock app inventor. We can circle back to this later! |
The basics
npm run format
andnpm run lint
The details
Resolves
Fixes an issue where a Blockly workspace cannot be injected into a yet-to-be-attached element.
Proposed Changes
Check whether the ownerDocument of the injection div is the current document if the initial check for containment fails (e.g., due to the element not having been append elsewhere to the DOM).
Behavior Before Change
If a
div
is created andinject
is called to create a workspace before that div has been attached, an error is thrown.Behavior After Change
The check for membership in the document is made more permissive, so if the
div
is owned by thedocument
it will still be used to create a workspace (to be attached at a later time).Reason for Changes
In App Inventor, screens are separated into design and blocks, with the design being shown first. We must create a workspace and have it contain the blocks to participate in code generation, but the workspace will not be shown until the user clicks on the "Blocks" button. The existing check is too restrictive in the cases where a workspace needs to be created before it is shown.
Test Coverage
Tested by loading the playground (semantics of existing working Blockly apps should not change) and also in the branch of App Inventor being brought up to date with mainline Blockly.
Documentation
No documentation updates should be required as part of this change.
Additional Information