1 package org.joshy.html.css; 2 3 import org.joshy.u; 4 import java.io.*; 5 import java.util.List ; 6 import org.w3c.dom.*; 7 import org.w3c.dom.css.*; 8 import org.w3c.css.sac.*; 9 import com.steadystate.css.*; 10 import com.steadystate.css.parser.*; 11 12 13 public class RuleFinder { 14 List styles; 15 public RuleFinder(List styles) { 16 this.styles = styles; 17 } 18 19 20 21 public CSSStyleDeclaration findRule(Node elem, String property, boolean inherit) { 22 for(int s=styles.size()-1; s>=0; s--) { 24 JStyle style = (JStyle)styles.get(s); 25 for(int i=0; i<style.selector_list.getLength(); i++) { 27 Selector sel = style.selector_list.item(i); 28 if(matchSelector(sel,elem)) { 30 CSSStyleDeclaration dec = style.declaration; 31 if(dec != null && dec.getPropertyValue(property) != null && 33 !dec.getPropertyValue(property).equals("")) { 34 return dec; 35 } 36 } 37 } 38 } 39 40 if(inherit) { 42 if(elem.getParentNode() != null) { 43 return findRule(elem.getParentNode(),property,inherit); 44 } 45 } 46 47 return null; 49 } 50 51 52 53 public boolean match(String selector, Node node) { 54 try { 55 CSSOMParser parser = new CSSOMParser(); 56 SelectorList list = parser.parseSelectors(new InputSource(new StringReader(selector))); 57 for(int i=0; i<list.getLength(); i++) { 58 Selector sel = list.item(i); 59 if(matchSelector(sel,node)) { 60 return true; 61 } 62 } 63 } catch (Exception ex) { 64 u.p(ex); 65 } 66 return false; 67 } 68 69 70 71 private boolean matchSelector(Selector selector, Node node) { 72 if(selector.getSelectorType() == selector.SAC_ELEMENT_NODE_SELECTOR) { 73 return matchElement((ElementSelector)selector,node); 74 } 75 if(selector.getSelectorType() == selector.SAC_CONDITIONAL_SELECTOR) { 76 ConditionalSelector cond_sel = (ConditionalSelector)selector; 77 return matchConditional(cond_sel,node); 78 } 79 if(selector.getSelectorType() == selector.SAC_DESCENDANT_SELECTOR) { 80 DescendantSelector desc_sel = (DescendantSelector)selector; 81 return matchDescendant(desc_sel,node); 82 } 83 u.p("unrecognized selector type: " + selector + " node = " + node.getNodeName()); 84 return false; 85 } 86 87 88 89 private boolean matchElement(ElementSelector selector, Node node) { 90 if(selector.getLocalName() == null) { 92 return true; 93 } 94 if(selector.getLocalName().equals(node.getNodeName())) { 95 return true; 96 } 97 return false; 98 } 99 100 101 private boolean matchConditional(ConditionalSelector selector, Node node) { 102 Condition cond = selector.getCondition(); 103 if(cond.getConditionType() == cond.SAC_CLASS_CONDITION) { 104 return matchClassConditional((AttributeCondition)cond,selector.getSimpleSelector(),node); 105 } 106 if(cond.getConditionType() == cond.SAC_ID_CONDITION) { 107 return matchIDConditional((AttributeCondition)cond,selector.getSimpleSelector(),node); 108 } 109 return false; 110 } 111 112 113 private boolean matchDescendant(DescendantSelector selector, Node node) { 114 SimpleSelector child = selector.getSimpleSelector(); 115 Selector parent = selector.getAncestorSelector(); 116 if(matchSelector(child,node)) { 117 Node current_node = node; 119 while(true) { 120 Node parent_node = current_node.getParentNode(); 121 if(parent_node == null) { 122 return false; 123 } 124 if(matchSelector(parent,parent_node)) { 125 return true; 126 } 127 current_node = parent_node; 128 } 129 } 130 return false; 131 } 132 133 134 private boolean matchClassConditional(AttributeCondition cond, SimpleSelector sel, Node node) { 135 if(!(node.getNodeType() == node.ELEMENT_NODE)) { 137 return false; 138 } 139 Element elem = (Element)node; 140 if(!elem.hasAttribute("class")) { 142 return false; 143 } 144 if(!elem.getAttribute("class").equals(cond.getValue())) { 146 return false; 147 } 148 149 ElementSelector es = (ElementSelector)sel; 152 if(es.getLocalName() == null) { 153 return true; 154 } 155 if(!((ElementSelector)sel).getLocalName().equals(node.getNodeName())) { 156 return false; 157 } 158 return true; 159 } 160 161 private boolean matchIDConditional(AttributeCondition cond, SimpleSelector sel, Node node) { 162 if(!(node.getNodeType() == node.ELEMENT_NODE)) { return false; } 164 Element elem = (Element)node; 165 if(!elem.hasAttribute("id")) { return false; } 167 if(!elem.getAttribute("id").equals(cond.getValue())) { return false; } 169 170 ElementSelector es = (ElementSelector)sel; 172 if(es.getLocalName() == null) { 175 return true; 176 } 177 if(!((ElementSelector)sel).getLocalName().equals(node.getNodeName())) { return false; } 178 return true; 179 } 180 } 181 | Popular Tags |