Skip to content

Commit

Permalink
fix(ie): Fix IE compatibility issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Mingze Xiao committed Sep 2, 2020
1 parent 895461a commit eda38fb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/BoxAnnotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import BaseAnnotator from './common/BaseAnnotator';
import ImageAnnotator from './image/ImageAnnotator';
import DocumentAnnotator from './document/DocumentAnnotator';
import { IntlOptions, Permissions, PERMISSIONS, Type } from './@types';
import './utils/polyfill';

type Annotator = {
CONSTRUCTOR: typeof BaseAnnotator;
Expand Down
32 changes: 26 additions & 6 deletions src/document/__tests__/docUtil-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,38 @@ describe('docUtil', () => {
});

describe('getRange()', () => {
const mockSelection = {
getRangeAt: () => 'range',
isCollapsed: false,
rangeCount: 1,
type: 'Range',
};

test.each`
selection | result
${null} | ${null}
${{ type: 'Caret' }} | ${null}
${{ isCollapsed: true, type: 'Range' }} | ${null}
${{ isCollapsed: true, rangeCount: 0, type: 'Range' }} | ${null}
${{ getRangeAt: () => 'range', rangeCount: 1, type: 'Range' }} | ${'range'}
selection | result
${null} | ${null}
${{ ...mockSelection, type: 'Caret' }} | ${null}
${{ ...mockSelection, isCollapsed: true }} | ${null}
${{ ...mockSelection, rangeCount: 0 }} | ${null}
${mockSelection} | ${'range'}
`('should return range as $result', ({ selection, result }) => {
jest.spyOn(window, 'getSelection').mockImplementationOnce(() => selection);

expect(getRange()).toBe(result);
});

test('should ignore selection type on IE', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore
// @ts-ignore
window.MSInputMethodContext = 'context';
document.documentMode = 11;
jest.spyOn(window, 'getSelection').mockReturnValueOnce(({
...mockSelection,
type: undefined,
} as unknown) as Selection);

expect(getRange()).toBe('range');
});
});

describe('getSelection()', () => {
Expand Down
14 changes: 13 additions & 1 deletion src/document/docUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ type Selection = {
range: Range;
};

declare global {
interface Document {
documentMode?: number;
}
}

export function isIE(): boolean {
return !!window.MSInputMethodContext && !!document.documentMode;
}

/**
* Finds the closest ancestor DOM element with the specified class.
*/
Expand All @@ -29,7 +39,9 @@ export function getPageNumber(element: Element | null): number | undefined {

export function getRange(): Range | null {
const selection = window.getSelection();
if (!selection || selection.type !== 'Range' || selection.isCollapsed || !selection.rangeCount) {

// Selection in IE does not have type property
if (!selection || selection.isCollapsed || !selection.rangeCount || (!isIE() && selection.type !== 'Range')) {
return null;
}

Expand Down

0 comments on commit eda38fb

Please sign in to comment.