summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/tools/scripts/toc.js
blob: 120d4be039fd1af903f128f1c308c0912e20cd3c (plain)
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
// grab the table of contents filled with all the anchors
function __result_handler() {
    function getMap() {
	var toc_element = document.getElementById("contents").nextElementSibling;

	function getSection() {
	    function getIds(node) {
		var a = [];
	    
		var nodes = node.querySelectorAll('*[id]');
		for (var i = 0; i < nodes.length; i++) {
		    a.push(nodes[i].getAttribute("id"));
		}
		return a;
	    }
	
	    function getTOCIds() {
		var a = [];
	    
		var nodes = toc_element.querySelectorAll('li');
		for (var i = 0; i < nodes.length; i++) {
		    var href = nodes[i].firstElementChild.getAttribute("href");
		    a.push(href.substring(1));
		}
		return a;
	    }

	    var obj = new Object();
	    var ids = getIds(document);
	    var toc = getTOCIds();
    
	    for (var i = 1; i < toc.length; i++) {
		var key1 = toc[i-1];
		var key2 = toc[i];
		var map = [];

		var index1 = ids.indexOf(key1);
		var index2 = ids.indexOf(key2);

		if ((index2-index1) > 1) {
		    for (var j = index1+1; j < index2;j++) {
			map.push(ids[j]);
		    }
		}

		obj[key1] = map;
	    }
	    {
		var key = toc[toc.length-1];
		var index = ids.indexOf(key);
		var map = [];

		for (var j = index+1; j < ids.length;j++) {
		    map.push(ids[j]);
		}
		obj[key] = map;
	    }
    
	    return obj;
	}

	function section(id) {
	    this.id = id;
	}
	function addSubSection(section, sub) {
	    if (typeof section.sections === "undefined") {
		section.sections = [];
	    }
	    section.sections.push(sub);
	}

	function li(el, map) {
	    var obj = new section(el.firstElementChild.getAttribute("href").substring(1));
	    obj.title = el.firstElementChild.textContent;
	    var child = el.firstElementChild;

	    var m = map[obj.id];
	    for (var i = 0; i < m.length; i++)  {	    
		var sub = new section(m[i]); 
		addSubSection(obj, sub);
	    }
	    while (child !== null) {
		if (child.nodeName === "OL") ol(child, obj, map);
		child = child.nextElementSibling;
	    }
	    return obj;
	}

	function ol(el, section, map) {
	    var child = el.firstElementChild;
	    while (child !== null) {
		addSubSection(section, li(child, map));
		child = child.nextElementSibling;
	    }
	}

	var map = getSection();
	var main = new section("___main___");
	main.title = document.title;

	ol(toc_element, main, map);

	return main;
    }

    return getMap();
}