-
-
Notifications
You must be signed in to change notification settings - Fork 925
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
Remove dependance on global window and document #2897
Conversation
I've got a better idea:
This comes with some added benefits:
|
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.
See reply
Yep sounds great, I'd suggest doing it in stages as changing the export is a breaking change for anyone who uses render directly ( like me :) ) IMO:
|
I had a look at only using parent.ownerDocument but that's not viable since DocumentFragment doesn't have an owner document. Best case we could use parent.ownerDocument when it's available and fall back to the top-level render target for when parent is a DocumentFragment. This (and my PR) does still leave an issue where render would no longer be able to target DocumentFragments. Maybe a solution to this is to keep the code as is, but use the target node if no document is passed in. |
I've updated to implement the last solution I proposed. This adds a number of fn calls that may have a slight perf impact, we could inline if required. |
Any browser that returns And this has been the case since DOM Level 1, back in 1998: https://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-one-core.html#ID-1950641247 DOM Level 2 created one other exception, Edit: grammar, clarity. |
@KoryNunn Forgot to ping. Also, WPT tests this for the It's difficult to imagine how You can safely it exists for all nodes with a If you truly want something bulletproof, you can use this code at the top of the render function and then proceed to use the current node's // The condition is equivalent to the following, but saves some space and is slightly faster:
// `root.nodeType !== 1 && root.nodeType !== 10`
if (~0x402 >> root.nodeType & 1) {
// Set the real root to an element if a document node
if (root.nodeType === 9) {
root = root.documentElement
} else {
throw new TypeError("Root must be a document, an element, or a document fragment")
}
} Edit: note to self and proofread before sending. (Missed some words.) |
Yep I was pretty surprised that DocumentFragment didn't have ownerDocument, and it turns out I just forgot to add it to the domMock for DocumentFragment PR now only uses parent.ownerDocument Closure still exists as there are other variables that seem necessary . Edit: I'd personally just say direct rendering into |
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.
Okay, minor nit: can you move the getDocument
call all the activeElement
calls accept into activeElement
itself? Otherwise looks fine.
Also, can you run the benchmark suite before and after this patch and share the results here? I expect this to have minor impact on create but to be insignificant elsewhere, and I'd like to have some validation here. |
Updated. Perf results: Before:
After:
Looks like negligible difference. |
* Remove dependance on global window and document * Use any available document, prioritising parent.ownerDocument * Fix mockDom for DocumentFragment, revert to better ownerDocument implementation * Simplify activeElement usage
Use window and document from render target instead of using globals.
This makes unit and intergration testing much easier.
Description
$window and $doc get set when
render
is called, and are based on the passeddom
Motivation and Context
This decouples mithril from its environment and instead only couples it to DOM.
This allows tests environments to be more easily established, making testing in Node etc easier.
This also allows tests to run in parallel, which, while not needed for mithril it's self, can be useful in larger applications that use mithril.
How Has This Been Tested?
All the existing tests pass
Types of changes
Checklist:
docs/changelog.md
Happy to check the last points off above if this PR is likely to get merged.