dom/tests/js/tables/changeCell.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/dom/tests/js/tables/changeCell.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,104 @@
     1.4 +/* -*- Mode: js; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +function findBody(node)
    1.10 +{
    1.11 +  if (node.nodeType != Node.ELEMENT_NODE) {
    1.12 +    return null;
    1.13 +  }
    1.14 +  var children = node.childNodes;
    1.15 +  if (children == null) {
    1.16 +    return null;
    1.17 +  }
    1.18 +  var length = children.length;
    1.19 +  var child = null;
    1.20 +  var count = 0;
    1.21 +  while (count < length) {
    1.22 +    child = children[count];
    1.23 +    if (child.tagName == "BODY") {
    1.24 +      dump("BODY found");
    1.25 +      return child;
    1.26 +    }
    1.27 +    var body = findBody(child);
    1.28 +    if (null != body) {
    1.29 +      return body;
    1.30 +    }
    1.31 +    count++;
    1.32 +  }
    1.33 +  return null;
    1.34 +}
    1.35 +
    1.36 +// Given the body element, find the first table element
    1.37 +function findTable(body)
    1.38 +{
    1.39 +  // XXX A better way to do this would be to use getElementsByTagName(), but
    1.40 +  // it isn't implemented yet...
    1.41 +  var children = body.childNodes
    1.42 +  if (children == null) {
    1.43 +    return null;
    1.44 +  }
    1.45 +  var length = children.length;
    1.46 +  var child = null;
    1.47 +  var count = 0;
    1.48 +  while (count < length) {
    1.49 +    child = children[count];
    1.50 +    if (child.nodeType == Node.ELEMENT_NODE) {
    1.51 +      if (child.tagName == "TABLE") {
    1.52 +        dump("TABLE found");
    1.53 +        break;
    1.54 +      }
    1.55 +    }
    1.56 +    count++;
    1.57 +  }
    1.58 +
    1.59 +  return child;
    1.60 +}
    1.61 +
    1.62 +// Given a table element, find the first table body
    1.63 +function findTableBody(table)
    1.64 +{
    1.65 +  // XXX A better way to do this would be to use getElementsByTagName(), but
    1.66 +  // it isn't implemented yet...
    1.67 +  var children = table.childNodes
    1.68 +  if (children == null) {
    1.69 +    return null;
    1.70 +  }
    1.71 +  var length = children.length;
    1.72 +  var child = null;
    1.73 +  var count = 0;
    1.74 +  while (count < length) {
    1.75 +    child = children[count];
    1.76 +    if (child.nodeType == Node.ELEMENT_NODE) {
    1.77 +      if (child.tagName == "TBODY") {
    1.78 +        break;
    1.79 +      }
    1.80 +    }
    1.81 +    count++;
    1.82 +  }
    1.83 +
    1.84 +  return child;
    1.85 +}
    1.86 +
    1.87 +// Change the text of the first table cell
    1.88 +function changeCell(table)
    1.89 +{
    1.90 +  // Get a table body
    1.91 +  var body = findTableBody(table)
    1.92 +
    1.93 +  // The table body's first child is a table row
    1.94 +  var row = body.firstChild
    1.95 +
    1.96 +  // The row's first child is a table cell
    1.97 +  var cell = row.firstChild
    1.98 +
    1.99 +  // Get the cell's text
   1.100 +  var text = cell.firstChild
   1.101 +
   1.102 +  // Append some text
   1.103 +  text.append(" NEW TEXT")
   1.104 +}
   1.105 +
   1.106 +changeCell(findTable(findBody(document.documentElement)))
   1.107 +

mercurial