mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-25 14:56:20 +00:00
5c04118c07
Centralize href computation in getHref(). Because getHref() is provided by the generic LinkAnnotation class, the subclass implementation is now simpler. Bug: 51487 Change-Id: Ia6ca85bc84b4f4453b572285836adb631e8d0683
89 lines
2 KiB
JavaScript
89 lines
2 KiB
JavaScript
/*!
|
|
* VisualEditor DataModel LinkAnnotation class.
|
|
*
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* DataModel link annotation.
|
|
*
|
|
* Represents `<a>` tags that don't have a specific type.
|
|
*
|
|
* @class
|
|
* @extends ve.dm.Annotation
|
|
* @constructor
|
|
* @param {Object} element
|
|
*/
|
|
ve.dm.LinkAnnotation = function VeDmLinkAnnotation( element ) {
|
|
// Parent constructor
|
|
ve.dm.Annotation.call( this, element );
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
OO.inheritClass( ve.dm.LinkAnnotation, ve.dm.Annotation );
|
|
|
|
/* Static Properties */
|
|
|
|
ve.dm.LinkAnnotation.static.name = 'link';
|
|
|
|
ve.dm.LinkAnnotation.static.matchTagNames = ['a'];
|
|
|
|
ve.dm.LinkAnnotation.static.splitOnWordbreak = true;
|
|
|
|
ve.dm.LinkAnnotation.static.toDataElement = function ( domElements ) {
|
|
return {
|
|
'type': 'link',
|
|
'attributes': {
|
|
'href': domElements[0].getAttribute( 'href' )
|
|
}
|
|
};
|
|
};
|
|
|
|
ve.dm.LinkAnnotation.static.toDomElements = function ( dataElement, doc ) {
|
|
var domElement = doc.createElement( 'a' );
|
|
domElement.setAttribute( 'href', this.getHref( dataElement ) );
|
|
return [ domElement ];
|
|
};
|
|
|
|
/**
|
|
* Get the link href from linear data. Helper function for toDomElements.
|
|
*
|
|
* Subclasses can override this if they provide complex href computation.
|
|
*
|
|
* @static
|
|
* @method
|
|
* @inheritable
|
|
* @param {Object} dataElement Linear model element
|
|
* @returns {string} Link href
|
|
*/
|
|
ve.dm.LinkAnnotation.static.getHref = function ( dataElement ) {
|
|
return dataElement.attributes.href;
|
|
};
|
|
|
|
/* Methods */
|
|
|
|
/**
|
|
* Convenience wrapper for .getHref() on the current element.
|
|
* @see #static-getHref
|
|
* @returns {string} Link href
|
|
*/
|
|
ve.dm.LinkAnnotation.prototype.getHref = function () {
|
|
return this.constructor.static.getHref( this.element );
|
|
};
|
|
|
|
/**
|
|
* @returns {Object}
|
|
*/
|
|
ve.dm.LinkAnnotation.prototype.getComparableObject = function () {
|
|
return {
|
|
'type': this.getType(),
|
|
'href': this.getAttribute( 'href' )
|
|
};
|
|
};
|
|
|
|
/* Registration */
|
|
|
|
ve.dm.modelRegistry.register( ve.dm.LinkAnnotation );
|