Project

General

Profile

1
/*
2
 * Copyright (c) 2009, 2010, 2011 and 2012 Frank G. Bennett, Jr. All Rights
3
 * Reserved.
4
 *
5
 * The contents of this file are subject to the Common Public
6
 * Attribution License Version 1.0 (the “License”); you may not use
7
 * this file except in compliance with the License. You may obtain a
8
 * copy of the License at:
9
 *
10
 * http://bitbucket.org/fbennett/citeproc-js/src/tip/LICENSE.
11
 *
12
 * The License is based on the Mozilla Public License Version 1.1 but
13
 * Sections 1.13, 14 and 15 have been added to cover use of software over a
14
 * computer network and provide for limited attribution for the
15
 * Original Developer. In addition, Exhibit A has been modified to be
16
 * consistent with Exhibit B.
17
 *
18
 * Software distributed under the License is distributed on an “AS IS”
19
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
20
 * the License for the specific language governing rights and limitations
21
 * under the License.
22
 *
23
 * The Original Code is the citation formatting software known as
24
 * "citeproc-js" (an implementation of the Citation Style Language
25
 * [CSL]), including the original test fixtures and software located
26
 * under the ./tests subdirectory of the distribution archive.
27
 *
28
 * The Original Developer is not the Initial Developer and is
29
 * __________. If left blank, the Original Developer is the Initial
30
 * Developer.
31
 *
32
 * The Initial Developer of the Original Code is Frank G. Bennett,
33
 * Jr. All portions of the code written by Frank G. Bennett, Jr. are
34
 * Copyright (c) 2009, 2010, 2011, and 2012 Frank G. Bennett, Jr. All Rights Reserved.
35
 *
36
 * Alternatively, the contents of this file may be used under the
37
 * terms of the GNU Affero General Public License (the [AGPLv3]
38
 * License), in which case the provisions of [AGPLv3] License are
39
 * applicable instead of those above. If you wish to allow use of your
40
 * version of this file only under the terms of the [AGPLv3] License
41
 * and not to allow others to use your version of this file under the
42
 * CPAL, indicate your decision by deleting the provisions above and
43
 * replace them with the notice and other provisions required by the
44
 * [AGPLv3] License. If you do not delete the provisions above, a
45
 * recipient may use your version of this file under either the CPAL
46
 * or the [AGPLv3] License.”
47
 */
