Skip to content
Closed
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
654eb22
Writing Flow: Collapse range in horizontal edge check by direction
aduth Apr 27, 2018
f7dcc8a
Try alternative approach
ellatrix Apr 30, 2018
bef44c3
Writing Flow: Move collapsed selection condition to WritingFlow
aduth May 2, 2018
a8728bf
Writing Flow: Vertical navigate from uncollapsed selections
aduth May 2, 2018
79fc388
Writing Flow: Use Selection#isCollapsed in place of hasCollapsedSelec…
aduth May 2, 2018
83d3ff7
Rich Text: Verify collapsed selection in delete behavior
aduth May 2, 2018
33184f9
Utils: Improve range rect detection for br-separated content
aduth May 2, 2018
f2ae9ef
Testing: Add Writing Flow E2E tests
aduth May 2, 2018
d81ac98
Try getRectangleFromRange fix without manipulating live DOM
ellatrix May 2, 2018
284900e
Utils: Update caret to selection in edge tests comments
aduth May 3, 2018
bc252af
Writing Flow: Invert position when isReverse not aligned to backward
aduth May 3, 2018
a961282
Try direct selection
ellatrix May 3, 2018
6431770
Only look at focusNode
ellatrix May 3, 2018
c0a0fc3
Utils: Document horizontal edge focusOffset
aduth May 3, 2018
b5a54df
Utils: Use temporary text node for rect fallback
aduth May 3, 2018
e087ddd
Writing Flow: Test horizontal navigation as handled
aduth May 3, 2018
7b174ad
Element: Fix Fragment render error on empty children
aduth May 10, 2018
a528868
Testing: Add global guard against ZWSP in E2E content retrieval
aduth May 10, 2018
d6f1ab9
RichText: Remove ZWSP on focusout
aduth May 10, 2018
4523390
RichText: Consider emptiness by Children count utility
aduth May 10, 2018
31596df
Writing Flow: Consider horizontal handled by stopPropagation in RichText
aduth May 10, 2018
fa4ad6d
RichText: Get content by converting tree node to element
aduth May 10, 2018
2afb649
Writing Flow: Consider as edge by effective caret position
aduth May 10, 2018
dab93de
Rich Text: Return element fragment as array
aduth May 10, 2018
9dde841
Testing: Attempt to resolve race conditions with TinyMCE initialization
aduth May 10, 2018
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
Prev Previous commit
Next Next commit
Utils: Use temporary text node for rect fallback
  • Loading branch information
aduth committed May 9, 2018
commit b5a54df3ed0907f7ba87b844b6a2ea25c05c0509
38 changes: 11 additions & 27 deletions utils/dom.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
* External dependencies
*/
import { includes, first } from 'lodash';
import { includes } from 'lodash';
import tinymce from 'tinymce';

/**
* Browser dependencies
*/
const { getComputedStyle } = window;
const { TEXT_NODE, ELEMENT_NODE } = window.Node;
const { TEXT_NODE } = window.Node;

/**
* Check whether the selection is horizontally at the edge of the container.
Expand Down Expand Up @@ -129,35 +129,19 @@ export function getRectangleFromRange( range ) {
return range.getBoundingClientRect();
}

const { startContainer, startOffset } = range;
let rect = range.getClientRects()[ 0 ];

// If the collapsed range starts (and therefore ends) at an element node,
// `getClientRects` will return undefined. To fix this we can get the
// selected child node and calculate a rectangle for that.
if ( startContainer.nodeType === ELEMENT_NODE ) {
const { childNodes } = startContainer;
const selectedNode = childNodes[
// Make sure there can be no errors.
Math.min( startOffset, childNodes.length - 1 )
];

if ( selectedNode.nodeType === ELEMENT_NODE ) {
return selectedNode.getBoundingClientRect();
}

// Text nodes have no `getBoundingClientRect` method, so create a range.
range = document.createRange();
range.setStart( selectedNode, 0 );
range.setEnd( selectedNode, 0 );

return range.getBoundingClientRect();
// `getClientRects` can be empty in some browsers. This can be resolved
// by adding a temporary text node to the range.
if ( ! rect ) {
const padNode = document.createTextNode( '\u200b' );
Copy link
Member

Choose a reason for hiding this comment

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

If this works for all cases in your testing, then cool, let's do it like this and I'll ponder there could be any better solution.

range.insertNode( padNode );
rect = range.getClientRects()[ 0 ];
padNode.parentNode.removeChild( padNode );
}

// For normal collapsed ranges (exception above), the bounding rectangle of
// the range may be inaccurate in some browsers. There will only be one
// rectangle since it is a collapsed range, so it is safe to pass this as
// the union of them. This works consistently in all browsers.
return first( range.getClientRects() );
return rect;
}

/**
Expand Down