/*! * VisualEditor MediaWiki ArticleTargetSaver. * * @copyright See AUTHORS.txt * @license The MIT License (MIT); see LICENSE.txt */ /** * Target saver. * * Light-weight saver. * * @class mw.libs.ve.targetSaver * @singleton * @hideconstructor */ ( function () { mw.libs.ve = mw.libs.ve || {}; mw.libs.ve.targetSaver = { /** * Preload the library required for deflating so the user doesn't * have to wait when postHtml is called. */ preloadDeflate: function () { mw.loader.load( 'mediawiki.deflate' ); }, /** * Compress a string with deflate. * * @param {string} html HTML to deflate * @return {jQuery.Promise} Promise resolved with deflated HTML */ deflate: function ( html ) { return mw.loader.using( 'mediawiki.deflate' ).then( () => mw.deflate( html ) ); }, /** * Get HTML to send to Parsoid. * * If the document was generated from scratch (e.g. inside VisualEditor's converter), the * source document can be passed in to transplant the head tag, as well as the attributes * on the html and body tags. * * @param {HTMLDocument} newDoc Document generated by ve.dm.Converter. Will be modified. * @param {HTMLDocument} [oldDoc] Old document to copy attributes from. * @return {string} Full HTML document */ getHtml: function ( newDoc, oldDoc ) { function copyAttributes( from, to ) { Array.prototype.forEach.call( from.attributes, ( attr ) => { to.setAttribute( attr.name, attr.value ); } ); } if ( oldDoc ) { // Copy the head from the old document for ( let i = 0, len = oldDoc.head.childNodes.length; i < len; i++ ) { newDoc.head.appendChild( oldDoc.head.childNodes[ i ].cloneNode( true ) ); } // Copy attributes from the old document for the html, head and body copyAttributes( oldDoc.documentElement, newDoc.documentElement ); copyAttributes( oldDoc.head, newDoc.head ); copyAttributes( oldDoc.body, newDoc.body ); } // Filter out junk that may have been added by browser plugins $( newDoc ) .find( [ 'script', // T54884, T65229, T96533, T103430 'noscript', // T144891 'object', // T65229 'style:not( [ data-mw ] ):not( [ data-mw-deduplicate ] )', // T55252, but allow