mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-28 08:10:35 +00:00
Wikitext paste: Move MWWikitextStringTransferHandler matchRegExp into a registry
This allows extensions to register their own rules for detecting wikitext, e.g. matching '<ref>'. Bonus: Strip the autogenerated <references/> tag which gets inserted when converting a <ref>. Bug: T128805 Change-Id: I7a38db45d25e86ff3f3f6199aa04425ec98f7cc4
This commit is contained in:
parent
67c207a6f5
commit
5cc10a810a
|
@ -1072,6 +1072,7 @@
|
|||
"modules/ve-mw/ui/ve.ui.MWCommandRegistry.js",
|
||||
"modules/ve-mw/ui/ve.ui.MWSequenceRegistry.js",
|
||||
"modules/ve-mw/ui/ve.ui.MWExtensionWindow.js",
|
||||
"modules/ve-mw/ui/ve.ui.MWWikitextTransferRegistry.js",
|
||||
"modules/ve-mw/ui/commands/ve.ui.MWWikitextWarningCommand.js",
|
||||
"modules/ve-mw/ui/datatransferhandlers/ve.ui.MWWikitextStringTransferHandler.js",
|
||||
"modules/ve-mw/ui/widgets/ve.ui.MWAceEditorWidget.js",
|
||||
|
|
|
@ -38,18 +38,10 @@ ve.ui.MWWikitextStringTransferHandler.static.types =
|
|||
|
||||
ve.ui.MWWikitextStringTransferHandler.static.handlesPaste = true;
|
||||
|
||||
/**
|
||||
* Heuristic pattern which attempts to discover wikitext, without
|
||||
* incurring too many false positives.
|
||||
*
|
||||
* Currently the pattern looks for ==...==, [[...]], or {{...}}
|
||||
* which occur on a single line of max 80 characters.
|
||||
*/
|
||||
ve.ui.MWWikitextStringTransferHandler.static.matchRegExp =
|
||||
/(^\s*(={2,6})[^=\r\n]{1,80}\2\s*$)|\[\[.{1,80}\]\]|\{\{.{1,80}\}\}/m;
|
||||
|
||||
ve.ui.MWWikitextStringTransferHandler.static.matchFunction = function ( item ) {
|
||||
var text = item.getAsString();
|
||||
var i, rule,
|
||||
text = item.getAsString(),
|
||||
registry = ve.ui.mwWikitextTransferRegistry;
|
||||
|
||||
// If the mime type is explicitly wikitext (ie, not plain text),
|
||||
// always accept.
|
||||
|
@ -65,8 +57,15 @@ ve.ui.MWWikitextStringTransferHandler.static.matchFunction = function ( item ) {
|
|||
|
||||
// Use a heuristic regexp to find text likely to be wikitext.
|
||||
// This test could be made more sophisticated in the future.
|
||||
if ( this.matchRegExp.test( text ) ) {
|
||||
return true;
|
||||
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;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
@ -103,7 +102,13 @@ ve.ui.MWWikitextStringTransferHandler.prototype.process = function () {
|
|||
|
||||
doc = handler.surface.getModel().getDocument().newFromHtml(
|
||||
response.visualeditor.content,
|
||||
null // No sanitization, since HTML is from Parsoid
|
||||
{
|
||||
external: {
|
||||
// Blacklist reference lists as they were likely generated by mistake, see T101553
|
||||
blacklist: [ 'mwReferencesList' ]
|
||||
}
|
||||
// No additional sanitization, since HTML is from Parsoid
|
||||
}
|
||||
);
|
||||
|
||||
if ( !doc.data.hasContent() ) {
|
||||
|
|
31
modules/ve-mw/ui/ve.ui.MWWikitextTransferRegistry.js
Normal file
31
modules/ve-mw/ui/ve.ui.MWWikitextTransferRegistry.js
Normal file
|
@ -0,0 +1,31 @@
|
|||
/*!
|
||||
* VisualEditor MediaWiki WikitextTransferRegistry and registrations.
|
||||
*
|
||||
* @copyright 2011-2016 VisualEditor Team and others; see AUTHORS.txt
|
||||
* @license The MIT License (MIT); see LICENSE.txt
|
||||
*/
|
||||
|
||||
/**
|
||||
* Heuristic patterns which attempts to discover wikitext, without
|
||||
* incurring too many false positives.
|
||||
*
|
||||
* Rules can be regular expressions or strings
|
||||
*/
|
||||
ve.ui.mwWikitextTransferRegistry = new OO.Registry();
|
||||
|
||||
ve.ui.mwWikitextTransferRegistry.register(
|
||||
'heading',
|
||||
// ==...== on a single line of max 80 characters
|
||||
/(^\s*(={2,6})[^=\r\n]{1,80}\2\s*$)/m
|
||||
);
|
||||
|
||||
ve.ui.mwWikitextTransferRegistry.register(
|
||||
'link',
|
||||
// [[...]] on a single line of max 80 characters
|
||||
/\[\[.{1,80}\]\]/m
|
||||
);
|
||||
|
||||
ve.ui.mwWikitextTransferRegistry.register(
|
||||
'template',
|
||||
'{{'
|
||||
);
|
Loading…
Reference in a new issue