Support external links in MWInternalLinkAnnotation

As URLs from the clipboard are always absolute, we need to detect if these
are from the same wiki as the current document, and if so convert back to
relative for Parsoid.

Bug: 58136
Change-Id: Id251afe65193fc6356628f1deb5ed757f8a6d347
This commit is contained in:
Ed Sanders 2014-01-06 13:56:55 +00:00
parent 2284cb9aae
commit 217ea940d5
2 changed files with 77 additions and 4 deletions

View file

@ -35,13 +35,33 @@ ve.dm.MWInternalLinkAnnotation.static.name = 'link/mwInternal';
ve.dm.MWInternalLinkAnnotation.static.matchRdfaTypes = ['mw:WikiLink'];
ve.dm.MWInternalLinkAnnotation.static.toDataElement = function ( domElements ) {
// Get title from href
function regexEscape( str ) {
return str.replace( /([.?*+^$[\]\\(){}|-])/g, '\\$1' );
}
var matches, normalizedTitle,
// Protocol relative base
relativeBase = new mw.Uri( mw.config.get( 'wgArticlePath' ) ).toString().replace( /^https?:/, '' ),
relativeBaseRegex = new RegExp( regexEscape( relativeBase ).replace( regexEscape( '$1' ), '(.*)' ) ),
href = domElements[0].getAttribute( 'href' ),
// Protocol relative href
relativeHref = href.replace( /^https?:/, '' );
// Check if this matches the server's article path
matches = relativeHref.match ( relativeBaseRegex );
if ( matches ) {
// Take the relative path
href = matches[1];
}
// The href is simply the title, unless we're dealing with a page that has slashes in its name
// in which case it's preceded by one or more instances of "./" or "../", so strip those
/*jshint regexp:false */
var matches = domElements[0].getAttribute( 'href' ).match( /^((?:\.\.?\/)*)(.*)$/ ),
// Normalize capitalisation and underscores
normalizedTitle = ve.dm.MWInternalLinkAnnotation.static.normalizeTitle( matches[2] );
matches = href.match( /^((?:\.\.?\/)*)(.*)$/ );
// Normalize capitalisation and underscores
normalizedTitle = ve.dm.MWInternalLinkAnnotation.static.normalizeTitle( matches[2] );
return {
'type': 'link/mwInternal',
'attributes': {

View file

@ -5,6 +5,8 @@
* @license The MIT License (MIT); see LICENSE.txt
*/
/*global mw */
/**
* @class
* @singleton
@ -155,6 +157,32 @@ ve.dm.mwExample.MWTransclusion.mixedStoreItems = {
'value': $( ve.dm.mwExample.MWTransclusion.mixed ).toArray()
};
ve.dm.mwExample.MWInternalLink = {
'absoluteHref': new mw.Uri( '/wiki/Foo/Bar' ).toString()
};
ve.dm.mwExample.MWInternalLink.absoluteOpen = '<a rel="mw:WikiLink" href="' + ve.dm.mwExample.MWInternalLink.absoluteHref + '">';
ve.dm.mwExample.MWInternalLink.absoluteData = {
'type': 'link/mwInternal',
'attributes': {
'title': 'Foo/Bar',
'origTitle': 'Foo/Bar',
'normalizedTitle': 'Foo/Bar',
'hrefPrefix': ''
},
'htmlAttributes': [
{
'values': {
'href': ve.dm.mwExample.MWInternalLink.absoluteHref,
'rel': 'mw:WikiLink'
},
'computed': {
'href': ve.dm.mwExample.MWInternalLink.absoluteHref
}
}
]
};
ve.dm.mwExample.MWBlockImage = {
'html':
'<figure typeof="mw:Image/Thumb" class="mw-halign-right foobar">' +
@ -1501,6 +1529,31 @@ ve.dm.mwExample.domToDataCases = {
{ 'type': '/internalList' }
]
},
'internal link with absolute path': {
'body': '<p>' + ve.dm.mwExample.MWInternalLink.absoluteOpen + 'Foo</a></p>',
'data': [
{ 'type': 'paragraph' },
[
'F',
[ ve.dm.mwExample.MWInternalLink.absoluteData ]
],
[
'o',
[ ve.dm.mwExample.MWInternalLink.absoluteData ]
],
[
'o',
[ ve.dm.mwExample.MWInternalLink.absoluteData ]
],
{ 'type': '/paragraph' },
{ 'type': 'internalList' },
{ 'type': '/internalList' }
],
'normalizedBody': '<p><a rel="mw:WikiLink" href="Foo/Bar">Foo</a></p>',
'mwConfig': {
'wgArticlePath': '/wiki/$1'
}
},
'numbered external link (empty mw:Extlink)': {
'body': '<p>Foo<a rel="mw:ExtLink" href="http://www.example.com"></a>Bar</p>',
'data': [