mediawiki-extensions-Visual.../modules/ve/dm/annotations/ve.dm.LinkAnnotation.js
Ed Sanders 277c4f6c28 Change custom .getHash functions to .getHashObject
As described in the bug, ve.getHash performs JSON.stringify so to
customise a hash the object should just return an object to be
hashed, not the hash string itself.

Bug: 46895
Change-Id: If11071d4b04a01e25102ffb57240882f650ee10d
2013-04-08 23:29:56 +01:00

97 lines
2.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 {HTMLElement|Object} element
*/
ve.dm.LinkAnnotation = function VeDmLinkAnnotation( element ) {
// Parent constructor
ve.dm.Annotation.call( this, element );
};
/* Inheritance */
ve.inheritClass( ve.dm.LinkAnnotation, ve.dm.Annotation );
/* Static Properties */
/**
* @static
* @property static.name
* @inheritdoc
*/
ve.dm.LinkAnnotation.static.name = 'link';
/**
* @static
* @property static.matchTagNames
* @inheritdoc
*/
ve.dm.LinkAnnotation.static.matchTagNames = ['a'];
/* Methods */
/**
* Get annotation data, especially the href of the link.
*
* @method
* @param {HTMLElement} element
* @returns {Object} Annotation data, containing href property
*/
ve.dm.LinkAnnotation.prototype.getAnnotationData = function( element ) {
return { 'href': element.getAttribute( 'href' ) };
};
/**
* Convert to an object with HTML element information.
*
* @method
* @returns {Object} HTML element information, including tag and attributes properties
*/
ve.dm.LinkAnnotation.prototype.toHTML = function () {
var parentResult = ve.dm.Annotation.prototype.toHTML.call( this );
parentResult.tag = 'a';
parentResult.attributes.href = this.data.href;
return parentResult;
};
/**
* Get the hash object of the link annotation.
*
* This extends the basic annotation hash by adding htmlAttributes.rel
* if it present.
*
* This is a custom hash function for ve#getHash.
*
* @method
* @returns {Object} Object to hash
*/
ve.dm.LinkAnnotation.prototype.getHashObject = function () {
var keys = [ 'name', 'data' ], obj = {}, i;
for ( i = 0; i < keys.length; i++ ) {
if ( this[keys[i]] !== undefined ) {
obj[keys[i]] = this[keys[i]];
}
}
if ( this.htmlAttributes && this.htmlAttributes.rel ) {
obj.htmlAttributes = {};
obj.htmlAttributes.rel = this.htmlAttributes.rel;
}
return obj;
};
/* Registration */
ve.dm.modelRegistry.register( ve.dm.LinkAnnotation );