Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ module.exports = {
'no-var': 'error',
'etc/no-const-enum': ['error', { allowLocal: true }],
'import/no-default-export': 'error',
'no-restricted-globals': [
'error',
{
name: 'window',
message: 'Do not use global window',
},
{
name: 'document',
message: 'Do not use global document',
},
],
},
overrides: [
{
Expand All @@ -159,5 +170,15 @@ module.exports = {
'import/no-default-export': 'off',
},
},
{
files: [
'roosterjs-editor-adapter/**/*.ts',
'roosterjs-react/**/*.ts',
'roosterjs-react/**/*.tsx',
],
rules: {
'no-restricted-globals': 'off',
},
},
Comment on lines +173 to +182
Copy link

Copilot AI Dec 16, 2025

Choose a reason for hiding this comment

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

The eslint rule now restricts global document and window usage, but test files are not included in the override exceptions. Test files commonly use the global document object for creating test elements (e.g., document.createElement('a')), which is acceptable in test contexts. Consider adding test files to the override exceptions, such as '**/test/**/*.ts' or '**/*.test.ts', to avoid eslint violations in test code.

Copilot uses AI. Check for mistakes.
],
};
2 changes: 1 addition & 1 deletion packages/roosterjs-content-model-core/lib/editor/Editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export class Editor implements IEditor {
* @returns The HTML document which contains this editor
*/
getDocument(): Document {
return this.getCore().physicalRoot.ownerDocument;
return this.getCore().environment.document;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function createEditorEnvironment(
const appVersion = navigator?.appVersion ?? '';

return {
document: contentDiv.ownerDocument,
domToModelSettings: createDomToModelSettings(options),
modelToDomSettings: createModelToDomSettings(options),
isMac: appVersion.indexOf('Mac') != -1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe('getContentForCopy', () => {
getContentModelCopy: (): ContentModelDocument => createContentModelDocument(),
getEnvironment: (): EditorEnvironment => {
return {
document: mockDocument,
isSafari: false,
domToModelSettings: {} as ContentModelSettings<
DomToModelOption,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ describe('FormatPlugin', () => {
const mockedEnvironment = {
domToModelSettings: createDomToModelSettings({}),
modelToDomSettings: createModelToDomSettings({}),
document,
} as EditorEnvironment;

beforeEach(() => {
Expand Down Expand Up @@ -271,6 +272,7 @@ describe('FormatPlugin for default format', () => {
const mockedEnvironment = {
domToModelSettings: createDomToModelSettings({}),
modelToDomSettings: createModelToDomSettings({}),
document,
} as EditorEnvironment;

beforeEach(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('createEditorCore', () => {
htmlToDOM: mockedDOMHelper,
};
const mockedTrustHtmlHandler = 'TRUSTED' as any;
const mockedDocument: Document = 'DOCUMENT' as any;

beforeEach(() => {
spyOn(createEditorCorePlugins, 'createEditorCorePlugins').and.returnValue(mockedPlugins);
Expand Down Expand Up @@ -83,6 +84,7 @@ describe('createEditorCore', () => {
mockedLifeCyclePlugin,
],
environment: {
document: mockedDocument,
isMac: false,
isAndroid: false,
isIOS: false,
Expand Down Expand Up @@ -120,7 +122,7 @@ describe('createEditorCore', () => {

it('No options', () => {
const mockedDiv = {
ownerDocument: {},
ownerDocument: mockedDocument,
attributes: {
a: 'b',
},
Expand All @@ -143,7 +145,7 @@ describe('createEditorCore', () => {

it('With options', () => {
const mockedDiv = {
ownerDocument: {},
ownerDocument: mockedDocument,
attributes: {
a: 'b',
},
Expand Down Expand Up @@ -201,14 +203,15 @@ describe('createEditorCore', () => {
});

it('Android', () => {
const mockedDiv = {
ownerDocument: {
defaultView: {
navigator: {
userAgent: 'Android',
},
const mockedDocument: Document = {
defaultView: {
navigator: {
userAgent: 'Android',
},
},
} as any;
const mockedDiv = {
ownerDocument: mockedDocument,
attributes: {
a: 'b',
},
Expand All @@ -217,6 +220,7 @@ describe('createEditorCore', () => {

runTest(mockedDiv, mockedOptions, {
environment: {
document: mockedDocument,
isMac: false,
isAndroid: true,
isIOS: false,
Expand All @@ -236,14 +240,15 @@ describe('createEditorCore', () => {
});

it('Android+Safari', () => {
const mockedDiv = {
ownerDocument: {
defaultView: {
navigator: {
userAgent: 'Android AppleWebKit',
},
const mockedDocument: Document = {
defaultView: {
navigator: {
userAgent: 'Android AppleWebKit',
},
},
} as any;
const mockedDiv = {
ownerDocument: mockedDocument,
attributes: {
a: 'b',
},
Expand All @@ -252,6 +257,7 @@ describe('createEditorCore', () => {

runTest(mockedDiv, mockedOptions, {
environment: {
document: mockedDocument,
isMac: false,
isAndroid: true,
isIOS: false,
Expand All @@ -271,14 +277,15 @@ describe('createEditorCore', () => {
});

it('Mac', () => {
const mockedDiv = {
ownerDocument: {
defaultView: {
navigator: {
appVersion: 'Mac',
},
const mockedDocument: Document = {
defaultView: {
navigator: {
appVersion: 'Mac',
},
},
} as any;
const mockedDiv = {
ownerDocument: mockedDocument,
attributes: {
a: 'b',
},
Expand All @@ -287,6 +294,7 @@ describe('createEditorCore', () => {

runTest(mockedDiv, mockedOptions, {
environment: {
document: mockedDocument,
isMac: true,
isAndroid: false,
isIOS: false,
Expand All @@ -306,14 +314,15 @@ describe('createEditorCore', () => {
});

it('Safari', () => {
const mockedDiv = {
ownerDocument: {
defaultView: {
navigator: {
userAgent: 'AppleWebKit',
},
const mockedDocument: Document = {
defaultView: {
navigator: {
userAgent: 'AppleWebKit',
},
},
} as any;
const mockedDiv = {
ownerDocument: mockedDocument,
attributes: {
a: 'b',
},
Expand All @@ -322,6 +331,7 @@ describe('createEditorCore', () => {

runTest(mockedDiv, mockedOptions, {
environment: {
document: mockedDocument,
isMac: false,
isAndroid: false,
isIOS: false,
Expand All @@ -341,14 +351,15 @@ describe('createEditorCore', () => {
});

it('Chrome', () => {
const mockedDiv = {
ownerDocument: {
defaultView: {
navigator: {
userAgent: 'AppleWebKit Chrome',
},
const mockedDocument: Document = {
defaultView: {
navigator: {
userAgent: 'AppleWebKit Chrome',
},
},
} as any;
const mockedDiv = {
ownerDocument: mockedDocument,
attributes: {
a: 'b',
},
Expand All @@ -357,6 +368,7 @@ describe('createEditorCore', () => {

runTest(mockedDiv, mockedOptions, {
environment: {
document: mockedDocument,
isMac: false,
isAndroid: false,
isIOS: false,
Expand All @@ -376,15 +388,16 @@ describe('createEditorCore', () => {
});

it('iOS iPhone Safari', () => {
const mockedDiv = {
ownerDocument: {
defaultView: {
navigator: {
userAgent:
'Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148',
},
const mockedDocument: Document = {
defaultView: {
navigator: {
userAgent:
'Mozilla/5.0 (iPhone; CPU iPhone OS 18_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148',
},
},
} as any;
const mockedDiv = {
ownerDocument: mockedDocument,
attributes: {
a: 'b',
},
Expand All @@ -393,6 +406,7 @@ describe('createEditorCore', () => {

runTest(mockedDiv, mockedOptions, {
environment: {
document: mockedDocument,
isMac: false,
isAndroid: false,
isIOS: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createContentModelDocument } from '../creators/createContentModelDocument';
import { createDomToModelContextWithConfig } from '../../domToModel/context/createDomToModelContext';
import { createModelToDomContextWithConfig } from '../../modelToDom/context/createModelToDomContext';
import { createText } from '../creators/createText';
import { ensureParagraph } from './ensureParagraph';
import { createModelToDomContextWithConfig } from '../../modelToDom/context/createModelToDomContext';
import { createDomToModelContextWithConfig } from '../../domToModel/context/createDomToModelContext';
import type {
ContentModelSegmentFormat,
DomToModelContext,
Expand All @@ -20,7 +20,7 @@ export function normalizeSegmentFormat(
format: ContentModelSegmentFormat,
environment: EditorEnvironment
): ContentModelSegmentFormat {
const span = document.createElement('span');
const span = environment.document.createElement('span');
const segment = createText('text', format);

const domToModelContext: DomToModelContext = createDomToModelContextWithConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const handleImage: ContentModelSegmentHandler<ContentModelImage> = (
segmentNodes
) => {
const img = doc.createElement('img');
const element = document.createElement('span');
const element = doc.createElement('span');

parent.appendChild(element);
element.appendChild(img);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('normalizeSegmentFormat', () => {
modelToDomSettings: {
calculated: createModelToDomConfig([]),
},
document: document,
} as EditorEnvironment;
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,12 @@ export class AutoFormatPlugin implements EditorPlugin {
autoMailto,
});

if (linkSegment) {
return createAnchor(linkSegment.link?.format.href || '', linkSegment.text);
if (linkSegment && this.editor) {
return createAnchor(
this.editor.getDocument(),
linkSegment.link?.format.href || '',
linkSegment.text
);
}
return false;
},
Expand Down Expand Up @@ -339,8 +343,8 @@ export class AutoFormatPlugin implements EditorPlugin {
}
}

const createAnchor = (url: string, text: string) => {
const anchor = document.createElement('a');
const createAnchor = (doc: Document, url: string, text: string) => {
const anchor = doc.createElement('a');
anchor.href = url;
anchor.textContent = text;
return anchor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@ export class FindReplacePlugin implements EditorPlugin {
initialize(editor: IEditor) {
this.editor = editor;

const win = editor.getDocument().defaultView ?? window;
const win = editor.getDocument().defaultView;

this.context.findHighlight.initialize(win);
this.context.replaceHighlight.initialize(win);
if (win) {
this.context.findHighlight.initialize(win);
this.context.replaceHighlight.initialize(win);
}

this.editor.setEditorStyle(FindHighlightRuleKey, this.findHighlightStyle, [
FindHighlightSelector,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ export function generateDataURL(image: HTMLImageElement, editInfo: ImageMetadata

const imageWidth = nWidth * (1 - left - right);
const imageHeight = nHeight * (1 - top - bottom);
const doc = image.ownerDocument;
const win = doc.defaultView;

// Adjust the canvas size and scaling for high display resolution
const devicePixelRatio = window.devicePixelRatio || 1;
const canvas = document.createElement('canvas');
const devicePixelRatio = win?.devicePixelRatio || 1;
const canvas = doc.createElement('canvas');
const { targetWidth, targetHeight } = generatedImageSize;
canvas.width = targetWidth * devicePixelRatio;
canvas.height = targetHeight * devicePixelRatio;
Expand Down
Loading
Loading