From aeca19c9ea2c513bffd49e00e01c705004b7f033 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Mon, 27 Jun 2016 15:19:53 +0100 Subject: [PATCH] Ensure text version of counter is written to external links when copying Bug: T137503 Change-Id: Iae791253ea272847984fee5d0586dc302027f1bd --- .../nodes/ve.dm.MWNumberedExternalLinkNode.js | 23 +++++++++++++++++-- modules/ve-mw/tests/dm/ve.dm.mwExample.js | 3 ++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/modules/ve-mw/dm/nodes/ve.dm.MWNumberedExternalLinkNode.js b/modules/ve-mw/dm/nodes/ve.dm.MWNumberedExternalLinkNode.js index 9011b9af29..a8cc1256a1 100644 --- a/modules/ve-mw/dm/nodes/ve.dm.MWNumberedExternalLinkNode.js +++ b/modules/ve-mw/dm/nodes/ve.dm.MWNumberedExternalLinkNode.js @@ -55,8 +55,27 @@ ve.dm.MWNumberedExternalLinkNode.static.toDataElement = function ( domElements ) }; }; -ve.dm.MWNumberedExternalLinkNode.static.toDomElements = function ( dataElement, doc ) { - var domElement = doc.createElement( 'a' ); +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. + if ( converter.isForClipboard() ) { + 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 + ']' ) ); + } domElement.setAttribute( 'href', dataElement.attributes.href ); domElement.setAttribute( 'rel', 'mw:ExtLink' ); return [ domElement ]; diff --git a/modules/ve-mw/tests/dm/ve.dm.mwExample.js b/modules/ve-mw/tests/dm/ve.dm.mwExample.js index 73d8c439ab..e87cf585e1 100644 --- a/modules/ve-mw/tests/dm/ve.dm.mwExample.js +++ b/modules/ve-mw/tests/dm/ve.dm.mwExample.js @@ -1106,7 +1106,8 @@ ve.dm.mwExample.domToDataCases = { { type: '/paragraph' }, { type: 'internalList' }, { type: '/internalList' } - ] + ], + clipboardBody: '

Foo[1]Bar

' }, 'URL link': { body: '

mw

',