Skip to content
This repository
Browse code

CSS selectors solving

  • Loading branch information...
commit 9b08eafe6a1ff2bf903c05a858a912c876c422c9 1 parent c12e625
authored November 05, 2011
17  library/NTM.php
@@ -164,7 +164,13 @@ public function getHTML()
164 164
     
165 165
     public function getElementsByCSS($css)
166 166
     {
167  
-        return $this->selectElements($this->CSStoAST($css));
  167
+        $selectors = $this->CSStoAST($css);
  168
+        $ret = array();
  169
+        foreach($selectors as $selector)
  170
+        {
  171
+            $ret = array_merge($ret,$this->selectElements($selector));
  172
+        }
  173
+        return $ret;
168 174
     }
169 175
     
170 176
     /**
@@ -175,11 +181,11 @@ public function getElementsByCSS($css)
175 181
     public function CSStoAST($css)
176 182
     {
177 183
         $tempfile = fopen('php://temp/','rw');
178  
-        fwrite($tempfile, $code);
  184
+        fwrite($tempfile, $css);
179 185
         fseek($tempfile, 0);
180 186
         
181  
-        $lexer = new CSS3Lexer($tempfile);
182  
-        $parser = new CSS3Parser();
  187
+        $lexer = new NTM\CSS3Lexer($tempfile);
  188
+        $parser = new NTM\CSS3Parser();
183 189
         
184 190
         //$parser->trace(STDOUT, '#');
185 191
 
@@ -197,6 +203,9 @@ public function selectElements(NTM\CSS\SimpleSelector $selector)
197 203
     {
198 204
         $elements = $this->xpath->query($selector->XPath());
199 205
         
  206
+        var_dump($selector->XPath());
  207
+        var_dump($elements);
  208
+        
200 209
         $result = array();
201 210
         
202 211
         foreach($elements as $element)
2  library/NTM/CSS/AdjacentSibling.php
@@ -11,7 +11,7 @@ class AdjacentSibling extends Combinator
11 11
 {
12 12
     public function check(\DOMElement $el)
13 13
     {
14  
-        
  14
+        return $this->selector->check($el->previouseSibling);
15 15
     }
16 16
 }
17 17
 
2  library/NTM/CSS/Child.php
@@ -12,7 +12,7 @@ class Child extends Combinator
12 12
 {
13 13
     public function check(\DOMElement $el)
14 14
     {
15  
-    
  15
+        return $this->selector->check($el->parentNode);
16 16
     }
17 17
 }
18 18
 
7  library/NTM/CSS/Descendant.php
@@ -10,7 +10,12 @@ class Descendant extends Combinator
10 10
 {
11 11
     public function check(\DOMElement $el)
12 12
     {
13  
-        
  13
+        while($p=$el->parentNode)
  14
+        {
  15
+            if($this->selector->check($p))
  16
+                return true;
  17
+        }
  18
+        return false;
14 19
     }
15 20
 }
16 21
 
7  library/NTM/CSS/GeneralSibling.php
@@ -11,7 +11,12 @@ class GeneralSibling extends Combinator
11 11
 {
12 12
     public function check(\DOMElement $el)
13 13
     {
14  
-        return '/*/'.$this->selector->XPath();
  14
+        while($p=$el->previouseSibling)
  15
+        {
  16
+            if($this->selector->check($p))
  17
+                return true;
  18
+        }
  19
+        return false;
15 20
     }
16 21
 }
17 22
 
4  library/NTM/CSS/SimpleSelector.php
@@ -31,10 +31,10 @@ static public function instance()
31 31
     public function XPath()
32 32
     {
33 33
         //
34  
-        //  */div[@id]
  34
+        //  //div[@id]
35 35
         //
36 36
         
37  
-        $xpath = '*/';
  37
+        $xpath = '//';
38 38
         
39 39
         if($this->namespace)
40 40
         {

0 notes on commit 9b08eaf

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