Skip to content
Browse files

Improve efficiency of :first-child and :last-child

  • Loading branch information...
1 parent 9b636a4 commit 6a94745bb0e300563cd40fb0b393f1e58eef65c9 @pbrant pbrant committed Nov 16, 2010
Showing with 11 additions and 16 deletions.
  1. +11 −16 src/java/org/xhtmlrenderer/css/extend/lib/DOMTreeResolver.java
View
27 src/java/org/xhtmlrenderer/css/extend/lib/DOMTreeResolver.java
@@ -21,7 +21,6 @@
import org.w3c.dom.Element;
import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
import org.xhtmlrenderer.css.extend.TreeResolver;
/**
@@ -55,37 +54,33 @@ public String getElementName(Object element) {
public boolean isFirstChildElement(Object element) {
org.w3c.dom.Node parent = ((org.w3c.dom.Element) element).getParentNode();
- NodeList nl = parent.getChildNodes();
-
- int i = 0;
- while (i < nl.getLength() && nl.item(i).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
- i++;
+ Node currentChild = parent.getFirstChild();
+ while (currentChild != null && currentChild.getNodeType() != Node.ELEMENT_NODE) {
+ currentChild = currentChild.getNextSibling();
}
- return (nl.item(i) == element);
+ return currentChild == element;
}
-
+
public boolean isLastChildElement(Object element) {
org.w3c.dom.Node parent = ((org.w3c.dom.Element) element).getParentNode();
- NodeList nl = parent.getChildNodes();
-
- int i = nl.getLength()-1;
- while (i > 0 && nl.item(i).getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) {
- i--;
+ Node currentChild = parent.getLastChild();
+ while (currentChild != null && currentChild.getNodeType() != Node.ELEMENT_NODE) {
+ currentChild = currentChild.getPreviousSibling();
}
- return (nl.item(i) == element);
+ return currentChild == element;
}
public boolean matchesElement(Object element, String namespaceURI, String name) {
Element e = (Element)element;
String localName = e.getLocalName();
String eName;
-
+
if (localName == null) {
eName = e.getNodeName();
} else {
eName = localName;
}
-
+
if (namespaceURI != null) {
return name.equals(localName) && namespaceURI.equals(e.getNamespaceURI());
} else if (namespaceURI == TreeResolver.NO_NAMESPACE) {

0 comments on commit 6a94745

Please sign in to comment.
Something went wrong with that request. Please try again.