Debian Codegraph @

extensions/VisualEditor/lib/ve/tests/dm/ve.dm.ModelRegistry.test.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel ModelRegistry tests.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 QUnit.module( 've.dm.ModelRegistry' );
8
9 /* Stubs */
10 ve.dm.example.checkForPickMe = function ( node ) {
11 return node.hasAttribute && node.hasAttribute( 'pickme' );
12 };
13
14 /* Nothing set */
15 ve.dm.example.StubNothingSetAnnotation = function VeDmStubNothingSetAnnotation() {
16 ve.dm.example.StubNothingSetAnnotation.super.apply( this, arguments );
17 };
18 OO.inheritClass( ve.dm.example.StubNothingSetAnnotation, ve.dm.Annotation );
19 ve.dm.example.StubNothingSetAnnotation.static.name = 'stubnothingset';
20
21 /* Single tag */
22 ve.dm.example.StubSingleTagAnnotation = function VeDmStubSingleTagAnnotation() {
23 ve.dm.example.StubSingleTagAnnotation.super.apply( this, arguments );
24 };
25 OO.inheritClass( ve.dm.example.StubSingleTagAnnotation, ve.dm.Annotation );
26 ve.dm.example.StubSingleTagAnnotation.static.name = 'stubsingletag';
27 ve.dm.example.StubSingleTagAnnotation.static.matchTagNames = [ 'a' ];
28
29 /* Single type with any allowed */
30 ve.dm.example.StubSingleTypeWithAnyAllowedAnnotation = function VeDmStubSingleTypeWithAnyAllowedAnnotation() {
31 ve.dm.example.StubSingleTypeWithAnyAllowedAnnotation.super.apply( this, arguments );
32 };
33 OO.inheritClass( ve.dm.example.StubSingleTypeWithAnyAllowedAnnotation, ve.dm.Annotation );
34 ve.dm.example.StubSingleTypeWithAnyAllowedAnnotation.static.name = 'stubsingletypewithanyallowed';
35 ve.dm.example.StubSingleTypeWithAnyAllowedAnnotation.static.matchRdfaTypes = [ 'ext:foo' ];
36 ve.dm.example.StubSingleTypeWithAnyAllowedAnnotation.static.allowedRdfaTypes = null;
37
38 /* Single type with single allowed */
39 ve.dm.example.StubSingleTypeWithAllowedAnnotation = function VeDmStubSingleTypeWithAllowedAnnotation() {
40 ve.dm.example.StubSingleTypeWithAllowedAnnotation.super.apply( this, arguments );
41 };
42 OO.inheritClass( ve.dm.example.StubSingleTypeWithAllowedAnnotation, ve.dm.Annotation );
43 ve.dm.example.StubSingleTypeWithAllowedAnnotation.static.name = 'stubsingletypewithallowed';
44 ve.dm.example.StubSingleTypeWithAllowedAnnotation.static.matchRdfaTypes = [ 'ext:foo' ];
45 ve.dm.example.StubSingleTypeWithAllowedAnnotation.static.allowedRdfaTypes = [ 'bar' ];
46
47 /* Single type */
48 ve.dm.example.StubSingleTypeAnnotation = function VeDmStubSingleTypeAnnotation() {
49 ve.dm.example.StubSingleTypeAnnotation.super.apply( this, arguments );
50 };
51 OO.inheritClass( ve.dm.example.StubSingleTypeAnnotation, ve.dm.Annotation );
52 ve.dm.example.StubSingleTypeAnnotation.static.name = 'stubsingletype';
53 ve.dm.example.StubSingleTypeAnnotation.static.matchRdfaTypes = [ 'ext:foo' ];
54
55 /* Single tag and type */
56 ve.dm.example.StubSingleTagAndTypeAnnotation = function VeDmStubSingleTagAndTypeAnnotation() {
57 ve.dm.example.StubSingleTagAndTypeAnnotation.super.apply( this, arguments );
58 };
59 OO.inheritClass( ve.dm.example.StubSingleTagAndTypeAnnotation, ve.dm.Annotation );
60 ve.dm.example.StubSingleTagAndTypeAnnotation.static.name = 'stubsingletagandtype';
61 ve.dm.example.StubSingleTagAndTypeAnnotation.static.matchTagNames = [ 'a' ];
62 ve.dm.example.StubSingleTagAndTypeAnnotation.static.matchRdfaTypes = [ 'ext:foo' ];
63
64 /* Function */
65 ve.dm.example.StubFuncAnnotation = function VeDmStubFuncAnnotation() {
66 ve.dm.example.StubFuncAnnotation.super.apply( this, arguments );
67 };
68 OO.inheritClass( ve.dm.example.StubFuncAnnotation, ve.dm.Annotation );
69 ve.dm.example.StubFuncAnnotation.static.name = 'stubfunc';
70 ve.dm.example.StubFuncAnnotation.static.matchFunction = ve.dm.example.checkForPickMe;
71
72 /* Tag and function */
73 ve.dm.example.StubSingleTagAndFuncAnnotation = function VeDmStubSingleTagAndFuncAnnotation() {
74 ve.dm.example.StubSingleTagAndFuncAnnotation.super.apply( this, arguments );
75 };
76 OO.inheritClass( ve.dm.example.StubSingleTagAndFuncAnnotation, ve.dm.Annotation );
77 ve.dm.example.StubSingleTagAndFuncAnnotation.static.name = 'stubsingletagandfunc';
78 ve.dm.example.StubSingleTagAndFuncAnnotation.static.matchTagNames = [ 'a' ];
79 ve.dm.example.StubSingleTagAndFuncAnnotation.static.matchFunction = ve.dm.example.checkForPickMe;
80
81 /* Tag and function (allows extra types) */
82 ve.dm.example.StubSingleTagAndFuncExtraAnnotation = function VeDmStubSingleTagAndFuncExtraAnnotation() {
83 ve.dm.example.StubSingleTagAndFuncExtraAnnotation.super.apply( this, arguments );
84 };
85 OO.inheritClass( ve.dm.example.StubSingleTagAndFuncExtraAnnotation, ve.dm.Annotation );
86 ve.dm.example.StubSingleTagAndFuncExtraAnnotation.static.name = 'stubsingletagandfuncextra';
87 ve.dm.example.StubSingleTagAndFuncExtraAnnotation.static.matchTagNames = [ 'a' ];
88 ve.dm.example.StubSingleTagAndFuncExtraAnnotation.static.matchFunction = ve.dm.example.checkForPickMe;
89 ve.dm.example.StubSingleTagAndFuncExtraAnnotation.static.allowedRdfaTypes = [ 'ext:bar' ];
90
91 /* Type and function */
92 ve.dm.example.StubSingleTypeAndFuncAnnotation = function VeDmStubSingleTypeAndFuncAnnotation() {
93 ve.dm.example.StubSingleTypeAndFuncAnnotation.super.apply( this, arguments );
94 };
95 OO.inheritClass( ve.dm.example.StubSingleTypeAndFuncAnnotation, ve.dm.Annotation );
96 ve.dm.example.StubSingleTypeAndFuncAnnotation.static.name = 'stubsingletypeandfunc';
97 ve.dm.example.StubSingleTypeAndFuncAnnotation.static.matchRdfaTypes = [ 'ext:foo' ];
98 ve.dm.example.StubSingleTypeAndFuncAnnotation.static.matchFunction = ve.dm.example.checkForPickMe;
99
100 /* Tag, type and function */
101 ve.dm.example.StubSingleTagAndTypeAndFuncAnnotation = function VeDmStubSingleTagAndTypeAndFuncAnnotation() {
102 ve.dm.example.StubSingleTagAndTypeAndFuncAnnotation.super.apply( this, arguments );
103 };
104 OO.inheritClass( ve.dm.example.StubSingleTagAndTypeAndFuncAnnotation, ve.dm.Annotation );
105 ve.dm.example.StubSingleTagAndTypeAndFuncAnnotation.static.name = 'stubsingletagandtypeandfunc';
106 ve.dm.example.StubSingleTagAndTypeAndFuncAnnotation.static.matchTagNames = [ 'a' ];
107 ve.dm.example.StubSingleTagAndTypeAndFuncAnnotation.static.matchRdfaTypes = [ 'ext:foo' ];
108 ve.dm.example.StubSingleTagAndTypeAndFuncAnnotation.static.matchFunction = ve.dm.example.checkForPickMe;
109
110 /* Type 'bar' */
111 ve.dm.example.StubBarNode = function VeDmStubBarNode() {
112 ve.dm.example.StubBarNode.super.apply( this, arguments );
113 };
114 OO.inheritClass( ve.dm.example.StubBarNode, ve.dm.BranchNode );
115 ve.dm.example.StubBarNode.static.name = 'stub-bar';
116 ve.dm.example.StubBarNode.static.matchRdfaTypes = [ 'bar' ];
117 // HACK keep ve.dm.Converter happy for now
118 // TODO once ve.dm.Converter is rewritten, this can be removed
119 ve.dm.example.StubBarNode.static.toDataElement = function () {};
120 ve.dm.example.StubBarNode.static.toDomElements = function () {};
121
122 /* Tag 'abbr', type 'ext:abbr' */
123 ve.dm.example.StubAbbrNode = function VeDmStubAbbrNode() {
124 ve.dm.example.StubAbbrNode.super.apply( this, arguments );
125 };
126 OO.inheritClass( ve.dm.example.StubAbbrNode, ve.dm.BranchNode );
127 ve.dm.example.StubAbbrNode.static.name = 'stub-abbr';
128 ve.dm.example.StubAbbrNode.static.matchTagNames = [ 'abbr' ];
129 ve.dm.example.StubAbbrNode.static.matchRdfaTypes = [ 'ext:abbr' ];
130
131 /* Tag 'abbr', type /^ext:/ */
132 ve.dm.example.StubRegExpNode = function VeDmStubRegExpNode() {
133 ve.dm.example.StubRegExpNode.super.apply( this, arguments );
134 };
135 OO.inheritClass( ve.dm.example.StubRegExpNode, ve.dm.BranchNode );
136 ve.dm.example.StubRegExpNode.static.name = 'stub-regexp';
137 ve.dm.example.StubRegExpNode.static.matchTagNames = [ 'abbr' ];
138 ve.dm.example.StubRegExpNode.static.matchRdfaTypes = [
139 /^ext:/
140 ];
141
142 /* Tests */
143
144 QUnit.test( 'register/unregister/matchElement', ( assert ) => {
145 const registry = new ve.dm.ModelRegistry();
146
147 let element = document.createElement( 'a' );
148
149 assert.strictEqual( registry.matchElement( element ), null, 'matchElement() returns null if registry empty' );
150
151 registry.register( ve.dm.example.StubNothingSetAnnotation );
152 registry.register( ve.dm.example.StubSingleTagAnnotation );
153 registry.register( ve.dm.example.StubSingleTypeWithAnyAllowedAnnotation );
154 registry.register( ve.dm.example.StubSingleTypeWithAllowedAnnotation );
155 registry.register( ve.dm.example.StubSingleTypeAnnotation );
156 registry.register( ve.dm.example.StubSingleTagAndTypeAnnotation );
157 registry.register( ve.dm.example.StubFuncAnnotation );
158 registry.register( ve.dm.example.StubSingleTagAndFuncExtraAnnotation );
159 registry.register( ve.dm.example.StubSingleTagAndFuncAnnotation );
160 registry.register( ve.dm.example.StubSingleTypeAndFuncAnnotation );
161 registry.register( ve.dm.example.StubSingleTagAndTypeAndFuncAnnotation );
162 registry.register( ve.dm.example.StubBarNode );
163 registry.register( ve.dm.example.StubAbbrNode );
164 registry.register( ve.dm.example.StubRegExpNode );
165
166 element = document.createElement( 'b' );
167 assert.strictEqual( registry.matchElement( element ), 'stubnothingset', 'nothingset matches anything' );
168 element.setAttribute( 'rel', 'ext:foo' );
169 assert.strictEqual( registry.matchElement( element ), 'stubsingletype', 'type-only match' );
170 element.setAttribute( 'rel', ' ext:foo ' );
171 assert.strictEqual( registry.matchElement( element ), 'stubsingletype', 'type-only match (extra spaces)' );
172 element.setAttribute( 'rel', 'ext:foo bar' );
173 assert.strictEqual( registry.matchElement( element ), 'stubsingletypewithallowed', 'type-only match with extra allowed type' );
174 element.setAttribute( 'rel', 'ext:foo bar' );
175 assert.strictEqual( registry.matchElement( element ), 'stubsingletypewithallowed', 'type-only match with extra allowed type (extra spaces)' );
176 element.setAttribute( 'rel', 'ext:foo bar baz quux whee' );
177 assert.strictEqual( registry.matchElement( element ), 'stubsingletypewithanyallowed', 'type-only match with many extra types' );
178 element = document.createElement( 'a' );
179 assert.strictEqual( registry.matchElement( element ), 'stubsingletag', 'tag-only match' );
180 element.setAttribute( 'rel', 'ext:foo' );
181 assert.strictEqual( registry.matchElement( element ), 'stubsingletagandtype', 'tag and type match' );
182 element.setAttribute( 'pickme', 'true' );
183 assert.strictEqual( registry.matchElement( element ), 'stubsingletagandtypeandfunc', 'tag, type and func match' );
184 element.removeAttribute( 'rel' );
185 assert.strictEqual( registry.matchElement( element ), 'stubsingletagandfunc', 'tag and func match' );
186 element.setAttribute( 'rel', 'ext:bar' );
187 assert.strictEqual( registry.matchElement( element ), 'stubsingletagandfuncextra', 'tag and func match (with allowed extra type)' );
188 element = document.createElement( 'b' );
189 element.setAttribute( 'pickme', 'true' );
190 assert.strictEqual( registry.matchElement( element ), 'stubfunc', 'func-only match' );
191 element.setAttribute( 'rel', 'ext:foo' );
192 assert.strictEqual( registry.matchElement( element ), 'stubsingletypeandfunc', 'type and func match' );
193 element = document.createElement( 'abbr' );
194 element.setAttribute( 'rel', 'ext:baz' );
195 assert.strictEqual( registry.matchElement( element ), 'stub-regexp', 'RegExp type match' );
196 element.setAttribute( 'rel', 'ext:abbr' );
197 assert.strictEqual( registry.matchElement( element ), 'stub-abbr', 'String match overrides RegExp match' );
198
199 assert.strictEqual(
200 registry.registrationOrder[ ve.dm.example.StubNothingSetAnnotation.static.name ],
201 0,
202 'Model given a registration order'
203 );
204 registry.register( ve.dm.example.StubNothingSetAnnotation );
205 assert.strictEqual(
206 registry.registrationOrder[ ve.dm.example.StubNothingSetAnnotation.static.name ],
207 0,
208 'Double registration is a no-op (doesn\'t affect registration order)'
209 );
210
211 registry.unregister( ve.dm.example.StubAbbrNode );
212 element.removeAttribute( 'typeof' );
213 element.setAttribute( 'rel', 'ext:abbr' );
214 assert.strictEqual( registry.matchElement( element ), 'stub-regexp', 'RegExp type match after string match is unregistered' );
215
216 assert.deepEqual(
217 registry.modelsByTag[ 1 ].a,
218 [ 'stubsingletagandtypeandfunc', 'stubsingletagandfunc', 'stubsingletagandfuncextra' ],
219 'tag and func creates entries in modelsByTag[ 1 ]'
220 );
221 assert.deepEqual(
222 registry.modelsByTypeAndTag[ 1 ][ 'ext:foo' ].a,
223 [ 'stubsingletagandtypeandfunc' ],
224 'tag and func creates entries in modelsByTypeAndTag[ 1 ]'
225 );
226 registry.unregister( ve.dm.example.StubSingleTagAndFuncAnnotation );
227 registry.unregister( ve.dm.example.StubSingleTagAndFuncExtraAnnotation );
228 registry.unregister( ve.dm.example.StubSingleTagAndTypeAndFuncAnnotation );
229 assert.deepEqual(
230 registry.modelsByTag[ 1 ][ ve.dm.example.StubSingleTagAndFuncAnnotation.static.matchTagNames[ 0 ] ],
231 [],
232 'unregister removes entries in modelsByTag[ 1 ]'
233 );
234 assert.deepEqual(
235 registry.modelsByTypeAndTag[ 1 ][ 'ext:foo' ].a,
236 [],
237 'unregister removes entries in modelsByTypeAndTag[ 1 ]'
238 );
239
240 } );
241
242 QUnit.test( 'isAnnotation', ( assert ) => {
243 const allAnnotationTags = [ 'a', 'abbr', 'b', 'big', 'code', 'dfn', 'font', 'i', 'kbd', 'mark', 'q', 's', 'samp', 'small', 'span', 'sub', 'sup', 'time', 'u', 'var' ],
244 nonAnnotationTags = [ 'h1', 'p', 'ul', 'li', 'table', 'tr', 'td' ];
245
246 for ( let i = 0, len = allAnnotationTags.length; i < len; i++ ) {
247 const node = document.createElement( allAnnotationTags[ i ] );
248 assert.strictEqual(
249 ve.dm.modelRegistry.isAnnotation( node ),
250 true,
251 allAnnotationTags[ i ] + ' annotation'
252 );
253 }
254
255 for ( let i = 0, len = nonAnnotationTags.length; i < len; i++ ) {
256 const node = document.createElement( nonAnnotationTags[ i ] );
257 assert.strictEqual(
258 ve.dm.modelRegistry.isAnnotation( node ),
259 false,
260 allAnnotationTags[ i ] + ' non-annotation'
261 );
262 }
263
264 const node = document.createElement( 'span' );
265 node.setAttribute( 'rel', 've:Alien' );
266 assert.strictEqual( ve.dm.modelRegistry.isAnnotation( node ), false, 'alien span' );
267 node.removeAttribute( 'rel' );
268 assert.strictEqual( ve.dm.modelRegistry.isAnnotation( node ), true, 'non-alien span' );
269 } );