-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathparser.js
127 lines (109 loc) · 3.1 KB
/
parser.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* External dependencies
*/
import { expect } from 'chai';
import { text } from 'hpq';
/**
* Internal dependencies
*/
import { default as parse, getBlockAttributes } from '../parser';
import { getBlocks, unregisterBlock, setUnknownTypeHandler, registerBlock } from '../registration';
describe( 'block parser', () => {
afterEach( () => {
setUnknownTypeHandler( undefined );
getBlocks().forEach( ( block ) => {
unregisterBlock( block.slug );
} );
} );
describe( 'getBlockAttributes()', () => {
it( 'should merge attributes from function implementation', () => {
const blockSettings = {
attributes: function( rawContent ) {
return {
content: rawContent + ' & Chicken'
};
}
};
const blockNode = {
blockType: 'core/test-block',
attrs: {
align: 'left'
},
rawContent: 'Ribs'
};
expect( getBlockAttributes( blockNode, blockSettings ) ).to.eql( {
align: 'left',
content: 'Ribs & Chicken'
} );
} );
it( 'should merge attributes from query object implementation', () => {
const blockSettings = {
attributes: {
emphasis: text( 'strong' )
}
};
const blockNode = {
blockType: 'core/test-block',
attrs: {
align: 'left'
},
rawContent: '<span>Ribs <strong>& Chicken</strong></span>'
};
expect( getBlockAttributes( blockNode, blockSettings ) ).to.eql( {
align: 'left',
emphasis: '& Chicken'
} );
} );
it( 'should return parsed attributes for block without attributes defined', () => {
const blockSettings = {};
const blockNode = {
blockType: 'core/test-block',
attrs: {
align: 'left'
},
rawContent: '<span>Ribs <strong>& Chicken</strong></span>'
};
expect( getBlockAttributes( blockNode, blockSettings ) ).to.eql( {
align: 'left'
} );
} );
} );
describe( 'parse()', () => {
it( 'should parse the post content, ignoring unknown blocks', () => {
registerBlock( 'core/test-block', {
attributes: function( rawContent ) {
return {
content: rawContent + ' & Chicken'
};
}
} );
const parsed = parse(
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<p>Broccoli</p>' +
'<!-- wp:core/unknown-block -->Ribs<!-- /wp:core/unknown-block -->'
);
expect( parsed ).to.have.lengthOf( 1 );
expect( parsed[ 0 ].blockType ).to.equal( 'core/test-block' );
expect( parsed[ 0 ].attributes ).to.eql( {
content: 'Ribs & Chicken'
} );
expect( parsed[ 0 ].uid ).to.be.a( 'string' );
} );
it( 'should parse the post content, using unknown block handler', () => {
registerBlock( 'core/test-block', {} );
registerBlock( 'core/unknown-block', {} );
setUnknownTypeHandler( 'core/unknown-block' );
const parsed = parse(
'<!-- wp:core/test-block -->Ribs<!-- /wp:core/test-block -->' +
'<p>Broccoli</p>' +
'<!-- wp:core/unknown-block -->Ribs<!-- /wp:core/unknown-block -->'
);
expect( parsed ).to.have.lengthOf( 3 );
expect( parsed.map( ( { blockType } ) => blockType ) ).to.eql( [
'core/test-block',
'core/unknown-block',
'core/unknown-block'
] );
} );
} );
} );