dom/tests/js/tables/changeCell.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:680d6472b6f2
1 /* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 function findBody(node)
7 {
8 if (node.nodeType != Node.ELEMENT_NODE) {
9 return null;
10 }
11 var children = node.childNodes;
12 if (children == null) {
13 return null;
14 }
15 var length = children.length;
16 var child = null;
17 var count = 0;
18 while (count < length) {
19 child = children[count];
20 if (child.tagName == "BODY") {
21 dump("BODY found");
22 return child;
23 }
24 var body = findBody(child);
25 if (null != body) {
26 return body;
27 }
28 count++;
29 }
30 return null;
31 }
32
33 // Given the body element, find the first table element
34 function findTable(body)
35 {
36 // XXX A better way to do this would be to use getElementsByTagName(), but
37 // it isn't implemented yet...
38 var children = body.childNodes
39 if (children == null) {
40 return null;
41 }
42 var length = children.length;
43 var child = null;
44 var count = 0;
45 while (count < length) {
46 child = children[count];
47 if (child.nodeType == Node.ELEMENT_NODE) {
48 if (child.tagName == "TABLE") {
49 dump("TABLE found");
50 break;
51 }
52 }
53 count++;
54 }
55
56 return child;
57 }
58
59 // Given a table element, find the first table body
60 function findTableBody(table)
61 {
62 // XXX A better way to do this would be to use getElementsByTagName(), but
63 // it isn't implemented yet...
64 var children = table.childNodes
65 if (children == null) {
66 return null;
67 }
68 var length = children.length;
69 var child = null;
70 var count = 0;
71 while (count < length) {
72 child = children[count];
73 if (child.nodeType == Node.ELEMENT_NODE) {
74 if (child.tagName == "TBODY") {
75 break;
76 }
77 }
78 count++;
79 }
80
81 return child;
82 }
83
84 // Change the text of the first table cell
85 function changeCell(table)
86 {
87 // Get a table body
88 var body = findTableBody(table)
89
90 // The table body's first child is a table row
91 var row = body.firstChild
92
93 // The row's first child is a table cell
94 var cell = row.firstChild
95
96 // Get the cell's text
97 var text = cell.firstChild
98
99 // Append some text
100 text.append(" NEW TEXT")
101 }
102
103 changeCell(findTable(findBody(document.documentElement)))
104

mercurial