mediawiki-extensions-Visual.../modules/ve-mw/dm/annotations/ve.dm.MWNowikiAnnotation.js
Timo Tijhof 44623c9b2a ve.copy: Remove obsolete copyArray and copyObject
These have been pointing to the same method for a while now,
we can safely remove these obsolete aliases and just use it
as generic copy.

* Each file touched by my editor had its new line at EOF fixed
  where absent
* Don't copy an otherwise unused empty object
  (ve.dm.Converter)
* Use common ve#copy syntax instead to create a link
  (ve.dm.Document, ve.dm.example)
* Remove redundant conditionals for isArray/copyArray/copyObject
  (ve.dm.example)

Change-Id: If560e658dc1fb59bf01f702c97e3e82a50a8a255
2013-07-30 01:44:22 +02:00

85 lines
2.5 KiB
JavaScript

/*!
* VisualEditor DataModel MWNowikiAnnotation class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* DataModel MediaWiki nowiki annotation
*
* Represents `<nowiki>` tags (in HTML as `<span typeof="mw:Nowiki">`) and unwraps them when they change
* so as to retrigger Parsoid's escaping mechanism.
*
* @class
* @extends ve.dm.Annotation
* @constructor
* @param {Object} element [description]
*/
ve.dm.MWNowikiAnnotation = function VeDmMwNowikiAnnotation( element ) {
// Parent constructor
ve.dm.Annotation.call( this, element );
};
/* Inheritance */
ve.inheritClass( ve.dm.MWNowikiAnnotation, ve.dm.Annotation );
/* Static Properties */
ve.dm.MWNowikiAnnotation.static.name = 'mwNowiki';
ve.dm.MWNowikiAnnotation.static.matchRdfaTypes = [ 'mw:Nowiki' ];
ve.dm.MWNowikiAnnotation.static.toDataElement = function ( domElements ) {
return {
'type': 'mwNowiki',
'attributes': {
'originalDomElements': ve.copy( domElements )
}
};
};
ve.dm.MWNowikiAnnotation.static.toDomElements = function ( dataElement, doc, converter, childDomElements ) {
var i, len,
originalDomElements = dataElement.attributes.originalDomElements,
originalChildren = originalDomElements && originalDomElements[0] && originalDomElements[0].childNodes,
contentsChanged = false,
domElement = document.createElement( 'span' );
// Determine whether the contents changed
if ( !originalChildren || childDomElements.length !== originalChildren.length ) {
contentsChanged = true;
} else {
for ( i = 0, len = originalChildren.length; i < len; i++ ) {
if ( !originalChildren[i].isEqualNode( childDomElements[i] ) ) {
contentsChanged = true;
break;
}
}
}
// If the contents changed, unwrap, otherwise, restore
if ( contentsChanged ) {
return [];
}
domElement.setAttribute( 'typeof', 'mw:Nowiki' );
return [ domElement ];
};
ve.dm.MWNowikiAnnotation.static.getHashObject = function ( dataElement ) {
var parentResult = ve.dm.Annotation.static.getHashObject( dataElement );
if ( parentResult.attributes.originalDomElements ) {
// If present, replace originalDomElements with a DOM summary
parentResult.attributes = ve.copy( parentResult.attributes );
parentResult.attributes.originalDomElements = ve.copy(
parentResult.attributes.originalDomElements, ve.convertDomElements
);
}
return parentResult;
};
/* Registration */
ve.dm.modelRegistry.register( ve.dm.MWNowikiAnnotation );