| 1 | /*! |
| 2 | * VisualEditor annotated text content state chunk class |
| 3 | * |
| 4 | * @copyright See AUTHORS.txt |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * Uniformly annotated chunk of text content |
| 9 | * |
| 10 | * @class |
| 11 | * |
| 12 | * @constructor |
| 13 | * @param {string} text Plain text |
| 14 | * @param {HTMLElement[]} elements Annotation elements in force |
| 15 | * @param {string} type If this is a unicorn then 'unicorn', else 'text' |
| 16 | */ |
| 17 | ve.ce.TextStateChunk = function VeCeTextStateChunk( text, elements, type ) { |
| 18 | /** |
| 19 | * @property {string} text The plain text of this chunk |
| 20 | */ |
| 21 | this.text = text; |
| 22 | |
| 23 | /** |
| 24 | * @property {HTMLElement[]} elements The annotation elements open at this chunk |
| 25 | */ |
| 26 | this.elements = elements; |
| 27 | |
| 28 | /** |
| 29 | * @property {string} type The chunk type: 'text' or 'unicorn' |
| 30 | */ |
| 31 | this.type = type; |
| 32 | }; |
| 33 | |
| 34 | /* Inheritance */ |
| 35 | |
| 36 | OO.initClass( ve.ce.TextStateChunk ); |
| 37 | |
| 38 | /* Static methods */ |
| 39 | |
| 40 | /** |
| 41 | * Test whether two elements are equal as annotations. |
| 42 | * TODO: Improve this test. Currently annotations are compared by tag name and |
| 43 | * certain attributes, which results in a stricter test than that of |
| 44 | * ve.dm.ModelRegistry#matchElement . That is, a element pair that tests unequal |
| 45 | * here might still both match the same ve.dm.Annotation object. |
| 46 | * |
| 47 | * @static |
| 48 | * @param {HTMLElement} element1 An annotation element |
| 49 | * @param {HTMLElement} element2 Another annotation element |
| 50 | * @return {boolean} True if the elements are equal as annotations |
| 51 | */ |
| 52 | ve.ce.TextStateChunk.static.compareElements = function ( element1, element2 ) { |
| 53 | return element1 === element2 || ( |
| 54 | element1.nodeName === element2.nodeName && |
| 55 | element1.getAttribute( 'class' ) === element2.getAttribute( 'class' ) && |
| 56 | element1.getAttribute( 'typeof' ) === element2.getAttribute( 'typeof' ) && |
| 57 | element1.getAttribute( 'property' ) === element2.getAttribute( 'property' ) && |
| 58 | element1.getAttribute( 'href' ) === element2.getAttribute( 'href' ) |
| 59 | ); |
| 60 | }; |
| 61 | |
| 62 | /* Methods */ |
| 63 | |
| 64 | /** |
| 65 | * Test whether this chunk has the same annotations (in order) as another |
| 66 | * |
| 67 | * @param {ve.ce.TextStateChunk} other The other chunk |
| 68 | * @return {boolean} True if the chunks have the same annotations |
| 69 | */ |
| 70 | ve.ce.TextStateChunk.prototype.hasEqualElements = function ( other ) { |
| 71 | if ( this.elements === other.elements ) { |
| 72 | return true; |
| 73 | } |
| 74 | if ( this.elements.length !== other.elements.length ) { |
| 75 | return false; |
| 76 | } |
| 77 | for ( let i = 0, len = this.elements.length; i < len; i++ ) { |
| 78 | if ( !this.constructor.static.compareElements( this.elements[ i ], other.elements[ i ] ) ) { |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | return true; |
| 83 | }; |
| 84 | |
| 85 | /** |
| 86 | * Test whether this chunk is equal to another chunk in both elements and text. |
| 87 | * |
| 88 | * @param {ve.ce.TextStateChunk} other The other chunk |
| 89 | * @return {boolean} True if the chunks are equal |
| 90 | */ |
| 91 | ve.ce.TextStateChunk.prototype.isEqual = function ( other ) { |
| 92 | return this.text === other.text && this.type === other.type && this.hasEqualElements( other ); |
| 93 | }; |