Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/datatransferhandlers/ve.ui.UrlStringTransferHandler.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface UrlStringTransferHandler class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Handle pastes and drag-and-drop of URLs and links.
9 * Attempts to preserve link titles when possible.
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.UrlStringTransferHandler = function VeUiUrlStringTransferHandler() {
19 // Parent constructor
20 ve.ui.UrlStringTransferHandler.super.apply( this, arguments );
21 };
22
23 /* Inheritance */
24
25 OO.inheritClass( ve.ui.UrlStringTransferHandler, ve.ui.PlainTextStringTransferHandler );
26
27 /* Static properties */
28
29 ve.ui.UrlStringTransferHandler.static.name = 'urlString';
30
31 ve.ui.UrlStringTransferHandler.static.types = [
32 // DnD standard, no title information
33 'text/uri-list',
34 // Support: Firefox
35 // Firefox type, preserves title
36 'text/x-moz-url',
37 // Used in GNOME drag-and-drop
38 'text/x-uri',
39 // Identify links in pasted plain text as well
40 ...ve.ui.UrlStringTransferHandler.super.static.types
41 ];
42
43 ve.ui.UrlStringTransferHandler.static.handlesPaste = true;
44
45 /**
46 * RegExp matching an external url.
47 *
48 * @property {RegExp}
49 * @private
50 */
51 ve.ui.UrlStringTransferHandler.static.urlRegExp = null; // Initialized below
52
53 ve.init.Platform.static.initializedPromise.then( () => {
54 ve.ui.UrlStringTransferHandler.static.urlRegExp =
55
56 new RegExp(
57 ve.init.platform.getExternalLinkUrlProtocolsRegExp().source +
58 '\\S+$',
59 'i' // Protocols are case-insensitive
60 );
61 } );
62
63 /* Methods */
64
65 ve.ui.UrlStringTransferHandler.static.matchFunction = function ( item ) {
66 // Match all specific mime types
67 if ( ve.ui.UrlStringTransferHandler.static.types.indexOf( item.type ) >= 0 &&
68 item.type !== 'text/plain' ) {
69 return true;
70 }
71
72 // If the type if unspecified or text/plain, then let's check whether it
73 // is a valid URL.
74 return ve.ui.UrlStringTransferHandler.static.urlRegExp.test(
75 item.getAsString()
76 );
77 };
78
79 /**
80 * @inheritdoc
81 */
82 ve.ui.UrlStringTransferHandler.prototype.process = function () {
83 const surface = this.surface,
84 store = surface.getModel().getDocument().getStore(),
85 linkAction = ve.ui.actionFactory.create( 'link', surface ),
86 data = this.item.getAsString();
87
88 let links;
89 switch ( this.item.type ) {
90 case 'text/uri-list':
91 // text/uri-list has embedded comments; remove them before
92 // autolinking. In theory the embedded comments can be
93 // used for link titles, but I've never seen this done by
94 // real apps. You could add code here to annotate the
95 // links with the comment information if you can find a
96 // spec for how it should be done.
97 links = data.replace( /^#.*(\r\n?|\n|$)/mg, '' ).trim()
98 .split( /[\r\n]+/g ).map( ( line ) => ( { href: line } ) );
99 // Support: Chrome
100 // When Chrome uses this mime type the link titles can
101 // be extracted from the 'text/html' version of the item.
102 // Let's try that.
103 if ( this.item.data.htmlStringData ) {
104 const html = this.item.data.htmlStringData;
105 const doc = ve.createDocumentFromHtml( html );
106 links = $.makeArray( doc.querySelectorAll( 'a[href]' ) )
107 .map( ( a ) => ( { href: a.href, title: a.textContent } ) );
108 }
109 break;
110
111 case 'text/x-moz-url':
112 // text/x-moz-url includes titles with the links (alternating lines)
113 links = data.match( /^(.*)(\r\n?|\n)(.*)$/mg ).map( ( item ) => {
114 item = item.split( /[\r\n]+/ );
115 return { href: item[ 0 ], title: item[ 1 ] };
116 } );
117 break;
118
119 default:
120 // A single URL
121 links = [ { href: data.trim() } ];
122 break;
123 }
124
125 // Create linked text.
126 const result = [];
127 links.forEach( ( link ) => {
128 const annotation = linkAction.getLinkAnnotation( link.href ),
129 annotationSet = new ve.dm.AnnotationSet( store, store.hashAll( [
130 annotation
131 ] ) ),
132 title = link.title || annotation.getDisplayTitle(),
133 content = title.split( '' );
134
135 // Put a space between multiple links
136 if ( result.length ) {
137 result.push( ' ' );
138 }
139
140 ve.dm.Document.static.addAnnotationsToData( content, annotationSet );
141 for ( let i = 0; i < content.length; i++ ) {
142 result.push( content[ i ] );
143 }
144 } );
145 this.resolve( result );
146 };
147
148 /* Registration */
149
150 ve.ui.dataTransferHandlerFactory.register( ve.ui.UrlStringTransferHandler );