| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
( function () { |
| 18 |
mw.libs.ve = mw.libs.ve || {}; |
| 19 |
|
| 20 |
mw.libs.ve.targetSaver = { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
preloadDeflate: function () { |
| 26 |
mw.loader.load( 'mediawiki.deflate' ); |
| 27 |
}, |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
deflate: function ( html ) { |
| 36 |
return mw.loader.using( 'mediawiki.deflate' ).then( () => mw.deflate( html ) ); |
| 37 |
|
| 38 |
}, |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
getHtml: function ( newDoc, oldDoc ) { |
| 52 |
function copyAttributes( from, to ) { |
| 53 |
Array.prototype.forEach.call( from.attributes, ( attr ) => { |
| 54 |
to.setAttribute( attr.name, attr.value ); |
| 55 |
} ); |
| 56 |
} |
| 57 |
|
| 58 |
if ( oldDoc ) { |
| 59 |
|
| 60 |
for ( let i = 0, len = oldDoc.head.childNodes.length; i < len; i++ ) { |
| 61 |
newDoc.head.appendChild( oldDoc.head.childNodes[ i ].cloneNode( true ) ); |
| 62 |
} |
| 63 |
|
| 64 |
copyAttributes( oldDoc.documentElement, newDoc.documentElement ); |
| 65 |
copyAttributes( oldDoc.head, newDoc.head ); |
| 66 |
copyAttributes( oldDoc.body, newDoc.body ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
$( newDoc ) |
| 71 |
.find( [ |
| 72 |
'script', |
| 73 |
'noscript', |
| 74 |
'object', |
| 75 |
'style:not( [ data-mw ] ):not( [ data-mw-deduplicate ] )', |
| 76 |
'embed', |
| 77 |
'a[href^="javascript:"]', |
| 78 |
'img[src^="data:"]', |
| 79 |
'div[id="myEventWatcherDiv"]', |
| 80 |
'div[id="sendToInstapaperResults"]', |
| 81 |
'div[id="kloutify"]', |
| 82 |
'div[id^="mittoHidden"]', |
| 83 |
'div.hon.certificateLink', |
| 84 |
'div.donut-container', |
| 85 |
'div.shield-container' |
| 86 |
].join( ',' ) ) |
| 87 |
.each( ( j, el ) => { |
| 88 |
function truncate( text, l ) { |
| 89 |
return text.length > l ? text.slice( 0, l ) + '…' : text; |
| 90 |
} |
| 91 |
const errorMessage = 'DOM content matching deny list found:\n' + truncate( el.outerHTML, 100 ) + |
| 92 |
'\nContext:\n' + truncate( el.parentNode.outerHTML, 200 ); |
| 93 |
mw.log.error( errorMessage ); |
| 94 |
const err = new Error( errorMessage ); |
| 95 |
err.name = 'VeDomDenyListWarning'; |
| 96 |
mw.errorLogger.logError( err, 'error.visualeditor' ); |
| 97 |
$( el ).remove(); |
| 98 |
} ); |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
$( newDoc ).find( '[data-mw-section-id]:not( section )' ).removeAttr( 'data-mw-section-id' ); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
mw.libs.ve.deduplicateStyles( newDoc.body ); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
return '<!doctype html>' + ve.properOuterHtml( newDoc.documentElement ); |
| 112 |
}, |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
deflateDoc: function ( doc, oldDoc ) { |
| 122 |
return this.deflate( this.getHtml( doc, oldDoc ) ); |
| 123 |
}, |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
saveDoc: function ( doc, extraData, options ) { |
| 136 |
return this.deflateDoc( doc ).then( ( html ) => this.postHtml( |
| 137 |
html, |
| 138 |
null, |
| 139 |
extraData, |
| 140 |
options |
| 141 |
) ); |
| 142 |
}, |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
postWikitext: function ( wikitext, extraData, options ) { |
| 159 |
return this.postContent( $.extend( { wikitext: wikitext }, extraData ), options ); |
| 160 |
}, |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
postHtml: function ( html, cacheKey, extraData, options ) { |
| 175 |
options = options || {}; |
| 176 |
let data; |
| 177 |
if ( cacheKey ) { |
| 178 |
data = $.extend( { cachekey: cacheKey }, extraData ); |
| 179 |
} else { |
| 180 |
data = $.extend( { html: html }, extraData ); |
| 181 |
} |
| 182 |
return this.postContent( data, options ).then( |
| 183 |
null, |
| 184 |
( code, response ) => { |
| 185 |
|
| 186 |
if ( options.onCacheKeyFail ) { |
| 187 |
options.onCacheKeyFail(); |
| 188 |
} |
| 189 |
if ( code === 'badcachekey' ) { |
| 190 |
|
| 191 |
return this.postHtml( |
| 192 |
html, |
| 193 |
null, |
| 194 |
extraData, |
| 195 |
options |
| 196 |
); |
| 197 |
} |
| 198 |
|
| 199 |
return $.Deferred().reject( code, response ).promise(); |
| 200 |
} |
| 201 |
); |
| 202 |
}, |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
postContent: function ( data, options ) { |
| 219 |
options = options || {}; |
| 220 |
const api = options.api || new mw.Api(); |
| 221 |
|
| 222 |
let start; |
| 223 |
if ( options.now ) { |
| 224 |
start = options.now(); |
| 225 |
} |
| 226 |
|
| 227 |
data = $.extend( |
| 228 |
{ |
| 229 |
action: 'visualeditoredit', |
| 230 |
paction: 'save', |
| 231 |
useskin: mw.config.get( 'skin' ), |
| 232 |
|
| 233 |
mobileformat: !!mw.config.get( 'wgMFMode' ), |
| 234 |
formatversion: 2, |
| 235 |
errorformat: 'html', |
| 236 |
errorlang: mw.config.get( 'wgUserLanguage' ), |
| 237 |
errorsuselocal: true |
| 238 |
}, |
| 239 |
data |
| 240 |
); |
| 241 |
|
| 242 |
const action = data.action; |
| 243 |
|
| 244 |
const request = api.postWithToken( 'csrf', data, { |
| 245 |
contentType: 'multipart/form-data', |
| 246 |
trackEditAttemptStepSessionId: true |
| 247 |
} ); |
| 248 |
|
| 249 |
return request.then( |
| 250 |
( response, jqxhr ) => { |
| 251 |
const responseData = response[ action ]; |
| 252 |
|
| 253 |
|
| 254 |
if ( options.track && options.eventName ) { |
| 255 |
const eventData = { |
| 256 |
bytes: require( 'mediawiki.String' ).byteLength( jqxhr.responseText ), |
| 257 |
duration: options.now() - start |
| 258 |
}; |
| 259 |
const fullEventName = 'performance.system.' + options.eventName + |
| 260 |
( responseData.cachekey ? '.withCacheKey' : '.withoutCacheKey' ); |
| 261 |
options.track( fullEventName, eventData ); |
| 262 |
} |
| 263 |
|
| 264 |
let error; |
| 265 |
if ( !responseData ) { |
| 266 |
error = { |
| 267 |
code: 'invalidresponse', |
| 268 |
html: mw.message( 'api-clientside-error-invalidresponse' ).parse() |
| 269 |
}; |
| 270 |
} else if ( responseData.result !== 'success' ) { |
| 271 |
|
| 272 |
|
| 273 |
return $.Deferred().reject( 'no-error-no-success', response ).promise(); |
| 274 |
} else { |
| 275 |
|
| 276 |
switch ( responseData.paction ) { |
| 277 |
case 'save': |
| 278 |
case 'serialize': |
| 279 |
if ( typeof responseData.content !== 'string' ) { |
| 280 |
error = { |
| 281 |
code: 'invalidcontent', |
| 282 |
html: mw.message( 'api-clientside-error-invalidresponse' ).parse() |
| 283 |
}; |
| 284 |
} |
| 285 |
break; |
| 286 |
case 'diff': |
| 287 |
if ( typeof responseData.diff !== 'string' ) { |
| 288 |
error = { |
| 289 |
code: 'invalidcontent', |
| 290 |
html: mw.message( 'api-clientside-error-invalidresponse' ).parse() |
| 291 |
}; |
| 292 |
} |
| 293 |
break; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
if ( error ) { |
| 298 |
|
| 299 |
return $.Deferred().reject( error.code, { errors: [ error ] } ).promise(); |
| 300 |
} |
| 301 |
return responseData; |
| 302 |
}, |
| 303 |
( code, response ) => { |
| 304 |
const responseText = OO.getProp( response, 'xhr', 'responseText' ); |
| 305 |
|
| 306 |
if ( responseText && options.track && options.eventName ) { |
| 307 |
const eventData = { |
| 308 |
bytes: require( 'mediawiki.String' ).byteLength( responseText ), |
| 309 |
duration: options.now() - start |
| 310 |
}; |
| 311 |
let fullEventName; |
| 312 |
if ( code === 'badcachekey' ) { |
| 313 |
fullEventName = 'performance.system.' + options.eventName + '.badCacheKey'; |
| 314 |
} else { |
| 315 |
fullEventName = 'performance.system.' + options.eventName + '.withoutCacheKey'; |
| 316 |
} |
| 317 |
options.track( fullEventName, eventData ); |
| 318 |
} |
| 319 |
return $.Deferred().reject( code, response ).promise(); |
| 320 |
} |
| 321 |
); |
| 322 |
} |
| 323 |
}; |
| 324 |
}() ); |