      1 /*
      2  Copyright 2008-2023
      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 <https://www.gnu.org/licenses/>
     29  and <https://opensource.org/licenses/MIT/>.
     30  */
     31
     32 /*global JXG: true, define: true, DOMParser: true, ActiveXObject: true*/
     33 /*jslint nomen: true, plusplus: true*/
     34
     35 import JXG from "../jxg";
     36 import Type from "./type";
     37
     38 /**
     39  * Holds browser independent xml parsing routines. Won't work in environments other than browsers.
     40  * @namespace
     41  */
     42 JXG.XML = {
     43     /**
     44      * Cleans out unneccessary whitespaces in a chunk of xml.
     45      * @param {Object} el
     46      */
     47     cleanWhitespace: function (el) {
     48         var cur = el.firstChild;
     49
     50         while (Type.exists(cur)) {
     51             if (cur.nodeType === 3 && !/\S/.test(cur.nodeValue)) {
     52                 el.removeChild(cur);
     53             } else if (cur.nodeType === 1) {
     54                 this.cleanWhitespace(cur);
     55             }
     56             cur = cur.nextSibling;
     57         }
     58     },
     59
     60     /**
     61      * Converts a given string into a XML tree.
     62      * @param {String} str
     63      * @returns {Object} The xml tree represented by the root node.
     64      */
     65     parse: function (str) {
     66         var parser, tree, DP;
     67
     68         // DOMParser is a function in all browsers, except older IE and Safari.
     69         // In IE it does not exists (workaround in else branch), in Safari it's an object.
     70         if (typeof DOMParser === "function" || typeof DOMParser === "object") {
     71             DP = DOMParser;
     72         } else {
     73             // IE workaround, since there is no DOMParser
     74             DP = function () {
     75                 this.parseFromString = function (str) {
     76                     var d;
     77
     78                     if (typeof ActiveXObject === "function") {
     79                         d = new ActiveXObject("MSXML.DomDocument");
     80                         d.loadXML(str);
     81                     }
     82
     83                     return d;
     84                 };
     85             };
     86         }
     87
     88         parser = new DP();
     89         tree = parser.parseFromString(str, "text/xml");
     90         this.cleanWhitespace(tree);
     91
     92         return tree;
     93     }
     94 };
     95
     96 export default JXG.XML;
     97