-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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 typing for DOMParser #8871
Fix typing for DOMParser #8871
Conversation
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 is a good concept IMO.
The only thing is it should reference the factory instead of instantiating an instance of DOMParser for these reasons:
Also please:
- replace all usages in fabric code (find and replace should do the trick)
- add a changelog entry
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.
Great work, thanks!
Pushed a test and a minor change
@asturur I am merging unless you have objections
This is good because it takes fabric a step further to move away from JSDOM! |
Yay, thanks for reviewing! |
I have questions. |
i want to be clear that i don't think that should be direction at all, as of today it isn't. JSDOM was introduced exactly to avoid maintainin 2 paths of code. If you remove JSDOM you have to grow getEnv and the understanding and handling of the dom that i don't want to. FabricJS draws things and it doesn't want to solve isomorphim issues between node and browser, is just a field i don't want to get into. |
My intention for this PR is to solve the typing issue for #8869 |
I will explain my point.
All are not needed in node but were made possible for the sake of a quick and simple solution (and to allow devs to do stranger things) and because it was too complex to break up the logic (it is not the case anymore, we achieved most of it already). However, this solution might not suffice to make fabric work offscreen, and I want it to. Perf gains are too crazy not to. So many perf issues can be resolved simply by rendering things offscreen beforehand, stuff that we discussed, e.g. rendering special cases such as scaling, zooming and panning, even filtering. What is needed that is not already included in env or in this PR that JSDOM gives:
I couldn't think of anything else. Offscreen doesn't have a window/document concept as well and can't mutate the dom and is by most means the same as node in terms of how you would use it I guess, just for rendering, not for interaction. Loading JSDOM into it sounds silly to me. I am not sure how that would work. It must use Parsing is a good candidate for offscreen handling. This is how this topic is related to this PR. So having it in env is good IMO. That is my point. But it is not strictly related to this PR. |
The SVG code uses a lot of the DOM code that JSDOM polyfills. |
Good point. |
@tushuhei for me the type isse should be solved in this way: diff --git a/src/env/index.ts b/src/env/index.ts
index dcaa6a5a9..5e88f87c4 100644
--- a/src/env/index.ts
+++ b/src/env/index.ts
@@ -21,4 +21,5 @@ export const getEnv = () => env || getBrowserEnv();
export const getDocument = (): Document => getEnv().document;
-export const getWindow = (): Window | DOMWindow => getEnv().window;
+export const getWindow = (): (Window & typeof globalThis) | DOMWindow =>
+ getEnv().window;
diff --git a/src/env/types.ts b/src/env/types.ts
index b0c09c0ec..110cb2780 100644
--- a/src/env/types.ts
+++ b/src/env/types.ts
@@ -7,7 +7,7 @@ export type TCopyPasteData = {
};
export type TFabricEnv = {
document: Document;
- window: Window | DOMWindow;
+ window: (Window & typeof globalThis) | DOMWindow;
isTouchSupported: boolean;
WebGLProbe: GLProbe;
dispose(element: Element): void; And i m not saying that because i don't want the new function in the env, but because if we have a type issue we should be able to solve it with types and not adding new code. What is your thought? |
To me looks a decent solution as well |
@asturur I agree with your suggested change. I updated the code accordingly. |
Motivation
Related to #8869.
Creating a
DOMParser
from thewindow
object within the parser module leads to a TypeScript error, which entailsany
casting. This PR proposes encapsulating theDOMParser
instantiation in theenv
script to make it work isomorphically, so thatloadSVGFromString
can simply callgetDOMParser().parseFromString
.