| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
( function () { |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
ve.dm.ModelRegistry = function VeDmModelRegistry() { |
| 17 |
|
| 18 |
OO.Registry.call( this ); |
| 19 |
|
| 20 |
|
| 21 |
this.modelsByTag = [ {}, {} ]; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
this.modelsByTypeAndTag = []; |
| 26 |
|
| 27 |
|
| 28 |
this.modelsWithTypeRegExps = [ [], [] ]; |
| 29 |
|
| 30 |
} |
| 31 |
this.registrationOrder = {}; |
| 32 |
this.nextNumber = 0; |
| 33 |
this.extSpecificTypes = []; |
| 34 |
}; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
OO.inheritClass( ve.dm.ModelRegistry, OO.Registry ); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
function addType( obj, ...keys ) { |
| 58 |
const value = keys.pop(); |
| 59 |
let o = obj; |
| 60 |
|
| 61 |
let i, len; |
| 62 |
for ( i = 0, len = keys.length - 1; i < len; i++ ) { |
| 63 |
if ( o[ keys[ i ] ] === undefined ) { |
| 64 |
o[ keys[ i ] ] = {}; |
| 65 |
} |
| 66 |
o = o[ keys[ i ] ]; |
| 67 |
} |
| 68 |
o[ keys[ i ] ] = o[ keys[ i ] ] || []; |
| 69 |
o[ keys[ i ] ].unshift( value ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
function removeType( obj, ...keys ) { |
| 83 |
const value = keys.pop(), |
| 84 |
arr = ve.getProp( obj, ...keys ); |
| 85 |
|
| 86 |
if ( arr ) { |
| 87 |
const index = arr.indexOf( value ); |
| 88 |
if ( index !== -1 ) { |
| 89 |
arr.splice( index, 1 ); |
| 90 |
} |
| 91 |
|
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
ve.dm.ModelRegistry.prototype.register = function ( constructor ) { |
| 106 |
const name = constructor.static && constructor.static.name; |
| 107 |
|
| 108 |
if ( typeof name !== 'string' || name === '' ) { |
| 109 |
throw new Error( 'Model names must be strings and must not be empty' ); |
| 110 |
} |
| 111 |
if ( !( constructor.prototype instanceof ve.dm.Model ) ) { |
| 112 |
throw new Error( 'Models must be subclasses of ve.dm.Model' ); |
| 113 |
} |
| 114 |
if ( this.lookup( name ) === constructor ) { |
| 115 |
|
| 116 |
|
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
if ( constructor.prototype instanceof ve.dm.Annotation ) { |
| 122 |
ve.dm.annotationFactory.register( constructor ); |
| 123 |
} else if ( constructor.prototype instanceof ve.dm.Node ) { |
| 124 |
ve.dm.nodeFactory.register( constructor ); |
| 125 |
} else { |
| 126 |
throw new Error( 'No factory associated with this ve.dm.Model subclass' ); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
ve.dm.ModelRegistry.super.prototype.register.call( this, name, constructor ); |
| 131 |
|
| 132 |
const tags = constructor.static.matchTagNames === null ? |
| 133 |
[ '' ] : |
| 134 |
constructor.static.matchTagNames; |
| 135 |
const types = constructor.static.getMatchRdfaTypes() === null ? |
| 136 |
[ '' ] : |
| 137 |
constructor.static.getMatchRdfaTypes(); |
| 138 |
|
| 139 |
for ( let i = 0; i < tags.length; i++ ) { |
| 140 |
|
| 141 |
addType( this.modelsByTag, +!!constructor.static.matchFunction, |
| 142 |
tags[ i ], name |
| 143 |
); |
| 144 |
} |
| 145 |
for ( let i = 0; i < types.length; i++ ) { |
| 146 |
if ( types[ i ] instanceof RegExp ) { |
| 147 |
|
| 148 |
|
| 149 |
addType( this.modelsWithTypeRegExps, +!!constructor.static.matchFunction, name ); |
| 150 |
} else { |
| 151 |
for ( let j = 0; j < tags.length; j++ ) { |
| 152 |
addType( this.modelsByTypeAndTag, |
| 153 |
+!!constructor.static.matchFunction, types[ i ], tags[ j ], name |
| 154 |
); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
this.registrationOrder[ name ] = this.nextNumber++; |
| 160 |
}; |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
ve.dm.ModelRegistry.prototype.unregister = function ( constructor ) { |
| 171 |
const name = constructor.static && constructor.static.name; |
| 172 |
|
| 173 |
if ( typeof name !== 'string' || name === '' ) { |
| 174 |
throw new Error( 'Model names must be strings and must not be empty' ); |
| 175 |
} |
| 176 |
if ( !( constructor.prototype instanceof ve.dm.Model ) ) { |
| 177 |
throw new Error( 'Models must be subclasses of ve.dm.Model' ); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
if ( constructor.prototype instanceof ve.dm.Annotation ) { |
| 182 |
ve.dm.annotationFactory.unregister( constructor ); |
| 183 |
} else if ( constructor.prototype instanceof ve.dm.Node ) { |
| 184 |
ve.dm.nodeFactory.unregister( constructor ); |
| 185 |
} else { |
| 186 |
throw new Error( 'No factory associated with this ve.dm.Model subclass' ); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
ve.dm.ModelRegistry.super.prototype.unregister.call( this, name ); |
| 191 |
|
| 192 |
const tags = constructor.static.matchTagNames === null ? |
| 193 |
[ '' ] : |
| 194 |
constructor.static.matchTagNames; |
| 195 |
const types = constructor.static.getMatchRdfaTypes() === null ? |
| 196 |
[ '' ] : |
| 197 |
constructor.static.getMatchRdfaTypes(); |
| 198 |
|
| 199 |
for ( let i = 0; i < tags.length; i++ ) { |
| 200 |
|
| 201 |
removeType( this.modelsByTag, +!!constructor.static.matchFunction, |
| 202 |
tags[ i ], name |
| 203 |
); |
| 204 |
} |
| 205 |
for ( let i = 0; i < types.length; i++ ) { |
| 206 |
if ( types[ i ] instanceof RegExp ) { |
| 207 |
|
| 208 |
|
| 209 |
removeType( this.modelsWithTypeRegExps, +!!constructor.static.matchFunction, name ); |
| 210 |
} else { |
| 211 |
for ( let j = 0; j < tags.length; j++ ) { |
| 212 |
removeType( this.modelsByTypeAndTag, |
| 213 |
+!!constructor.static.matchFunction, types[ i ], tags[ j ], name |
| 214 |
); |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
delete this.registrationOrder[ name ]; |
| 220 |
}; |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
ve.dm.ModelRegistry.prototype.matchElement = function ( node, forceAboutGrouping, excludeTypes ) { |
| 251 |
const nodeName = node.nodeName.toLowerCase(); |
| 252 |
const types = []; |
| 253 |
|
| 254 |
const byRegistrationOrderDesc = ( a, b ) => this.registrationOrder[ b ] - this.registrationOrder[ a ]; |
| 255 |
|
| 256 |
const matchTypeRegExps = ( type, tag, withFunc ) => { |
| 257 |
const matchedModels = [], |
| 258 |
models = this.modelsWithTypeRegExps[ +withFunc ]; |
| 259 |
for ( let j = 0; j < models.length; j++ ) { |
| 260 |
if ( excludeTypes && excludeTypes.indexOf( models[ j ] ) !== -1 ) { |
| 261 |
continue; |
| 262 |
} |
| 263 |
const matchTypes = this.registry[ models[ j ] ].static.getMatchRdfaTypes(); |
| 264 |
for ( let k = 0; k < matchTypes.length; k++ ) { |
| 265 |
if ( |
| 266 |
matchTypes[ k ] instanceof RegExp && |
| 267 |
matchTypes[ k ].test( type ) && |
| 268 |
( |
| 269 |
( tag === '' && this.registry[ models[ j ] ].static.matchTagNames === null ) || |
| 270 |
( this.registry[ models[ j ] ].static.matchTagNames || [] ).indexOf( tag ) !== -1 |
| 271 |
) |
| 272 |
) { |
| 273 |
matchedModels.push( models[ j ] ); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
return matchedModels; |
| 278 |
}; |
| 279 |
|
| 280 |
const allTypesAllowed = ( model ) => { |
| 281 |
let allowedTypes = model.static.getAllowedRdfaTypes(); |
| 282 |
const matchTypes = model.static.getMatchRdfaTypes(); |
| 283 |
|
| 284 |
|
| 285 |
if ( allowedTypes === null ) { |
| 286 |
return true; |
| 287 |
} |
| 288 |
|
| 289 |
if ( matchTypes !== null ) { |
| 290 |
|
| 291 |
allowedTypes = allowedTypes.concat( matchTypes ); |
| 292 |
} |
| 293 |
|
| 294 |
function checkType( rule, type ) { |
| 295 |
return rule instanceof RegExp ? rule.test( type ) : rule === type; |
| 296 |
} |
| 297 |
|
| 298 |
for ( let j = 0; j < types.length; j++ ) { |
| 299 |
let typeAllowed = false; |
| 300 |
for ( let k = 0; k < allowedTypes.length; k++ ) { |
| 301 |
if ( checkType( allowedTypes[ k ], types[ j ] ) ) { |
| 302 |
typeAllowed = true; |
| 303 |
break; |
| 304 |
} |
| 305 |
} |
| 306 |
if ( !typeAllowed ) { |
| 307 |
return false; |
| 308 |
} |
| 309 |
} |
| 310 |
return true; |
| 311 |
}; |
| 312 |
|
| 313 |
const matchWithFunc = ( tag ) => { |
| 314 |
let queue = [], |
| 315 |
queue2 = []; |
| 316 |
for ( let j = 0; j < types.length; j++ ) { |
| 317 |
|
| 318 |
ve.batchPush( queue, ve.getProp( this.modelsByTypeAndTag, 1, types[ j ], tag ) || [] ); |
| 319 |
if ( excludeTypes ) { |
| 320 |
queue = OO.simpleArrayDifference( queue, excludeTypes ); |
| 321 |
} |
| 322 |
ve.batchPush( queue2, matchTypeRegExps( types[ j ], tag, true ) ); |
| 323 |
} |
| 324 |
|
| 325 |
queue = queue.filter( ( name ) => allTypesAllowed( this.lookup( name ) ) ); |
| 326 |
queue2 = queue2.filter( ( name ) => allTypesAllowed( this.lookup( name ) ) ); |
| 327 |
if ( forceAboutGrouping ) { |
| 328 |
|
| 329 |
queue = queue.filter( ( name ) => this.registry[ name ].static.enableAboutGrouping ); |
| 330 |
queue2 = queue2.filter( ( name ) => this.registry[ name ].static.enableAboutGrouping ); |
| 331 |
} |
| 332 |
|
| 333 |
queue.sort( byRegistrationOrderDesc ); |
| 334 |
queue2.sort( byRegistrationOrderDesc ); |
| 335 |
ve.batchPush( queue, queue2 ); |
| 336 |
for ( let j = 0; j < queue.length; j++ ) { |
| 337 |
if ( this.registry[ queue[ j ] ].static.matchFunction( node ) ) { |
| 338 |
return queue[ j ]; |
| 339 |
} |
| 340 |
} |
| 341 |
return null; |
| 342 |
}; |
| 343 |
|
| 344 |
const matchWithoutFunc = ( tag ) => { |
| 345 |
let queue = [], |
| 346 |
queue2 = [], |
| 347 |
winningName = null; |
| 348 |
for ( let j = 0; j < types.length; j++ ) { |
| 349 |
|
| 350 |
ve.batchPush( queue, ve.getProp( this.modelsByTypeAndTag, 0, types[ j ], tag ) || [] ); |
| 351 |
if ( excludeTypes ) { |
| 352 |
queue = OO.simpleArrayDifference( queue, excludeTypes ); |
| 353 |
} |
| 354 |
ve.batchPush( queue2, matchTypeRegExps( types[ j ], tag, false ) ); |
| 355 |
} |
| 356 |
|
| 357 |
queue = queue.filter( ( name ) => allTypesAllowed( this.lookup( name ) ) ); |
| 358 |
queue2 = queue2.filter( ( name ) => allTypesAllowed( this.lookup( name ) ) ); |
| 359 |
if ( forceAboutGrouping ) { |
| 360 |
|
| 361 |
queue = queue.filter( ( name ) => this.registry[ name ].static.enableAboutGrouping ); |
| 362 |
queue2 = queue2.filter( ( name ) => this.registry[ name ].static.enableAboutGrouping ); |
| 363 |
} |
| 364 |
|
| 365 |
queue = queue.length > 0 ? queue : queue2; |
| 366 |
|
| 367 |
for ( let j = 0; j < queue.length; j++ ) { |
| 368 |
if ( |
| 369 |
winningName === null || |
| 370 |
this.registrationOrder[ winningName ] < this.registrationOrder[ queue[ j ] ] |
| 371 |
) { |
| 372 |
winningName = queue[ j ]; |
| 373 |
} |
| 374 |
} |
| 375 |
return winningName; |
| 376 |
}; |
| 377 |
|
| 378 |
if ( node.getAttribute ) { |
| 379 |
if ( node.getAttribute( 'rel' ) ) { |
| 380 |
types.push( ...node.getAttribute( 'rel' ).trim().split( /\s+/ ) ); |
| 381 |
} |
| 382 |
if ( node.getAttribute( 'typeof' ) ) { |
| 383 |
types.push( ...node.getAttribute( 'typeof' ).trim().split( /\s+/ ) ); |
| 384 |
} |
| 385 |
if ( node.getAttribute( 'property' ) ) { |
| 386 |
types.push( ...node.getAttribute( 'property' ).trim().split( /\s+/ ) ); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
let winner; |
| 391 |
if ( types.length ) { |
| 392 |
|
| 393 |
winner = matchWithFunc( nodeName ); |
| 394 |
if ( winner !== null ) { |
| 395 |
return winner; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
winner = matchWithFunc( '' ); |
| 402 |
if ( winner !== null ) { |
| 403 |
return winner; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
let matches = ve.getProp( this.modelsByTag, 1, nodeName ) || []; |
| 409 |
|
| 410 |
|
| 411 |
for ( let i = 0; i < matches.length; i++ ) { |
| 412 |
const m = this.registry[ matches[ i ] ]; |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
if ( m.static.getMatchRdfaTypes() === null && m.static.matchFunction( node ) && allTypesAllowed( m ) ) { |
| 417 |
return matches[ i ]; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
matches = ve.getProp( this.modelsByTypeAndTag, 1, '', '' ) || []; |
| 428 |
for ( let i = 0; i < matches.length; i++ ) { |
| 429 |
const m = this.registry[ matches[ i ] ]; |
| 430 |
if ( m.static.matchFunction( node ) && allTypesAllowed( m ) ) { |
| 431 |
return matches[ i ]; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
winner = matchWithoutFunc( nodeName ); |
| 437 |
if ( winner !== null ) { |
| 438 |
return winner; |
| 439 |
} |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
winner = matchWithoutFunc( '' ); |
| 445 |
if ( winner !== null ) { |
| 446 |
return winner; |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
matches = ve.getProp( this.modelsByTag, 0, nodeName ) || []; |
| 451 |
|
| 452 |
|
| 453 |
for ( let i = 0; i < matches.length; i++ ) { |
| 454 |
const m = this.registry[ matches[ i ] ]; |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
if ( m.static.getMatchRdfaTypes() === null && allTypesAllowed( m ) ) { |
| 459 |
return matches[ i ]; |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
matches = ve.getProp( this.modelsByTypeAndTag, 0, '', '' ) || []; |
| 467 |
for ( let i = 0; i < matches.length; i++ ) { |
| 468 |
const m = this.registry[ matches[ i ] ]; |
| 469 |
if ( allTypesAllowed( m ) ) { |
| 470 |
return matches[ i ]; |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
return null; |
| 476 |
}; |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
ve.dm.ModelRegistry.prototype.isAnnotation = function ( node ) { |
| 485 |
const modelClass = this.lookup( this.matchElement( node ) ); |
| 486 |
return ( modelClass && modelClass.prototype ) instanceof ve.dm.Annotation; |
| 487 |
}; |
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
ve.dm.modelRegistry = new ve.dm.ModelRegistry(); |
| 492 |
|
| 493 |
}() ); |