Skip to content

Replace "instanceof" due to DOM realm mixin when Blazor is IFRAME-d #56367

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

Closed
wants to merge 1 commit into from

Conversation

GStoynev
Copy link

When the Blazor page is in an IFRAME in Chrome (or Edge), the Comments (and potentially other HTML elements) in the frame fail the instanceof test because they're instances of frame's parent's Comment.

Replacing instanceof with this prototype check fixes the issue.

Issue is documented here: #55107

Fixes #55107

…s (and potentially other HTML elements) in the frame fail the instanceof test because they're instances of frame's parent's Comment.

Replacing instanceof with this prototype check fixes the issue.

Issue is documented here: dotnet#55107
@ghost ghost added the area-blazor Includes: Blazor, Razor Components label Jun 21, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jun 21, 2024
@dotnet-policy-service dotnet-policy-service bot added this to the 8.0.x milestone Jun 21, 2024
@GStoynev
Copy link
Author

@dotnet-policy-service agree

@GStoynev GStoynev marked this pull request as ready for review June 22, 2024 13:00
@GStoynev GStoynev requested a review from a team as a code owner June 22, 2024 13:00
@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Jun 29, 2024
Comment on lines +68 to +71
if (Object.prototype.toString.call(element) === "[object Comment]") {
// We sanitize start comments by removing all the information from it now that we don't need it anymore
// as it adds noise to the DOM.
element.textContent = '!';
(element as unknown as Comment).textContent = '!';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is incorrect, the right check should be element instanceof element.ownerDocument.defaultView.Comment.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof#instanceof_and_multiple_realms

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, rather than spread this logic all over, we should put it on a helper method isComment to ensure the check is the same across all the callsites.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested using element instanceof element.ownerDocument.defaultView.Comment. Result is similar to the original Microsoft implementation - the Comment element is NOT detected as such in all cases.

Here is the code and the console output when the error is present:

if (e instanceof Comment) {
  return e.parentNode;
} else if (Object.prototype.toString.call(e) === "[object Comment]") {
  console.warn("---> This *was NOT* detected as a Comment, but *is* one. element instanceof element.ownerDocument.defaultView.Comment is ", e instanceof e.ownerDocument.defaultView.Comment)
}

throw new Error("Not a valid logical element ", e)

When the error occurs, this is what is printed in the console - notice the false:
---> This *was NOT* detected as a Comment, but *is* one. Caller is reaching out for the parent node here. Btw element instanceof element.ownerDocument.defaultView.Comment is false

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More console output while stopped at a breakpoint during console.warn above:
image

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GStoynev why can't we do (element instance of Comment || (window.frameElement && element instanceof window.frameElement.ownerDocument.defaultView.Comment))

Can you put up a repro as a public github repository to make sure we are looking at the same thing? I would expect element.ownerDocument to be the same as window.frameElement.ownerDocument when element is inside the iframe.

@javiercn
Copy link
Member

javiercn commented Jul 1, 2024

Also, please retarget this PR to main. Changes should go first to main, and then we decide through our process if we backport them to previous versions.

@javiercn javiercn added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Jul 5, 2024
@dotnet-policy-service dotnet-policy-service bot added pr: pending author input For automation. Specifically separate from Needs: Author Feedback and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Jul 5, 2024
@dotnet-policy-service dotnet-policy-service bot added the stale Indicates a stale issue. These issues will be closed automatically soon. label Jul 15, 2024
@dotnet-policy-service dotnet-policy-service bot removed this from the 8.0.x milestone Jul 19, 2024
@GStoynev
Copy link
Author

Also, please retarget this PR to main. Changes should go first to main, and then we decide through our process if we backport them to previous versions.

Considering proposed alternative by @javiercn doesn't give us what we want, should I proceed with my original idea and issue the PR against main?

@GStoynev
Copy link
Author

GStoynev commented Aug 9, 2024

@javiercn, I'm back from a trip. I think some of our back-and-forth has disappeared - I had posted here the screenshots from (window.frameElement && element instanceof window.frameElement.ownerDocument.defaultView.Comment. Using that construct didn't provide the expected result - its evaluating to the same result as what plain instaneof gives us.

Also, you had mentioned about a minimal reproduction - unfortunately I am unable to provide a minimal reproduction at this time as we're working with a larger closed-source code base.

If an alternative to instanceof is acceptable, why not use what I originally proposed?

This check is incorrect

Could you elaborate why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun pr: pending author input For automation. Specifically separate from Needs: Author Feedback stale Indicates a stale issue. These issues will be closed automatically soon.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants