| 1 |
/* |
| 2 |
* The contents of this file are subject to the Mozilla Public |
| 3 |
* License Version 1.1 (the"License"); you may not use this file |
| 4 |
* except in compliance with the License. You may obtain a copy of |
| 5 |
* the License at http://www.mozilla.org/MPL/ |
| 6 |
* |
| 7 |
* Software distributed under the License is distributed on an"AS |
| 8 |
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or |
| 9 |
* implied. See the License for the specific language governing |
| 10 |
* rights and limitations under the License. |
| 11 |
* |
| 12 |
* The Original Code is Mozilla MathML Project. |
| 13 |
* |
| 14 |
* The Initial Developer of the Original Code is The University Of |
| 15 |
* Queensland. Portions created by The University Of Queensland are |
| 16 |
* Copyright (C) 2002 The University Of Queensland. All Rights Reserved. |
| 17 |
* |
| 18 |
* Contributor(s): |
| 19 |
* Roger B. Sidje <[email protected]> |
| 20 |
* Steve Swanson <[email protected]> |
| 21 |
* |
| 22 |
*/ |
| 23 |
|
| 24 |
var gDebug = 0; |
| 25 |
var gLineCount = 0; |
| 26 |
var gStartTargetLine = 0; |
| 27 |
var gEndTargetLine = 0; |
| 28 |
var gTargetNode = null; |
| 29 |
var gWYSIWYGWindow = null; |
| 30 |
// had to use an absolute path here, otherwise Mozilla is confused when the |
| 31 |
// script is loaded from a directory different from where the CSS resides |
| 32 |
var gViewSourceCSS = 'http://www.mozilla.org/projects/mathml/demo/viewsource.css'; |
| 33 |
|
| 34 |
function viewmath(evt) { |
| 35 |
evt.preventBubble(); |
| 36 |
gLineCount = 0; |
| 37 |
gStartTargetLine = 0; |
| 38 |
gEndTargetLine = 0; |
| 39 |
gTargetNode = evt.target; |
| 40 |
if (gTargetNode.nodeType == Node.TEXT_NODE) { // leaf text |
| 41 |
gTargetNode = gTargetNode.parentNode; |
| 42 |
} |
| 43 |
var m = findmathparent(evt.target); |
| 44 |
if (m) { |
| 45 |
gWYSIWYGWindow = open("", "wysiwyg", "scrollbars=1,resizable=1,width=500,height=200"); |
| 46 |
gWYSIWYGWindow.document.open(); |
| 47 |
with (gWYSIWYGWindow.document) { |
| 48 |
writeln('<html><base target="wysiwyg">'); |
| 49 |
writeln('<head><title>MathML WYSIWYG Source</title>'); |
| 50 |
writeln('<link rel="stylesheet" type="text/css" href="'+gViewSourceCSS+'">'); |
| 51 |
writeln('<style>#target{border:dashed 1px; background-color:lightyellow;}</style>'); |
| 52 |
writeln('<script>'); |
| 53 |
writeln('function scrollToTarget() {'); |
| 54 |
// bug: self.location.hash = 'anchor'; doesn't work |
| 55 |
writeln(' var anchor = document.anchors["target"];'); |
| 56 |
writeln(' scrollTo(0/*anchor.offsetLeft*/, anchor.offsetTop);'); |
| 57 |
writeln('}'); |
| 58 |
writeln('</script>'); |
| 59 |
writeln('</head>'); |
| 60 |
write('<body id="viewsource" onload="window.focus();scrollToTarget();">'); |
| 61 |
//if wrapping... write('<body id="viewsource" class="wrap" onload="window.focus();scrollToTarget();">'); |
| 62 |
write('<pre>'); |
| 63 |
writeln(getOuterMarkup(m,0)); |
| 64 |
writeln('</pre></body></html>'); |
| 65 |
} |
| 66 |
gWYSIWYGWindow.document.close(); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
function findmathparent(node) { |
| 71 |
while (node && node.nodeName.indexOf("math") == -1) |
| 72 |
node = node.parentNode; |
| 73 |
return node; |
| 74 |
} |
| 75 |
|
| 76 |
function getInnerMarkup(n, indent) { |
| 77 |
var str = ''; |
| 78 |
for (var i = 0; i<n.childNodes.length; i++) |
| 79 |
str += getOuterMarkup(n.childNodes.item(i),indent); |
| 80 |
return str; |
| 81 |
} |
| 82 |
|
| 83 |
function getOuterMarkup(n, indent) { |
| 84 |
var newline = ''; |
| 85 |
var padding = ''; |
| 86 |
var str = ''; |
| 87 |
if (n == gTargetNode) { |
| 88 |
gStartTargetLine = gLineCount; |
| 89 |
str += '</pre><a name="target"></a><pre id="target">'; |
| 90 |
} |
| 91 |
|
| 92 |
switch (n.nodeType) { |
| 93 |
case Node.ELEMENT_NODE: // Element |
| 94 |
// to avoid the wide gap problem, '\n' is not emitted on the first |
| 95 |
// line and the lines before & after the <pre id="target">...</pre> |
| 96 |
newline = ''; |
| 97 |
if (gLineCount > 0) { |
| 98 |
newline = '\n'; |
| 99 |
} |
| 100 |
if (gLineCount == gStartTargetLine || |
| 101 |
gLineCount == gEndTargetLine) { |
| 102 |
newline = ''; |
| 103 |
} |
| 104 |
gLineCount++; |
| 105 |
if (gDebug) { |
| 106 |
newline += gLineCount; |
| 107 |
} |
| 108 |
for (var k=0; k<indent; k++) { |
| 109 |
padding += ' '; |
| 110 |
} |
| 111 |
str += newline + padding + '<<span class="start-tag">' + n.nodeName + '</span>'; |
| 112 |
for (var i=0; i<n.attributes.length; i++) { |
| 113 |
var attr = n.attributes.item(i); |
| 114 |
if (!gDebug && attr.nodeName.match(/^-moz-math-/)) { |
| 115 |
continue; |
| 116 |
} |
| 117 |
str += ' <span class="attribute-name">'; |
| 118 |
str += attr.nodeName; |
| 119 |
str += '</span>=<span class="attribute-value">"'; |
| 120 |
str += unicode2entity(attr.nodeValue); |
| 121 |
str += '"</span>'; |
| 122 |
} |
| 123 |
str += '>'; |
| 124 |
var oldLine = gLineCount; |
| 125 |
str += getInnerMarkup(n, indent + 2); |
| 126 |
if (oldLine == gLineCount) { |
| 127 |
newline = ''; |
| 128 |
padding = ''; |
| 129 |
} |
| 130 |
else { |
| 131 |
newline = '\n'; |
| 132 |
if (gLineCount == gEndTargetLine) { |
| 133 |
newline = ''; |
| 134 |
} |
| 135 |
gLineCount++; |
| 136 |
if (gDebug) { |
| 137 |
newline += gLineCount; |
| 138 |
} |
| 139 |
} |
| 140 |
str += newline + padding + '</<span class="end-tag">' + n.nodeName + '</span>>'; |
| 141 |
break; |
| 142 |
case Node.TEXT_NODE: // Text |
| 143 |
var tmp = n.nodeValue; |
| 144 |
tmp = tmp.toString(); |
| 145 |
tmp = tmp.replace(/(\n|\r|\t)+/g, " "); |
| 146 |
tmp = tmp.replace(/\s+/," "); |
| 147 |
tmp = tmp.replace(/^\s+/, ""); |
| 148 |
tmp = tmp.replace(/\s+$/,""); |
| 149 |
if (tmp.length != 0) { |
| 150 |
str += '<span class="text">' + unicode2entity(tmp) + '</span>'; |
| 151 |
} |
| 152 |
break; |
| 153 |
default: |
| 154 |
break; |
| 155 |
} |
| 156 |
if (n == gTargetNode) { |
| 157 |
gEndTargetLine = gLineCount; |
| 158 |
str += '</pre><pre>'; |
| 159 |
} |
| 160 |
return str; |
| 161 |
} |