-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathdom.js
More file actions
62 lines (58 loc) · 1.94 KB
/
dom.js
File metadata and controls
62 lines (58 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* Given a block client ID, returns the corresponding DOM node for the block,
* if exists. As much as possible, this helper should be avoided, and used only
* in cases where isolated behaviors need remote access to a block node.
*
* @param {string} clientId Block client ID.
*
* @return {Element} Block DOM node.
*/
export function getBlockDOMNode( clientId ) {
return document.querySelector( '[data-block="' + clientId + '"]' );
}
/**
* Given a block client ID, returns the corresponding DOM node for the block
* focusable wrapper, if exists. As much as possible, this helper should be
* avoided, and used only in cases where isolated behaviors need remote access
* to a block node.
*
* @param {string} clientId Block client ID.
*
* @return {Element} Block DOM node.
*/
export function getBlockFocusableWrapper( clientId ) {
return getBlockDOMNode( clientId ).closest( '.editor-block-list__block' );
}
/**
* Returns true if the given HTMLElement is a block focus stop. Blocks without
* their own text fields rely on the focus stop to be keyboard navigable.
*
* @param {HTMLElement} element Element to test.
*
* @return {boolean} Whether element is a block focus stop.
*/
export function isBlockFocusStop( element ) {
return element.classList.contains( 'editor-block-list__block' );
}
/**
* Returns true if two elements are contained within the same block.
*
* @param {HTMLElement} a First element.
* @param {HTMLElement} b Second element.
*
* @return {boolean} Whether elements are in the same block.
*/
export function isInSameBlock( a, b ) {
return a.closest( '[data-block]' ) === b.closest( '[data-block]' );
}
/**
* Returns true if the given HTMLElement contains inner blocks (an InnerBlocks
* element).
*
* @param {HTMLElement} element Element to test.
*
* @return {boolean} Whether element contains inner blocks.
*/
export function hasInnerBlocksContext( element ) {
return !! element.querySelector( '.editor-block-list__layout' );
}