mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-29 00:30:44 +00:00
6f8307d5d2
Because the Parsoid prefix format changed from /mw:Foo to /mw/Foo , the href format for internal links has changed from "/Foo" to "Foo". So the href is now simply the title, except that it may be preceded by one or more "../" if the title of the page we're on contains a '/'. So instead of stripping the leading slash from internal link hrefs and putting it back on the way out, only strip any leading "../"s and dump the titles directly into the hrefs on the way out. Also update the link test case for this, and add a test case for the ../ stripping. Change-Id: I3e0bdde20f22cda34eb45fc351de5e780419b6a2
89 lines
2.5 KiB
JavaScript
89 lines
2.5 KiB
JavaScript
/**
|
|
* VisualEditor data model LinkAnnotation class.
|
|
*
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* DataModel annotation for a link.
|
|
*
|
|
* @class
|
|
* @constructor
|
|
* @extends {ve.dm.Annotation}
|
|
*/
|
|
ve.dm.LinkAnnotation = function () {
|
|
// Inheritance
|
|
ve.dm.Annotation.call( this );
|
|
};
|
|
|
|
/* Static Members */
|
|
|
|
/**
|
|
* Converters.
|
|
*
|
|
* @see {ve.dm.Converter}
|
|
* @static
|
|
* @member
|
|
*/
|
|
ve.dm.LinkAnnotation.converters = {
|
|
'domElementTypes': ['a'],
|
|
'toDomElement': function ( subType, annotation ) {
|
|
var link = document.createElement( 'a' ), key, attributes;
|
|
// Restore html/* attributes
|
|
// TODO this should be done for all annotations, factor this out in the new API
|
|
attributes = annotation.data.htmlAttributes;
|
|
for ( key in attributes ) {
|
|
link.setAttribute( key, attributes[key] );
|
|
}
|
|
|
|
link.setAttribute( 'rel', 'mw:' + subType );
|
|
if ( subType === 'WikiLink' ) {
|
|
// Set href to title
|
|
// FIXME space -> _ is MW-specific
|
|
link.setAttribute( 'href', annotation.data.title.replace( / /g, '_' ) );
|
|
} else if ( subType === 'ExtLink' || subType === 'ExtLink/Numbered' || subType === 'ExtLink/URL' ) {
|
|
// Set href directly
|
|
link.setAttribute( 'href', annotation.data.href );
|
|
}
|
|
return link;
|
|
},
|
|
'toDataAnnotation': function ( tag, element ) {
|
|
var rel = element.getAttribute( 'rel' ) || '',
|
|
subType = rel.split( ':' )[1] || 'unknown',
|
|
href = element.getAttribute( 'href' ),
|
|
retval = {
|
|
'type': 'link/' + subType,
|
|
'data': {}
|
|
},
|
|
i, attribute;
|
|
if ( subType === 'WikiLink' ) {
|
|
// Get title from href
|
|
// 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 "../", so strip those.
|
|
// FIXME Both this and _ -> space are MW-specific
|
|
retval.data.title = href.replace( /^(\.\.\/)*/, '' ).replace( /_/g, ' ' );
|
|
} else if ( subType === 'ExtLink' || subType === 'ExtLink/Numbered' || subType === 'ExtLink/URL' ) {
|
|
retval.data.href = href;
|
|
}
|
|
|
|
// Preserve HTML attributes
|
|
// TODO this should be done for all annotations, factor this out in the new API
|
|
retval.data.htmlAttributes = {};
|
|
for ( i = 0; i < element.attributes.length; i++ ) {
|
|
attribute = element.attributes[i];
|
|
retval.data.htmlAttributes[attribute.name] = attribute.value;
|
|
}
|
|
return retval;
|
|
}
|
|
};
|
|
|
|
/* Registration */
|
|
|
|
ve.dm.annotationFactory.register( 'link', ve.dm.LinkAnnotation );
|
|
|
|
/* Inheritance */
|
|
|
|
ve.extendClass( ve.dm.LinkAnnotation, ve.dm.Annotation );
|