2015-08-07 18:16:09 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWWikitextStringTransferHandler class.
|
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see http://ve.mit-license.org
|
2015-08-07 18:16:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Detect an attempt to paste wikitext, and convert it to proper
|
|
|
|
* HTML.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.ui.PlainTextStringTransferHandler
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.ui.Surface} surface
|
|
|
|
* @param {ve.ui.DataTransferItem} item
|
|
|
|
*/
|
|
|
|
ve.ui.MWWikitextStringTransferHandler = function VeUiMWWikitextStringTransferHandler() {
|
|
|
|
// Parent constructor
|
|
|
|
ve.ui.MWWikitextStringTransferHandler.super.apply( this, arguments );
|
2015-08-23 10:54:21 +00:00
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.parsoidRequest = null;
|
2015-08-07 18:16:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWWikitextStringTransferHandler, ve.ui.PlainTextStringTransferHandler );
|
|
|
|
|
|
|
|
/* Static properties */
|
|
|
|
|
|
|
|
ve.ui.MWWikitextStringTransferHandler.static.name = 'wikitextString';
|
|
|
|
|
|
|
|
ve.ui.MWWikitextStringTransferHandler.static.types =
|
|
|
|
ve.ui.MWWikitextStringTransferHandler.super.static.types.concat(
|
|
|
|
[ 'text/x-wiki' ]
|
|
|
|
);
|
|
|
|
|
|
|
|
ve.ui.MWWikitextStringTransferHandler.static.handlesPaste = true;
|
|
|
|
|
|
|
|
ve.ui.MWWikitextStringTransferHandler.static.matchFunction = function ( item ) {
|
2016-03-05 16:50:25 +00:00
|
|
|
var i, rule,
|
|
|
|
text = item.getAsString(),
|
|
|
|
registry = ve.ui.mwWikitextTransferRegistry;
|
2015-08-07 18:16:09 +00:00
|
|
|
|
|
|
|
// If the mime type is explicitly wikitext (ie, not plain text),
|
|
|
|
// always accept.
|
|
|
|
if ( item.type === 'text/x-wiki' ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Detect autolink opportunities for magic words.
|
|
|
|
// (The link should be the only contents of paste to match this heuristic)
|
2015-08-25 05:48:16 +00:00
|
|
|
if ( ve.dm.MWMagicLinkNode.static.validateContent( text.trim() ) ) {
|
2015-08-07 18:16:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use a heuristic regexp to find text likely to be wikitext.
|
|
|
|
// This test could be made more sophisticated in the future.
|
2016-03-05 16:50:25 +00:00
|
|
|
for ( i in registry.registry ) {
|
|
|
|
rule = registry.registry[ i ];
|
|
|
|
if ( rule instanceof RegExp ) {
|
|
|
|
if ( registry.registry[ i ].test( text ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if ( text.indexOf( rule ) !== -1 ) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-08-07 18:16:09 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2021-01-20 19:49:27 +00:00
|
|
|
/**
|
|
|
|
* Create a new document from HTML from Parsoid
|
|
|
|
*
|
|
|
|
* @param {string} html HTML from Parsoid
|
|
|
|
* @param {ve.dm.Document} targetDoc DM document this will eventually be merged with
|
|
|
|
* @return {ve.dm.Document} New document
|
|
|
|
*/
|
|
|
|
ve.ui.MWWikitextStringTransferHandler.static.createDocumentFromParsoidHtml = function ( html, targetDoc ) {
|
|
|
|
var doc, surface, i, data,
|
|
|
|
htmlDoc = ve.createDocumentFromHtml( html );
|
|
|
|
|
|
|
|
// Strip RESTBase IDs
|
|
|
|
mw.libs.ve.stripRestbaseIds( htmlDoc );
|
|
|
|
|
|
|
|
// Strip legacy IDs, for example in section headings
|
|
|
|
mw.libs.ve.stripParsoidFallbackIds( htmlDoc.body );
|
|
|
|
|
|
|
|
// Pass an empty object for the second argument (importRules) so that clipboard mode is used
|
|
|
|
// TODO: Fix that API
|
|
|
|
doc = targetDoc.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.TransactionBuilder.static.newFromRemoval(
|
|
|
|
doc,
|
|
|
|
surface.getDocument().getDocumentNode().getNodeFromOffset( i + 1 ).getOuterRange()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone elements to avoid about attribute conflicts (T204007)
|
|
|
|
doc.data.cloneElements( true );
|
|
|
|
|
|
|
|
return doc;
|
|
|
|
};
|
|
|
|
|
2015-08-07 18:16:09 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWWikitextStringTransferHandler.prototype.process = function () {
|
2021-01-20 19:49:27 +00:00
|
|
|
var handler = this,
|
2015-08-07 18:16:09 +00:00
|
|
|
wikitext = this.item.getAsString();
|
|
|
|
|
2017-03-07 15:44:50 +00:00
|
|
|
// We already know how to handle wikitext magic links, no need for the API call
|
|
|
|
if ( ve.dm.MWMagicLinkNode.static.validateContent( wikitext.trim() ) ) {
|
|
|
|
handler.resolve( [
|
|
|
|
{
|
|
|
|
type: 'link/mwMagic',
|
|
|
|
attributes: {
|
|
|
|
content: wikitext.trim()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: '/link/mwMagic'
|
|
|
|
}
|
|
|
|
] );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-07 18:16:09 +00:00
|
|
|
function failure() {
|
|
|
|
// There's no DTH fallback handling for failures, so just paste
|
|
|
|
// the raw wikitext if things go wrong.
|
|
|
|
handler.resolve( wikitext );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert wikitext to html using Parsoid.
|
2018-04-06 11:30:57 +00:00
|
|
|
this.parsoidRequest = ve.init.target.parseWikitextFragment( wikitext, false, this.surface.getModel().getDocument() );
|
2015-08-23 10:54:21 +00:00
|
|
|
|
|
|
|
// Don't immediately chain, as this.parsoidRequest must be abortable
|
|
|
|
this.parsoidRequest.then( function ( response ) {
|
2021-01-20 19:49:27 +00:00
|
|
|
var doc;
|
2016-03-26 22:30:51 +00:00
|
|
|
|
2015-08-07 18:16:09 +00:00
|
|
|
if ( ve.getProp( response, 'visualeditor', 'result' ) !== 'success' ) {
|
|
|
|
return failure();
|
|
|
|
}
|
|
|
|
|
2021-01-20 19:49:27 +00:00
|
|
|
doc = handler.constructor.static.createDocumentFromParsoidHtml(
|
|
|
|
response.visualeditor.content,
|
|
|
|
handler.surface.getModel().getDocument()
|
|
|
|
);
|
2018-09-12 13:52:54 +00:00
|
|
|
|
2015-11-18 01:42:47 +00:00
|
|
|
if ( !doc.data.hasContent() ) {
|
|
|
|
return failure();
|
|
|
|
}
|
2021-01-20 19:49:27 +00:00
|
|
|
|
2015-08-07 18:16:09 +00:00
|
|
|
handler.resolve( doc );
|
|
|
|
}, failure );
|
2015-08-23 10:54:21 +00:00
|
|
|
|
|
|
|
this.createProgress( this.parsoidRequest, ve.msg( 'visualeditor-wikitext-progress' ) );
|
|
|
|
// Indeterminate progress
|
|
|
|
this.setProgress( null );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWWikitextStringTransferHandler.prototype.abort = function () {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWWikitextStringTransferHandler.super.prototype.abort.apply( this, arguments );
|
|
|
|
|
|
|
|
if ( this.parsoidRequest ) {
|
|
|
|
this.parsoidRequest.abort();
|
|
|
|
}
|
2015-08-07 18:16:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.dataTransferHandlerFactory.register( ve.ui.MWWikitextStringTransferHandler );
|