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 typing for DOMParser #8871

Merged
merged 16 commits into from
Apr 30, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- chore(TS): Add getDOMParserFactory to env [#8871](https://github.com/fabricjs/fabric.js/pull/8871)
- fix(Path, Obect) Fix path parsing edge case for zeroed arc command and for too small canvas patterns [#8853](https://github.com/fabricjs/fabric.js/pull/8853)

## [6.0.0-beta3]
Expand Down
1 change: 1 addition & 0 deletions src/env/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ export const getEnv = (): TFabricEnv => {
// noop
},
copyPasteData,
DOMParser: window.DOMParser,
};
};
2 changes: 2 additions & 0 deletions src/env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ export const getEnv = () => env || getBrowserEnv();
export const getDocument = (): Document => getEnv().document;

export const getWindow = (): Window | DOMWindow => getEnv().window;

export const getDOMParser = () => new (getEnv().DOMParser)();
1 change: 1 addition & 0 deletions src/env/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const getEnv = (): TFabricEnv => {
}
},
copyPasteData,
DOMParser: JSDOMWindow.DOMParser,
};
};

Expand Down
1 change: 1 addition & 0 deletions src/env/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export type TFabricEnv = {
WebGLProbe: GLProbe;
dispose(element: Element): void;
copyPasteData: TCopyPasteData;
DOMParser: typeof DOMParser;
};
4 changes: 2 additions & 2 deletions src/parser/loadSVGFromString.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//@ts-nocheck
import { getWindow } from '../env';
import { getDOMParser } from '../env';
import { parseSVGDocument } from './parseSVGDocument';

/**
Expand All @@ -13,7 +13,7 @@ import { parseSVGDocument } from './parseSVGDocument';
* @param {AbortSignal} [options.signal] handle aborting, see https://developer.mozilla.org/en-US/docs/Web/API/AbortController/signal
*/
export function loadSVGFromString(string, callback, reviver, options) {
const parser = new (getWindow().DOMParser)(),
const parser = getDOMParser(),
doc = parser.parseFromString(string.trim(), 'text/xml');
parseSVGDocument(
doc.documentElement,
Expand Down
6 changes: 5 additions & 1 deletion test/unit/env.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ QUnit.module('env', (hooks) => {
const required = require('../../dist/index.js');
assert.notOk(global.window, 'no window');
assert.notOk(global.document, 'no document');
const win = { devicePixelRatio: 1.25 };
class DOMParserStub {

}
const win = { devicePixelRatio: 1.25, DOMParser:DOMParserStub };
const doc = { foo: 'bar' };
global.window = win;
global.document = doc;
[imported, required].forEach(fabric => {
assert.equal(fabric.getEnv().window, win, 'window should match');
assert.equal(fabric.getEnv().document, doc, 'document should match');
assert.equal(fabric.getEnv().DOMParser, DOMParserStub, 'DOMParser should match');
});
done();
});
Expand Down