Wikitext paste: Discard autoGenerated items after conversion

Otherwise the reference contents (stored in the reflist) is lost.

Bug: T134228
Depends-On: I718963e3c460a5bed38fba4152b03442bbe6169e
Change-Id: I20171eb97eb1b4757a2c8a25217adb4d680a9057
This commit is contained in:
Ed Sanders 2016-05-03 15:11:00 +01:00 committed by James D. Forrester
parent 04f1f0ba03
commit f30000ed4c
2 changed files with 60 additions and 10 deletions

View file

@ -229,6 +229,45 @@ QUnit.test( 'convert', function ( assert ) {
{ type: '/internalList' }
]
},
{
msg: 'Simple reference',
pasteString: '<ref>Foo</ref>',
pasteType: 'text/plain',
parsoidResponse: '<p><span about="#mwt2" class="mw-ref" id="cite_ref-1" rel="dc:references" typeof="mw:Extension/ref" data-mw=\'{"name":"ref","body":{"id":"mw-reference-text-cite_note-1"},"attrs":{}}\'>[1]</span></p>' +
'<ol class="mw-references" typeof="mw:Extension/references" about="#mwt3" data-mw=\'{"name":"references","attrs":{},"autoGenerated":true}\'>' +
'<li about="#cite_note-1" id="cite_note-1">↑ <span id="mw-reference-text-cite_note-1" class="mw-reference-text">Foo</span></li>' +
'</ol>',
annotations: [],
expectedData: [
{
type: 'mwReference',
attributes: {
mw: {
attrs: {},
body: {
id: 'mw-reference-text-cite_note-1'
},
name: 'ref'
},
contentsUsed: true,
listGroup: 'mwReference/',
listIndex: 0,
listKey: 'auto/0',
originalMw: '{"name":"ref","body":{"id":"mw-reference-text-cite_note-1"},"attrs":{}}',
refGroup: '',
refListItemId: 'mw-reference-text-cite_note-1'
}
},
{ type: '/mwReference' },
{ type: 'internalList' },
{ type: 'internalItem' },
{ type: 'paragraph', internal: { generated: 'wrapper' } },
'F', 'o', 'o',
{ type: '/paragraph' },
{ type: '/internalItem' },
{ type: '/internalList' }
]
},
{
msg: 'Reference template with autoGenerated content',
pasteString: '{{reference}}',

View file

@ -76,7 +76,8 @@ ve.ui.MWWikitextStringTransferHandler.static.matchFunction = function ( item ) {
* @inheritdoc
*/
ve.ui.MWWikitextStringTransferHandler.prototype.process = function () {
var handler = this,
var i, data,
handler = this,
wikitext = this.item.getAsString();
function failure() {
@ -102,22 +103,32 @@ ve.ui.MWWikitextStringTransferHandler.prototype.process = function () {
}
htmlDoc = ve.createDocumentFromHtml( response.visualeditor.content );
// Filter out auto-generated items, e.g. reference lists
$( htmlDoc.body ).find( '[data-mw]' ).each( function () {
var dataMw = JSON.parse( this.getAttribute( 'data-mw' ) );
if ( dataMw.autoGenerated ) {
this.remove();
}
} );
doc = handler.surface.getModel().getDocument().newFromHtml( htmlDoc );
// Pass an empty object for the second argument (importRules) so that clipboard mode is used
// TODO: Fix that API
doc = handler.surface.getModel().getDocument().newFromHtml( htmlDoc, {} );
data = doc.data.data;
surface = new ve.dm.Surface( doc );
// Filter out auto-generated items, e.g. reference lists
// This is done after conversion as the autoGenerated item may contain data
// required by other non-autoGenerated items, e.g. reference contents
for ( i = data.length - 1; i >= 0; i-- ) {
if ( ve.getProp( data[ i ], 'attributes', 'mw', 'autoGenerated' ) ) {
surface.change(
ve.dm.Transaction.newFromRemoval(
doc,
surface.getDocument().getDocumentNode().getNodeFromOffset( i + 1 ).getOuterRange()
)
);
}
}
if ( !doc.data.hasContent() ) {
return failure();
}
// Attempt to undo outermost p-wrapping if possible
surface = new ve.dm.Surface( doc );
try {
surface.change(
ve.dm.Transaction.newFromWrap( doc, new ve.Range( 0, doc.data.countNonInternalElements() ), [], [], [ { type: 'paragraph' } ], [] )