Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/ve.dm.ClassAttributeNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel ClassAttribute class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * DataModel class-attribute node.
9 *
10 * Used for nodes which use classes to store attributes.
11 *
12 * @class
13 * @abstract
14 *
15 * @constructor
16 */
17 ve.dm.ClassAttributeNode = function VeDmClassAttributeNode() {};
18
19 /* Inheritance */
20
21 OO.initClass( ve.dm.ClassAttributeNode );
22
23 /* Static methods */
24
25 /**
26 * Mapping from class names to attributes
27 *
28 * e.g. { alignLeft: { align: 'left' } } sets the align attribute to 'left'
29 * if the element has the class 'alignLeft'
30 *
31 * @type {Object}
32 */
33 ve.dm.ClassAttributeNode.static.classAttributes = {};
34
35 ve.dm.ClassAttributeNode.static.preserveHtmlAttributes = function ( attribute ) {
36 return attribute !== 'class';
37 };
38
39 /**
40 * Set attributes from a class attribute
41 *
42 * Unrecognized classes are also preserved.
43 *
44 * @param {Object} attributes Attributes object to modify
45 * @param {string|null} classAttr Class attribute from an element
46 */
47 ve.dm.ClassAttributeNode.static.setClassAttributes = function ( attributes, classAttr ) {
48 const classNames = classAttr ? classAttr.trim().split( /\s+/ ) : [];
49
50 if ( !classNames.length ) {
51 return;
52 }
53
54 const unrecognizedClasses = [];
55 for ( let i = 0, l = classNames.length; i < l; i++ ) {
56 const className = classNames[ i ];
57 if ( Object.prototype.hasOwnProperty.call( this.classAttributes, className ) ) {
58 attributes = ve.extendObject( attributes, this.classAttributes[ className ] );
59 } else {
60 unrecognizedClasses.push( className );
61 }
62 }
63
64 attributes.originalClasses = classAttr;
65 attributes.unrecognizedClasses = unrecognizedClasses;
66 };
67
68 /**
69 * Get class attribute from element attributes
70 *
71 * @param {Object|undefined} attributes Element attributes
72 * @return {string|null} Class name, or null if no classes to set
73 */
74 ve.dm.ClassAttributeNode.static.getClassAttrFromAttributes = function ( attributes ) {
75 attributes = attributes || {};
76
77 let classNames = [];
78 for ( const className in this.classAttributes ) {
79 const classAttributeSet = this.classAttributes[ className ];
80 let hasClass = true;
81 for ( const key in classAttributeSet ) {
82 if ( attributes[ key ] !== classAttributeSet[ key ] ) {
83 hasClass = false;
84 break;
85 }
86 }
87 if ( hasClass ) {
88 classNames.push( className );
89 }
90 }
91
92 if ( attributes.unrecognizedClasses ) {
93 classNames = OO.simpleArrayUnion( classNames, attributes.unrecognizedClasses );
94 }
95
96 // If no meaningful change in classes, preserve order
97 if (
98 attributes.originalClasses &&
99 ve.compareClassLists( attributes.originalClasses, classNames )
100 ) {
101 return attributes.originalClasses;
102 } else if ( classNames.length > 0 ) {
103 return classNames.join( ' ' );
104 }
105
106 return null;
107 };
108
109 // eslint-disable-next-line jsdoc/require-param
110 /**
111 * @see ve.dm.Node
112 */
113 ve.dm.ClassAttributeNode.static.sanitize = function ( dataElement ) {
114 if ( dataElement.attributes ) {
115 delete dataElement.attributes.unrecognizedClasses;
116 }
117 };