Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/dm/annotations/ve.dm.MWNowikiAnnotation.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel MWNowikiAnnotation class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * DataModel MediaWiki nowiki annotation
10 *
11 * Represents `<nowiki>` tags (in HTML as `<span typeof="mw:Nowiki">`) and unwraps them when they change
12 * so as to retrigger Parsoid's escaping mechanism.
13 *
14 * @class
15 * @extends ve.dm.Annotation
16 * @constructor
17 * @param {Object} element [description]
18 */
19 ve.dm.MWNowikiAnnotation = function VeDmMWNowikiAnnotation() {
20 // Parent constructor
21 ve.dm.MWNowikiAnnotation.super.apply( this, arguments );
22 };
23
24 /* Inheritance */
25
26 OO.inheritClass( ve.dm.MWNowikiAnnotation, ve.dm.Annotation );
27
28 /* Static Properties */
29
30 ve.dm.MWNowikiAnnotation.static.name = 'mwNowiki';
31
32 ve.dm.MWNowikiAnnotation.static.matchRdfaTypes = [ 'mw:Nowiki' ];
33
34 ve.dm.MWNowikiAnnotation.static.toDomElements = function ( dataElement, doc, converter, childDomElements ) {
35 const originalDomElements = converter.getStore().value( dataElement.originalDomElementsHash ),
36 originalChildren = originalDomElements && originalDomElements[ 0 ] && originalDomElements[ 0 ].childNodes,
37 domElement = document.createElement( 'span' );
38
39 let contentsChanged = false;
40 // Determine whether the contents changed
41 if ( !originalChildren || childDomElements.length !== originalChildren.length ) {
42 contentsChanged = true;
43 } else {
44 for ( let i = 0, len = originalChildren.length; i < len; i++ ) {
45 if ( !originalChildren[ i ].isEqualNode( childDomElements[ i ] ) ) {
46 contentsChanged = true;
47 break;
48 }
49 }
50 }
51
52 // If the contents changed, unwrap, otherwise, restore
53 if ( contentsChanged ) {
54 return [];
55 }
56 domElement.setAttribute( 'typeof', 'mw:Nowiki' );
57 return [ domElement ];
58 };
59
60 /* Registration */
61
62 ve.dm.modelRegistry.register( ve.dm.MWNowikiAnnotation );