Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/datatransferhandlers/ve.ui.MWWikitextStringTransferHandler.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MWWikitextStringTransferHandler class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Detect an attempt to paste wikitext, and convert it to proper
9 * HTML.
10 *
11 * @class
12 * @extends ve.ui.PlainTextStringTransferHandler
13 *
14 * @constructor
15 * @param {ve.ui.Surface} surface
16 * @param {ve.ui.DataTransferItem} item
17 */
18 ve.ui.MWWikitextStringTransferHandler = function VeUiMWWikitextStringTransferHandler() {
19 // Parent constructor
20 ve.ui.MWWikitextStringTransferHandler.super.apply( this, arguments );
21
22 // Properties
23 this.parsoidRequest = null;
24 };
25
26 /* Inheritance */
27
28 OO.inheritClass( ve.ui.MWWikitextStringTransferHandler, ve.ui.PlainTextStringTransferHandler );
29
30 /* Static properties */
31
32 ve.ui.MWWikitextStringTransferHandler.static.name = 'wikitextString';
33
34 ve.ui.MWWikitextStringTransferHandler.static.types = [
35 ...ve.ui.MWWikitextStringTransferHandler.super.static.types,
36 'text/x-wiki'
37 ];
38
39 ve.ui.MWWikitextStringTransferHandler.static.handlesPaste = true;
40
41 ve.ui.MWWikitextStringTransferHandler.static.matchFunction = function ( item ) {
42 const text = item.getAsString(),
43 registry = ve.ui.mwWikitextTransferRegistry;
44
45 // If the mime type is explicitly wikitext (ie, not plain text),
46 // always accept.
47 if ( item.type === 'text/x-wiki' ) {
48 return true;
49 }
50
51 // Detect autolink opportunities for magic words.
52 // (The link should be the only contents of paste to match this heuristic)
53 if ( ve.dm.MWMagicLinkNode.static.validateContent( text.trim() ) ) {
54 return true;
55 }
56
57 // Use a heuristic regexp to find text likely to be wikitext.
58 // This test could be made more sophisticated in the future.
59 for ( const i in registry.registry ) {
60 const rule = registry.registry[ i ];
61 if ( rule instanceof RegExp ) {
62 if ( registry.registry[ i ].test( text ) ) {
63 return true;
64 }
65 } else if ( text.indexOf( rule ) !== -1 ) {
66 return true;
67 }
68 }
69 return false;
70 };
71
72 /**
73 * Create a new document from HTML from Parsoid
74 *
75 * @param {string} html HTML from Parsoid
76 * @param {ve.dm.Document} targetDoc DM document this will eventually be merged with
77 * @return {ve.dm.Document} New document
78 */
79 ve.ui.MWWikitextStringTransferHandler.static.createDocumentFromParsoidHtml = function ( html, targetDoc ) {
80 const htmlDoc = ve.createDocumentFromHtml( html );
81
82 // Strip RESTBase IDs
83 mw.libs.ve.stripRestbaseIds( htmlDoc );
84
85 // Strip legacy IDs, for example in section headings
86 mw.libs.ve.stripParsoidFallbackIds( htmlDoc.body );
87
88 // Pass an empty object for the second argument (importRules) so that clipboard mode is used
89 // TODO: Fix that API
90 const doc = targetDoc.newFromHtml( htmlDoc, {} );
91 const data = doc.data.data;
92 const surface = new ve.dm.Surface( doc );
93
94 // Filter out auto-generated items, e.g. reference lists
95 // This is done after conversion as the autoGenerated item may contain data
96 // required by other non-autoGenerated items, e.g. reference contents
97 for ( let i = data.length - 1; i >= 0; i-- ) {
98 if ( ve.getProp( data[ i ], 'attributes', 'mw', 'autoGenerated' ) ) {
99 surface.change(
100 ve.dm.TransactionBuilder.static.newFromRemoval(
101 doc,
102 surface.getDocument().getDocumentNode().getNodeFromOffset( i + 1 ).getOuterRange()
103 )
104 );
105 }
106 }
107
108 // Clone elements to avoid about attribute conflicts (T204007)
109 doc.data.cloneElements( true );
110
111 return doc;
112 };
113
114 /* Methods */
115
116 /**
117 * @inheritdoc
118 */
119 ve.ui.MWWikitextStringTransferHandler.prototype.process = function () {
120 const wikitext = this.item.getAsString();
121
122 // We already know how to handle wikitext magic links, no need for the API call
123 if ( ve.dm.MWMagicLinkNode.static.validateContent( wikitext.trim() ) ) {
124 this.resolve( [
125 {
126 type: 'link/mwMagic',
127 attributes: {
128 content: wikitext.trim()
129 }
130 },
131 {
132 type: '/link/mwMagic'
133 }
134 ] );
135 return;
136 }
137
138 const failure = () => {
139 // There's no DTH fallback handling for failures, so just paste
140 // the raw wikitext if things go wrong.
141 this.resolve( wikitext );
142 };
143
144 // Convert wikitext to html using Parsoid.
145 this.parsoidRequest = ve.init.target.parseWikitextFragment( wikitext, false, this.surface.getModel().getDocument() );
146
147 // Don't immediately chain, as this.parsoidRequest must be abortable
148 this.parsoidRequest.then( ( response ) => {
149 if ( ve.getProp( response, 'visualeditor', 'result' ) !== 'success' ) {
150 return failure();
151 }
152
153 const doc = this.constructor.static.createDocumentFromParsoidHtml(
154 response.visualeditor.content,
155 this.surface.getModel().getDocument()
156 );
157
158 if ( !doc.data.hasContent() ) {
159 return failure();
160 }
161
162 this.resolve( doc );
163 }, failure );
164
165 this.createProgress( this.parsoidRequest, ve.msg( 'visualeditor-wikitext-progress' ) );
166 // Indeterminate progress
167 this.setProgress( null );
168 };
169
170 /**
171 * @inheritdoc
172 */
173 ve.ui.MWWikitextStringTransferHandler.prototype.abort = function () {
174 // Parent method
175 ve.ui.MWWikitextStringTransferHandler.super.prototype.abort.apply( this, arguments );
176
177 if ( this.parsoidRequest ) {
178 this.parsoidRequest.abort();
179 }
180 };
181
182 /* Registration */
183
184 ve.ui.dataTransferHandlerFactory.register( ve.ui.MWWikitextStringTransferHandler );