Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/dm/nodes/ve.dm.MWIncludesNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel MWIncludesNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * DataModel MW node for noinclude, includeonly and onlyinclude tags.
10 *
11 * @class
12 * @extends ve.dm.AlienInlineNode
13 * @constructor
14 * @param {Object} element Reference to element in linear model
15 */
16 ve.dm.MWIncludesNode = function VeDmMWIncludesNode() {
17 // Parent constructor
18 ve.dm.MWIncludesNode.super.apply( this, arguments );
19 };
20
21 /* Inheritance */
22
23 OO.inheritClass( ve.dm.MWIncludesNode, ve.dm.AlienInlineNode );
24
25 /* Static Properties */
26
27 ve.dm.MWIncludesNode.static.name = 'mwIncludes';
28
29 ve.dm.MWIncludesNode.static.matchRdfaTypes = [
30 /^mw:Includes\//
31 ];
32
33 /* Static Methods */
34
35 /**
36 * @inheritdoc
37 */
38 ve.dm.MWIncludesNode.static.toDataElement = function ( domElements ) {
39 const mwDataJSON = domElements[ 0 ].getAttribute( 'data-mw' ),
40 type = domElements[ 0 ].getAttribute( 'typeof' );
41
42 const dataElement = {
43 type: 'mwIncludes',
44 attributes: {
45 type: type
46 }
47 };
48
49 if ( mwDataJSON !== null ) {
50 dataElement.attributes.mw = JSON.parse( mwDataJSON );
51 }
52
53 if ( type === 'mw:Includes/IncludeOnly/End' ) {
54 // We don't want to allow typing between this and the opening tag, as the content is stored in
55 // data-mw as wikitext. Therefore pretend that includeonly nodes have an implicit closing tag.
56 // This should be fine since the closing tag directly follows the opening tag.
57 return [];
58 }
59
60 return dataElement;
61 };
62
63 /**
64 * @inheritdoc
65 */
66 ve.dm.MWIncludesNode.static.toDomElements = function ( dataElement, doc, converter ) {
67 const el = doc.createElement( 'meta' );
68 el.setAttribute( 'typeof', dataElement.attributes.type );
69 if ( dataElement.attributes.mw ) {
70 el.setAttribute( 'data-mw', JSON.stringify( dataElement.attributes.mw ) );
71 }
72
73 const els = [ el ];
74 if ( dataElement.attributes.type === 'mw:Includes/IncludeOnly' ) {
75 // includeonly nodes have an implicit closing tag
76 ve.batchPush( els, ve.dm.MWIncludesNode.static.toDomElements( {
77 type: 'mwIncludes',
78 attributes: {
79 type: 'mw:Includes/IncludeOnly/End'
80 }
81 }, doc, converter ) );
82 }
83
84 return els;
85 };
86
87 /* Methods */
88
89 ve.dm.MWIncludesNode.prototype.getWikitextTag = function () {
90 const map = {
91 'mw:Includes/NoInclude': '<noinclude>',
92 'mw:Includes/NoInclude/End': '</noinclude>',
93 'mw:Includes/OnlyInclude': '<onlyinclude>',
94 'mw:Includes/OnlyInclude/End': '</onlyinclude>',
95 'mw:Includes/IncludeOnly': '<includeonly>…</includeonly>'
96 };
97 return map[ this.getAttribute( 'type' ) ];
98 };
99
100 /* Registration */
101
102 ve.dm.modelRegistry.register( ve.dm.MWIncludesNode );