All files / src/dm/annotations ve.dm.LinkAnnotation.js

100% Statements 43/43
100% Branches 8/8
100% Functions 12/12
100% Lines 43/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                                1x   95x         1x       1x   1x   1x 29x 2x   27x               1x 19x 19x 19x     1x 5x 4x 4x 3x   1x       1x                         1x 298x                     1x 279x                   1x 5x               1x 6x 6x 6x 1x   5x           1x 361x                 1x 6x 6x 6x     1x 1x     1x 1x         1x  
/*!
 * VisualEditor DataModel LinkAnnotation class.
 *
 * @copyright See AUTHORS.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() {
	// Parent constructor
	ve.dm.LinkAnnotation.super.apply( this, arguments );
};
 
/* 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.toDataElement = function ( domElements ) {
	if ( !domElements[ 0 ].hasAttribute( 'href' ) ) {
		return ve.dm.SpanAnnotation.static.toDataElement.apply( ve.dm.SpanAnnotation.static, arguments );
	}
	return {
		type: this.name,
		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 ];
};
 
ve.dm.LinkAnnotation.static.describeChange = function ( key, change ) {
	if ( key === 'href' ) {
		var diff = this.getAttributeDiff( change.from, change.to );
		if ( diff ) {
			return ve.htmlMsg( 'visualeditor-changedesc-link-href-diff', diff );
		} else {
			return ve.htmlMsg( 'visualeditor-changedesc-link-href', this.wrapText( 'del', change.from ), this.wrapText( 'ins', change.to ) );
		}
	}
	// Parent method
	return ve.dm.LinkAnnotation.super.static.describeChange.apply( this, arguments );
};
 
/**
 * Get the link href from linear data. Helper function for toDomElements.
 *
 * Subclasses can override this if they provide complex href computation.
 *
 * @static
 * @inheritable
 * @param {Object} dataElement Linear model element
 * @return {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
 * @return {string} Link href
 */
ve.dm.LinkAnnotation.prototype.getHref = function () {
	return this.constructor.static.getHref( this.element );
};
 
/**
 * Get the display title for this link
 *
 * Can be overridden by special link types.
 *
 * @return {string} Display title
 */
ve.dm.LinkAnnotation.prototype.getDisplayTitle = function () {
	return this.getHref();
};
 
/**
 * Get the fragment / hash for the current href
 *
 * @return {string|null} The fragment, or null if none is present
 */
ve.dm.LinkAnnotation.prototype.getFragment = function () {
	var href = this.getHref(),
		hashIndex = href.indexOf( '#' );
	if ( hashIndex === -1 ) {
		return null;
	}
	return href.slice( hashIndex + 1 );
};
 
/**
 * @inheritdoc
 */
ve.dm.LinkAnnotation.prototype.getComparableObject = function () {
	return {
		type: this.getType(),
		href: this.getAttribute( 'href' )
	};
};
 
/**
 * @inheritdoc
 */
ve.dm.LinkAnnotation.prototype.getComparableHtmlAttributes = function () {
	var comparableAttributes = ve.dm.LinkAnnotation.super.prototype.getComparableHtmlAttributes.call( this );
	delete comparableAttributes.href;
	return comparableAttributes;
};
 
ve.dm.LinkAnnotation.prototype.describeAdded = function () {
	return [ ve.msg( 'visualeditor-changedesc-link-added', this.getDisplayTitle() ) ];
};
 
ve.dm.LinkAnnotation.prototype.describeRemoved = function () {
	return [ ve.msg( 'visualeditor-changedesc-link-removed', this.getDisplayTitle() ) ];
};
 
/* Registration */
 
ve.dm.modelRegistry.register( ve.dm.LinkAnnotation );