Skip to content

Commit

Permalink
Fix a Text Selection Exception in Safari (WordPress#8255)
Browse files Browse the repository at this point in the history
* Catch a Text Selection Exception in Safari; fixes WordPress#8254

* Improve the inline explanation of why we need a try/catch block
  • Loading branch information
chrisvanpatten authored and antpb committed Oct 26, 2018
1 parent bdfcb30 commit da9ac58
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions packages/dom/src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,13 +433,25 @@ export function placeCaretAtVerticalEdge( container, isReverse, rect, mayUseScro
* @return {boolean} True if the element is an text field, false if not.
*/
export function isTextField( element ) {
const { nodeName, selectionStart, contentEditable } = element;

return (
( nodeName === 'INPUT' && selectionStart !== null ) ||
( nodeName === 'TEXTAREA' ) ||
contentEditable === 'true'
);
try {
const { nodeName, selectionStart, contentEditable } = element;

return (
( nodeName === 'INPUT' && selectionStart !== null ) ||
( nodeName === 'TEXTAREA' ) ||
contentEditable === 'true'
);
} catch ( error ) {
// Safari throws an exception when trying to get `selectionStart`
// on non-text <input> elements (which, understandably, don't
// have the text selection API). We catch this via a try/catch
// block, as opposed to a more explicit check of the element's
// input types, because of Safari's non-standard behavior. This
// also means we don't have to worry about the list of input
// types that support `selectionStart` changing as the HTML spec
// evolves over time.
return false;
}
}

/**
Expand Down

0 comments on commit da9ac58

Please sign in to comment.