1 38 package com.gargoylesoftware.htmlunit.javascript.host; 39 40 import org.jaxen.JaxenException; 41 import org.mozilla.javascript.Context; 42 import org.mozilla.javascript.Function; 43 import org.mozilla.javascript.Scriptable; 44 45 import com.gargoylesoftware.htmlunit.html.HtmlElement; 46 import com.gargoylesoftware.htmlunit.html.HtmlTable; 47 import com.gargoylesoftware.htmlunit.html.HtmlTableRow; 48 import com.gargoylesoftware.htmlunit.html.xpath.HtmlUnitXPath; 49 import com.gargoylesoftware.htmlunit.javascript.ElementArray; 50 51 58 public class TableRow extends HTMLElement { 59 private static final long serialVersionUID = 3256441404401397812L; 60 private ElementArray cells_; 62 65 public TableRow() { 66 } 67 68 72 public void jsConstructor() { 73 } 74 75 81 public int jsxGet_rowIndex() { 82 final HtmlTableRow row = (HtmlTableRow) getHtmlElementOrDie(); 83 final HtmlTable table = row.getEnclosingTable(); 84 return table.getRows().indexOf(row); 85 } 86 87 91 public Object jsxGet_cells() { 92 if (cells_ == null) { 93 cells_ = (ElementArray) makeJavaScriptObject(ElementArray.JS_OBJECT_NAME); 94 try { 95 cells_.init(getDomNodeOrDie(), new HtmlUnitXPath(".//td")); 96 } 97 catch (final JaxenException e) { 98 throw Context.reportRuntimeError("Failed to initialize row.cells: " + e.getMessage()); 99 } 100 } 101 return cells_; 102 } 103 104 116 public static Object jsxFunction_insertCell(final Context cx, final Scriptable s, 117 final Object [] args, final Function f) { 118 final TableRow row = (TableRow) s; 119 final HtmlTableRow htmlRow = (HtmlTableRow) row.getDomNodeOrDie(); 120 121 final int position = getIntArg(0, args, -1); 122 123 final boolean indexValid = (position >= -1 && position <= htmlRow.getCells().size()); 124 if (indexValid) { 125 final HtmlElement newCell = htmlRow.getPage().createElement("td"); 126 if (position == -1 || position == htmlRow.getCells().size()) { 127 htmlRow.appendChild(newCell); 128 } 129 else { 130 htmlRow.getCell(position).insertBefore(newCell); 131 } 132 return row.getScriptableFor(newCell); 133 } 134 else { 135 throw Context.reportRuntimeError("Index or size is negative or greater than the allowed amount"); 136 } 137 } 138 } 139 | Popular Tags |