to make it styleable.
var frag = maindoc.createDocumentFragment(); // to hold span and space
var span = maindoc.createElement("span"); // span to hold number
span.className = "TOCSectNum"; // make it styleable
span.appendChild(maindoc.createTextNode(sectionNumber)); // add sect#
frag.appendChild(span); // Add span to fragment
frag.appendChild(maindoc.createTextNode(" ")); // Then add a space
if(second == 0)section.insertBefore(frag, section.firstChild); // Add both to header
// Create an anchor to mark the beginning of this section.
var anchor = maindoc.createElement("a");
anchor.name = "TOC"+sectionNumber; // Name the anchor so we can link
anchor.id = "TOC"+sectionNumber; // In IE generated anchors need ids
// Insert the anchor and link immediately before the section header
section.parentNode.insertBefore(anchor, section);
// Now create a link to this section.
var link = d.createElement("a");
link.name = "TOC"+sectionNumber;
link.href = ""; // Set link destination
link.innerHTML = section.innerHTML; // Make link text same as heading
// Place the link in a div that is styleable based on the level
var entry = d.createElement("div");
entry.className = "TOCEntry TOCLevel" + level; // For CSS styling
entry.appendChild(link);
// And add the div to the TOC container
container.appendChild(entry);
}
// This method recursively traverses the tree rooted at node n, looking
// for through tags and appends them to the sections array.
function findSections(n, sects) {
// Loop through all the children of n
for(var m = n.firstChild; m != null; m = m.nextSibling) {
// Skip any nodes that are not elements.
if (m.nodeType != 1 /* Node.Element_NODE */) continue;
// Skip the container element since it may have its own heading
// if (m == container) continue;
// As an optimization, skip
tags since headings are not
// supposed to appear inside paragraphs. (We could also skip
// lists,
tags, etc., but is the most common one.)
if (m.tagName == "P") continue; // optimization
// If we didn't skip the child node, check whether it is a heading.
// If so, add it to the array. Otherwise, recurse on it.
// Note that the DOM is interface-based not class-based so we
// cannot simply test whether (m instanceof HTMLHeadingElement).
if (m.tagName.length==2 && m.tagName.charAt(0)=="H") sects.push(m);
else findSections(m, sects);
}
}
second = 1;
aSet = d.getElementsByTagName("a")
for(w=0;wTable of Contents