-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
source.js
63 lines (55 loc) · 1.51 KB
/
source.js
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
63
/**
* WordPress dependencies
*/
import { createElement } from '@wordpress/element';
/**
* External dependencies
*/
import { nodeListToReact, nodeToReact } from 'dom-react';
import { flow } from 'lodash';
import {
attr as originalAttr,
prop as originalProp,
html as originalHtml,
text as originalText,
query as originalQuery,
} from 'hpq';
/**
* Given a source function creator, returns a new function which applies an
* internal flag to the created source.
*
* @param {Function} fn Original source function creator
* @return {Function} Modified source function creator
*/
function withKnownSourceFlag( fn ) {
return flow( fn, ( source ) => {
source._wpBlocksKnownSource = true;
return source;
} );
}
export const attr = withKnownSourceFlag( originalAttr );
export const prop = withKnownSourceFlag( originalProp );
export const html = withKnownSourceFlag( originalHtml );
export const text = withKnownSourceFlag( originalText );
export const query = withKnownSourceFlag( originalQuery );
export const children = withKnownSourceFlag( ( selector ) => {
return ( domNode ) => {
let match = domNode;
if ( selector ) {
match = domNode.querySelector( selector );
}
if ( match ) {
return nodeListToReact( match.childNodes || [], createElement );
}
return [];
};
} );
export const node = withKnownSourceFlag( ( selector ) => {
return ( domNode ) => {
let match = domNode;
if ( selector ) {
match = domNode.querySelector( selector );
}
return nodeToReact( match, createElement );
};
} );