@wordpress/dom 3.2.3 → 3.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +0 -1
  2. package/build/dom/caret-range-from-point.js +2 -2
  3. package/build/dom/caret-range-from-point.js.map +1 -1
  4. package/build/dom/is-edge.js +1 -1
  5. package/build/dom/is-edge.js.map +1 -1
  6. package/build/dom/place-caret-at-edge.js +108 -0
  7. package/build/dom/place-caret-at-edge.js.map +1 -0
  8. package/build/dom/place-caret-at-horizontal-edge.js +2 -84
  9. package/build/dom/place-caret-at-horizontal-edge.js.map +1 -1
  10. package/build/dom/place-caret-at-vertical-edge.js +6 -56
  11. package/build/dom/place-caret-at-vertical-edge.js.map +1 -1
  12. package/build-module/dom/caret-range-from-point.js +2 -2
  13. package/build-module/dom/caret-range-from-point.js.map +1 -1
  14. package/build-module/dom/is-edge.js +1 -1
  15. package/build-module/dom/is-edge.js.map +1 -1
  16. package/build-module/dom/place-caret-at-edge.js +95 -0
  17. package/build-module/dom/place-caret-at-edge.js.map +1 -0
  18. package/build-module/dom/place-caret-at-horizontal-edge.js +2 -81
  19. package/build-module/dom/place-caret-at-horizontal-edge.js.map +1 -1
  20. package/build-module/dom/place-caret-at-vertical-edge.js +6 -54
  21. package/build-module/dom/place-caret-at-vertical-edge.js.map +1 -1
  22. package/build-types/dom/caret-range-from-point.d.ts +2 -2
  23. package/build-types/dom/is-edge.d.ts.map +1 -1
  24. package/build-types/dom/place-caret-at-edge.d.ts +9 -0
  25. package/build-types/dom/place-caret-at-edge.d.ts.map +1 -0
  26. package/build-types/dom/place-caret-at-horizontal-edge.d.ts.map +1 -1
  27. package/build-types/dom/place-caret-at-vertical-edge.d.ts +4 -5
  28. package/build-types/dom/place-caret-at-vertical-edge.d.ts.map +1 -1
  29. package/package.json +2 -2
  30. package/src/dom/caret-range-from-point.js +2 -2
  31. package/src/dom/is-edge.js +4 -1
  32. package/src/dom/place-caret-at-edge.js +95 -0
  33. package/src/dom/place-caret-at-horizontal-edge.js +2 -83
  34. package/src/dom/place-caret-at-vertical-edge.js +6 -64
  35. package/src/test/dom.js +8 -0
  36. package/tsconfig.tsbuildinfo +1 -1
@@ -1,36 +1,7 @@
1
1
  /**
2
2
  * Internal dependencies
3
3
  */
4
- import { assertIsDefined } from '../utils/assert-is-defined';
5
- /**
6
- * Internal dependencies
7
- */
8
-
9
- import hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';
10
- import isInputOrTextArea from './is-input-or-text-area';
11
- import isRTL from './is-rtl';
12
- /**
13
- * Gets the range to place.
14
- *
15
- * @param {HTMLElement} container Focusable element.
16
- * @param {boolean} isReverse True for end, false for start.
17
- *
18
- * @return {Range|null} The range to place.
19
- */
20
-
21
- function getRange(container, isReverse) {
22
- const {
23
- ownerDocument
24
- } = container; // In the case of RTL scripts, the horizontal edge is at the opposite side.
25
-
26
- const isReverseDir = isRTL(container) ? !isReverse : isReverse;
27
- const containerRect = container.getBoundingClientRect(); // When placing at the end (isReverse), find the closest range to the bottom
28
- // right corner. When placing at the start, to the top left corner.
29
-
30
- const x = isReverse ? containerRect.right - 1 : containerRect.left + 1;
31
- const y = isReverseDir ? containerRect.bottom - 1 : containerRect.top + 1;
32
- return hiddenCaretRangeFromPoint(ownerDocument, x, y, container);
33
- }
4
+ import placeCaretAtEdge from './place-caret-at-edge';
34
5
  /**
35
6
  * Places the caret at start or end of a given element.
36
7
  *
@@ -38,57 +9,7 @@ function getRange(container, isReverse) {
38
9
  * @param {boolean} isReverse True for end, false for start.
39
10
  */
40
11
 
