summaryrefslogtreecommitdiffstats
path: root/toolkit/components/microformats/test/static/javascript/parse.js
blob: 588e403eef2716231d0d00b3a788ac7596cfef9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*!
	parse
	Used by http://localhost:3000/
	Copyright (C) 2010 - 2015 Glenn Jones. All Rights Reserved.
	MIT License: https://raw.github.com/glennjones/microformat-shiv/master/license.txt
*/

window.onload = function() {

    var form;
    form= document.getElementById('mf-form');

    form.onsubmit = function(e){
        e = (e)? e : window.event;

        if (e.preventDefault) {
             e.preventDefault();
        } else {
             event.returnValue = false;
        }


        var html,
            baseUrl,
            filter,
            collapsewhitespace,
            overlappingversions,
            impliedPropertiesByVersion,
            dateformatElt,
            dateformat,
            doc,
            node,
            options,
            mfJSON,
            parserJSONElt;

        // get data from html
        html = document.getElementById('html').value;
        baseUrl = document.getElementById('baseurl').value;
        filters = document.getElementById('filters').value;
        collapsewhitespace = document.getElementById('collapsewhitespace').checked;
        //overlappingversions = document.getElementById('overlappingversions').checked;
        //impliedPropertiesByVersion  = document.getElementById('impliedPropertiesByVersion').checked;
        parseLatLonGeo = document.getElementById('parseLatLonGeo').checked;
        dateformatElt = document.getElementById("dateformat");
        dateformat = dateformatElt.options[dateformatElt.selectedIndex].value;
        parserJSONElt = document.querySelector('#parser-json pre code')


        var dom = new DOMParser();
        doc = dom.parseFromString( html, 'text/html' );

        options ={
            'document': doc,
            'node': doc,
            'dateFormat': dateformat,
            'parseLatLonGeo': false
        };

        if(baseUrl.trim() !== ''){
            options.baseUrl = baseUrl;
        }

        if(filters.trim() !== ''){
            if(filters.indexOf(',') > -1){
               options.filters = trimArrayItems(filters.split(','));
            }else{
                options.filters = [filters.trim()];
            }
        }

        if(collapsewhitespace === true){
            options.textFormat = 'normalised';
        }

        /*
        if(overlappingversions === true){
            options.overlappingVersions = false;
        }

        if(impliedPropertiesByVersion === true){
            options.impliedPropertiesByVersion = true;
        }
        */

        if(parseLatLonGeo === true){
            options.parseLatLonGeo = true
        }

        if(options.baseUrl){
            html = '<base href="' + baseUrl+ '">' + html;
        }



        // parse direct into Modules to help debugging
        if(window.Modules){
            var parser = new Modules.Parser();
            mfJSON = parser.get(options);
        }else if(window.Microformats){
            mfJSON = Microformats.get(options);
        }


        // format output
        parserJSONElt.innerHTML = htmlEscape( js_beautify( JSON.stringify(mfJSON) ) );
        //prettyPrint();

    }


};





function htmlEscape(str) {
    return String(str)
            .replace(/&/g, '&amp;')
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;')
            .replace(/</g, '&lt;')
            .replace(/>/g, '&gt;');
}


function trimArrayItems( arr ){
    return arr.map(function(item){
        return item.trim();
    })
}