michael@0: /* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: function findBody(node) michael@0: { michael@0: if (node.nodeType != Node.ELEMENT_NODE) { michael@0: return null; michael@0: } michael@0: var children = node.childNodes; michael@0: if (children == null) { michael@0: return null; michael@0: } michael@0: var length = children.length; michael@0: var child = null; michael@0: var count = 0; michael@0: while (count < length) { michael@0: child = children[count]; michael@0: if (child.tagName == "BODY") { michael@0: dump("BODY found"); michael@0: return child; michael@0: } michael@0: var body = findBody(child); michael@0: if (null != body) { michael@0: return body; michael@0: } michael@0: count++; michael@0: } michael@0: return null; michael@0: } michael@0: michael@0: // Given the body element, find the first table element michael@0: function findTable(body) michael@0: { michael@0: // XXX A better way to do this would be to use getElementsByTagName(), but michael@0: // it isn't implemented yet... michael@0: var children = body.childNodes michael@0: if (children == null) { michael@0: return null; michael@0: } michael@0: var length = children.length; michael@0: var child = null; michael@0: var count = 0; michael@0: while (count < length) { michael@0: child = children[count]; michael@0: if (child.nodeType == Node.ELEMENT_NODE) { michael@0: if (child.tagName == "TABLE") { michael@0: dump("TABLE found"); michael@0: break; michael@0: } michael@0: } michael@0: count++; michael@0: } michael@0: michael@0: return child; michael@0: } michael@0: michael@0: // Given a table element, find the first table body michael@0: function findTableBody(table) michael@0: { michael@0: // XXX A better way to do this would be to use getElementsByTagName(), but michael@0: // it isn't implemented yet... michael@0: var children = table.childNodes michael@0: if (children == null) { michael@0: return null; michael@0: } michael@0: var length = children.length; michael@0: var child = null; michael@0: var count = 0; michael@0: while (count < length) { michael@0: child = children[count]; michael@0: if (child.nodeType == Node.ELEMENT_NODE) { michael@0: if (child.tagName == "TBODY") { michael@0: break; michael@0: } michael@0: } michael@0: count++; michael@0: } michael@0: michael@0: return child; michael@0: } michael@0: michael@0: // Change the text of the first table cell michael@0: function changeCell(table) michael@0: { michael@0: // Get a table body michael@0: var body = findTableBody(table) michael@0: michael@0: // The table body's first child is a table row michael@0: var row = body.firstChild michael@0: michael@0: // The row's first child is a table cell michael@0: var cell = row.firstChild michael@0: michael@0: // Get the cell's text michael@0: var text = cell.firstChild michael@0: michael@0: // Append some text michael@0: text.append(" NEW TEXT") michael@0: } michael@0: michael@0: changeCell(findTable(findBody(document.documentElement))) michael@0: