Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel MWTable class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * DataModel MediaWiki table node.
10 *
11 * @class
12 * @extends ve.dm.TableNode
13 * @mixes ve.dm.ClassAttributeNode
14 *
15 * @constructor
16 * @param {Object} [element] Reference to element in linear model
17 * @param {ve.dm.Node[]} [children]
18 */
19 ve.dm.MWTableNode = function VeDmMWTableNode() {
20 // Parent constructor
21 ve.dm.MWTableNode.super.apply( this, arguments );
22
23 // Mixin constructors
24 ve.dm.ClassAttributeNode.call( this );
25 };
26
27 /* Inheritance */
28
29 OO.inheritClass( ve.dm.MWTableNode, ve.dm.TableNode );
30
31 OO.mixinClass( ve.dm.MWTableNode, ve.dm.ClassAttributeNode );
32
33 /* Static Properties */
34
35 ve.dm.MWTableNode.static.name = 'mwTable';
36
37 ve.dm.MWTableNode.static.allowedRdfaTypes = [ 'mw:ExpandedAttrs' ];
38
39 ve.dm.MWTableNode.static.classAttributes = {
40 wikitable: { wikitable: true },
41 sortable: { sortable: true },
42 'mw-collapsible': { collapsible: true },
43 'mw-collapsed': { collapsed: true }
44 };
45
46 // Tables in wikitext only work in some contexts, they're impossible e.g. in list items
47 ve.dm.MWTableNode.static.suggestedParentNodeTypes = [
48 'document', 'div', 'tableCell', 'tableCaption', 'mwImageCaption', 'section',
49 // TODO: `paragraph` isn't really a suggested table parent. However,
50 // allowing it here interacts with our post-insertion cleanup for block
51 // nodes so that empty paragraphs get properly removed. We should find a
52 // cleaner way to do this. See: T201573.
53 'paragraph'
54 ];
55
56 // HACK: users of parentNodeTypes should be fixed to check for inherited classes.
57 ve.dm.TableSectionNode.static.parentNodeTypes.push( 'mwTable' );
58 ve.dm.TableCaptionNode.static.parentNodeTypes.push( 'mwTable' );
59 ve.dm.TableRowNode.static.childNodeTypes.push( 'mwTransclusionTableCell' );
60
61 ve.dm.MWTableNode.static.toDataElement = function ( domElements ) {
62 const dataElement = { type: this.name },
63 domElement = domElements[ 0 ],
64 classAttr = domElement.getAttribute( 'class' );
65
66 const attributes = {
67 hasExpandedAttrs: ( domElement.getAttribute( 'typeof' ) || '' ).indexOf( 'mw:ExpandedAttrs' ) !== -1
68 };
69
70 this.setClassAttributes( attributes, classAttr );
71
72 dataElement.attributes = attributes;
73
74 return dataElement;
75 };
76
77 ve.dm.MWTableNode.static.toDomElements = function ( dataElement, doc ) {
78 const element = doc.createElement( 'table' ),
79 classAttr = dataElement.attributes && this.getClassAttrFromAttributes( dataElement.attributes );
80
81 if ( classAttr ) {
82 // eslint-disable-next-line mediawiki/class-doc
83 element.className = classAttr;
84 }
85
86 return [ element ];
87 };
88
89 ve.dm.MWTableNode.static.sanitize = function ( dataElement ) {
90 // Mixin method
91 ve.dm.ClassAttributeNode.static.sanitize.call( this, dataElement );
92
93 ve.setProp( dataElement, 'attributes', 'wikitable', true );
94 };
95
96 /* Registration */
97
98 ve.dm.modelRegistry.register( ve.dm.MWTableNode );