Skip to content
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

Merged
merged 1 commit into from
Jun 13, 2023

Conversation

ewpatton
Copy link
Contributor

The basics

  • I branched from develop
  • My pull request is against develop
  • My code follows the style guide
  • I ran npm run format and npm 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 and inject 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 the document 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

Change-Id: I616cb30b8a3292b3be9f58536d7bfb9444e529d4
@ewpatton ewpatton requested a review from a team as a code owner June 12, 2023 22:07
@ewpatton ewpatton requested a review from BeksOmega June 12, 2023 22:07
@github-actions github-actions bot added the PR: fix Fixes a bug label Jun 12, 2023
if (!document.contains(containerElement)) {
if (
!document.contains(containerElement) &&
document !== containerElement?.ownerDocument
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

@BeksOmega
Copy link
Collaborator

@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.

@maribethb
Copy link
Contributor

@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)?

@BeksOmega
Copy link
Collaborator

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!

@BeksOmega BeksOmega merged commit f373809 into google:develop Jun 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: fix Fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants