From 782feeb765b392e0a69ff35cc57c4b92cd5796ea Mon Sep 17 00:00:00 2001 From: Yasuhiro Matsumoto Date: Fri, 23 Mar 2012 12:23:53 +1100 Subject: [PATCH] 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 --- doc/godocs.js | 102 +++++++++++++++++++++++++++++--------------------- 1 file changed, 59 insertions(+), 43 deletions(-) diff --git a/doc/godocs.js b/doc/godocs.js index cc96cf6743e..37573136abc 100644 --- a/doc/godocs.js +++ b/doc/godocs.js @@ -18,13 +18,6 @@ function bindEvent(el, e, fn) { el.attachEvent('on'+e, fn); } } -bindEvent(window, 'load', godocs_onload); - -function godocs_onload() { - godocs_bindSearchEvents(); - godocs_generateTOC(); - godocs_bindExamples(); -} function godocs_bindSearchEvents() { var search = document.getElementById('search'); @@ -39,7 +32,7 @@ function godocs_bindSearchEvents() { } } function restoreInactive() { - if (search.value != "") { + if (search.value !== "") { return; } if (search.type != "search") { @@ -52,6 +45,26 @@ function godocs_bindSearchEvents() { bindEvent(search, 'blur', restoreInactive); } +/* Returns the "This sweet header" from

This sweet header

. + * 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 * links. "Decorates" the element with id=="nav" with this table of contents. */ @@ -138,56 +151,59 @@ function godocs_generateTOC() { tocCell.appendChild(dl2); } -/* Returns the "This sweet header" from

This sweet header

. - * 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); +function getElementsByClassName(base, clazz) { + if (base.getElementsByClassName) { + return base.getElementsByClassName(clazz); + } + var elements = base.getElementsByTagName('*'), foundElements = []; + 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) { - var heading = eg.getElementsByClassName("exampleHeading"); + var heading = getElementsByClassName(eg, "exampleHeading"); + var callback = function() { + if (eg.className == "example") { + eg.className = "exampleVisible"; + } else { + eg.className = "example"; + } + }; for (var i = 0; i < heading.length; i++) { - bindEvent(heading[i], "click", function() { - if (eg.className == "example") { - eg.className = "exampleVisible"; - } else { - eg.className = "example"; - } - }); + bindEvent(heading[i], "click", callback); } } function godocs_bindExampleLink(l) { var prefix = "example_"; bindEvent(l, "click", function() { var i = l.href.indexOf("#"+prefix); - if (i < 0) + if (i < 0) { return; + } var id = prefix + l.href.slice(i+1+prefix.length); var eg = document.getElementById(id); 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);