diff --git a/modules/ve-mw/ui/inspectors/ve.ui.MWLinkInspector.js b/modules/ve-mw/ui/inspectors/ve.ui.MWLinkInspector.js index 5e41266ebd..651ba6b14c 100644 --- a/modules/ve-mw/ui/inspectors/ve.ui.MWLinkInspector.js +++ b/modules/ve-mw/ui/inspectors/ve.ui.MWLinkInspector.js @@ -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 ); diff --git a/modules/ve-mw/ui/widgets/ve.ui.MWLinkTargetInputWidget.js b/modules/ve-mw/ui/widgets/ve.ui.MWLinkTargetInputWidget.js index 6ab9a45cc0..2bbf88cc2c 100644 --- a/modules/ve-mw/ui/widgets/ve.ui.MWLinkTargetInputWidget.js +++ b/modules/ve-mw/ui/widgets/ve.ui.MWLinkTargetInputWidget.js @@ -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' ) }