2012-07-19 00:11:26 +00:00
|
|
|
/**
|
|
|
|
* VisualEditor data model LinkAnnotation class.
|
2012-07-19 21:25:16 +00:00
|
|
|
*
|
2012-07-19 00:11:26 +00:00
|
|
|
* @copyright 2011-2012 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
2012-05-31 23:50:16 +00:00
|
|
|
/**
|
|
|
|
* DataModel annotation for a link.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @extends {ve.dm.Annotation}
|
|
|
|
*/
|
2012-08-07 01:50:44 +00:00
|
|
|
ve.dm.LinkAnnotation = function () {
|
2012-05-31 23:50:16 +00:00
|
|
|
// Inheritance
|
|
|
|
ve.dm.Annotation.call( this );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Static Members */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converters.
|
|
|
|
*
|
|
|
|
* @see {ve.dm.Converter}
|
|
|
|
* @static
|
|
|
|
* @member
|
|
|
|
*/
|
|
|
|
ve.dm.LinkAnnotation.converters = {
|
2012-06-07 00:47:27 +00:00
|
|
|
'domElementTypes': ['a'],
|
2012-08-07 01:50:44 +00:00
|
|
|
'toDomElement': function ( subType, annotation ) {
|
2012-07-26 03:46:57 +00:00
|
|
|
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 );
|
2012-08-14 01:02:40 +00:00
|
|
|
if ( subType === 'WikiLink' ) {
|
2012-08-14 00:59:16 +00:00
|
|
|
// Set href to title
|
2012-07-27 00:30:35 +00:00
|
|
|
// FIXME space -> _ is MW-specific
|
2012-08-14 00:59:16 +00:00
|
|
|
link.setAttribute( 'href', annotation.data.title.replace( / /g, '_' ) );
|
2012-08-09 19:53:54 +00:00
|
|
|
} else if ( subType === 'ExtLink' || subType === 'ExtLink/Numbered' || subType === 'ExtLink/URL' ) {
|
2012-07-26 03:46:57 +00:00
|
|
|
// Set href directly
|
2012-06-08 22:17:18 +00:00
|
|
|
link.setAttribute( 'href', annotation.data.href );
|
2012-06-07 22:02:25 +00:00
|
|
|
}
|
2012-06-19 22:45:01 +00:00
|
|
|
return link;
|
2012-05-31 23:50:16 +00:00
|
|
|
},
|
2012-08-07 01:50:44 +00:00
|
|
|
'toDataAnnotation': function ( tag, element ) {
|
2012-06-12 01:02:21 +00:00
|
|
|
var rel = element.getAttribute( 'rel' ) || '',
|
2012-06-19 22:45:01 +00:00
|
|
|
subType = rel.split( ':' )[1] || 'unknown',
|
2012-06-14 01:54:24 +00:00
|
|
|
href = element.getAttribute( 'href' ),
|
2012-06-12 01:02:21 +00:00
|
|
|
retval = {
|
2012-06-19 22:45:01 +00:00
|
|
|
'type': 'link/' + subType,
|
|
|
|
'data': {}
|
2012-07-26 03:46:57 +00:00
|
|
|
},
|
|
|
|
i, attribute;
|
2012-08-14 01:02:40 +00:00
|
|
|
if ( subType === 'WikiLink' ) {
|
2012-08-14 00:59:16 +00:00
|
|
|
// 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, ' ' );
|
2012-08-09 19:53:54 +00:00
|
|
|
} else if ( subType === 'ExtLink' || subType === 'ExtLink/Numbered' || subType === 'ExtLink/URL' ) {
|
2012-06-19 22:45:01 +00:00
|
|
|
retval.data.href = href;
|
|
|
|
}
|
2012-07-26 03:46:57 +00:00
|
|
|
|
|
|
|
// 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;
|
2012-06-12 01:02:21 +00:00
|
|
|
}
|
|
|
|
return retval;
|
2012-05-31 23:50:16 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.dm.annotationFactory.register( 'link', ve.dm.LinkAnnotation );
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
ve.extendClass( ve.dm.LinkAnnotation, ve.dm.Annotation );
|