2013-10-17 20:15:36 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWNumberedExternalLinkNode class.
|
|
|
|
*
|
2018-01-03 00:54:47 +00:00
|
|
|
* @copyright 2011-2018 VisualEditor Team and others; see AUTHORS.txt
|
2013-10-17 20:15:36 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DataModel MediaWiki numbered external link node.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.dm.LeafNode
|
2014-09-17 00:33:39 +00:00
|
|
|
* @mixins ve.dm.FocusableNode
|
2014-06-07 03:17:26 +00:00
|
|
|
*
|
2013-10-17 20:15:36 +00:00
|
|
|
* @constructor
|
|
|
|
* @param {Object} [element] Reference to element in linear model
|
|
|
|
*/
|
2014-06-07 03:17:26 +00:00
|
|
|
ve.dm.MWNumberedExternalLinkNode = function VeDmMWNumberedExternalLinkNode() {
|
2013-10-17 20:15:36 +00:00
|
|
|
// Parent constructor
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.dm.MWNumberedExternalLinkNode.super.apply( this, arguments );
|
2014-09-17 00:33:39 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
ve.dm.FocusableNode.call( this );
|
2013-10-17 20:15:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.dm.MWNumberedExternalLinkNode, ve.dm.LeafNode );
|
|
|
|
|
2014-09-17 00:33:39 +00:00
|
|
|
OO.mixinClass( ve.dm.MWNumberedExternalLinkNode, ve.dm.FocusableNode );
|
|
|
|
|
2013-10-17 20:15:36 +00:00
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
ve.dm.MWNumberedExternalLinkNode.static.name = 'link/mwNumberedExternal';
|
|
|
|
|
|
|
|
ve.dm.MWNumberedExternalLinkNode.static.isContent = true;
|
|
|
|
|
|
|
|
ve.dm.MWNumberedExternalLinkNode.static.matchTagNames = [ 'a' ];
|
|
|
|
|
2018-03-05 17:43:28 +00:00
|
|
|
ve.dm.MWNumberedExternalLinkNode.static.matchRdfaTypes = [ 'mw:ExtLink', 'mw:NumberedLink' ];
|
2013-10-17 20:15:36 +00:00
|
|
|
|
2014-08-02 10:34:30 +00:00
|
|
|
ve.dm.MWNumberedExternalLinkNode.static.blacklistedAnnotationTypes = [ 'link' ];
|
|
|
|
|
2016-03-17 15:30:59 +00:00
|
|
|
ve.dm.MWNumberedExternalLinkNode.static.matchFunction = function ( domElement ) {
|
2018-03-05 17:43:28 +00:00
|
|
|
// Must be empty, or explicitly flagged as a numbered link. We can't just
|
|
|
|
// rely on emptiness, because we give the link content for cross-document
|
|
|
|
// pastes so it won't be pruned. (And so it'll be functional in non-wiki
|
|
|
|
// contexts.)
|
|
|
|
return domElement.childNodes.length === 0 || domElement.getAttribute( 'rel' ).indexOf( 'mw:NumberedLink' ) !== -1;
|
2013-10-17 20:15:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
ve.dm.MWNumberedExternalLinkNode.static.toDataElement = function ( domElements ) {
|
|
|
|
return {
|
2014-08-22 20:50:48 +00:00
|
|
|
type: this.name,
|
|
|
|
attributes: {
|
2015-08-19 17:33:02 +00:00
|
|
|
href: domElements[ 0 ].getAttribute( 'href' )
|
2013-10-17 20:15:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-06-27 14:19:53 +00:00
|
|
|
ve.dm.MWNumberedExternalLinkNode.static.toDomElements = function ( dataElement, doc, converter ) {
|
|
|
|
var counter, offset,
|
|
|
|
node = this,
|
|
|
|
domElement = doc.createElement( 'a' );
|
|
|
|
|
|
|
|
// Ensure there is a text version of the counter in the clipboard
|
|
|
|
// as external documents may not have the same stylesheet - and Firefox
|
|
|
|
// discards empty tags on copy.
|
2018-04-28 13:43:18 +00:00
|
|
|
if ( converter.doesModeNeedRendering() ) {
|
2016-06-27 14:19:53 +00:00
|
|
|
counter = 1;
|
|
|
|
offset = converter.documentData.indexOf( dataElement );
|
|
|
|
|
|
|
|
if ( offset !== -1 ) {
|
|
|
|
converter.documentData.slice( 0, offset ).forEach( function ( el ) {
|
|
|
|
if ( el.type && el.type === node.name ) {
|
|
|
|
counter++;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
domElement.appendChild( doc.createTextNode( '[' + counter + ']' ) );
|
|
|
|
}
|
2013-10-17 20:15:36 +00:00
|
|
|
domElement.setAttribute( 'href', dataElement.attributes.href );
|
2018-03-05 17:43:28 +00:00
|
|
|
domElement.setAttribute( 'rel', 'mw:ExtLink mw:NumberedLink' );
|
2013-10-17 20:15:36 +00:00
|
|
|
return [ domElement ];
|
|
|
|
};
|
|
|
|
|
2015-08-13 00:58:23 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convenience wrapper for .getHref() on the current element.
|
|
|
|
*
|
|
|
|
* @return {string} Link href
|
|
|
|
*/
|
|
|
|
ve.dm.MWNumberedExternalLinkNode.prototype.getHref = function () {
|
|
|
|
return this.element.attributes.href;
|
|
|
|
};
|
|
|
|
|
2013-10-17 20:15:36 +00:00
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.dm.modelRegistry.register( ve.dm.MWNumberedExternalLinkNode );
|