OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package com.dom_distiller.client; | 5 package com.dom_distiller.client; |
6 | 6 |
7 import com.google.gwt.core.client.JsArray; | 7 import com.google.gwt.core.client.JsArray; |
8 | 8 |
9 import com.google.gwt.dom.client.Element; | 9 import com.google.gwt.dom.client.Element; |
10 import com.google.gwt.dom.client.Node; | 10 import com.google.gwt.dom.client.Node; |
11 import com.google.gwt.dom.client.NodeList; | 11 import com.google.gwt.dom.client.NodeList; |
| 12 import com.google.gwt.dom.client.Style; |
12 | 13 |
13 public class DomUtil { | 14 public class DomUtil { |
14 /** | 15 /** |
15 * GWT does not provide a way to get a list of all attributes that have been
explicitly set on a | 16 * GWT does not provide a way to get a list of all attributes that have been
explicitly set on a |
16 * DOM element (only a way to query the value of a particular attribute). In
javascript, this | 17 * DOM element (only a way to query the value of a particular attribute). In
javascript, this |
17 * list is accessible as elem.attributes. | 18 * list is accessible as elem.attributes. |
18 * | 19 * |
19 * @Return The element's attribute list from javascript. | 20 * @Return The element's attribute list from javascript. |
20 */ | 21 */ |
21 public static native JsArray<Node> getAttributes(Element elem) /*-{ | 22 public static native JsArray<Node> getAttributes(Element elem) /*-{ |
(...skipping 17 matching lines...) Expand all Loading... |
39 | 40 |
40 return @com.dom_distiller.client.DomUtil::javaGetFirstElementWithClassNa
me(Lcom/google/gwt/dom/client/Element;Ljava/lang/String;)(root, className); | 41 return @com.dom_distiller.client.DomUtil::javaGetFirstElementWithClassNa
me(Lcom/google/gwt/dom/client/Element;Ljava/lang/String;)(root, className); |
41 }-*/; | 42 }-*/; |
42 | 43 |
43 // Returns the first element with |className| in the tree rooted at |root|,
null if none is | 44 // Returns the first element with |className| in the tree rooted at |root|,
null if none is |
44 // found. |className| is expected to be in lower case. | 45 // found. |className| is expected to be in lower case. |
45 public static Element javaGetFirstElementWithClassName(Element root, String
className) { | 46 public static Element javaGetFirstElementWithClassName(Element root, String
className) { |
46 NodeList<Element> allElems = root.getElementsByTagName("*"); | 47 NodeList<Element> allElems = root.getElementsByTagName("*"); |
47 for (int i = 0; i < allElems.getLength(); i++) { | 48 for (int i = 0; i < allElems.getLength(); i++) { |
48 Element elem = allElems.getItem(i); | 49 Element elem = allElems.getItem(i); |
49 String classAttr = elem.getClassName().toLowerCase(); | 50 if (hasClassName(elem, className)) return elem; |
50 // Make sure |className| is not the substring of another name in |cl
assAttr|, so check | |
51 // for whitespaces before and after. | |
52 if ((" " + classAttr + " ").contains(" " + className + " ")) return
elem; | |
53 } | 51 } |
54 return null; | 52 return null; |
55 } | 53 } |
56 | 54 |
| 55 public static boolean hasClassName(Element elem, String className) { |
| 56 String classAttr = elem.getClassName().toLowerCase(); |
| 57 // Make sure |className| is not the substring of another name in |classA
ttr|, so check |
| 58 // for whitespaces before and after. |
| 59 return (" " + classAttr + " ").contains(" " + className + " "); |
| 60 } |
| 61 |
| 62 /** |
| 63 * @Return The CSS style of an element after applying the active stylesheet
s and resolving any |
| 64 * basic computation the style's value(s) may contain. |
| 65 * @param el - DOM element |
| 66 */ |
| 67 public static native Style getComputedStyle(Element el) /*-{ |
| 68 return getComputedStyle(el, null); |
| 69 }-*/; |
57 } | 70 } |
OLD | NEW |