48
if ("undefined" === typeof CSL_IS_IE) {
49
    var CSL_IS_IE;
50
};
51
var CSL_CHROME = function () {
52
    if ("undefined" == typeof DOMParser || CSL_IS_IE) {
53
        CSL_IS_IE = true;
54
        DOMParser = function() {};
55
        DOMParser.prototype.parseFromString = function(str, contentType) {
56
            if ("undefined" != typeof ActiveXObject) {
57
                var xmldata = new ActiveXObject('MSXML.DomDocument');
58
                xmldata.async = false;
59
                xmldata.loadXML(str);
60
                return xmldata;
61
            } else if ("undefined" != typeof XMLHttpRequest) {
62
                var xmldata = new XMLHttpRequest;
63
                if (!contentType) {
64
                    contentType = 'text/xml';
65
                }
66
                xmldata.open('GET', 'data:' + contentType + ';charset=utf-8,' + encodeURIComponent(str), false);
67
                if(xmldata.overrideMimeType) {
68
                    xmldata.overrideMimeType(contentType);
69
                }
70
                xmldata.send(null);
71
                return xmldata.responseXML;
72
            }
73
        };
74
        this.hasAttributes = function (node) {
75
            var ret;
76
            if (node.attributes && node.attributes.length) {
77
                ret = true;
78
            } else {
79
                ret = false;
80
            }
81
            return ret;
82
        };
83
    } else {
84
        this.hasAttributes = function (node) {
85
            var ret;
86
            if (node.attributes && node.attributes.length) {
87
                ret = true;
88
            } else {
89
                ret = false;
90
            }
91
            return ret;
92
        };
93
    }
94
    this.importNode = function (doc, srcElement) {
95
        if ("undefined" == typeof doc.importNode) {
96
            var ret = this._importNode(doc, srcElement, true);
97
        } else {
98
            var ret = doc.importNode(srcElement, true);
99
        }
100
        return ret;
101
    };
102
    this._importNode = function(doc, node, allChildren) {
103
        switch (node.nodeType) {
104
            case 1:
105
                var newNode = doc.createElement(node.nodeName);
106
                if (node.attributes && node.attributes.length > 0)
107
                    for (var i = 0, il = node.attributes.length; i < il;)
108
                        newNode.setAttribute(node.attributes[i].nodeName, node.getAttribute(node.attributes[i++].nodeName));
109
                    if (allChildren && node.childNodes && node.childNodes.length > 0)
110
                        for (var i = 0, il = node.childNodes.length; i < il;)
111
                            newNode.appendChild(this._importNode(doc, node.childNodes[i++], allChildren));
112
                return newNode;
113
                break;
114
            case 3:
115
            case 4:
116
            case 8:
117
        }
118
    };
119
    this.parser = new DOMParser();
120
    var str = "<docco><institution institution-parts=\"long\" delimiter=\", \" substitute-use-first=\"1\" use-last=\"1\"><institution-part name=\"long\"/></institution></docco>";
121
    var inst_doc = this.parser.parseFromString(str, "text/xml");
122
    var inst_node = inst_doc.getElementsByTagName("institution");
123
    this.institution = inst_node.item(0);
124
    var inst_part_node = inst_doc.getElementsByTagName("institution-part");
125
    this.institutionpart = inst_part_node.item(0);
126
    this.ns = "http://purl.org/net/xbiblio/csl";
127
};
128
CSL_CHROME.prototype.clean = function (xml) {
129
    xml = xml.replace(/<\?[^?]+\?>/g, "");
130
    xml = xml.replace(/<![^>]+>/g, "");
131
    xml = xml.replace(/^\s+/, "");
132
    xml = xml.replace(/\s+$/, "");
133
    xml = xml.replace(/^\n*/, "");
134
    return xml;
135
};
136
CSL_CHROME.prototype.getStyleId = function (myxml) {
137
    var text = "";
138
    var node = myxml.getElementsByTagName("id");
139
    if (node && node.length) {
140
        node = node.item(0);
141
    }
142
    if (node) {
143
        text = node.textContent;
144
    }
145
    if (!text) {
146
        text = node.innerText;
147
    }
148
    if (!text) {
149
        text = node.innerHTML;
150
    }
151
    return text;
152
};
153
CSL_CHROME.prototype.children = function (myxml) {
154
    var children, pos, len, ret;
155
    if (myxml) {
156
        ret = [];
157
        children = myxml.childNodes;
158
        for (pos = 0, len = children.length; pos < len; pos += 1) {
159
            if (children[pos].nodeName != "#text") {
160
                ret.push(children[pos]);
161
            }
162
        }
163
        return ret;
164
    } else {
165
        return [];
166
    }
167
};
168
CSL_CHROME.prototype.nodename = function (myxml) {
169
    var ret = myxml.nodeName;
170
    return ret;
171
};
172
CSL_CHROME.prototype.attributes = function (myxml) {
173
    var ret, attrs, attr, key, xml, pos, len;
174
    ret = new Object();
175
    if (myxml && this.hasAttributes(myxml)) {
176
        attrs = myxml.attributes;
177
        for (pos = 0, len=attrs.length; pos < len; pos += 1) {
178
            attr = attrs[pos];
179
            ret["@" + attr.name] = attr.value;
180
        }
181
    }
182
    return ret;
183
};
184
CSL_CHROME.prototype.content = function (myxml) {
185
    var ret;
186
    if ("undefined" != typeof myxml.textContent) {
187
        ret = myxml.textContent;
188
    } else if ("undefined" != typeof myxml.innerText) {
189
        ret = myxml.innerText;
190
    } else {
191
        ret = myxml.txt;
192
    }
193
    return ret;
194
};
195
CSL_CHROME.prototype.namespace = {
196
    "xml":"http://www.w3.org/XML/1998/namespace"
197
}
198
CSL_CHROME.prototype.numberofnodes = function (myxml) {
199
    if (myxml) {
200
        return myxml.length;
201
    } else {
202
        return 0;
203
    }
204
};
205
CSL_CHROME.prototype.getAttributeName = function (attr) {
206
    var ret = attr.name;
207
    return ret;
208
}
209
CSL_CHROME.prototype.getAttributeValue = function (myxml,name,namespace) {
210
    var ret = "";
211
    if (namespace) {
212
        name = namespace+":"+name;
213
    }
214
    if (myxml && this.hasAttributes(myxml) && myxml.getAttribute(name)) {
215
        ret = myxml.getAttribute(name);
216
    }
217
    return ret;
218
}
219
CSL_CHROME.prototype.getNodeValue = function (myxml,name) {
220
    var ret = "";
221
    if (name){
222
        var vals = myxml.getElementsByTagName(name);
223
        if (vals.length > 0) {
224
            if ("undefined" != typeof vals[0].textContent) {
225
                ret = vals[0].textContent;
226
            } else if ("undefined" != typeof vals[0].innerText) {
227
                ret = vals[0].innerText;
228
            } else {
229
                ret = vals[0].text;
230
            }
231
        }
232
    } else {
233
        ret = myxml;
234
    }
235
    if (ret && ret.childNodes && (ret.childNodes.length == 0 || (ret.childNodes.length == 1 && ret.firstChild.nodeName == "#text"))) {
236
        if ("undefined" != typeof ret.textContent) {
237
            ret = ret.textContent;
238
        } else if ("undefined" != typeof ret.innerText) {
239
            ret = ret.innerText;
240
        } else {
241
            ret = ret.text;
242
        }
243
    }
244
    return ret;
245
}
246
CSL_CHROME.prototype.setAttributeOnNodeIdentifiedByNameAttribute = function (myxml,nodename,partname,attrname,val) {
247
    var pos, len, xml, nodes, node;
248
    if (attrname.slice(0,1) === '@'){
249
        attrname = attrname.slice(1);
250
    }
251
    nodes = myxml.getElementsByTagName(nodename);
252
    for (pos = 0, len = nodes.length; pos < len; pos += 1) {
253
        node = nodes[pos];
254
        if (node.getAttribute("name") != partname) {
255
            continue;
256
        }
257
        node.setAttribute(attrname, val);
258
    }
259
}
260
CSL_CHROME.prototype.deleteNodeByNameAttribute = function (myxml,val) {
261
    var pos, len, node, nodes;
262
    nodes = myxml.childNodes;
263
    for (pos = 0, len = nodes.length; pos < len; pos += 1) {
264
        node = nodes[pos];
265
        if (!node || node.nodeType == node.TEXT_NODE) {
266
            continue;
267
        }
268
        if (this.hasAttributes(node) && node.getAttribute("name") == val) {
269
            myxml.removeChild(nodes[pos]);
270
        }
271
    }
272
}
273
CSL_CHROME.prototype.deleteAttribute = function (myxml,attr) {
274
    myxml.removeAttribute(attr);
275
}
276
CSL_CHROME.prototype.setAttribute = function (myxml,attr,val) {
277
    if (!myxml.ownerDocument) {
278
        myxml = myxml.firstChild;
279
    }
280
    if (["function", "unknown"].indexOf(typeof myxml.setAttribute) > -1) {
281
        myxml.setAttribute(attr, val);
282
    }
283
    return false;
284
}
285
CSL_CHROME.prototype.nodeCopy = function (myxml) {
286
    var cloned_node = myxml.cloneNode(true);
287
    return cloned_node;
288
}
289
CSL_CHROME.prototype.getNodesByName = function (myxml,name,nameattrval) {
290
    var ret, nodes, node, pos, len;
291
    ret = [];
292
    nodes = myxml.getElementsByTagName(name);
293
    for (pos = 0, len = nodes.length; pos < len; pos += 1) {
294
        node = nodes.item(pos);
295
        if (nameattrval && !(this.hasAttributes(node) && node.getAttribute("name") == nameattrval)) {
296
            continue;
297
        }
298
        ret.push(node);
299
    }
300
    return ret;
301
}
302
CSL_CHROME.prototype.nodeNameIs = function (myxml,name) {
303
    if (name == myxml.nodeName) {
304
        return true;
305
    }
306
    return false;
307
}
308
CSL_CHROME.prototype.makeXml = function (myxml) {
309
    var ret, topnode;
310
    if (!myxml) {
311
        myxml = "<docco><bogus/></docco>";
312
    }
313
    myxml = myxml.replace(/\s*<\?[^>]*\?>\s*\n*/g, "");
314
    var nodetree = this.parser.parseFromString(myxml, "application/xml");
315
    return nodetree.firstChild;
316
};
317
CSL_CHROME.prototype.insertChildNodeAfter = function (parent,node,pos,datexml) {
318
    var myxml, xml;
319
    myxml = this.importNode(node.ownerDocument, datexml);
320
    parent.replaceChild(myxml, node);
321
     return parent;
322
};
323
CSL_CHROME.prototype.insertPublisherAndPlace = function(myxml) {
324
    var group = myxml.getElementsByTagName("group");
325
    for (var i = 0, ilen = group.length; i < ilen; i += 1) {
326
        var node = group.item(i);
327
        var skippers = [];
328
        for (var j = 0, jlen = node.childNodes.length; j < jlen; j += 1) {
329
            if (node.childNodes.item(j).nodeType !== 1) {
330
                skippers.push(j);
331
            }
332
        }
333
        if (node.childNodes.length - skippers.length === 2) {
334
            var twovars = [];
335
            for (var j = 0, jlen = 2; j < jlen; j += 1) {
336
                if (skippers.indexOf(j) > -1) {
337
                    continue;
338
                }
339
                var child = node.childNodes.item(j);                    
340
                var subskippers = [];
341
                for (var k = 0, klen = child.childNodes.length; k < klen; k += 1) {
342
                    if (child.childNodes.item(k).nodeType !== 1) {
343
                        subskippers.push(k);
344
                    }
345
                }
346
                if (child.childNodes.length - subskippers.length === 0) {
347
                    twovars.push(child.getAttribute('variable'));
348
                    if (child.getAttribute('suffix')
349
                        || child.getAttribute('prefix')) {
350
                        twovars = [];
351
                        break;
352
                    }
353
                }
354
            }
355
            if (twovars.indexOf("publisher") > -1 && twovars.indexOf("publisher-place") > -1) {
356
                node.setAttribute('has-publisher-and-publisher-place', true);
357
            }
358
        }
359
    }
360
};
361
CSL_CHROME.prototype.addMissingNameNodes = function(myxml) {
362
    var nameslist = myxml.getElementsByTagName("names");
363
    for (var i = 0, ilen = nameslist.length; i < ilen; i += 1) {
364
        var names = nameslist.item(i);
365
        var namelist = names.getElementsByTagName("name");
366
        if ((!namelist || namelist.length === 0)
367
            && names.parentNode.tagName.toLowerCase() !== "substitute") {
368
            var doc = names.ownerDocument;
369
            var name = doc.createElement("name");
370
            names.appendChild(name);
371
        }
372
    }
373
};
374
CSL_CHROME.prototype.addInstitutionNodes = function(myxml) {
375
    var names, thenames, institution, theinstitution, name, thename, xml, pos, len;
376
    names = myxml.getElementsByTagName("names");
377
    for (pos = 0, len = names.length; pos < len; pos += 1) {
378
        thenames = names.item(pos);
379
        name = thenames.getElementsByTagName("name");
380
        if (name.length == 0) {
381
            continue;
382
        }
383
        institution = thenames.getElementsByTagName("institution");
384
        if (institution.length == 0) {
385
            theinstitution = this.importNode(myxml.ownerDocument, this.institution);
386
            theinstitutionpart = theinstitution.getElementsByTagName("institution-part").item(0);
387
            thename = name.item(0);
388
            thenames.insertBefore(theinstitution, thename.nextSibling);
389
            for (var j = 0, jlen = CSL.INSTITUTION_KEYS.length; j < jlen; j += 1) {
390
                var attrname = CSL.INSTITUTION_KEYS[j];
391
                var attrval = thename.getAttribute(attrname);
392
                if (attrval) {
393
                    theinstitutionpart.setAttribute(attrname, attrval);
394
                }
395
            }
396
            var nameparts = thename.getElementsByTagName("name-part");
397
            for (var j = 0, jlen = nameparts.length; j < jlen; j += 1) {
398
                if ('family' === nameparts[j].getAttribute('name')) {
399
                    for (var k = 0, klen = CSL.INSTITUTION_KEYS.length; k < klen; k += 1) {
400
                        var attrname = CSL.INSTITUTION_KEYS[k];
401
                        var attrval = nameparts[j].getAttribute(attrname);
402
                        if (attrval) {
403
                            theinstitutionpart.setAttribute(attrname, attrval);
404
                        }
405
                    }
406
                }
407
            }
408
        }
409
    }
410
};
411
CSL_CHROME.prototype.flagDateMacros = function(myxml) {
412
    var pos, len, thenode, thedate;
413
    nodes = myxml.getElementsByTagName("macro");
414
    for (pos = 0, len = nodes.length; pos < len; pos += 1) {
415
        thenode = nodes.item(pos);
416
        thedate = thenode.getElementsByTagName("date");
417
        if (thedate.length) {
418
            thenode.setAttribute('macro-has-date', 'true');
419
        }
420
    }
421
};
(6-6/7)