mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-12-11 05:56:11 +00:00
Merge "Fix handling of pasted internal red links"
This commit is contained in:
commit
be6777590c
|
@ -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 );
|
||||
|
|
|
@ -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',
|
||||
|
|
Loading…
Reference in a new issue