-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Conversation
…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
@dotnet-policy-service agree |
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 = '!'; |
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.
This check is incorrect, the right check should be element instanceof element.ownerDocument.defaultView.Comment
.
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.
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.
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.
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
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.
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.
@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.
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? |
@javiercn, I'm back from a trip. I think some of our back-and-forth has disappeared - I had posted here the screenshots from 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?
Could you elaborate why? |
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