Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/widgets/ve.ui.MWInternalLinkAnnotationWidget.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWInternalLinkAnnotationWidget class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * Creates an ve.ui.MWInternalLinkAnnotationWidget object.
10 *
11 * @class
12 * @extends ve.ui.LinkAnnotationWidget
13 *
14 * @constructor
15 * @param {Object} [config] Configuration options
16 */
17 ve.ui.MWInternalLinkAnnotationWidget = function VeUiMWInternalLinkAnnotationWidget() {
18 // Parent constructor
19 ve.ui.MWInternalLinkAnnotationWidget.super.apply( this, arguments );
20 };
21
22 /* Inheritance */
23
24 OO.inheritClass( ve.ui.MWInternalLinkAnnotationWidget, ve.ui.LinkAnnotationWidget );
25
26 /* Static Methods */
27
28 /**
29 * @inheritdoc
30 */
31 ve.ui.MWInternalLinkAnnotationWidget.static.getAnnotationFromText = function ( value ) {
32 const trimmed = value.trim(),
33 title = mw.Title.newFromText( trimmed );
34
35 if ( !title ) {
36 return null;
37 }
38 return ve.dm.MWInternalLinkAnnotation.static.newFromTitle( title );
39 };
40
41 /**
42 * @inheritdoc
43 */
44 ve.ui.MWInternalLinkAnnotationWidget.static.getTextFromAnnotation = function ( annotation ) {
45 return annotation ? annotation.getAttribute( 'normalizedTitle' ) : '';
46 };
47
48 /* Methods */
49
50 /**
51 * Create a text input widget to be used by the annotation widget
52 *
53 * @param {Object} [config] Configuration options
54 * @return {OO.ui.TextInputWidget} Text input widget
55 */
56 ve.ui.MWInternalLinkAnnotationWidget.prototype.createInputWidget = function ( config ) {
57 const input = new mw.widgets.TitleSearchWidget( ve.extendObject( {
58 icon: 'search',
59 excludeCurrentPage: true,
60 showImages: mw.config.get( 'wgVisualEditorConfig' ).usePageImages,
61 showDescriptions: mw.config.get( 'wgVisualEditorConfig' ).usePageDescriptions,
62 showDisambigsLast: true,
63 showInterwikis: true,
64 searchFragments: true,
65 addQueryInput: false,
66 api: ve.init.target.getContentApi(),
67 cache: ve.init.platform.linkCache
68 }, config ) );
69
70 // Put query first in DOM
71 // TODO: Consider upstreaming this to SearchWidget
72 input.$element.prepend( input.$query );
73
74 // Remove 'maxlength', because it should not apply to full URLs, which we allow users to paste
75 // here. Maximum length of page titles will still be enforced by JS validation later (we can't
76 // override maxLength config option, because that would break the validation).
77 input.getQuery().$input.removeAttr( 'maxlength' );
78
79 input.query.$input.attr( 'aria-label', mw.msg( 'visualeditor-linkinspector-button-link-internal' ) );
80 return input;
81 };
82
83 /**
84 * @inheritdoc
85 */
86 ve.ui.MWInternalLinkAnnotationWidget.prototype.getTextInputWidget = function () {
87 return this.input.query;
88 };
89
90 // #getHref returns the link title, not a fully resolved URL, however the only
91 // use case of widget.getHref is for link insertion text, which expects a title.
92 //
93 // Callers needing the full resolved URL should use ve.resolveUrl
94
95 /**
96 * @inheritdoc
97 */
98 ve.ui.MWInternalLinkAnnotationWidget.prototype.onTextChange = function ( value ) {
99 const htmlDoc = this.getElementDocument(),
100 namespacesWithSubpages = mw.config.get( 'wgVisualEditorConfig' ).namespacesWithSubpages,
101 basePageObj = mw.Title.newFromText( mw.config.get( 'wgRelevantPageName' ) );
102 // Specific thing we want to check: has a valid URL for an internal page
103 // been pasted into here, in which case we want to convert it to just the
104 // page title. This has to happen /here/ because a URL can reference a
105 // valid page while not being a valid Title (e.g. if it contains a "%").
106 if ( ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( value ) ) {
107 const targetData = mw.libs.ve.getTargetDataFromHref(
108 value,
109 htmlDoc
110 );
111 if ( targetData.isInternal ) {
112 value = targetData.title;
113 this.input.query.setValue( targetData.title );
114 }
115 } else if ( namespacesWithSubpages.indexOf( basePageObj.namespace ) !== -1 && value[ 0 ] === '/' ) {
116 // This does make it more-difficult to deliberately link to a page in the
117 // default namespace that starts with a / when you're on a subpage-allowing
118 // namespace. However, the exact same trick you need to know to make it work
119 // in plain wikitext applies: search for `:/foo`.
120 value = basePageObj.getPrefixedText() + value;
121 this.input.query.setValue( value );
122 }
123 return ve.ui.MWInternalLinkAnnotationWidget.super.prototype.onTextChange.call( this, value );
124 };