Merge "Fix handling of pasted internal red links"

This commit is contained in:
jenkins-bot 2020-02-07 21:04:53 +00:00 committed by Gerrit Code Review
commit be6777590c
2 changed files with 46 additions and 8 deletions

View file

@ -128,28 +128,38 @@ ve.dm.MWInternalLinkAnnotation.static.newFromTitle = function ( title, rawTitle
* True if the href pointed to the local wiki, false if href is external
*/
ve.dm.MWInternalLinkAnnotation.static.getTargetDataFromHref = function ( href, doc ) {
var relativeBase, relativeBaseRegex, relativeHref, isInternal, matches, data;
var relativeBase, relativeBaseRegex, relativeHref, isInternal, matches, data, uri;
function regexEscape( str ) {
return str.replace( /([.?*+^$[\]\\(){}|-])/g, '\\$1' );
}
// Protocol relative base
relativeBase = ve.resolveUrl( mw.config.get( 'wgArticlePath' ), doc ).replace( /^https?:/i, '' );
relativeBaseRegex = new RegExp( regexEscape( relativeBase ).replace( regexEscape( '$1' ), '(.*)' ) );
// Protocol relative href
relativeHref = href.replace( /^https?:/i, '' );
// Paths without a host portion are assumed to be internal
isInternal = !/^\/\//.test( relativeHref );
// Check if this matches the server's article path
matches = relativeHref.match( relativeBaseRegex );
// Check if this matches the server's article path
// Protocol relative base
relativeBase = ve.resolveUrl( mw.config.get( 'wgArticlePath' ), doc ).replace( /^https?:/i, '' );
relativeBaseRegex = new RegExp( regexEscape( relativeBase ).replace( regexEscape( '$1' ), '(.*)' ) );
matches = relativeHref.match( relativeBaseRegex );
if ( matches && matches[ 1 ].split( '#' )[ 0 ].indexOf( '?' ) === -1 ) {
// Take the relative path
href = matches[ 1 ];
isInternal = true;
}
// Check if this matches the server's script path (as used by red links)
relativeBase = ve.resolveUrl( mw.config.get( 'wgScript' ), doc ).replace( /^https?:/i, '' );
if ( relativeHref.indexOf( relativeBase ) === 0 ) {
uri = new mw.Uri( relativeHref );
if ( uri.query.title ) {
href = uri.query.title;
isInternal = true;
}
}
// This href doesn't necessarily come from Parsoid (and it might not have the "./" prefix), but
// this method will work fine.
data = ve.parseParsoidResourceName( href );

View file

@ -9,12 +9,27 @@ QUnit.module( 've.dm.MWInternalLinkAnnotation' );
QUnit.test( 'toDataElement', function ( assert ) {
var i, l,
doc = ve.dm.example.createExampleDocument(),
internalLink = function ( pageTitle ) {
externalLink = function ( href ) {
var link = document.createElement( 'a' );
link.setAttribute( 'href', location.origin + mw.Title.newFromText( pageTitle ).getUrl() );
link.setAttribute( 'href', href );
return link;
},
internalLink = function ( pageTitle, params ) {
var link = document.createElement( 'a' );
link.setAttribute( 'href', location.origin + mw.Title.newFromText( pageTitle ).getUrl( params ) );
return link;
},
cases = [
{
msg: 'Not an internal link',
element: externalLink( 'http://example.com/' ),
expected: {
type: 'link/mwExternal',
attributes: {
href: 'http://example.com/'
}
}
},
{
msg: 'Simple',
element: internalLink( 'Foo' ),
@ -28,6 +43,19 @@ QUnit.test( 'toDataElement', function ( assert ) {
}
}
},
{
msg: 'Red link',
element: internalLink( 'Foo', { action: 'edit', redlink: '1' } ),
expected: {
type: 'link/mwInternal',
attributes: {
lookupTitle: 'Foo',
normalizedTitle: 'Foo',
origTitle: 'Foo',
title: 'Foo'
}
}
},
{
// Because percent-encoded URLs aren't valid titles, but what they decode to might be
msg: 'Percent encoded characters',