<commit_before>"use strict";
/**
 * Copyright (c) 2011, <NAME> <EMAIL>, http://graphicore.de
 * 
 * You should have received a copy of the MIT License along with this program.
 * If not, see http://www.opensource.org/licenses/mit-license.php
 * 
 * This returns the browsers xml tools or if in nodejs a mapping to the
 * libxmljs api so that it can be used like the w3c tools.
 * 
 * IMPORTANT for nodejs with libxmljs: As the need of this project regarding
 * xml tools is not so big, there will likely be no full DOM w3c api
 * available. Thus, things like running jQuery on a libxmljs element won't
 * work.
 * As it appears the jsdom people are making exactly that possible, so the
 * notice above might get outdated.
 */

define([
    'ufojs/errors'
], function(
    errors
) {
    var DependencyError = errors.Dependency,
        TypeError = errors.Type,
        NotImplementedError = errors.NotImplemented,
        ParserError = errors.Parser;
    
    var xml, implementation;
    if(typeof DOMParser !== 'undefined') {
        implementation = 'w3c';
        //this is straightforward, we just map the apis that are there
        xml = {
            get implementation() {
                return implementation;
            },
            Parser: DOMParser,
            Serializer: XMLSerializer,
            XPathEvaluator: XPathEvaluator,
            XPathResult: XPathResult,
        // XPathException is undefined in Firefox
        //    XPathException: XPathException,
            XSLTProcessor: XSLTProcessor,
            Node: window.Node,
            createDocument: function() {
                var args = [].slice.call(arguments);
                return document.implementation.createDocument.apply(
                    document.implementation, args);
            },
            createDocumentType: function() {
                var args = [].slice.call(arguments);
                return document.implementation.createDocumentType.apply(
                    document.implementation, args);
            },
            parseXMLString: function(string) {
                var parser = new xml.Parser();
                return parser.parseFromString(string, 'text/xml');
            }
        }
        return xml;
    }
    /**
     * this is for the nodejs environment<commit_msg>raise a Parser error when xml parsing fails<commit_after>"use strict";
/**
 * Copyright (c) 2011, <NAME> <EMAIL>, http://graphicore.de
 * 
 * You should have received a copy of the MIT License along with this program.
 * If not, see http://www.opensource.org/licenses/mit-license.php
 * 
 * This returns the browsers xml tools or if in nodejs a mapping to the
 * libxmljs api so that it can be used like the w3c tools.
 * 
 * IMPORTANT for nodejs with libxmljs: As the need of this project regarding
 * xml tools is not so big, there will likely be no full DOM w3c api
 * available. Thus, things like running jQuery on a libxmljs element won't
 * work.
 * As it appears the jsdom people are making exactly that possible, so the
 * notice above might become outdated.
 */

define([
    'ufojs/errors'
], function(
    errors
) {
    var DependencyError = errors.Dependency,
        TypeError = errors.Type,
        NotImplementedError = errors.NotImplemented,
        ParserError = errors.Parser;
    
    var xml, implementation;
    if(typeof DOMParser !== 'undefined') {
        implementation = 'w3c';
        //this is straightforward, we just map the apis that are there
        xml = {
            get implementation() {
                return implementation;
            },
            /**
             * This strange sort of extending is done because:
             * a) native objects can't be extended by the ussual prototype
             *    mechanism.
             * b) The browser parsers fail at throwing Errors when parsing
             *    fails, and we need a common api.
             */
            Parser: function() {
                this._parser = new DOMParser();
            },
            Serializer: XMLSerializer,
            XPathEvaluator: XPathEvaluator,
            XPathResult: XPathResult,
        // XPathException is undefined in Firefox
        //    XPathException: XPathException,
            XSLTProcessor: XSLTProcessor,
            Node: window.Node,
            createDocument: function() {
                var args = [].slice.call(arguments);
                return document.implementation.createDocument.apply(
                    document.implementation, args);
            },
            createDocumentType: function() {
                var args = [].slice.call(arguments);
                return document.implementation.createDocumentType.apply(
                    document.implementation, args);
            },
            parseXMLString: function(string) {
                var parser = new xml.Parser();
                return parser.parseFromString(string, 'text/xml');
            }
        }
        
        //TODO: implement more of the parser interfaces if needed
        xml.Parser.prototype.parseFromString = function(markup, type) {
            var result = this._parser.parseFromString(markup, type);
            
            // in case of an error chrome returns an html document where
            // somwhere is a <parsererror> element embedded: 
            // <html xmlns="http://www.w3.org/1999/xhtml"><body><parsererror style=" ... "><h3> ...
            // sometimes its not html :/
            //
            // firefox returns a document like this:
            // <?xml-stylesheet href="chrome://global/locale/intl.css" type="text/css"?>
            // <parsererror xmlns="http://www.mozilla.org/newlayout/xml/parsererror.xml">
            // ...
            
            if(result.documentElement.tagName === 'parsererror')
                // this works in firefox
                throw new ParserError('XML DOMParser: ' + result.documentElement.textContent);
            // maybe, if we're going to parse xhtml we should try to find
            // more indicators. Right now html as root element is just fine,
            // because there is no other case for an html result in ufoJS.
            // cmon, sometimes its not html in webkit :-|
            // else if(result.documentElement.tagName === 'html') {
            else {
               // handle the webkit error if it is there
               // this is from: https://bugs.webkit.org/show_bug.cgi?id=13057
                   
                var nsResolverWebkit = function (prefix) {
                        if (prefix == "x")
                            return "http://www.w3.org/1999/xhtml";
                        return null;
                    }
                  , expression = "//x:parsererror//x:div"
                  , errorNode = (new xml.XPathEvaluator())
                        .evaluate(expression, result, nsResolverWebkit, 0, null)
                        .iterateNext()
                  ;
               if (errorNode)
                   throw new ParserError('XML DOMParser: ' + errorNode.textContent);
            }
            return result;
        }
            
        return xml;
    }
    /**
     * this is for the nodejs environment
{'fraction_non_alphanumeric': 0.08912680756395995, 'fraction_numerical': 0.004310344827586207, 'mean_word_length': 1.7517214996174446, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 1, 'lorem ipsum': 0, 'www.': 5, 'xml': 28}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6387872', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1751, 'n_words': 597}