41
-
42
12
  export default function placeCaretAtHorizontalEdge(container, isReverse) {
43
- if (!container) {
44
- return;
45
- }
46
-
47
- container.focus();
48
-
49
- if (isInputOrTextArea(container)) {
50
- // The element may not support selection setting.
51
- if (typeof container.selectionStart !== 'number') {
52
- return;
53
- }
54
-
55
- if (isReverse) {
56
- container.selectionStart = container.value.length;
57
- container.selectionEnd = container.value.length;
58
- } else {
59
- container.selectionStart = 0;
60
- container.selectionEnd = 0;
61
- }
62
-
63
- return;
64
- }
65
-
66
- if (!container.isContentEditable) {
67
- return;
68
- }
69
-
70
- let range = getRange(container, isReverse); // If no range range can be created or it is outside the container, the
71
- // element may be out of view.
72
-
73
- if (!range || !range.startContainer || !container.contains(range.startContainer)) {
74
- container.scrollIntoView(isReverse);
75
- range = getRange(container, isReverse);
76
-
77
- if (!range || !range.startContainer || !container.contains(range.startContainer)) {
78
- return;
79
- }
80
- }
81
-
82
- const {
83
- ownerDocument
84
- } = container;
85
- const {
86
- defaultView
87
- } = ownerDocument;
88
- assertIsDefined(defaultView, 'defaultView');
89
- const selection = defaultView.getSelection();
90
- assertIsDefined(selection, 'selection');
91
- selection.removeAllRanges();
92
- selection.addRange(range);
13
+ return placeCaretAtEdge(container, isReverse, undefined);
93
14
  }
94
15
  //# sourceMappingURL=place-caret-at-horizontal-edge.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/dom/src/dom/place-caret-at-horizontal-edge.js"],"names":["assertIsDefined","hiddenCaretRangeFromPoint","isInputOrTextArea","isRTL","getRange","container","isReverse","ownerDocument","isReverseDir","containerRect","getBoundingClientRect","x","right","left","y","bottom","top","placeCaretAtHorizontalEdge","focus","selectionStart","value","length","selectionEnd","isContentEditable","range","startContainer","contains","scrollIntoView","defaultView","selection","getSelection","removeAllRanges","addRange"],"mappings":"AAAA;AACA;AACA;AACA,SAASA,eAAT,QAAgC,4BAAhC;AAEA;AACA;AACA;;AACA,OAAOC,yBAAP,MAAsC,iCAAtC;AACA,OAAOC,iBAAP,MAA8B,yBAA9B;AACA,OAAOC,KAAP,MAAkB,UAAlB;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,SAASC,QAAT,CAAmBC,SAAnB,EAA8BC,SAA9B,EAA0C;AACzC,QAAM;AAAEC,IAAAA;AAAF,MAAoBF,SAA1B,CADyC,CAEzC;;AACA,QAAMG,YAAY,GAAGL,KAAK,CAAEE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K,CAACC,cADR,IAEA,CAAEpB,SAAS,CAACqB,QAAV,CAAoBF,KAAK,CAACC,cAA1B,CAHH,EAIE;AACDpB,IAAAA,SAAS,CAACsB,cAAV,CAA0BrB,SAA1B;AACAkB,IAAAA,KAAK,GAAGpB,QAAQ,CAAEC,SAAF,EAAaC,SAAb,CAAhB;;AAEA,QACC,CAAEkB,KAAF,IACA,CAAEA,KAAK,CAACC,cADR,IAEA,CAAEpB,SAAS,CAACqB,QAAV,CAAoBF,KAAK,CAACC,cAA1B,CAHH,EAIE;AACD;AACA;AACD;;AAED,QAAM;AAAElB,IAAAA;AAAF,MAAoBF,SAA1B;AACA,QAAM;AAAEuB,IAAAA;AAAF,MAAkBrB,aAAxB;AACAP,EAAAA,eAAe,CAAE4B,WAAF,EAAe,aAAf,CAAf;AACA,QAAMC,SAAS,GAAGD,WAAW,CAACE,YAAZ,EAAlB;AACA9B,EAAAA,eAAe,CAAE6B,SAAF,EAAa,WAAb,CAAf;AACAA,EAAAA,SAAS,CAACE,eAAV;AACAF,EAAAA,SAAS,CAACG,QAAV,CAAoBR,KAApB;AACA","sourcesContent":["/**\n * Internal dependencies\n */\nimport { assertIsDefined } from '../utils/assert-is-defined';\n\n/**\n * Internal dependencies\n */\nimport hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';\nimport isInputOrTextArea from './is-input-or-text-area';\nimport isRTL from './is-rtl';\n\n/**\n * Gets the range to place.\n *\n * @param {HTMLElement} container Focusable element.\n * @param {boolean} isReverse True for end, false for start.\n *\n * @return {Range|null} The range to place.\n */\nfunction getRange( container, isReverse ) {\n\tconst { ownerDocument } = container;\n\t// In the case of RTL scripts, the horizontal edge is at the opposite side.\n\tconst isReverseDir = isRTL( container ) ? ! isReverse : isReverse;\n\tconst containerRect = container.getBoundingClientRect();\n\t// When placing at the end (isReverse), find the closest range to the bottom\n\t// right corner. When placing at the start, to the top left corner.\n\tconst x = isReverse ? containerRect.right - 1 : containerRect.left + 1;\n\tconst y = isReverseDir ? containerRect.bottom - 1 : containerRect.top + 1;\n\treturn hiddenCaretRangeFromPoint( ownerDocument, x, y, container );\n}\n\n/**\n * Places the caret at start or end of a given element.\n *\n * @param {HTMLElement} container Focusable element.\n * @param {boolean} isReverse True for end, false for start.\n */\nexport default function placeCaretAtHorizontalEdge( container, isReverse ) {\n\tif ( ! container ) {\n\t\treturn;\n\t}\n\n\tcontainer.focus();\n\n\tif ( isInputOrTextArea( container ) ) {\n\t\t// The element may not support selection setting.\n\t\tif ( typeof container.selectionStart !== 'number' ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( isReverse ) {\n\t\t\tcontainer.selectionStart = container.value.length;\n\t\t\tcontainer.selectionEnd = container.value.length;\n\t\t} else {\n\t\t\tcontainer.selectionStart = 0;\n\t\t\tcontainer.selectionEnd = 0;\n\t\t}\n\n\t\treturn;\n\t}\n\n\tif ( ! container.isContentEditable ) {\n\t\treturn;\n\t}\n\n\tlet range = getRange( container, isReverse );\n\n\t// If no range range can be created or it is outside the container, the\n\t// element may be out of view.\n\tif (\n\t\t! range ||\n\t\t! range.startContainer ||\n\t\t! container.contains( range.startContainer )\n\t) {\n\t\tcontainer.scrollIntoView( isReverse );\n\t\trange = getRange( container, isReverse );\n\n\t\tif (\n\t\t\t! range ||\n\t\t\t! range.startContainer ||\n\t\t\t! container.contains( range.startContainer )\n\t\t) {\n\t\t\treturn;\n\t\t}\n\t}\n\n\tconst { ownerDocument } = container;\n\tconst { defaultView } = ownerDocument;\n\tassertIsDefined( defaultView, 'defaultView' );\n\tconst selection = defaultView.getSelection();\n\tassertIsDefined( selection, 'selection' );\n\tselection.removeAllRanges();\n\tselection.addRange( range );\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/dom/src/dom/place-caret-at-horizontal-edge.js"],"names":["placeCaretAtEdge","placeCaretAtHorizontalEdge","container","isReverse","undefined"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,gBAAP,MAA6B,uBAA7B;AAEA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASC,0BAAT,CAAqCC,SAArC,EAAgDC,SAAhD,EAA4D;AAC1E,SAAOH,gBAAgB,CAAEE,SAAF,EAAaC,SAAb,EAAwBC,SAAxB,CAAvB;AACA","sourcesContent":["/**\n * Internal dependencies\n */\nimport placeCaretAtEdge from './place-caret-at-edge';\n\n/**\n * Places the caret at start or end of a given element.\n *\n * @param {HTMLElement} container Focusable element.\n * @param {boolean} isReverse True for end, false for start.\n */\nexport default function placeCaretAtHorizontalEdge( container, isReverse ) {\n\treturn placeCaretAtEdge( container, isReverse, undefined );\n}\n"]}
