-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsciiMathParserBrowserUtilities.js
More file actions
109 lines (94 loc) · 3.49 KB
/
Copy pathAsciiMathParserBrowserUtilities.js
File metadata and controls
109 lines (94 loc) · 3.49 KB
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
/*
Copyright (c) 2011-2012, The University of Edinburgh
All Rights Reserved
This file is part of AsciiMathParser.js
AsciiMathParser.js is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by the
Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
AsciiMathParser.js is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License (at
http://www.gnu.org/licences/lgpl.html) for more details.
You should have received a copy of the GNU Lesser General Public License
along with AsciiMathParser. If not, see <http://www.gnu.org/licenses/lgpl.html>.
AsciiMathParserBrowserUtilities.js
==================================
This contains some helper functions that might be useful if you
want to use AsciiMathParser.js in a browser.
Dependencies:
AsciiMathParser.js
*/
AsciiMathParserBrowserUtilities = {
createXmlDocument: function() {
var doc;
if (document.implementation && document.implementation.createDocument) {
/* Gecko, Webkit, Opera */
doc = document.implementation.createDocument("", "", null);
}
else {
try {
/* Internet Explorer */
doc = new ActiveXObject("Microsoft.XMLDOM");
}
catch (e) {
throw new Error("I don't know how to create a DOM Document in this browser");
}
}
return doc;
},
serializeXmlNode: function(node) {
var xml;
try {
/* Gecko, Webkit, Opera */
var serializer = new XMLSerializer();
xml = serializer.serializeToString(node);
}
catch (e) {
try {
/* Internet Explorer */
xml = node.xml;
}
catch (e) {
throw new Error("I don't know how to serialize XML in this browser");
}
}
return xml;
},
indentMathmlString: function(str) {
var indent = arguments[1] || 2;
var newline = arguments[2] || "\n";
/* This goes for a simple "split on <" approach, which is fine if the input
* is assumed to be well-formed XML. It will fall apart otherwise!
*/
var result = "";
var doIndent = function(level) {
result += newline;
for (var j=0; j<indent * level; j++) {
result += ' ';
}
};
var parts = str.split("<");
var inTextElement = false;
var currentIndentLevel = 0;
for (i=1; i<parts.length; i++) { /* (Starting at 1 since 0 is "bit before <math>" which is empty) */
var part = parts[i];
if (part.charAt(0)=='/') { /* </element> */
if (!inTextElement) { /* </mrow> et al goes on newline */
doIndent(currentIndentLevel);
}
result += '<' + part;
currentIndentLevel--;
inTextElement = false;
}
else { /* <element>content? */
if (i>1) { /* Top-level <math> doesn't need indented */
doIndent(++currentIndentLevel);
}
result += '<' + part;
inTextElement = (part.indexOf(">") < part.length);
}
}
return result;
}
}