@wordpress/dom 3.2.2-next.253d9b6e21.0 → 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 (46) hide show
  1. package/README.md +0 -1
  2. package/build/dom/caret-range-from-point.js +7 -3
  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/phrasing-content.js +1 -1
  13. package/build/phrasing-content.js.map +1 -1
  14. package/build-module/dom/caret-range-from-point.js +7 -3
  15. package/build-module/dom/caret-range-from-point.js.map +1 -1
  16. package/build-module/dom/is-edge.js +1 -1
  17. package/build-module/dom/is-edge.js.map +1 -1
  18. package/build-module/dom/place-caret-at-edge.js +95 -0
  19. package/build-module/dom/place-caret-at-edge.js.map +1 -0
  20. package/build-module/dom/place-caret-at-horizontal-edge.js +2 -81
  21. package/build-module/dom/place-caret-at-horizontal-edge.js.map +1 -1
  22. package/build-module/dom/place-caret-at-vertical-edge.js +6 -54
  23. package/build-module/dom/place-caret-at-vertical-edge.js.map +1 -1
  24. package/build-module/phrasing-content.js +1 -1
  25. package/build-module/phrasing-content.js.map +1 -1
  26. package/build-types/dom/caret-range-from-point.d.ts +12 -4
  27. package/build-types/dom/caret-range-from-point.d.ts.map +1 -1
  28. package/build-types/dom/is-edge.d.ts.map +1 -1
  29. package/build-types/dom/place-caret-at-edge.d.ts +9 -0
  30. package/build-types/dom/place-caret-at-edge.d.ts.map +1 -0
  31. package/build-types/dom/place-caret-at-horizontal-edge.d.ts.map +1 -1
  32. package/build-types/dom/place-caret-at-vertical-edge.d.ts +4 -5
  33. package/build-types/dom/place-caret-at-vertical-edge.d.ts.map +1 -1
  34. package/build-types/phrasing-content.d.ts +2 -4
  35. package/build-types/phrasing-content.d.ts.map +1 -1
  36. package/build-types/tabbable.d.ts +3 -3
  37. package/build-types/tabbable.d.ts.map +1 -1
  38. package/package.json +2 -2
  39. package/src/dom/caret-range-from-point.js +8 -3
  40. package/src/dom/is-edge.js +4 -1
  41. package/src/dom/place-caret-at-edge.js +95 -0
  42. package/src/dom/place-caret-at-horizontal-edge.js +2 -83
  43. package/src/dom/place-caret-at-vertical-edge.js +6 -64
  44. package/src/phrasing-content.js +1 -1
  45. package/src/test/dom.js +8 -0
  46. package/tsconfig.tsbuildinfo +1 -1170
@@ -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
  }
@@ -32,7 +32,7 @@ const textContentSchema = {
32
32
  s: {},
33
33
  del: {},
34
34
  ins: {},
35
- a: { attributes: [ 'href', 'target', 'rel' ] },
35
+ a: { attributes: [ 'href', 'target', 'rel', 'id' ] },
36
36
  code: {},
37
37
  abbr: { attributes: [ 'title' ] },
38
38
  sub: {},
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', () => {