@@ -1,64 +1,16 @@
1
1
  /**
2
2
  * Internal dependencies
3
3
  */
4
- import placeCaretAtHorizontalEdge from './place-caret-at-horizontal-edge';
5
- import hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';
6
- import { assertIsDefined } from '../utils/assert-is-defined';
4
+ import placeCaretAtEdge from './place-caret-at-edge';
7
5
  /**
8
6
  * Places the caret at the top or bottom of a given element.
9
7
  *
10
- * @param {HTMLElement} container Focusable element.
11
- * @param {boolean} isReverse True for bottom, false for top.
12
- * @param {DOMRect} [rect] The rectangle to position the caret with.
13
- * @param {boolean} [mayUseScroll=true] True to allow scrolling, false to disallow.
8
+ * @param {HTMLElement} container Focusable element.
9
+ * @param {boolean} isReverse True for bottom, false for top.
10
+ * @param {DOMRect} [rect] The rectangle to position the caret with.
14
11
  */
15
12
 
16
- export default function placeCaretAtVerticalEdge(container, isReverse, rect, mayUseScroll = true) {
17
- if (!container) {
18
- return;
19
- }
20
-
21
- if (!rect || !container.isContentEditable) {
22
- placeCaretAtHorizontalEdge(container, isReverse);
23
- return;
24
- }
25
-
26
- container.focus(); // Offset by a buffer half the height of the caret rect. This is needed
27
- // because caretRangeFromPoint may default to the end of the selection if
28
- // offset is too close to the edge. It's unclear how to precisely calculate
29
- // this threshold; it may be the padded area of some combination of line
30
- // height, caret height, and font size. The buffer offset is effectively
31
- // equivalent to a point at half the height of a line of text.
32
-
33
- const buffer = rect.height / 2;
34
- const editableRect = container.getBoundingClientRect();
35
- const x = rect.left;
36
- const y = isReverse ? editableRect.bottom - buffer : editableRect.top + buffer;
37
- const {
38
- ownerDocument
39
- } = container;
40
- const {
41
- defaultView
42
- } = ownerDocument;
43
- const range = hiddenCaretRangeFromPoint(ownerDocument, x, y, container);
44
-
45
- if (!range || !container.contains(range.startContainer)) {
46
- if (mayUseScroll && (!range || !range.startContainer || !range.startContainer.contains(container))) {
47
- // Might be out of view.
48
- // Easier than attempting to calculate manually.
49
- container.scrollIntoView(isReverse);
50
- placeCaretAtVerticalEdge(container, isReverse, rect, false);
51
- return;
52
- }
53
-
54
- placeCaretAtHorizontalEdge(container, isReverse);
55
- return;
56
- }
57
-
58
- assertIsDefined(defaultView, 'defaultView');
59
- const selection = defaultView.getSelection();
60
- assertIsDefined(selection, 'selection');
61
- selection.removeAllRanges();
62
- selection.addRange(range);
13
+ export default function placeCaretAtVerticalEdge(container, isReverse, rect) {
14
+ return placeCaretAtEdge(container, isReverse, rect === null || rect === void 0 ? void 0 : rect.left);
63
15
  }
64
16
  //# sourceMappingURL=place-caret-at-vertical-edge.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/dom/src/dom/place-caret-at-vertical-edge.js"],"names":["placeCaretAtHorizontalEdge","hiddenCaretRangeFromPoint","assertIsDefined","placeCaretAtVerticalEdge","container","isReverse","rect","mayUseScroll","isContentEditable","focus","buffer","height","editableRect","getBoundingClientRect","x","left","y","bottom","top","ownerDocument","defaultView","range","contains","startContainer","scrollIntoView","selection","getSelection","removeAllRanges","addRange"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,0BAAP,MAAuC,kCAAvC;AACA,OAAOC,yBAAP,MAAsC,iCAAtC;AACA,SAASC,eAAT,QAAgC,4BAAhC;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASC,wBAAT,CACdC,SADc,EAEdC,SAFc,EAGdC,IAHc,EAIdC,YAAY,GAAG,IAJ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,CAAf;AACA,QAAMK,SAAS,GAAGL,WAAW,CAACM,YAAZ,EAAlB;AACAxB,EAAAA,eAAe,CAAEuB,SAAF,EAAa,WAAb,CAAf;AACAA,EAAAA,SAAS,CAACE,eAAV;AACAF,EAAAA,SAAS,CAACG,QAAV,CAAoBP,KAApB;AACA","sourcesContent":["/**\n * Internal dependencies\n */\nimport placeCaretAtHorizontalEdge from './place-caret-at-horizontal-edge';\nimport hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';\nimport { assertIsDefined } from '../utils/assert-is-defined';\n\n/**\n * Places the caret at the top or bottom of a given element.\n *\n * @param {HTMLElement} container Focusable element.\n * @param {boolean} isReverse True for bottom, false for top.\n * @param {DOMRect} [rect] The rectangle to position the caret with.\n * @param {boolean} [mayUseScroll=true] True to allow scrolling, false to disallow.\n */\nexport default function placeCaretAtVerticalEdge(\n\tcontainer,\n\tisReverse,\n\trect,\n\tmayUseScroll = true\n) {\n\tif ( ! container ) {\n\t\treturn;\n\t}\n\n\tif ( ! rect || ! container.isContentEditable ) {\n\t\tplaceCaretAtHorizontalEdge( container, isReverse );\n\t\treturn;\n\t}\n\n\tcontainer.focus();\n\n\t// Offset by a buffer half the height of the caret rect. This is needed\n\t// because caretRangeFromPoint may default to the end of the selection if\n\t// offset is too close to the edge. It's unclear how to precisely calculate\n\t// this threshold; it may be the padded area of some combination of line\n\t// height, caret height, and font size. The buffer offset is effectively\n\t// equivalent to a point at half the height of a line of text.\n\tconst buffer = rect.height / 2;\n\tconst editableRect = container.getBoundingClientRect();\n\tconst x = rect.left;\n\tconst y = isReverse\n\t\t? editableRect.bottom - buffer\n\t\t: editableRect.top + buffer;\n\n\tconst { ownerDocument } = container;\n\tconst { defaultView } = ownerDocument;\n\tconst range = hiddenCaretRangeFromPoint( ownerDocument, x, y, container );\n\n\tif ( ! range || ! container.contains( range.startContainer ) ) {\n\t\tif (\n\t\t\tmayUseScroll &&\n\t\t\t( ! range ||\n\t\t\t\t! range.startContainer ||\n\t\t\t\t! range.startContainer.contains( container ) )\n\t\t) {\n\t\t\t// Might be out of view.\n\t\t\t// Easier than attempting to calculate manually.\n\t\t\tcontainer.scrollIntoView( isReverse );\n\t\t\tplaceCaretAtVerticalEdge( container, isReverse, rect, false );\n\t\t\treturn;\n\t\t}\n\n\t\tplaceCaretAtHorizontalEdge( container, isReverse );\n\t\treturn;\n\t}\n\n\tassertIsDefined( defaultView, 'defaultView' );\n\tconst selection = defaultView.getSelection();\n\tassertIsDefined( selection, 'selection' );\n\tselection.removeAllRanges();\n\tselection.addRange( range );\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/dom/src/dom/place-caret-at-vertical-edge.js"],"names":["placeCaretAtEdge","placeCaretAtVerticalEdge","container","isReverse","rect","left"],"mappings":"AAAA;AACA;AACA;AACA,OAAOA,gBAAP,MAA6B,uBAA7B;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;;AACA,eAAe,SAASC,wBAAT,CAAmCC,SAAnC,EAA8CC,SAA9C,EAAyDC,IAAzD,EAAgE;AAC9E,SAAOJ,gBAAgB,CAAEE,SAAF,EAAaC,SAAb,EAAwBC,IAAxB,aAAwBA,IAAxB,uBAAwBA,IAAI,CAAEC,IAA9B,CAAvB;AACA","sourcesContent":["/**\n * Internal dependencies\n */\nimport placeCaretAtEdge from './place-caret-at-edge';\n\n/**\n * Places the caret at the top or bottom of a given element.\n *\n * @param {HTMLElement} container Focusable element.\n * @param {boolean} isReverse True for bottom, false for top.\n * @param {DOMRect} [rect] The rectangle to position the caret with.\n */\nexport default function placeCaretAtVerticalEdge( container, isReverse, rect ) {\n\treturn placeCaretAtEdge( container, isReverse, rect?.left );\n}\n"]}
@@ -5,8 +5,8 @@
5
5
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/caretRangeFromPoint
6
6
  *
7
7
  * @param {DocumentMaybeWithCaretPositionFromPoint} doc The document of the range.
8
- * @param {number} x Horizontal position within the current viewport.
9
- * @param {number} y Vertical position within the current viewport.
8
+ * @param {number} x Horizontal position within the current viewport.
9
+ * @param {number} y Vertical position within the current viewport.
10
10
  *
11
11
  * @return {Range | null} The best range for the given point.
12
12
  */
@@ -1 +1 @@
1
- {"version":3,"file":"is-edge.d.ts","sourceRoot":"","sources":["../../src/dom/is-edge.js"],"names":[],"mappings":"AAWA;;;;;;;;;;GAUG;AACH,0CANW,OAAO,aACP,OAAO,uCAGN,OAAO,CAyGlB"}
1
+ {"version":3,"file":"is-edge.d.ts","sourceRoot":"","sources":["../../src/dom/is-edge.js"],"names":[],"mappings":"AAWA;;;;;;;;;;GAUG;AACH,0CANW,OAAO,aACP,OAAO,uCAGN,OAAO,CA4GlB"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Places the caret at start or end of a given element.
3
+ *
4
+ * @param {HTMLElement} container Focusable element.
5
+ * @param {boolean} isReverse True for end, false for start.
6
+ * @param {number|undefined} x X coordinate to vertically position.
7
+ */
8
+ export default function placeCaretAtEdge(container: HTMLElement, isReverse: boolean, x: number | undefined): void;
9
+ //# sourceMappingURL=place-caret-at-edge.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"place-caret-at-edge.d.ts","sourceRoot":"","sources":["../../src/dom/place-caret-at-edge.js"],"names":[],"mappings":"AA+BA;;;;;;GAMG;AACH,oDAJW,WAAW,aACX,OAAO,KACP,MAAM,GAAC,SAAS,QA0D1B"}
@@ -1 +1 @@
1
- {"version":3,"file":"place-caret-at-horizontal-edge.d.ts","sourceRoot":"","sources":["../../src/dom/place-caret-at-horizontal-edge.js"],"names":[],"mappings":"AAgCA;;;;;GAKG;AACH,8DAHW,WAAW,aACX,OAAO,QA0DjB"}
1
+ {"version":3,"file":"place-caret-at-horizontal-edge.d.ts","sourceRoot":"","sources":["../../src/dom/place-caret-at-horizontal-edge.js"],"names":[],"mappings":"AAKA;;;;;GAKG;AACH,8DAHW,WAAW,aACX,OAAO,QAIjB"}
@@ -1,10 +1,9 @@
1
1
  /**
2
2
  * Places the caret at the top or bottom of a given element.
3
3
  *
4
- * @param {HTMLElement} container Focusable element.
5
- * @param {boolean} isReverse True for bottom, false for top.
6
- * @param {DOMRect} [rect] The rectangle to position the caret with.
7
- * @param {boolean} [mayUseScroll=true] True to allow scrolling, false to disallow.
4
+ * @param {HTMLElement} container Focusable element.
5
+ * @param {boolean} isReverse True for bottom, false for top.
6
+ * @param {DOMRect} [rect] The rectangle to position the caret with.
8
7
  */
9
- export default function placeCaretAtVerticalEdge(container: HTMLElement, isReverse: boolean, rect?: DOMRect | undefined, mayUseScroll?: boolean | undefined): void;
8
+ export default function placeCaretAtVerticalEdge(container: HTMLElement, isReverse: boolean, rect?: DOMRect | undefined): void;
10
9
  //# sourceMappingURL=place-caret-at-vertical-edge.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"place-caret-at-vertical-edge.d.ts","sourceRoot":"","sources":["../../src/dom/place-caret-at-vertical-edge.js"],"names":[],"mappings":"AAOA;;;;;;;GAOG;AACH,4DALW,WAAW,aACX,OAAO,wEA6DjB"}
1
+ {"version":3,"file":"place-caret-at-vertical-edge.d.ts","sourceRoot":"","sources":["../../src/dom/place-caret-at-vertical-edge.js"],"names":[],"mappings":"AAKA;;;;;;GAMG;AACH,4DAJW,WAAW,aACX,OAAO,oCAKjB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/dom",
3
- "version": "3.2.3",
3
+ "version": "3.2.4",
4
4
  "description": "DOM utilities module for WordPress.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -34,5 +34,5 @@
34
34
  "publishConfig": {
35
35
  "access": "public"
36
36
  },
37
- "gitHead": "98c42a7187f788fe3e023f04df7f5dcbdae4e4e7"
37
+ "gitHead": "8f7f052bc04e3f4eb50f479ced14be1489b9fa79"
38
38
  }
@@ -5,8 +5,8 @@
5
5
  * @see https://developer.mozilla.org/en-US/docs/Web/API/Document/caretRangeFromPoint
6
6
  *
7
7
  * @param {DocumentMaybeWithCaretPositionFromPoint} doc The document of the range.
8
- * @param {number} x Horizontal position within the current viewport.
9
- * @param {number} y Vertical position within the current viewport.
8
+ * @param {number} x Horizontal position within the current viewport.
9
+ * @param {number} y Vertical position within the current viewport.
10
10
  *
11
11
  * @return {Range | null} The best range for the given point.
12
12
  */
@@ -21,7 +21,10 @@ import isInputOrTextArea from './is-input-or-text-area';
21
21
  * @return {boolean} True if at the edge, false if not.
22
22
  */
23
23
  export default function isEdge( container, isReverse, onlyVertical = false ) {
24
- if ( isInputOrTextArea( container ) ) {
24
+ if (
25
+ isInputOrTextArea( container ) &&
26
+ typeof container.selectionStart === 'number'
27
+ ) {
25
28
  if ( container.selectionStart !== container.selectionEnd ) {
26
29
  return false;
27
30
  }
@@ -0,0 +1,95 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';
5
+ import { assertIsDefined } from '../utils/assert-is-defined';
6
+ import isInputOrTextArea from './is-input-or-text-area';
7
+ import isRTL from './is-rtl';
8
+
9
+ /**
10
+ * Gets the range to place.
11
+ *
12
+ * @param {HTMLElement} container Focusable element.
13
+ * @param {boolean} isReverse True for end, false for start.
14
+ * @param {number|undefined} x X coordinate to vertically position.
15
+ *
16
+ * @return {Range|null} The range to place.
17
+ */
18
+ function getRange( container, isReverse, x ) {
19
+ const { ownerDocument } = container;
20
+ // In the case of RTL scripts, the horizontal edge is at the opposite side.
21
+ const isReverseDir = isRTL( container ) ? ! isReverse : isReverse;
22
+ const containerRect = container.getBoundingClientRect();
23
+ // When placing at the end (isReverse), find the closest range to the bottom
24
+ // right corner. When placing at the start, to the top left corner.
25
+ if ( x === undefined ) {
26
+ x = isReverse ? containerRect.right - 1 : containerRect.left + 1;
27
+ }
28
+ const y = isReverseDir ? containerRect.bottom - 1 : containerRect.top + 1;
29
+ return hiddenCaretRangeFromPoint( ownerDocument, x, y, container );
30
+ }
31
+
32
+ /**
33
+ * Places the caret at start or end of a given element.
34
+ *
35
+ * @param {HTMLElement} container Focusable element.
36
+ * @param {boolean} isReverse True for end, false for start.
37
+ * @param {number|undefined} x X coordinate to vertically position.
38
+ */
39
+ export default function placeCaretAtEdge( container, isReverse, x ) {
40
+ if ( ! container ) {
41
+ return;
42
+ }
43
+
44
+ container.focus();
45
+
46
+ if ( isInputOrTextArea( container ) ) {
47
+ // The element may not support selection setting.
48
+ if ( typeof container.selectionStart !== 'number' ) {
49
+ return;
50
+ }
51
+
52
+ if ( isReverse ) {
53
+ container.selectionStart = container.value.length;
54
+ container.selectionEnd = container.value.length;
55
+ } else {
56
+ container.selectionStart = 0;
57
+ container.selectionEnd = 0;
58
+ }
59
+
60
+ return;
61
+ }
62
+
63
+ if ( ! container.isContentEditable ) {
64
+ return;
65
+ }
66
+
67
+ let range = getRange( container, isReverse, x );
68
+
69
+ // If no range range can be created or it is outside the container, the
70
+ // element may be out of view.
71
+ if (
72
+ ! range ||
73
+ ! range.startContainer ||
74
+ ! container.contains( range.startContainer )
75
+ ) {
76
+ container.scrollIntoView( isReverse );
77
+ range = range = getRange( container, isReverse, x );
78
+
79
+ if (
80
+ ! range ||
81
+ ! range.startContainer ||
82
+ ! container.contains( range.startContainer )
83
+ ) {
84
+ return;
85
+ }
86
+ }
87
+
88
+ const { ownerDocument } = container;
89
+ const { defaultView } = ownerDocument;
90
+ assertIsDefined( defaultView, 'defaultView' );
91
+ const selection = defaultView.getSelection();
92
+ assertIsDefined( selection, 'selection' );
93
+ selection.removeAllRanges();
94
+ selection.addRange( range );
95
+ }
@@ -1,34 +1,7 @@
1
1
  /**
2
2
  * Internal dependencies
3
3
  */
4
- import { assertIsDefined } from '../utils/assert-is-defined';
5
-
6
- /**
7
- * Internal dependencies
8
- */
9
- import hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';
10
- import isInputOrTextArea from './is-input-or-text-area';
11
- import isRTL from './is-rtl';
12
-
13
- /**
14
- * Gets the range to place.
15
- *
16
- * @param {HTMLElement} container Focusable element.
17
- * @param {boolean} isReverse True for end, false for start.
18
- *
19
- * @return {Range|null} The range to place.
20
- */
21
- function getRange( container, isReverse ) {
22
- const { ownerDocument } = container;
23
- // In the case of RTL scripts, the horizontal edge is at the opposite side.
24
- const isReverseDir = isRTL( container ) ? ! isReverse : isReverse;
25
- const containerRect = container.getBoundingClientRect();
26
- // When placing at the end (isReverse), find the closest range to the bottom
27
- // right corner. When placing at the start, to the top left corner.
28
- const x = isReverse ? containerRect.right - 1 : containerRect.left + 1;
29
- const y = isReverseDir ? containerRect.bottom - 1 : containerRect.top + 1;
30
- return hiddenCaretRangeFromPoint( ownerDocument, x, y, container );
31
- }
4
+ import placeCaretAtEdge from './place-caret-at-edge';
32
5
 
33
6
  /**
34
7
  * Places the caret at start or end of a given element.
@@ -37,59 +10,5 @@ function getRange( container, isReverse ) {
37
10
  * @param {boolean} isReverse True for end, false for start.
38
11
  */
39
12
  export default function placeCaretAtHorizontalEdge( container, isReverse ) {
40
- if ( ! container ) {
41
- return;
42
- }
43
-
44
- container.focus();
45
-
46
- if ( isInputOrTextArea( container ) ) {
47
- // The element may not support selection setting.
48
- if ( typeof container.selectionStart !== 'number' ) {
49
- return;
50
- }
51
-
52
- if ( isReverse ) {
53
- container.selectionStart = container.value.length;
54
- container.selectionEnd = container.value.length;
55
- } else {
56
- container.selectionStart = 0;
57
- container.selectionEnd = 0;
58
- }
59
-
60
- return;
61
- }
62
-
63
- if ( ! container.isContentEditable ) {
64
- return;
65
- }
66
-
67
- let range = getRange( container, isReverse );
68
-
69
- // If no range range can be created or it is outside the container, the
70
- // element may be out of view.
71
- if (
72
- ! range ||
73
- ! range.startContainer ||
74
- ! container.contains( range.startContainer )
75
- ) {
76
- container.scrollIntoView( isReverse );
77
- range = getRange( container, isReverse );
78
-
79
- if (
80
- ! range ||
81
- ! range.startContainer ||
82
- ! container.contains( range.startContainer )
83
- ) {
84
- return;
85
- }
86
- }
87
-
88
- const { ownerDocument } = container;
89
- const { defaultView } = ownerDocument;
90
- assertIsDefined( defaultView, 'defaultView' );
91
- const selection = defaultView.getSelection();
92
- assertIsDefined( selection, 'selection' );
93
- selection.removeAllRanges();
94
- selection.addRange( range );
13
+ return placeCaretAtEdge( container, isReverse, undefined );
95
14
  }
@@ -1,73 +1,15 @@
1
1
  /**
2
2
  * Internal dependencies
3
3
  */
4
- import placeCaretAtHorizontalEdge from './place-caret-at-horizontal-edge';
5
- import hiddenCaretRangeFromPoint from './hidden-caret-range-from-point';
6
- import { assertIsDefined } from '../utils/assert-is-defined';
4
+ import placeCaretAtEdge from './place-caret-at-edge';
7
5
 
8
6
  /**
9
7
  * Places the caret at the top or bottom of a given element.
10
8
  *
11
- * @param {HTMLElement} container Focusable element.
12
- * @param {boolean} isReverse True for bottom, false for top.
13
- * @param {DOMRect} [rect] The rectangle to position the caret with.
14
- * @param {boolean} [mayUseScroll=true] True to allow scrolling, false to disallow.
9
+ * @param {HTMLElement} container Focusable element.
10
+ * @param {boolean} isReverse True for bottom, false for top.
11
+ * @param {DOMRect} [rect] The rectangle to position the caret with.
15
12
  */
16
- export default function placeCaretAtVerticalEdge(
17
- container,
18
- isReverse,
19
- rect,
20
- mayUseScroll = true
21
- ) {
22
- if ( ! container ) {
23
- return;
24
- }
25
-
26
- if ( ! rect || ! container.isContentEditable ) {
27
- placeCaretAtHorizontalEdge( container, isReverse );
28
- return;
29
- }
30
-
31
- container.focus();
32
-
33
- // Offset by a buffer half the height of the caret rect. This is needed
34
- // because caretRangeFromPoint may default to the end of the selection if
35
- // offset is too close to the edge. It's unclear how to precisely calculate
36
- // this threshold; it may be the padded area of some combination of line
37
- // height, caret height, and font size. The buffer offset is effectively
38
- // equivalent to a point at half the height of a line of text.
39
- const buffer = rect.height / 2;
40
- const editableRect = container.getBoundingClientRect();
41
- const x = rect.left;
42
- const y = isReverse
43
- ? editableRect.bottom - buffer
44
- : editableRect.top + buffer;
45
-
46
- const { ownerDocument } = container;
47
- const { defaultView } = ownerDocument;
48
- const range = hiddenCaretRangeFromPoint( ownerDocument, x, y, container );
49
-
50
- if ( ! range || ! container.contains( range.startContainer ) ) {
51
- if (
52
- mayUseScroll &&
53
- ( ! range ||
54
- ! range.startContainer ||
55
- ! range.startContainer.contains( container ) )
56
- ) {
57
- // Might be out of view.
58
- // Easier than attempting to calculate manually.
59
- container.scrollIntoView( isReverse );
60
- placeCaretAtVerticalEdge( container, isReverse, rect, false );
61
- return;
62
- }
63
-
64
- placeCaretAtHorizontalEdge( container, isReverse );
65
- return;
66
- }
67
-
68
- assertIsDefined( defaultView, 'defaultView' );
69
- const selection = defaultView.getSelection();
70
- assertIsDefined( selection, 'selection' );
71
- selection.removeAllRanges();
72
- selection.addRange( range );
13
+ export default function placeCaretAtVerticalEdge( container, isReverse, rect ) {
14
+ return placeCaretAtEdge( container, isReverse, rect?.left );
73
15
  }
package/src/test/dom.js CHANGED
@@ -84,6 +84,14 @@ describe( 'DOM', () => {
84
84
  expect( isHorizontalEdge( div, true ) ).toBe( true );
85
85
  expect( isHorizontalEdge( div, false ) ).toBe( true );
86
86
  } );
87
+
88
+ it( 'should return true for input types that do not have selection ranges', () => {
89
+ const input = document.createElement( 'input' );
90
+ input.setAttribute( 'type', 'checkbox' );
91
+ parent.appendChild( input );
92
+ expect( isHorizontalEdge( input, true ) ).toBe( true );
93
+ expect( isHorizontalEdge( input, false ) ).toBe( true );
94
+ } );
87
95
  } );
88
96
 
89
97
  describe( 'placeCaretAtHorizontalEdge', () => {