Skip to content

Commit

Permalink
fix(range): Fix Range.getClientRects on IE/Edge (#577)
Browse files Browse the repository at this point in the history
* fix(range): Fix Range.getClientRects on IE/Edge

* feat(region): Move test to actions-test
  • Loading branch information
Mingze authored Sep 4, 2020
1 parent 53b00bd commit 8de3cf0
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/store/highlight/__mocks__/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ export const mockDOMRect: DOMRect = {
y: 200,
};

const mockTextNode = document.createTextNode('test');

export const mockRange: Range = ({
endContainer: mockTextNode,
getBoundingClientRect: () => mockDOMRect,
getClientRects: () => [mockDOMRect],
startContainer: mockTextNode,
} as unknown) as Range;
47 changes: 47 additions & 0 deletions src/store/highlight/__tests__/actions-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import state from '../__mocks__/highlightState';
import { mockContainerRect, mockDOMRect, mockRange } from '../__mocks__/data';
import { setSelectionAction } from '../actions';

describe('store/highlight/actions', () => {
describe('setSelectionAction', () => {
const arg = {
containerRect: mockContainerRect,
location: 1,
range: mockRange,
};

test('should prepare correct argument', () => {
expect(setSelectionAction(arg)).toEqual({
payload: state.selection,
type: 'SET_SELECTION',
});
});

test('should prepare correct argument in IE/Edge', () => {
jest.spyOn(document, 'createNodeIterator').mockReturnValueOnce(({
nextNode: jest.fn().mockReturnValueOnce(mockRange.startContainer),
} as unknown) as NodeIterator);
jest.spyOn(document, 'createRange').mockReturnValueOnce({
...new Range(),
getBoundingClientRect: jest.fn().mockReturnValueOnce(mockDOMRect),
selectNodeContents: jest.fn(),
setEnd: jest.fn(),
setStart: jest.fn(),
});
const range = {
...mockRange,
getClientRects: () => ({ length: 0 } as DOMRectList),
};

const newArg = {
...arg,
range,
};

expect(setSelectionAction(newArg)).toEqual({
payload: state.selection,
type: 'SET_SELECTION',
});
});
});
});
61 changes: 60 additions & 1 deletion src/store/highlight/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,59 @@ type Payload = {
payload: SelectionItem | null;
};

declare global {
interface Document {
createNodeIterator(
root: Node,
whatToShow?: number,
filter?: NodeFilter | Function,
entityReferenceExpansion?: boolean,
): NodeIterator;
}
}

export const getClientRects = (range: Range): DOMRect[] => {
const iterator = document.createNodeIterator(
range.commonAncestorContainer,
NodeFilter.SHOW_ALL,
function acceptNode() {
return NodeFilter.FILTER_ACCEPT;
},
false,
);

const newRange = document.createRange();
const rects = [];
let currentNode = iterator.nextNode();
while (currentNode) {
if (rects.length === 0 && currentNode !== range.startContainer) {
currentNode = iterator.nextNode();
// eslint-disable-next-line no-continue
continue;
}

// Only highlight Text nodes
if (currentNode.nodeType === Node.TEXT_NODE) {
newRange.selectNodeContents(currentNode);
if (currentNode === range.startContainer) {
newRange.setStart(currentNode, range.startOffset);
}
if (currentNode === range.endContainer) {
newRange.setEnd(currentNode, range.endOffset);
}
rects.push(newRange.getBoundingClientRect());
}

if (currentNode === range.endContainer) {
break;
}

currentNode = iterator.nextNode();
}

return rects;
};

export const getShape = ({ height, left, top, width }: DOMRect): Shape => ({
height,
width,
Expand All @@ -31,11 +84,17 @@ export const setSelectionAction = createAction(

const { containerRect, location, range } = arg;

let rects = Array.from(range.getClientRects());
// getClientRects on IE/Edge might return 0 rects
if (!rects.length) {
rects = getClientRects(range);
}

return {
payload: {
containerRect: getShape(containerRect),
location,
rects: combineRectsByRow(Array.from(range.getClientRects()).map(getShape)),
rects: combineRectsByRow(rects.map(getShape)),
},
};
},
Expand Down

0 comments on commit 8de3cf0

Please sign in to comment.