all files / src/dm/annotations/ ve.dm.LanguageAnnotation.js

100% Statements 27/27
100% Branches 13/13
100% Functions 6/6
100% Lines 27/27
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                                  35×                   50×   50×                       15× 15×   15× 12×     15×                                           32×                  
/*!
 * VisualEditor DataModel LanguageAnnotation class.
 *
 * @copyright 2011-2019 VisualEditor Team and others; see http://ve.mit-license.org
 */
 
/**
 * DataModel language annotation.
 *
 * Represents `<span>` tags with 'lang' and 'dir' properties.
 *
 * @class
 * @extends ve.dm.Annotation
 * @constructor
 * @param {Object} element
 */
ve.dm.LanguageAnnotation = function VeDmLanguageAnnotation() {
	// Parent constructor
	ve.dm.LanguageAnnotation.super.apply( this, arguments );
};
 
/* Inheritance */
 
OO.inheritClass( ve.dm.LanguageAnnotation, ve.dm.Annotation );
 
/* Static Properties */
 
ve.dm.LanguageAnnotation.static.name = 'meta/language';
 
ve.dm.LanguageAnnotation.static.matchTagNames = [ 'span' ];
 
ve.dm.LanguageAnnotation.static.matchFunction = function ( domElement ) {
	var lang = domElement.getAttribute( 'lang' ),
		dir = ( domElement.getAttribute( 'dir' ) || '' ).toLowerCase();
	return lang || dir === 'ltr' || dir === 'rtl';
};
 
ve.dm.LanguageAnnotation.static.applyToAppendedContent = true;
 
ve.dm.LanguageAnnotation.static.toDataElement = function ( domElements ) {
	return {
		type: this.name,
		attributes: {
			lang: domElements[ 0 ].getAttribute( 'lang' ),
			dir: domElements[ 0 ].getAttribute( 'dir' )
		}
	};
};
 
ve.dm.LanguageAnnotation.static.toDomElements = function ( dataElement, doc ) {
	var domElement = doc.createElement( 'span' );
	if ( dataElement.attributes.lang ) {
		domElement.setAttribute( 'lang', dataElement.attributes.lang );
	}
	if ( dataElement.attributes.dir ) {
		domElement.setAttribute( 'dir', dataElement.attributes.dir );
	}
 
	return [ domElement ];
};
 
ve.dm.LanguageAnnotation.static.describeChange = function ( key, change ) {
	if ( key === 'lang' ) {
		return ve.htmlMsg( 'visualeditor-changedesc-language',
			this.wrapText( 'del', ve.init.platform.getLanguageName( change.from.toLowerCase() ) ),
			this.wrapText( 'ins', ve.init.platform.getLanguageName( change.to.toLowerCase() ) )
		);
	}
 
	// TODO: Show something nicer than 'null', 'ltr', and 'rtl'.
	if ( key === 'dir' ) {
		return ve.htmlMsg( 'visualeditor-changedesc-direction',
			this.wrapText( 'del', change.from.toLowerCase() ),
			this.wrapText( 'ins', change.to.toLowerCase() )
		);
	}
 
	// Parent method
	return ve.dm.LanguageAnnotation.parent.static.describeChange.apply( this, arguments );
};
 
/* Methods */
 
/**
 * @return {Object}
 */
ve.dm.LanguageAnnotation.prototype.getComparableObject = function () {
	return {
		type: 'meta/language',
		lang: this.getAttribute( 'lang' ),
		dir: this.getAttribute( 'dir' )
	};
};
 
/* Registration */
 
ve.dm.modelRegistry.register( ve.dm.LanguageAnnotation );