Merge "Strip RESTBase IDs from HTML generated when wikitext is pasted"

This commit is contained in:
jenkins-bot 2016-10-14 00:55:48 +00:00 committed by Gerrit Code Review
commit a9240d6f2f
2 changed files with 32 additions and 17 deletions

View file

@ -9,16 +9,18 @@ QUnit.module( 've.ui.MWWikitextStringTransferHandler', QUnit.newMwEnvironment( {
setup: function () {
// Mock XHR for mw.Api()
this.server = MWWIKITEXT_MOCK_API ? this.sandbox.useFakeServer() : null;
}
ve.test.utils.mwEnvironment.setup.call( this );
},
teardown: ve.test.utils.mwEnvironment.teardown
} ) );
/* Tests */
function runWikitextStringHandlerTest( assert, server, string, mimeType, expectedResponse, expectedData, annotations, msg ) {
function runWikitextStringHandlerTest( assert, server, string, mimeType, expectedResponse, expectedData, annotations, assertDom, msg ) {
var handler, i, j, name,
done = assert.async(),
item = ve.ui.DataTransferItem.static.newFromString( string, mimeType ),
doc = ve.dm.example.createExampleDocument(),
doc = new ve.dm.Document( [] ),
mockSurface = {
getModel: function () {
return {
@ -53,7 +55,11 @@ function runWikitextStringHandlerTest( assert, server, string, mimeType, expecte
handler.getInsertableData().done( function ( doc2 ) {
var actualData = doc2.getData();
ve.dm.example.postprocessAnnotations( actualData, doc2.getStore() );
assert.equalLinearData( actualData, expectedData, msg + ': data match' );
if ( assertDom ) {
assert.equalLinearDataWithDom( doc2.getStore(), actualData, expectedData, msg + ': data match (with DOM)' );
} else {
assert.equalLinearData( actualData, expectedData, msg + ': data match' );
}
done();
} );
@ -144,21 +150,19 @@ QUnit.test( 'convert', function ( assert ) {
]
},
{
msg: 'Heading',
msg: 'Headings, only RESTBase IDs stripped',
pasteString: '==heading==',
pasteType: 'text/plain',
parsoidResponse: '<h2>heading</h2>',
parsoidResponse: '<h2 id="mwAB">foo</h2><h2 id="mw-meaningful-id">bar</h2>',
annotations: [],
assertDom: true,
expectedData: [
{ type: 'heading', attributes: { level: 2 } },
'h',
'e',
'a',
'd',
'i',
'n',
'g',
{ type: '/heading' },
{ type: 'mwHeading', attributes: { level: 2 }, originalDomElements: $( '<h2>foo</h2>' ).toArray() },
'f', 'o', 'o',
{ type: '/mwHeading' },
{ type: 'mwHeading', attributes: { level: 2 }, originalDomElements: $( '<h2 id="mw-meaningful-id">bar</h2>' ).toArray() },
'b', 'a', 'r',
{ type: '/mwHeading' },
{ type: 'internalList' },
{ type: '/internalList' }
]
@ -296,6 +300,9 @@ QUnit.test( 'convert', function ( assert ) {
QUnit.expect( cases.length * 2 );
for ( i = 0; i < cases.length; i++ ) {
runWikitextStringHandlerTest( assert, this.server, cases[ i ].pasteString, cases[ i ].pasteType, cases[ i ].parsoidResponse, cases[ i ].expectedData, cases[ i ].annotations, cases[ i ].msg );
runWikitextStringHandlerTest(
assert, this.server, cases[ i ].pasteString, cases[ i ].pasteType, cases[ i ].parsoidResponse,
cases[ i ].expectedData, cases[ i ].annotations, cases[ i ].assertDom, cases[ i ].msg
);
}
} );

View file

@ -96,7 +96,7 @@ ve.ui.MWWikitextStringTransferHandler.prototype.process = function () {
// Don't immediately chain, as this.parsoidRequest must be abortable
this.parsoidRequest.then( function ( response ) {
var htmlDoc, doc, surface;
var htmlDoc, doc, surface, elementsWithIds, len;
if ( ve.getProp( response, 'visualeditor', 'result' ) !== 'success' ) {
return failure();
@ -104,6 +104,14 @@ ve.ui.MWWikitextStringTransferHandler.prototype.process = function () {
htmlDoc = ve.createDocumentFromHtml( response.visualeditor.content );
// Strip RESTBase IDs
elementsWithIds = htmlDoc.querySelectorAll( '[id]' );
for ( i = 0, len = elementsWithIds.length; i < len; i++ ) {
if ( elementsWithIds[ i ].getAttribute( 'id' ).match( ve.init.platform.getMetadataIdRegExp() ) ) {
elementsWithIds[ i ].removeAttribute( 'id' );
}
}
// 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, {} );