| 1 | /*! |
| 2 | * VisualEditor DataModel Annotation class. |
| 3 | * |
| 4 | * @copyright See AUTHORS.txt |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * Generic DataModel annotation. |
| 9 | * |
| 10 | * This is an abstract class, annotations should extend this and call this constructor from their |
| 11 | * constructor. You should not instantiate this class directly. |
| 12 | * |
| 13 | * Annotations in the linear model are instances of subclasses of this class. Subclasses should |
| 14 | * only override static properties and functions. |
| 15 | * |
| 16 | * @class |
| 17 | * @extends ve.dm.Model |
| 18 | * @constructor |
| 19 | * @param {Object} element Linear model annotation |
| 20 | * @param {ve.dm.HashValueStore} store Store used by annotation |
| 21 | */ |
| 22 | ve.dm.Annotation = function VeDmAnnotation( element, store ) { |
| 23 | // Parent constructor |
| 24 | ve.dm.Annotation.super.call( this, element ); |
| 25 | |
| 26 | // Properties |
| 27 | this.name = this.constructor.static.name; // For ease of filtering |
| 28 | this.store = store; |
| 29 | }; |
| 30 | |
| 31 | /* Inheritance */ |
| 32 | |
| 33 | OO.inheritClass( ve.dm.Annotation, ve.dm.Model ); |
| 34 | |
| 35 | /* Static properties */ |
| 36 | |
| 37 | /** |
| 38 | * About grouping is not supported for annotations; setting this to true has no effect. |
| 39 | * |
| 40 | * @static |
| 41 | * @property {boolean} |
| 42 | * @inheritable |
| 43 | */ |
| 44 | ve.dm.Annotation.static.enableAboutGrouping = false; |
| 45 | |
| 46 | /** |
| 47 | * Automatically apply annotation to content inserted after it. |
| 48 | * |
| 49 | * @property {boolean} |
| 50 | * @see ve.ce.TextState#getChangeTransaction |
| 51 | */ |
| 52 | ve.dm.Annotation.static.applyToAppendedContent = true; |
| 53 | |
| 54 | /** |
| 55 | * Accept this annotation when the browser spontaneously adds it to view's DOM. |
| 56 | * |
| 57 | * @property {boolean} |
| 58 | */ |
| 59 | ve.dm.Annotation.static.inferFromView = false; |
| 60 | |
| 61 | /** |
| 62 | * Annotations which are removed when this one is applied |
| 63 | * |
| 64 | * @property {string[]} |
| 65 | */ |
| 66 | ve.dm.Annotation.static.removes = []; |
| 67 | |
| 68 | /** |
| 69 | * Move whitespace at the edge of the transaction outside of it when converting |
| 70 | * |
| 71 | * e.g. "foo<b> bar </b>baz" -> "foo <b>bar</b> baz" |
| 72 | * |
| 73 | * @property {boolean} |
| 74 | */ |
| 75 | ve.dm.Annotation.static.trimWhitespace = true; |
| 76 | |
| 77 | /** |
| 78 | * Static function to convert a linear model data element for this annotation type back to |
| 79 | * a DOM element. |
| 80 | * |
| 81 | * As special facilities for annotations, the annotated content that the returned element will |
| 82 | * wrap around is passed in as childDomElements, and this function may return an empty array to |
| 83 | * indicate that the annotation should produce no output. In that case, the child DOM elements will |
| 84 | * not be wrapped in anything and will be inserted directly into this annotation's parent. |
| 85 | * |
| 86 | * @abstract |
| 87 | * @static |
| 88 | * @inheritable |
| 89 | * @method |
| 90 | * @param {Object|Array} dataElement Linear model element or array of linear model data |
| 91 | * @param {HTMLDocument} doc HTML document for creating elements |
| 92 | * @param {ve.dm.Converter} converter Converter object to optionally call .getDomSubtreeFromData() on |
| 93 | * @param {Node[]} childDomElements Children that will be appended to the returned element |
| 94 | * @return {HTMLElement[]} Array of DOM elements; only the first element is used; may be empty |
| 95 | */ |
| 96 | ve.dm.Annotation.static.toDomElements = null; |
| 97 | |
| 98 | /* Methods */ |
| 99 | |
| 100 | /** |
| 101 | * Get an object containing comparable annotation properties. |
| 102 | * |
| 103 | * This is used by the converter to merge adjacent annotations. |
| 104 | * |
| 105 | * @return {Object} An object containing a subset of the annotation's properties |
| 106 | */ |
| 107 | ve.dm.Annotation.prototype.getComparableObject = function () { |
| 108 | const hashObject = this.getHashObject(); |
| 109 | delete hashObject.originalDomElementsHash; |
| 110 | return hashObject; |
| 111 | }; |
| 112 | |
| 113 | /** |
| 114 | * FIXME T126037: This method strips data-parsoid & RESTBase IDs from HTML attributes for comparisons. |
| 115 | * |
| 116 | * This should be removed once similar annotation merging is handled correctly |
| 117 | * by Parsoid. |
| 118 | * |
| 119 | * @return {Object} An object all HTML attributes except data-parsoid & RESTBase IDs |
| 120 | */ |
| 121 | ve.dm.Annotation.prototype.getComparableHtmlAttributes = function () { |
| 122 | const domElements = this.store && this.getOriginalDomElements( this.store ); |
| 123 | |
| 124 | if ( domElements && domElements[ 0 ] ) { |
| 125 | const comparableAttributes = ve.getDomAttributes( domElements[ 0 ] ); |
| 126 | delete comparableAttributes[ 'data-parsoid' ]; |
| 127 | |
| 128 | if ( comparableAttributes.id ) { |
| 129 | const metadataIdRegExp = ve.init.platform.getMetadataIdRegExp(); |
| 130 | if ( metadataIdRegExp && metadataIdRegExp.test( comparableAttributes.id ) ) { |
| 131 | delete comparableAttributes.id; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | return comparableAttributes; |
| 136 | } |
| 137 | return {}; |
| 138 | }; |
| 139 | |
| 140 | /** |
| 141 | * FIXME T126038: This method adds in HTML attributes so comparable objects |
| 142 | * aren't serialized together if they have different HTML attributes. |
| 143 | * |
| 144 | * This method needs to be different from #getComparableObject which is |
| 145 | * still used for editing annotations. |
| 146 | * |
| 147 | * @return {Object} An object containing a subset of the annotation's properties and HTML attributes |
| 148 | */ |
| 149 | ve.dm.Annotation.prototype.getComparableObjectForSerialization = function () { |
| 150 | const object = this.getComparableObject(), |
| 151 | htmlAttributes = this.getComparableHtmlAttributes(); |
| 152 | |
| 153 | if ( !ve.isEmptyObject( htmlAttributes ) ) { |
| 154 | object.htmlAttributes = htmlAttributes; |
| 155 | } |
| 156 | return object; |
| 157 | }; |
| 158 | |
| 159 | /** |
| 160 | * Check if the annotation was generated by the converter |
| 161 | * |
| 162 | * Used by compareToForSerialization to avoid merging generated annotations. |
| 163 | * |
| 164 | * @return {boolean} The annotation was generated |
| 165 | */ |
| 166 | ve.dm.Annotation.prototype.isGenerated = function () { |
| 167 | // Only annotations and nodes generated by the converter have originalDomElements set. |
| 168 | // If this annotation was not generated by the converter, this.getOriginalDomElements() |
| 169 | // will return an empty array. |
| 170 | return this.getOriginalDomElementsHash() !== undefined; |
| 171 | }; |
| 172 | |
| 173 | /** |
| 174 | * Compare two annotations using #getComparableObject |
| 175 | * |
| 176 | * @param {ve.dm.Annotation} annotation Other annotation to compare against |
| 177 | * @return {boolean} Annotation is comparable |
| 178 | */ |
| 179 | ve.dm.Annotation.prototype.compareTo = function ( annotation ) { |
| 180 | return ve.compare( |
| 181 | this.getComparableObject(), |
| 182 | annotation.getComparableObject() |
| 183 | ); |
| 184 | }; |
| 185 | |
| 186 | /** |
| 187 | * FIXME T126039: Compare to another annotation for serialization |
| 188 | * |
| 189 | * Compares two annotations using #getComparableObjectForSerialization, unless |
| 190 | * they are both generated annotations, in which case they must be identical. |
| 191 | * |
| 192 | * @param {ve.dm.Annotation} annotation Annotation to compare to |
| 193 | * @return {boolean} The other annotation is similar to this one |
| 194 | */ |
| 195 | ve.dm.Annotation.prototype.compareToForSerialization = function ( annotation ) { |
| 196 | // If both annotations were generated |
| 197 | if ( this.isGenerated() && annotation.isGenerated() ) { |
| 198 | return ve.compare( this.getHashObject(), annotation.getHashObject() ); |
| 199 | } |
| 200 | |
| 201 | return ve.compare( |
| 202 | this.getComparableObjectForSerialization(), |
| 203 | annotation.getComparableObjectForSerialization() |
| 204 | ); |
| 205 | }; |
| 206 | |
| 207 | /** |
| 208 | * Describe the addition of this annotation to some text |
| 209 | * |
| 210 | * @return {Array} Descriptions, list of strings or Node arrays |
| 211 | */ |
| 212 | ve.dm.Annotation.prototype.describeAdded = function () { |
| 213 | return []; |
| 214 | }; |
| 215 | |
| 216 | /** |
| 217 | * Describe the removal of this annotation from some text |
| 218 | * |
| 219 | * @return {Array} Descriptions, list of strings or Node arrays |
| 220 | */ |
| 221 | ve.dm.Annotation.prototype.describeRemoved = function () { |
| 222 | return []; |
| 223 | }; |