1 /**
  2  * Copyright (C) 2013 KO GmbH <copyright@kogmbh.com>
  3  *
  4  * @licstart
  5  * This file is part of WebODF.
  6  *
  7  * WebODF is free software: you can redistribute it and/or modify it
  8  * under the terms of the GNU Affero General Public License (GNU AGPL)
  9  * as published by the Free Software Foundation, either version 3 of
 10  * the License, or (at your option) any later version.
 11  *
 12  * WebODF is distributed in the hope that it will be useful, but
 13  * WITHOUT ANY WARRANTY; without even the implied warranty of
 14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  * GNU Affero General Public License for more details.
 16  *
 17  * You should have received a copy of the GNU Affero General Public License
 18  * along with WebODF.  If not, see <http://www.gnu.org/licenses/>.
 19  * @licend
 20  *
 21  * @source: http://www.webodf.org/
 22  * @source: https://github.com/kogmbh/WebODF/
 23  */
 24 
 25 /*global gui, runtime, odf*/
 26 
 27 /**
 28  * MimeDataExporter exports a passed range as several types
 29  * into the passed DataTransfer object
 30  * @constructor
 31  */
 32 gui.MimeDataExporter = function MimeDataExporter() {
 33     "use strict";
 34 
 35     var /**@type{!odf.TextSerializer}*/
 36         textSerializer;
 37 
 38     /**
 39      * Copy the contents of the supplied range into the passed dataTransfer.
 40      * @param {!DataTransfer} dataTransfer
 41      * @param {!Range} range Selection range to copy into the clipboard
 42      * @return {undefined}
 43      */
 44     this.exportRangeToDataTransfer = function (dataTransfer, range) {
 45         var document = range.startContainer.ownerDocument,
 46             serializedFragment,
 47             fragmentContainer;
 48 
 49         // the document fragment needs to be wrapped in a span as
 50         // text nodes cannot be inserted at the top level of the DOM
 51         fragmentContainer = document.createElement('span');
 52         fragmentContainer.appendChild(range.cloneContents());
 53         serializedFragment = textSerializer.writeToString(fragmentContainer);
 54         try {
 55             dataTransfer.setData('text/plain', serializedFragment);
 56         } catch(e) {
 57             // Internet Explorer only supports the 'Text' key being set
 58             // See http://msdn.microsoft.com/en-us/library/ie/ms536744%28v=vs.85%29.aspx
 59             // Could do some browser sniffing potentially, but this is less error prone as it
 60             // doesn't rely on the agent string being correct.
 61             dataTransfer.setData('Text', serializedFragment);
 62         }
 63     };
 64 
 65     function init() {
 66         textSerializer = new odf.TextSerializer();
 67         textSerializer.filter = new odf.OdfNodeFilter();
 68     }
 69 
 70     init();
 71 };
 72