1 /* 2 Copyright 2008-2013 3 Matthias Ehmann, 4 Michael Gerhaeuser, 5 Carsten Miller, 6 Bianca Valentin, 7 Alfred Wassermann, 8 Peter Wilfahrt 9 10 This file is part of JSXGraph. 11 12 JSXGraph is free software dual licensed under the GNU LGPL or MIT License. 13 14 You can redistribute it and/or modify it under the terms of the 15 16 * GNU Lesser General Public License as published by 17 the Free Software Foundation, either version 3 of the License, or 18 (at your option) any later version 19 OR 20 * MIT License: https://github.com/jsxgraph/jsxgraph/blob/master/LICENSE.MIT 21 22 JSXGraph is distributed in the hope that it will be useful, 23 but WITHOUT ANY WARRANTY; without even the implied warranty of 24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 25 GNU Lesser General Public License for more details. 26 27 You should have received a copy of the GNU Lesser General Public License and 28 the MIT License along with JSXGraph. If not, see <http://www.gnu.org/licenses/> 29 and <http://opensource.org/licenses/MIT/>. 30 */ 31 32 33 /*global JXG: true, define: true, DOMParser: true, ActiveXObject: true*/ 34 /*jslint nomen: true, plusplus: true*/ 35 36 /* depends: 37 jxg 38 utils/type 39 */ 40 41 define(['jxg', 'utils/type'], function (JXG, Type) { 42 43 "use strict"; 44 45 /** 46 * Holds browser independent xml parsing routines. Won't work in environments other than browsers. 47 * @namespace 48 */ 49 JXG.XML = { 50 /** 51 * Cleans out unneccessary whitespaces in a chunk of xml. 52 * @param {Object} el 53 */ 54 cleanWhitespace: function (el) { 55 var cur = el.firstChild; 56 57 while (Type.exists(cur)) { 58 if (cur.nodeType === 3 && !/\S/.test(cur.nodeValue)) { 59 el.removeChild(cur); 60 } else if (cur.nodeType === 1) { 61 this.cleanWhitespace(cur); 62 } 63 cur = cur.nextSibling; 64 } 65 }, 66 67 /** 68 * Converts a given string into a XML tree. 69 * @param {String} str 70 * @returns {Object} The xml tree represented by the root node. 71 */ 72 parse: function (str) { 73 var parser, tree, DP; 74 75 // DOMParser is a function in all browsers, except older IE and Safari. 76 // In IE it does not exists (workaround in else branch), in Safari it's an object. 77 if (typeof DOMParser === 'function' || typeof DOMParser === 'object') { 78 DP = DOMParser; 79 } else { 80 // IE workaround, since there is no DOMParser 81 DP = function () { 82 this.parseFromString = function (str) { 83 var d; 84 85 if (typeof ActiveXObject === 'function') { 86 d = new ActiveXObject('MSXML.DomDocument'); 87 d.loadXML(str); 88 } 89 90 return d; 91 }; 92 }; 93 } 94 95 parser = new DP(); 96 tree = parser.parseFromString(str, 'text/xml'); 97 this.cleanWhitespace(tree); 98 99 return tree; 100 } 101 }; 102 103 return JXG.XML; 104 });