/** * Gets a list of descendants of e that match the given class name. * * If the browser has the native method, that will be called. Otherwise, it * traverses descendents of the given element and returns the list of elements * with matching classname. * * @param e * @param className */ public static NodeList<Element> getElementsByClassName(Element e, String className) { if (QuirksConstants.SUPPORTS_GET_ELEMENTS_BY_CLASSNAME) { return getElementsByClassNameNative(e, className); } else { NodeList<Element> all = e.getElementsByTagName("*"); if (all == null) { return null; } JsArray<Element> ret = JavaScriptObject.createArray().cast(); for (int i = 0; i < all.getLength(); ++i) { Element item = all.getItem(i); if (className.equals(item.getClassName())) { ret.push(item); } } return ret.cast(); } }
private static Element getInputElement(NodeList<Node> nodes, int i, boolean shiftKeyDown) { Node node = nodes.getItem(i); if (((Element) node).getPropertyBoolean("disabled")) { return null; } // Focus on <input> and <button> but only if they are visible. if ((node.getNodeName().equals("INPUT") || node.getNodeName().equals("BUTTON") || node.getNodeName().equals("SELECT")) && !((Element) node).getStyle() .getDisplay() .equals("none")) { return (Element) node; } else if (node.getChildNodes() .getLength() > 0) { return getInputElement(node.getChildNodes(), shiftKeyDown); } return null; }
public void onModuleLoad() { // GWT.setUncaughtExceptionHandler(new ClientExceptionHandler()); NodeList nl = Document.get().getElementsByTagName("style"); final ArrayList<String> toLoad = new ArrayList<String>(); for (int i = 0; i < nl.getLength(); i++) { Element e = (Element) nl.getItem(i); if ("text/gss".equals(e.getAttribute("type"))) { GWT.log("Style = " + e.getInnerText(), null); } } Chronoscope.setMicroformatsEnabled(true); ChronoscopeOptions.setErrorReporting(true); Chronoscope.getInstance(); }
/** Makes splitter better. */ public void tuneSplitters() { NodeList<Node> nodes = splitPanel.getElement().getChildNodes(); boolean firstFound = false; for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.getItem(i); if (node.hasChildNodes()) { com.google.gwt.dom.client.Element el = node.getFirstChild().cast(); if ("gwt-SplitLayoutPanel-HDragger".equals(el.getClassName())) { if (!firstFound) { firstFound = true; tuneLeftSplitter(el); } else { tuneRightSplitter(el); } } else if ("gwt-SplitLayoutPanel-VDragger".equals(el.getClassName())) { tuneBottomSplitter(el); } } } }
/** Improves splitter visibility. */ private void tuneSplitter(SplitLayoutPanel splitLayoutPanel) { NodeList<Node> nodes = splitLayoutPanel.getElement().getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.getItem(i); if (node.hasChildNodes()) { Element el = node.getFirstChild().cast(); String className = el.getClassName(); if (HORIZONTAL_DRAGGER_CLASS.equals(className)) { tuneVerticalSplitter(el); } else if (VERTICAL_DRAGGER_CLASS.equals(className)) { tuneHorizontalSplitter(el); } } } }
/** * Get the element of the main article, if any. * @return An element of article (not necessarily the html5 article element). */ public static Element getArticleElement(Element root) { NodeList<Element> allArticles = root.getElementsByTagName("ARTICLE"); List<Element> visibleElements = getVisibleElements(allArticles); // Having multiple article elements usually indicates a bad case for this shortcut. // TODO(wychen): some sites exclude things like title and author in article element. if (visibleElements.size() == 1) { return visibleElements.get(0); } // Note that the CSS property matching is case sensitive, and "Article" is the correct // capitalization. String query = "[itemscope][itemtype*=\"Article\"],[itemscope][itemtype*=\"Posting\"]"; allArticles = DomUtil.querySelectorAll(root, query); visibleElements = getVisibleElements(allArticles); // It is commonly seen that the article is wrapped separately or in multiple layers. if (visibleElements.size() > 0) { return Element.as(DomUtil.getNearestCommonAncestor(visibleElements)); } return null; }
/** * Get the list of source URLs of this image. * It's more efficient to call after generateOutput(). * @return Source URLs or an empty List. */ public List<String> getImageUrlList() { if (cloned == null) { cloneAndProcessNode(); } List<String> imgUrls = new ArrayList<>(); NodeList<Element> imgs = DomUtil.querySelectorAll(cloned, "IMG, SOURCE"); for (int i = 0; i < imgs.getLength(); i++) { ImageElement ie = (ImageElement) imgs.getItem(i); if (!ie.getSrc().isEmpty()) { imgUrls.add(ie.getSrc()); } imgUrls.addAll(DomUtil.getAllSrcSetUrls(ie)); } return imgUrls; }
private static List<Element> getDirectDescendants(Element t) { List<Element> directDescendants = new ArrayList<Element>(); NodeList<Element> allDescendants = t.getElementsByTagName("*"); if (!hasNestedTables(t)) { for (int i = 0; i < allDescendants.getLength(); i++) { directDescendants.add(allDescendants.getItem(i)); } } else { for (int i = 0; i < allDescendants.getLength(); i++) { // Check if the current element is a direct descendant of the |t| table element in // question, as opposed to being a descendant of a nested table in |t|. Element e = allDescendants.getItem(i); Element parent = e.getParentElement(); while (parent != null) { if (parent.hasTagName("TABLE")) { if (parent == t) directDescendants.add(e); break; } parent = parent.getParentElement(); } } } return directDescendants; }
private void findTitle() { init(); mTitle = ""; if (mAllMeta.getLength() == 0) return; // Make sure there's a <title> element. NodeList<Element> titles = mRoot.getElementsByTagName("TITLE"); if (titles.getLength() == 0) return; // Extract title text from meta tag with "title" as name. for (int i = 0; i < mAllMeta.getLength(); i++) { MetaElement meta = MetaElement.as(mAllMeta.getItem(i)); if (meta.getName().equalsIgnoreCase("title")) { mTitle = meta.getContent(); break; } } }
private void findImages() { mImages = new ArrayList<MarkupParser.Image>(); NodeList<Element> allImages = mRoot.getElementsByTagName("IMG"); for (int i = 0; i < allImages.getLength(); i++) { ImageElement imgElem = ImageElement.as(allImages.getItem(i)); // As long as the image has a caption, it's relevant regardless of size; // otherwise, it's relevant if its size is good. String caption = getCaption(imgElem); if ((caption != null && !caption.isEmpty()) || isImageRelevantBySize(imgElem)) { // Add relevant image to list. MarkupParser.Image image = new MarkupParser.Image(); image.url = imgElem.getSrc(); image.caption = caption; image.width = imgElem.getWidth(); image.height = imgElem.getHeight(); mImages.add(image); } } }
private static String getCaption(ImageElement image) { // If |image| is a child of <figure>, then get the <figcaption> elements. Element parent = image.getParentElement(); if (!parent.hasTagName("FIGURE")) return ""; NodeList<Element> captions = parent.getElementsByTagName("FIGCAPTION"); int numCaptions = captions.getLength(); String caption = ""; if (numCaptions > 0 && numCaptions <= 2) { // Use javascript innerText (instead of javascript textContent) to get only visible // captions. for (int i = 0; i < numCaptions && caption.isEmpty(); i++) { caption = DomUtil.getInnerText(captions.getItem(i)); } } return caption; }
/** * The tree graph is: * 1 - 2 - 3 * \ 4 - 5 */ public void testNearestCommonAncestor() { Element div = TestUtil.createDiv(1); mBody.appendChild(div); Element div2 = TestUtil.createDiv(2); div.appendChild(div2); Element currDiv = TestUtil.createDiv(3); div2.appendChild(currDiv); Element finalDiv1 = currDiv; currDiv = TestUtil.createDiv(4); div2.appendChild(currDiv); currDiv.appendChild(TestUtil.createDiv(5)); assertEquals(div2, DomUtil.getNearestCommonAncestor(finalDiv1, currDiv.getChild(0))); NodeList nodeList = DomUtil.querySelectorAll(mRoot, "[id=\"3\"],[id=\"5\"]"); assertEquals(div2, DomUtil.getNearestCommonAncestor(TestUtil.nodeListToList(nodeList))); }
protected void gwtSetUp() throws Exception { mRoot = Document.get().getDocumentElement(); JsArray<Node> attrs = DomUtil.getAttributes(mRoot); String[] attrNames = new String[attrs.length()]; for (int i = 0; i < attrs.length(); i++) { attrNames[i] = attrs.get(i).getNodeName(); } for (int i = 0; i < attrNames.length; i++) { mRoot.removeAttribute(attrNames[i]); } assertEquals(0, DomUtil.getAttributes(mRoot).length()); NodeList<Node> children = mRoot.getChildNodes(); for (int i = children.getLength() - 1; i >= 0; i--) { children.getItem(i).removeFromParent(); } assertEquals(0, mRoot.getChildNodes().getLength()); mHead = Document.get().createElement("head"); mRoot.appendChild(mHead); mBody = Document.get().createElement("body"); mRoot.appendChild(mBody); // With this, the width of chrome window won't affect the layout. mRoot.getStyle().setProperty("width", "800px"); }
private void onOpenRow(Element row) { // Find the first HREF of the anchor of the select row (if any) if (row != null) { NodeList<Element> nodes = row.getElementsByTagName(AnchorElement.TAG); for (int i = 0; i < nodes.getLength(); i++) { String url = nodes.getItem(i).getAttribute("href"); if (!url.isEmpty()) { if (url.startsWith("#")) { Gerrit.display(url.substring(1)); } else { Window.Location.assign(url); } break; } } } saveSelectedTab(); }
private static boolean menuVisible(Element searchFrom, String label) { System.out.println("Looking for an <li> with text " + label); NodeList<com.google.gwt.dom.client.Element> liElems = searchFrom.getElementsByTagName("li"); for (int i = 0, n = liElems.getLength(); i < n; i++) { com.google.gwt.dom.client.Element item = liElems.getItem(i); if (item.getInnerText().contains(label)) { System.out.println("Found: " + item); return true; } else { System.out.println("Not this one!"); } } System.out.println("Not Found!"); return false; }
@Override public void removeColumn(int index) { if (columnsRemover != null) { columnsRemover.removeColumn(index); } else { Column<T, ?> col = getColumn(index); hiddenColumns.remove(col); super.removeColumn(index); NodeList<Element> colGroups = getElement().getElementsByTagName("colgroup"); if (colGroups != null && colGroups.getLength() == 1) { TableColElement colGroup = colGroups.getItem(0).cast(); if (getColumnCount() < colGroup.getChildCount()) { // It seems, that GWT's bug is still here. if (index >= 0 && index < colGroup.getChildCount()) { colGroup.removeChild(colGroup.getChild(index)); } } } } }
/** * Sets a column in the table to be sortable. This will convert the content in the * "th" to be something the user can click on. When the user clicks on it, the * internal state of the table will be altered *and* an event will be fired. * @param columnIndex * @param columnId */ public void setColumnSortable(int columnIndex, final String columnId) { Element thElement = null; NodeList<com.google.gwt.dom.client.Element> elementsByTagName = this.thead.getElementsByTagName("th"); //$NON-NLS-1$ if (columnIndex <= elementsByTagName.getLength()) { thElement = elementsByTagName.getItem(columnIndex).cast(); } if (thElement == null) { return; } String columnLabel = thElement.getInnerText(); thElement.setInnerText(""); //$NON-NLS-1$ SortableTableHeader widget = new SortableTableHeader(columnLabel, columnId); widget.removeFromParent(); DOM.appendChild(thElement, widget.getElement()); adopt(widget); widget.addClickHandler(new ClickHandler() { @Override public void onClick(ClickEvent event) { onColumnHeaderClick(columnId); } }); columnIdMap.put(columnId, widget); }
/** * Gets an element from the given parent element by ID. * @param context * @param id */ public static Element findElementById(Element context, String id) { NodeList<Node> nodes = context.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.getItem(i); if (((node.getNodeType() == Node.ELEMENT_NODE))) { if (id.equals(((Element) node).getAttribute("id"))) { //$NON-NLS-1$ return (Element) node; } else { Element elem = findElementById((Element) node, id); if (elem != null) { return elem; } } } } return null; }
@Override protected void updateSelection() { T value = getValue(); if(value != null){ getPaperElement().setSelected(null); PaperRadioButtonElement selectedOpt = null; NodeList<Element> items = getPaperElement().getElementsByTagName(PaperRadioButtonElement.TAG); for(int k = 0, len = items.getLength(); k < len; k++){ PaperRadioButtonElement item = (PaperRadioButtonElement) items.getItem(k); item.setChecked(false); if(value.equals(item.getName())){ selectedOpt = item; } } getPaperElement().setSelected(value.toString()); if(selectedOpt != null){ selectedOpt.setChecked(true); } } }
public void setSize(int width, int height) { NodeList<Element> iframeNodes = container.getElement().getElementsByTagName("iframe"); if (iframeNodes != null && iframeNodes.getLength() > 0) { int iFrameHeight = height - 30; iFrameHeight = iFrameHeight > 0 ? iFrameHeight : 0; iframeNodes.getItem(0).setAttribute("width", width + "px"); iframeNodes.getItem(0).setAttribute("height", iFrameHeight + "px"); int streamHeight = iFrameHeight - 87; streamHeight = streamHeight > 0 ? streamHeight : 0; setHeight(streamHeight); } }
@Override protected boolean resetFocusOnCell() { boolean focused = false; if (hasFilterHeaders() && filterFocusedCellColumn > -1 && filterFocusedCellRow > -1) { TableSectionElement thead = getTableHeadElement(); NodeList<TableRowElement> rows = thead.getRows(); if (filterFocusedCellRow < rows.getLength()) { TableRowElement row = rows.getItem(filterFocusedCellRow); NodeList<TableCellElement> cells = row.getCells(); if (filterFocusedCellColumn < cells.getLength()) { TableCellElement cell = cells.getItem(filterFocusedCellColumn); if (getHeaderBuilder().isHeader(cell)) { Header<?> header = getHeaderBuilder().getHeader(cell); Context context = new Context(0, 0, header.getKey()); focused = resetFocusOnFilterCellImpl(context, header, cell); } } } } if (!focused) { focused = super.resetFocusOnCell(); } return focused; }
/** * Removes all link tags in the head if not initialized. */ private void removeCssLinks() { if (this.isInit) { return; } this.isInit = true; // Remove all existing link element NodeList<Element> links = this.getHead().getElementsByTagName(LinkElement.TAG); int size = links.getLength(); for (int i = 0; i < size; i++) { LinkElement elem = LinkElement.as(links.getItem(0)); if ("stylesheet".equals(elem.getRel())) { elem.removeFromParent(); } } }
@Provides @Singleton public XSRFToken provideXSRFToken() { XSRFToken xsrfToken = new XSRFToken(); // get the CSRF token from the HTML document and initialize the // XSRFToken NodeList<MetaElement> metaTags = Document.get().getElementsByTagName(MetaElement.TAG).cast(); for (int i = 0; i < metaTags.getLength(); i++) { if (metaTags.getItem(i).getName().equals("X-CSRF-TOKEN")) { xsrfToken.setToken(metaTags.getItem(i).getContent()); break; } } return xsrfToken; }
/** * This is the entry point method. */ public void onModuleLoad() { if(Navigator.getUserAgent().contains("MSIE")) { NodeList<Element> divs = RootPanel.getBodyElement().getElementsByTagName("div"); for(int j = 0; j < divs.getLength(); j++) { Element element = divs.getItem(j); if(element.hasAttribute("name") && element.getAttribute("name").equals("opennms-interfacelist")) { createView(element); } } }else { NodeList<Element> nodes = RootPanel.getBodyElement().getElementsByTagName("opennms:interfacelist"); if(nodes.getLength() > 0) { for(int i = 0; i < nodes.getLength(); i++) { Element elem = nodes.getItem(i); createView(elem); } } } }
/** * This is the entry point method. */ public void onModuleLoad() { if(Window.Navigator.getUserAgent().contains("MSIE")) { NodeList<Element> divs = RootPanel.getBodyElement().getElementsByTagName("div"); for(int j = 0; j < divs.getLength(); j++) { Element element = divs.getItem(j); if(element.hasAttribute("name") && element.getAttribute("name").contains("opennms-nodeSuggestionCombobox")) { createView(element); } } }else { NodeList<Element> nodes = RootPanel.getBodyElement().getElementsByTagName("opennms:nodeSuggestionCombobox"); if(nodes.getLength() > 0) { for(int i = 0; i < nodes.getLength(); i++) { createView(nodes.getItem(i)); } } } }
@Override public void onModuleLoad() { if(Navigator.getUserAgent().contains("MSIE")) { NodeList<Element> divs = RootPanel.getBodyElement().getElementsByTagName("div"); for(int j = 0; j < divs.getLength(); j++) { Element element = divs.getItem(j); if(element.hasAttribute("name") && element.getAttribute("name").equals("opennms-snmpSelectList")) { createView(element); } } }else { NodeList<Element> nodes = RootPanel.getBodyElement().getElementsByTagName("opennms:snmpSelectList"); if(nodes.getLength() > 0) { for(int i = 0; i < nodes.getLength(); i++) { Element elem = nodes.getItem(i); createView(elem); } } } }
/** {@inheritDoc} */ public void showLocationDetails(final String name, final String htmlTitle, final String htmlContent) { final MQAPoi point = getMarker(name); if (point != null) { point.setInfoTitleHTML(htmlTitle); point.setInfoContentHTML(htmlContent); point.showInfoWindow(); final NodeList<Element> elements = Document.get().getElementsByTagName("div"); for (int i = 0; i < elements.getLength(); i++) { final Element e = elements.getItem(i); if (e.getClassName().equals("mqpoicontenttext")) { final Style s = e.getStyle(); s.setOverflow(Overflow.HIDDEN); break; } } } }
/** * This is the entry point method. */ public void onModuleLoad() { if(Window.Navigator.getUserAgent().contains("MSIE")) { NodeList<Element> divs = RootPanel.getBodyElement().getElementsByTagName("div"); for(int j = 0; j < divs.getLength(); j++) { Element element = divs.getItem(j); if(element.hasAttribute("name") && element.getAttribute("name").contains("opennms-kscReportCombobox")) { createView(element); } } }else { NodeList<Element> nodes = RootPanel.getBodyElement().getElementsByTagName("opennms:kscReportCombobox"); if(nodes.getLength() > 0) { for(int i = 0; i < nodes.getLength(); i++) { createView(nodes.getItem(i)); } } } }
private Element getLastResolutionElement() { DivElement div = getResolutionDiv(); if (div == null) { return null; } NodeList<Node> nodeList = div.getChildNodes(); if (nodeList == null) { return null; } int blockCount = nodeList.getLength(); if (blockCount < 1) { return null; } if (containsResBlockSpacer()) { int index = blockCount - 2; if (blockCount > 1 && index >= 0) { return Element.as(getResolutionDiv().getChild(index)); } return null; } return Element.as(getResolutionDiv().getLastChild()); }
/** * Remove a single row and all its widgets from the table * * @param rowIndex which row to add to (0 based, excluding thead) */ public void deleteRow(int rowIndex){ Element rowElem=rowElements.get(rowIndex); NodeList<Node> tds= rowElem.getChildNodes(); for(int i=0;i<tds.getLength();i++){ Element td=tds.getItem(i).cast(); for(Widget widget:wrapperMap.keySet()){ if(wrapperMap.get(widget).equals(td)){ remove(widget); break; } } } this.tbody.removeChild(rowElem); rowElements.remove(rowIndex); }
/** * Ensure that a td cell exists for the row at the given column * index. * @param tr the row * @param colIndex the column index (0 based) * @return the new or already existing td */ private Element ensureCell(Element tr, int colIndex) { NodeList<Node> tds = tr.getChildNodes(); int numTDs = tds.getLength(); if (colIndex < numTDs) { return tds.getItem(colIndex).cast(); } Element td = null; for (int c = numTDs; c <= colIndex; c++) { td = Document.get().createTDElement().cast(); if (this.columnClasses.containsKey(colIndex)) { td.setAttribute("class", this.columnClasses.get(colIndex)); //$NON-NLS-1$ } DOM.appendChild(tr, td); } return td; }