ui.MWLinkTargetInputWidget: Fix crash on invalid input

Though the initialisation works since core has been fixed, there
are still plently of cases where we take real user input that can
genuinely be invalid.

Most notably, you couldn't make a link to [[.com]] because the
link input widget would crash on an exception from mw.Title.

Even after core was fixed (and ".com" is now valid), one still
couldn't make that link. This time because '.' is an invalid title,
and we create a Title object for that while typing ".com".

ve.ui.MWLinkTargetInputWidget#getLookupMenuItemsFromData:
* Guarded against mw.Title throwing by using newFromText
  and checking it first.

ve.ui.MWLinkInspector#static.legalTitle:
* Removed in favour of checking whether newFromText returns a
  truthy value.

Change-Id: I580bfccb83f86be3ad7e83d31f0834e1cde7df9c
This commit is contained in:
Timo Tijhof 2013-10-09 23:29:29 +02:00 committed by Catrope
parent eb515188ec
commit d9153a2f1d
2 changed files with 11 additions and 21 deletions

View file

@ -48,7 +48,7 @@ ve.ui.MWLinkInspector.static.linkTargetInputWidget = ve.ui.MWLinkTargetInputWidg
* @returns {ve.dm.MWInternalLinkAnnotation|ve.dm.MWExternalLinkAnnotation|null}
*/
ve.ui.MWLinkInspector.prototype.getAnnotationFromText = function ( target ) {
var title;
var title = mw.Title.newFromText( target );
// Figure out if this is an internal or external link
if ( ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( target ) ) {
@ -59,13 +59,12 @@ ve.ui.MWLinkInspector.prototype.getAnnotationFromText = function ( target ) {
'href': target
}
} );
} else if ( ve.ui.MWLinkInspector.static.legalTitle.test( target ) ) {
} else if ( title ) {
// Internal link
// TODO: In the longer term we'll want to have autocompletion and existence and validity
// checks using AJAX
title = mw.Title.newFromText( target );
if ( title && ( title.getNamespaceId() === 6 || title.getNamespaceId() === 14 ) ) {
if ( title.getNamespaceId() === 6 || title.getNamespaceId() === 14 ) {
// File: or Category: link
// We have to prepend a colon so this is interpreted as a link
// rather than an image inclusion or categorization
@ -80,23 +79,12 @@ ve.ui.MWLinkInspector.prototype.getAnnotationFromText = function ( target ) {
}
} );
} else {
// Doesn't look like an external link and mw.Title considered it an illegal value,
// for an internal link.
return null;
}
};
/* Static Properties */
/**
* Regular expression matching a valid internal link
*
* This is a slightly modified version of the PHP regex
* - unicode range changed from 0080-00ff to 0080-ffff
* - added '(#.*)?' to allow section links
*
* @type {RegExp}
*/
ve.ui.MWLinkInspector.static.legalTitle = /^[ %!"$&'()*,\-.\/0-9:;=?@A-Z\\^_`a-z~\u0080-\uFFFF+]+(#.*)?$/;
/* Registration */
ve.ui.inspectorFactory.register( ve.ui.MWLinkInspector );

View file

@ -106,9 +106,10 @@ ve.ui.MWLinkTargetInputWidget.prototype.getLookupMenuItemsFromData = function (
// If not found, run value through mw.Title to avoid treating a match as a
// mismatch where normalisation would make them matching (bug 48476)
pageExistsExact = ve.indexOf( this.value, matchingPages ) !== -1,
pageExists =
pageExistsExact ||
ve.indexOf( new mw.Title( this.value ).getPrefixedText(), matchingPages ) !== -1;
titleObj = mw.Title.newFromText( this.value ),
pageExists = pageExistsExact || (
titleObj && ve.indexOf( titleObj.getPrefixedText(), matchingPages ) !== -1
);
// External link
if ( ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( this.value ) ) {
@ -124,7 +125,7 @@ ve.ui.MWLinkTargetInputWidget.prototype.getLookupMenuItemsFromData = function (
// Internal link
if ( !pageExists ) {
if ( ve.ui.MWLinkInspector.static.legalTitle.test( this.value ) ) {
if ( titleObj ) {
items.push( new ve.ui.MenuSectionItemWidget(
'newPage',
{ '$$': menu$$, 'label': ve.msg( 'visualeditor-linkinspector-suggest-new-page' ) }
@ -134,6 +135,7 @@ ve.ui.MWLinkTargetInputWidget.prototype.getLookupMenuItemsFromData = function (
{ '$$': menu$$, 'rel': 'newPage', 'label': this.value }
) );
} else {
// If no title object could be created, it means the title is illegal
item = new ve.ui.MenuSectionItemWidget(
'illegalTitle',
{ '$$': menu$$, 'label': ve.msg( 'visualeditor-linkinspector-illegal-title' ) }