Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/dm/nodes/ve.dm.MWMagicLinkNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel MWMagicLinkNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * DataModel MediaWiki magic link node.
10 *
11 * @class
12 * @extends ve.dm.LeafNode
13 * @mixes ve.dm.FocusableNode
14 *
15 * @constructor
16 * @param {Object} [element] Reference to element in linear model
17 */
18 ve.dm.MWMagicLinkNode = function VeDmMWMagicLinkNode() {
19 // Parent constructor
20 ve.dm.MWMagicLinkNode.super.apply( this, arguments );
21
22 // Mixin constructors
23 ve.dm.FocusableNode.call( this );
24 };
25
26 /* Inheritance */
27
28 OO.inheritClass( ve.dm.MWMagicLinkNode, ve.dm.LeafNode );
29
30 OO.mixinClass( ve.dm.MWMagicLinkNode, ve.dm.FocusableNode );
31
32 /* Static Properties */
33
34 ve.dm.MWMagicLinkNode.static.name = 'link/mwMagic';
35
36 ve.dm.MWMagicLinkNode.static.isContent = true;
37
38 ve.dm.MWMagicLinkNode.static.matchTagNames = [ 'a' ];
39
40 ve.dm.MWMagicLinkNode.static.matchRdfaTypes = [ 'mw:WikiLink', 'mw:ExtLink' ];
41
42 // Allow additional 'rel' values in Parsoid output (T321437)
43 ve.dm.MWMagicLinkNode.static.allowedRdfaTypes = [ 'nofollow', 'noreferrer', 'noopener' ];
44
45 ve.dm.MWMagicLinkNode.static.disallowedAnnotationTypes = [ 'link' ];
46
47 /**
48 * Determine whether the given `element` is a magic link.
49 *
50 * @param {HTMLElement} element
51 * @return {boolean} True if the element is a magic link
52 */
53 ve.dm.MWMagicLinkNode.static.matchFunction = function ( element ) {
54 const children = element.childNodes,
55 href = element.getAttribute( 'href' );
56 // All children must be text nodes, or a <span> representing an entity.
57 for ( let i = 0; i < children.length; i++ ) {
58 if ( children[ i ].nodeType === Node.TEXT_NODE ) {
59 continue;
60 }
61 // <span typeof='mw:Entity'>...</span> (for &nbsp;)
62 if ( children[ i ].nodeType === Node.ELEMENT_NODE &&
63 children[ i ].tagName === 'SPAN' &&
64 children[ i ].getAttribute( 'typeof' ) === 'mw:Entity' ) {
65 continue;
66 }
67 return false;
68 }
69
70 // Check that text content matches one of the magic link types and that
71 // the href matches that expected for the magic link type.
72 return ve.dm.MWMagicLinkNode.static.validateHref( element.textContent, href );
73 };
74
75 /**
76 * Test that a proposed content string is valid for a magic link.
77 * If `optType` is given, additionally verify that the content string is
78 * valid for the particular type of magic link (RFC/ISBN/PMID).
79 *
80 * @param {string} content
81 * The content string to test.
82 * @param {string} [optType]
83 * The desired type of magic link, one of "RFC", "ISBN", or "PMID".
84 * If not supplied, returns true if the content is valid for any one
85 * of these.
86 * @return {boolean}
87 * True if the content string is valid for a magic link of the appropriate
88 * type (or any type).
89 */
90 ve.dm.MWMagicLinkNode.static.validateContent = function ( content, optType ) {
91 const type = ve.dm.MWMagicLinkType.static.fromContent( content );
92 if ( type === null || ( optType !== undefined && type.type !== optType ) ) {
93 // Not a valid magic link, or a magic link of the wrong type.
94 return false;
95 }
96 return true;
97 };
98
99 /**
100 * Test that a proposed content string and href is valid for a magic link.
101 *
102 * @param {string} content
103 * The content string to test.
104 * @param {string} href
105 * The URL target of the magic link.
106 * @return {boolean}
107 * True if the content string and href are valid for a magic link.
108 */
109 ve.dm.MWMagicLinkNode.static.validateHref = function ( content, href ) {
110 const type = ve.dm.MWMagicLinkType.static.fromContent( content );
111 return type && type.matchHref( href );
112 };
113
114 /**
115 * Return a link annotation appropriate for converting a magic link
116 * with the given content into a simple link, or `null` if the given
117 * content is not a valid magic link.
118 *
119 * @param {string} content
120 * @return {ve.dm.MWExternalLinkAnnotation|ve.dm.MWInternalLinkAnnotation|null}
121 */
122 ve.dm.MWMagicLinkNode.static.annotationFromContent = function ( content ) {
123 const type = ve.dm.MWMagicLinkType.static.fromContent( content );
124 return type !== null ? type.getAnnotation() : null;
125 };
126
127 /**
128 * @inheritdoc
129 */
130 ve.dm.MWMagicLinkNode.static.toDataElement = function ( domElements ) {
131 const textContent = domElements[ 0 ].textContent,
132 htmlContent = domElements[ 0 ].innerHTML;
133 return {
134 type: this.name,
135 attributes: {
136 content: textContent,
137 // These next two attributes allow lossless round-tripping
138 // if the original wikitext contained html entities like
139 // &nbsp;
140 origText: textContent,
141 origHtml: htmlContent
142 }
143 };
144 };
145
146 /**
147 * @inheritdoc
148 */
149 ve.dm.MWMagicLinkNode.static.toDomElements = function ( dataElement, doc ) {
150 const content = dataElement.attributes.content,
151 type = ve.dm.MWMagicLinkType.static.fromContent( content ),
152 href = type.getHref(),
153 domElement = doc.createElement( 'a' );
154 domElement.setAttribute( 'href', href );
155 domElement.setAttribute( 'rel', type.rel );
156 if ( content === dataElement.attributes.origText ) {
157 // Preserve <span typeof="mw:Entity"> elements from the original.
158 domElement.innerHTML = dataElement.attributes.origHtml;
159 } else {
160 domElement.textContent = content;
161 }
162 return [ domElement ];
163 };
164
165 /* Methods */
166
167 /**
168 * Return the link target appropriate for this magic link node.
169 *
170 * @return {string} Link href
171 */
172 ve.dm.MWMagicLinkNode.prototype.getHref = function () {
173 const content = this.element.attributes.content,
174 type = ve.dm.MWMagicLinkType.static.fromContent( content );
175 return type.getHref();
176 };
177
178 /**
179 * Return the rel attribute appropriate for this magic link node.
180 *
181 * @return {string} Either "mw:ExtLink" or "mw:WikiLink"
182 */
183 ve.dm.MWMagicLinkNode.prototype.getRel = function () {
184 const content = this.element.attributes.content,
185 type = ve.dm.MWMagicLinkType.static.fromContent( content );
186 return type.rel;
187 };
188
189 /**
190 * Return the type of this magic link node: one of "RFC", "PMID", or "ISBN".
191 *
192 * @return {string} Magic link type
193 */
194 ve.dm.MWMagicLinkNode.prototype.getMagicType = function () {
195 const content = this.element.attributes.content,
196 type = ve.dm.MWMagicLinkType.static.fromContent( content );
197 return type.type;
198 };
199
200 /**
201 * Return the numeric code associated with this magic link node.
202 *
203 * @return {string}
204 */
205 ve.dm.MWMagicLinkNode.prototype.getCode = function () {
206 const content = this.element.attributes.content,
207 type = ve.dm.MWMagicLinkType.static.fromContent( content );
208 return type.num;
209 };
210
211 /**
212 * Encapsulation of a particular magic link type.
213 *
214 * @class
215 * @private
216 *
217 * @constructor
218 * @param {string} type
219 * The type of magic link; one of `'ISBN'`, `'PMID'`, or `'RFC'`.
220 * @param {string} rel
221 * The value of the link's "rel" attribute.
222 * Either `'mw:ExtLink'` or `'mw:WikiLink'`.
223 * @param {string} content
224 * The content of the magic link.
225 */
226 ve.dm.MWMagicLinkType = function VeDmMWMagicLinkType( type, rel, content ) {
227 this.type = type;
228 this.rel = rel;
229 this.content = content;
230 // Make the code available as a property; this is also used for
231 // validity checking.
232 this.code = this.getCode();
233 };
234
235 OO.initClass( ve.dm.MWMagicLinkType );
236
237 /**
238 * @inheritdoc ve.dm.MWMagicLinkNode#annotationFromContent
239 */
240 ve.dm.MWMagicLinkType.prototype.getAnnotation = function () {
241 return new ve.dm.MWExternalLinkAnnotation( {
242 type: 'link/mwExternal',
243 attributes: { href: this.getHref() }
244 } );
245 };
246
247 /**
248 * @inheritdoc ve.dm.MWMagicLinkNode#getCode
249 * @protected
250 */
251 ve.dm.MWMagicLinkType.prototype.getCode = function () {
252 const m = /^([A-Z]+)[\t \u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]+(\d+)$/.exec( this.content );
253 if ( !m || m[ 1 ] !== this.type ) {
254 return null;
255 }
256 return m[ 2 ];
257 };
258
259 /**
260 * @method ve.dm.MWMagicLinkNode.getHref
261 * @inheritdoc ve.dm.MWMagicLinkNode#getHref
262 */
263
264 /**
265 * Return true if the given href is appropriate for this magic link.
266 *
267 * @param {string} href
268 * @return {boolean}
269 */
270 ve.dm.MWMagicLinkType.prototype.matchHref = function ( href ) {
271 return href.replace( /^https?:/i, '' ) === this.getHref().replace( /^https?:/i, '' );
272 };
273
274 /**
275 * Return the subclass of {@link ve.dm.MWMagicLinkType}
276 * appropriate for the given content, or `null` if the content
277 * is not appropriate for a magic link.
278 *
279 * @param {string} content
280 * @return {ve.dm.MWMagicLinkType|null}
281 */
282 ve.dm.MWMagicLinkType.static.fromContent = function ( content ) {
283 let type = null;
284 const m = /^(ISBN|PMID|RFC)/.exec( content ),
285 typeStr = m && m[ 1 ];
286 if ( typeStr === 'ISBN' ) {
287 type = new ve.dm.MWMagicLinkIsbnType( content );
288 } else if ( typeStr === 'PMID' ) {
289 type = new ve.dm.MWMagicLinkPmidType( content );
290 } else if ( typeStr === 'RFC' ) {
291 type = new ve.dm.MWMagicLinkRfcType( content );
292 }
293 // validate parsed number
294 return type && type.code !== null ? type : null;
295 };
296
297 /**
298 * An ISBN magic link.
299 *
300 * @class
301 * @extends ve.dm.MWMagicLinkType
302 * @private
303 *
304 * @constructor
305 * @param {string} content
306 */
307 ve.dm.MWMagicLinkIsbnType = function VeDmMWMagicLinkIsbnType( content ) {
308 // Parent constructor
309 ve.dm.MWMagicLinkIsbnType.super.call( this, 'ISBN', 'mw:WikiLink', content );
310 };
311
312 OO.inheritClass( ve.dm.MWMagicLinkIsbnType, ve.dm.MWMagicLinkType );
313
314 /**
315 * @inheritdoc
316 */
317 ve.dm.MWMagicLinkIsbnType.prototype.getAnnotation = function () {
318 const conf = mw.config.get( 'wgVisualEditorConfig' ),
319 title = mw.Title.newFromText( conf.specialBooksources + '/' + this.code );
320 return ve.dm.MWInternalLinkAnnotation.static.newFromTitle( title );
321 };
322
323 /**
324 * @inheritdoc
325 */
326 ve.dm.MWMagicLinkIsbnType.prototype.getCode = function () {
327 const content = this.content;
328
329 if ( !/^ISBN[^-0-9][\s\S]+[0-9Xx]$/.test( content ) ) {
330 return null;
331 }
332
333 // Remove unicode whitespace and dashes
334 const spaceOrDash = /[-\t \u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]+/g;
335 const isbnCode = content.replace( spaceOrDash, '' ).replace( /^ISBN/, '' );
336
337 // Verify format of ISBN
338 if ( !/^(97[89])?\d{9}[0-9Xx]$/.test( isbnCode ) ) {
339 return null;
340 }
341 return isbnCode.toUpperCase();
342 };
343
344 /**
345 * @inheritdoc
346 */
347 ve.dm.MWMagicLinkIsbnType.prototype.getHref = function () {
348 const conf = mw.config.get( 'wgVisualEditorConfig' );
349 return './' + conf.specialBooksources + '/' + this.code;
350 };
351
352 /**
353 * @inheritdoc
354 */
355 ve.dm.MWMagicLinkIsbnType.prototype.matchHref = function ( href ) {
356 const conf = mw.config.get( 'wgVisualEditorConfig' ),
357 matches = /^(?:[.]+\/)*([^/]+)\/(\d+[Xx]?)$/.exec( href );
358
359 if ( !matches ) {
360 return false;
361 }
362 // conf.specialBooksources has localized name for Special:Booksources
363 const normalized = mw.libs.ve.decodeURIComponentIntoArticleTitle( matches[ 1 ], true ).replace( ' ', '_' );
364 if ( normalized !== 'Special:BookSources' && normalized !== conf.specialBooksources ) {
365 return false;
366 }
367 if ( matches[ 2 ] !== this.code ) {
368 return false;
369 }
370 return true;
371 };
372
373 /**
374 * A PMID magic link.
375 *
376 * @class
377 * @extends ve.dm.MWMagicLinkType
378 * @private
379 *
380 * @constructor
381 * @param {string} content
382 */
383 ve.dm.MWMagicLinkPmidType = function VeDmMWMagicLinkPmidType( content ) {
384 // Parent constructor
385 ve.dm.MWMagicLinkPmidType.super.call( this, 'PMID', 'mw:ExtLink', content );
386 };
387
388 OO.inheritClass( ve.dm.MWMagicLinkPmidType, ve.dm.MWMagicLinkType );
389
390 ve.dm.MWMagicLinkPmidType.prototype.getHref = function () {
391 return mw.msg( 'pubmedurl', this.code );
392 };
393
394 /**
395 * An RFC magic link.
396 *
397 * @class
398 * @extends ve.dm.MWMagicLinkType
399 * @private
400 *
401 * @constructor
402 * @param {string} content
403 */
404 ve.dm.MWMagicLinkRfcType = function VeDmMWMagicLinkRfcType( content ) {
405 // Parent constructor
406 ve.dm.MWMagicLinkRfcType.super.call( this, 'RFC', 'mw:ExtLink', content );
407 };
408
409 OO.inheritClass( ve.dm.MWMagicLinkRfcType, ve.dm.MWMagicLinkType );
410
411 ve.dm.MWMagicLinkRfcType.prototype.getHref = function () {
412 return mw.msg( 'rfcurl', this.code );
413 };
414
415 /* Registration */
416
417 ve.dm.modelRegistry.register( ve.dm.MWMagicLinkNode );