1
0
mirror of https://github.com/golang/go synced 2024-11-22 04:04:40 -07:00

doc/godoc.js: fix error on IE8.

* implement simple getElementsByClassName for IE8.
        * remove some lint warnings.

Fixes #3318.

R=golang-dev, adg
CC=golang-dev
https://golang.org/cl/5881054
This commit is contained in:
Yasuhiro Matsumoto 2012-03-23 12:23:53 +11:00 committed by Andrew Gerrand
parent 145c48c1d1
commit 782feeb765

View File

@ -18,13 +18,6 @@ function bindEvent(el, e, fn) {
el.attachEvent('on'+e, fn); el.attachEvent('on'+e, fn);
} }
} }
bindEvent(window, 'load', godocs_onload);
function godocs_onload() {
godocs_bindSearchEvents();
godocs_generateTOC();
godocs_bindExamples();
}
function godocs_bindSearchEvents() { function godocs_bindSearchEvents() {
var search = document.getElementById('search'); var search = document.getElementById('search');
@ -39,7 +32,7 @@ function godocs_bindSearchEvents() {
} }
} }
function restoreInactive() { function restoreInactive() {
if (search.value != "") { if (search.value !== "") {
return; return;
} }
if (search.type != "search") { if (search.type != "search") {
@ -52,6 +45,26 @@ function godocs_bindSearchEvents() {
bindEvent(search, 'blur', restoreInactive); bindEvent(search, 'blur', restoreInactive);
} }
/* Returns the "This sweet header" from <h2>This <i>sweet</i> header</h2>.
* Takes a node, returns a string.
*/
function godocs_nodeToText(node) {
var TEXT_NODE = 3; // Defined in Mozilla but not MSIE :(
var text = '';
for (var j = 0; j != node.childNodes.length; j++) {
var child = node.childNodes[j];
if (child.nodeType == TEXT_NODE) {
if (child.nodeValue != '[Top]') { //ok, that's a hack, but it works.
text = text + child.nodeValue;
}
} else {
text = text + godocs_nodeToText(child);
}
}
return text;
}
/* Generates a table of contents: looks for h2 and h3 elements and generates /* Generates a table of contents: looks for h2 and h3 elements and generates
* links. "Decorates" the element with id=="nav" with this table of contents. * links. "Decorates" the element with id=="nav" with this table of contents.
*/ */
@ -138,56 +151,59 @@ function godocs_generateTOC() {
tocCell.appendChild(dl2); tocCell.appendChild(dl2);
} }
/* Returns the "This sweet header" from <h2>This <i>sweet</i> header</h2>. function getElementsByClassName(base, clazz) {
* Takes a node, returns a string. if (base.getElementsByClassName) {
*/ return base.getElementsByClassName(clazz);
function godocs_nodeToText(node) {
var TEXT_NODE = 3; // Defined in Mozilla but not MSIE :(
var text = '';
for (var j = 0; j != node.childNodes.length; j++) {
var child = node.childNodes[j];
if (child.nodeType == TEXT_NODE) {
if (child.nodeValue != '[Top]') { //ok, that's a hack, but it works.
text = text + child.nodeValue;
} }
} else { var elements = base.getElementsByTagName('*'), foundElements = [];
text = text + godocs_nodeToText(child); for (var n in elements) {
if (clazz == elements[n].className) {
foundElements.push(elements[n]);
} }
} }
return text; return foundElements;
} }
function godocs_bindExamples() {
var examples = document.getElementsByClassName("example");
for (var i = 0; i < examples.length; i++) {
godocs_bindExampleToggle(examples[i]);
}
var links = document.getElementsByClassName("exampleLink");
for (var i = 0; i < links.length; i++) {
godocs_bindExampleLink(links[i]);
}
}
function godocs_bindExampleToggle(eg) { function godocs_bindExampleToggle(eg) {
var heading = eg.getElementsByClassName("exampleHeading"); var heading = getElementsByClassName(eg, "exampleHeading");
for (var i = 0; i < heading.length; i++) { var callback = function() {
bindEvent(heading[i], "click", function() {
if (eg.className == "example") { if (eg.className == "example") {
eg.className = "exampleVisible"; eg.className = "exampleVisible";
} else { } else {
eg.className = "example"; eg.className = "example";
} }
}); };
for (var i = 0; i < heading.length; i++) {
bindEvent(heading[i], "click", callback);
} }
} }
function godocs_bindExampleLink(l) { function godocs_bindExampleLink(l) {
var prefix = "example_"; var prefix = "example_";
bindEvent(l, "click", function() { bindEvent(l, "click", function() {
var i = l.href.indexOf("#"+prefix); var i = l.href.indexOf("#"+prefix);
if (i < 0) if (i < 0) {
return; return;
}
var id = prefix + l.href.slice(i+1+prefix.length); var id = prefix + l.href.slice(i+1+prefix.length);
var eg = document.getElementById(id); var eg = document.getElementById(id);
eg.className = "exampleVisible"; eg.className = "exampleVisible";
}); });
} }
function godocs_bindExamples() {
var examples = getElementsByClassName(document, "example");
for (var i = 0; i < examples.length; i++) {
godocs_bindExampleToggle(examples[i]);
}
var links = getElementsByClassName(document, "exampleLink");
for (i = 0; i < links.length; i++) {
godocs_bindExampleLink(links[i]);
}
}
function godocs_onload() {
godocs_bindSearchEvents();
godocs_generateTOC();
godocs_bindExamples();
}
bindEvent(window, 'load', godocs_onload);