Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/annotations/ve.dm.LinkAnnotation.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel LinkAnnotation class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * DataModel link annotation.
9 *
10 * Represents `<a>` tags that don't have a specific type.
11 *
12 * @class
13 * @extends ve.dm.Annotation
14 * @constructor
15 * @param {Object} element
16 */
17 ve.dm.LinkAnnotation = function VeDmLinkAnnotation() {
18 // Parent constructor
19 ve.dm.LinkAnnotation.super.apply( this, arguments );
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.dm.LinkAnnotation, ve.dm.Annotation );
25
26 /* Static Properties */
27
28 ve.dm.LinkAnnotation.static.name = 'link';
29
30 ve.dm.LinkAnnotation.static.matchTagNames = [ 'a' ];
31
32 ve.dm.LinkAnnotation.static.toDataElement = function ( domElements ) {
33 if ( !domElements[ 0 ].hasAttribute( 'href' ) ) {
34 return ve.dm.SpanAnnotation.static.toDataElement.apply( ve.dm.SpanAnnotation.static, arguments );
35 }
36 return {
37 type: this.name,
38 attributes: {
39 href: domElements[ 0 ].getAttribute( 'href' )
40 }
41 };
42 };
43
44 ve.dm.LinkAnnotation.static.toDomElements = function ( dataElement, doc ) {
45 const domElement = doc.createElement( 'a' );
46 domElement.setAttribute( 'href', this.getHref( dataElement ) );
47 return [ domElement ];
48 };
49
50 ve.dm.LinkAnnotation.static.describeChange = function ( key, change ) {
51 if ( key === 'href' ) {
52 const diff = this.getAttributeDiff( change.from, change.to );
53 if ( diff ) {
54 return ve.htmlMsg( 'visualeditor-changedesc-link-href-diff', diff );
55 } else {
56 return ve.htmlMsg( 'visualeditor-changedesc-link-href', this.wrapText( 'del', change.from ), this.wrapText( 'ins', change.to ) );
57 }
58 }
59 // Parent method
60 return ve.dm.LinkAnnotation.super.static.describeChange.apply( this, arguments );
61 };
62
63 /**
64 * Get the link href from linear data. Helper function for toDomElements.
65 *
66 * Subclasses can override this if they provide complex href computation.
67 *
68 * @static
69 * @inheritable
70 * @param {Object} dataElement Linear model element
71 * @return {string} Link href
72 */
73 ve.dm.LinkAnnotation.static.getHref = function ( dataElement ) {
74 return dataElement.attributes.href;
75 };
76
77 /* Methods */
78
79 /**
80 * Convenience wrapper for .getHref() on the current element.
81 *
82 * @see #static-getHref
83 * @return {string} Link href
84 */
85 ve.dm.LinkAnnotation.prototype.getHref = function () {
86 return this.constructor.static.getHref( this.element );
87 };
88
89 /**
90 * Get the display title for this link
91 *
92 * Can be overridden by special link types.
93 *
94 * @return {string} Display title
95 */
96 ve.dm.LinkAnnotation.prototype.getDisplayTitle = function () {
97 return this.getHref();
98 };
99
100 /**
101 * Get the fragment / hash for the current href
102 *
103 * @return {string|null} The fragment, or null if none is present
104 */
105 ve.dm.LinkAnnotation.prototype.getFragment = function () {
106 const href = this.getHref(),
107 hashIndex = href.indexOf( '#' );
108 if ( hashIndex === -1 ) {
109 return null;
110 }
111 return href.slice( hashIndex + 1 );
112 };
113
114 /**
115 * @inheritdoc
116 */
117 ve.dm.LinkAnnotation.prototype.getComparableObject = function () {
118 return {
119 type: this.getType(),
120 href: this.getAttribute( 'href' )
121 };
122 };
123
124 /**
125 * @inheritdoc
126 */
127 ve.dm.LinkAnnotation.prototype.getComparableHtmlAttributes = function () {
128 const comparableAttributes = ve.dm.LinkAnnotation.super.prototype.getComparableHtmlAttributes.call( this );
129 delete comparableAttributes.href;
130 return comparableAttributes;
131 };
132
133 ve.dm.LinkAnnotation.prototype.describeAdded = function () {
134 return [ ve.msg( 'visualeditor-changedesc-link-added', this.getDisplayTitle() ) ];
135 };
136
137 ve.dm.LinkAnnotation.prototype.describeRemoved = function () {
138 return [ ve.msg( 'visualeditor-changedesc-link-removed', this.getDisplayTitle() ) ];
139 };
140
141 /* Registration */
142
143 ve.dm.modelRegistry.register( ve.dm.LinkAnnotation );