2013-10-17 20:15:36 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWNumberedExternalLinkNode class.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright 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
|
2024-04-29 17:51:41 +00:00
|
|
|
* @mixes 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
|
|
|
|
2022-10-25 23:07:08 +00:00
|
|
|
// Allow additional 'rel' values in Parsoid output (T321437)
|
|
|
|
ve.dm.MWNumberedExternalLinkNode.static.allowedRdfaTypes = [ 'nofollow', 'noreferrer', 'noopener' ];
|
2022-10-24 16:36:31 +00:00
|
|
|
|
2020-06-10 11:15:18 +00:00
|
|
|
ve.dm.MWNumberedExternalLinkNode.static.disallowedAnnotationTypes = [ 'link' ];
|
2014-08-02 10:34:30 +00:00
|
|
|
|
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 ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const domElement = doc.createElement( 'a' );
|
2016-06-27 14:19:53 +00:00
|
|
|
|
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() ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
let counter = 1;
|
|
|
|
const offset = converter.documentData.indexOf( dataElement );
|
2016-06-27 14:19:53 +00:00
|
|
|
|
|
|
|
if ( offset !== -1 ) {
|
2024-04-30 16:44:25 +00:00
|
|
|
converter.documentData.slice( 0, offset ).forEach( ( el ) => {
|
2024-05-01 12:32:49 +00:00
|
|
|
if ( el.type && el.type === this.name ) {
|
2016-06-27 14:19:53 +00:00
|
|
|
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 );
|