Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/dm/annotations/ve.dm.MWInternalLinkAnnotation.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel MWInternalLinkAnnotation class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * DataModel MediaWiki internal link annotation.
10 *
11 * Example HTML sources:
12 *
13 * <a rel="mw:WikiLink">
14 *
15 * @class
16 * @extends ve.dm.LinkAnnotation
17 * @constructor
18 * @param {Object} element
19 */
20 ve.dm.MWInternalLinkAnnotation = function VeDmMWInternalLinkAnnotation() {
21 // Parent constructor
22 ve.dm.MWInternalLinkAnnotation.super.apply( this, arguments );
23 };
24
25 /* Inheritance */
26
27 OO.inheritClass( ve.dm.MWInternalLinkAnnotation, ve.dm.LinkAnnotation );
28
29 /* Static Properties */
30
31 ve.dm.MWInternalLinkAnnotation.static.name = 'link/mwInternal';
32
33 ve.dm.MWInternalLinkAnnotation.static.matchRdfaTypes = [ 'mw:WikiLink', 'mw:MediaLink' ];
34
35 // mw:MediaLink to non-existent files come with typeof="mw:Error"
36 ve.dm.MWInternalLinkAnnotation.static.allowedRdfaTypes = [ 'mw:Error', 'mw:LocalizedAttrs' ];
37
38 ve.dm.MWInternalLinkAnnotation.static.toDataElement = function ( domElements, converter ) {
39 const resource = domElements[ 0 ].getAttribute( 'resource' );
40
41 let targetData;
42 if ( resource ) {
43 targetData = mw.libs.ve.parseParsoidResourceName( resource );
44 } else {
45 targetData = mw.libs.ve.getTargetDataFromHref(
46 domElements[ 0 ].getAttribute( 'href' ),
47 converter.getTargetHtmlDocument()
48 );
49
50 if ( !targetData.isInternal ) {
51 return ve.dm.MWExternalLinkAnnotation.static.toDataElement( domElements, converter );
52 }
53 }
54
55 return {
56 type: this.name,
57 attributes: {
58 title: targetData.title,
59 normalizedTitle: this.normalizeTitle( targetData.title ),
60 lookupTitle: this.getLookupTitle( targetData.title )
61 }
62 };
63 };
64
65 /**
66 * Build element from a given mw.Title and raw title
67 *
68 * @param {mw.Title} title The title to link to.
69 * @return {Object} The element.
70 */
71 ve.dm.MWInternalLinkAnnotation.static.dataElementFromTitle = function ( title ) {
72 let target = title.toText();
73
74 if ( title.getFragment() ) {
75 target += '#' + title.getFragment();
76 }
77
78 const element = {
79 type: this.name,
80 attributes: {
81 title: target,
82 normalizedTitle: this.normalizeTitle( title ),
83 lookupTitle: this.getLookupTitle( title )
84 }
85 };
86
87 return element;
88 };
89
90 /**
91 * Build a ve.dm.MWInternalLinkAnnotation from a given mw.Title.
92 *
93 * @param {mw.Title} title The title to link to.
94 * @return {ve.dm.MWInternalLinkAnnotation} The annotation.
95 */
96 ve.dm.MWInternalLinkAnnotation.static.newFromTitle = function ( title ) {
97 const element = this.dataElementFromTitle( title );
98
99 return new ve.dm.MWInternalLinkAnnotation( element );
100 };
101
102 ve.dm.MWInternalLinkAnnotation.static.toDomElements = function () {
103 const parentResult = ve.dm.LinkAnnotation.static.toDomElements.apply( this, arguments );
104 // we just created that link so the 'rel' attribute should be safe
105 parentResult[ 0 ].setAttribute( 'rel', 'mw:WikiLink' );
106 return parentResult;
107 };
108
109 ve.dm.MWInternalLinkAnnotation.static.getHref = function ( dataElement ) {
110 let title = dataElement.attributes.title;
111
112 if ( title.slice( 0, 1 ) === '#' ) {
113 // Special case: For a newly created link to a #fragment with
114 // no explicit title use the current title as prefix (T218581)
115 // TODO: Pass a 'doc' param to getPageName
116 title = ve.init.target.getPageName() + title;
117 }
118
119 return mw.libs.ve.encodeParsoidResourceName( title );
120 };
121
122 /**
123 * Normalize title for comparison purposes.
124 * E.g. capitalisation and underscores.
125 *
126 * @param {string|mw.Title} original Original title
127 * @return {string} Normalized title, or the original string if it is invalid
128 */
129 ve.dm.MWInternalLinkAnnotation.static.normalizeTitle = function ( original ) {
130 const title = original instanceof mw.Title ? original : mw.Title.newFromText( original );
131 if ( !title ) {
132 return original;
133 }
134 return title.getPrefixedText() + ( title.getFragment() !== null ? '#' + title.getFragment() : '' );
135 };
136
137 /**
138 * Normalize title for lookup (search suggestion, existence) purposes.
139 *
140 * @param {string|mw.Title} original Original title
141 * @return {string} Normalized title, or the original string if it is invalid
142 */
143 ve.dm.MWInternalLinkAnnotation.static.getLookupTitle = function ( original ) {
144 const title = original instanceof mw.Title ? original : mw.Title.newFromText( original );
145 if ( !title ) {
146 return original;
147 }
148 return title.getPrefixedText();
149 };
150
151 /**
152 * Get the fragment for a title
153 *
154 * @static
155 * @param {string|mw.Title} original Original title
156 * @return {string|null} Fragment for the title, or null if it was invalid or missing
157 */
158 ve.dm.MWInternalLinkAnnotation.static.getFragment = function ( original ) {
159 const title = original instanceof mw.Title ? original : mw.Title.newFromText( original );
160 if ( !title ) {
161 return null;
162 }
163 return title.getFragment();
164 };
165
166 ve.dm.MWInternalLinkAnnotation.static.describeChange = function ( key, change ) {
167 if ( key === 'title' ) {
168 return ve.htmlMsg( 'visualeditor-changedesc-link-href', this.wrapText( 'del', change.from ), this.wrapText( 'ins', change.to ) );
169 }
170 return null;
171 };
172
173 /* Methods */
174
175 /**
176 * @inheritdoc
177 */
178 ve.dm.MWInternalLinkAnnotation.prototype.getComparableObject = function () {
179 return {
180 type: this.getType(),
181 normalizedTitle: this.getAttribute( 'normalizedTitle' )
182 };
183 };
184
185 /**
186 * @inheritdoc
187 */
188 ve.dm.MWInternalLinkAnnotation.prototype.getComparableHtmlAttributes = function () {
189 // Assume that wikitext never adds meaningful html attributes for comparison purposes,
190 // although ideally this should be decided by Parsoid (Bug T95028).
191 return {};
192 };
193
194 /**
195 * @inheritdoc
196 */
197 ve.dm.MWInternalLinkAnnotation.prototype.getDisplayTitle = function () {
198 return this.getAttribute( 'normalizedTitle' );
199 };
200
201 /**
202 * Convenience wrapper for .getFragment() on the current element.
203 *
204 * @see #static-getFragment
205 * @return {string} Fragment for the title, or an empty string if it was invalid
206 */
207 ve.dm.MWInternalLinkAnnotation.prototype.getFragment = function () {
208 return this.constructor.static.getFragment( this.getAttribute( 'normalizedTitle' ) );
209 };
210
211 /* Registration */
212
213 ve.dm.modelRegistry.register( ve.dm.MWInternalLinkAnnotation );