2013-10-17 20:15:36 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWNumberedExternalLinkNode class.
|
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 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-05-31 14:06:57 +00:00
|
|
|
ve.dm.MWNumberedExternalLinkNode.static.matchRdfaTypes = [ 'mw:ExtLink', 've: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.)
|
2018-05-31 14:06:57 +00:00
|
|
|
// Note that ve:NumberedLink is only used internally by VE for cross-document
|
|
|
|
// pastes and is never sent to Parsoid.
|
|
|
|
return domElement.childNodes.length === 0 || domElement.getAttribute( 'rel' ).indexOf( 've: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' );
|
|
|
|
|
2018-05-31 14:06:57 +00:00
|
|
|
domElement.setAttribute( 'href', dataElement.attributes.href );
|
|
|
|
domElement.setAttribute( 'rel', 'mw:ExtLink' );
|
|
|
|
|
2016-06-27 14:19:53 +00:00
|
|
|
// 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-05-31 14:06:57 +00:00
|
|
|
if ( converter.isForClipboard() ) {
|
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 + ']' ) );
|
2018-05-31 14:06:57 +00:00
|
|
|
// Explicitly mark as a numbered link as the node is no longer empty.
|
|
|
|
domElement.setAttribute( 'rel', 've:NumberedLink' );
|
2016-06-27 14:19:53 +00:00
|
|
|
}
|
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 );
|