浏览代码

[DomCrawler] fixed various bug with URIs

Fabien Potencier 14 年之前
父节点
当前提交
9ceec87eb8

+ 5 - 1
src/Symfony/Component/DomCrawler/Form.php

@@ -190,10 +190,14 @@ class Form implements \ArrayAccess
         $uri = $this->node->getAttribute('action');
         $urlHaveScheme = 'http' === substr($uri, 0, 4);
 
-        if (!$uri || '#' === $uri) {
+        if (!$uri) {
             $uri = $this->path;
         }
 
+        if ('#' === $uri[0]) {
+            $uri = $this->path.$uri;
+        }
+
         if (!in_array($this->getMethod(), array('post', 'put', 'delete')) && $queryString = http_build_query($this->getValues(), null, '&')) {
             $sep = false === strpos($uri, '?') ? '?' : '&';
             $uri .= $sep.$queryString;

+ 8 - 0
src/Symfony/Component/DomCrawler/Link.php

@@ -76,6 +76,14 @@ class Link
         $uri = $this->node->getAttribute('href');
         $urlHaveScheme = 'http' === substr($uri, 0, 4);
 
+        if (!$uri) {
+            $uri = $this->path;
+        }
+
+        if ('#' === $uri[0]) {
+            $uri = $this->path.$uri;
+        }
+
         $path = $this->path;
         if ('?' !== substr($uri, 0, 1) && '/' !== substr($path, -1)) {
             $path = substr($path, 0, strrpos($path, '/') + 1);

+ 11 - 4
tests/Symfony/Tests/Component/DomCrawler/FormTest.php

@@ -343,7 +343,7 @@ class FormTest extends \PHPUnit_Framework_TestCase
                 'chooses the path if the action attribute value is a sharp (#)',
                 '<form action="#" method="post"><input type="text" name="foo" value="foo" /><input type="submit" /></form>',
                 array(),
-                '/',
+                '/#',
             ),
         );
     }
@@ -389,20 +389,27 @@ class FormTest extends \PHPUnit_Framework_TestCase
     public function testBase()
     {
         $dom = new \DOMDocument();
-        $dom->loadHTML('<html><form method="post" action="foo.php"><input type="text" name="bar" value="bar" /><input type="submit" /></form></html>');
+        $dom->loadHTML('<form method="post" action="foo.php"><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
 
         $nodes = $dom->getElementsByTagName('input');
         $form = new Form($nodes->item($nodes->length - 1), null, 'http://www.bar.com/foobar/', '/', 'http://www.foo.com/');
         $this->assertEquals('http://www.foo.com/foo.php', $form->getUri());
     }
 
-    protected function createForm($form, $method = null, $host = null, $path = '/')
+    public function testUriWithAnchor()
+    {
+        $form = $this->createForm('<form action="#foo"><input type="submit" /></form>', null, 'http://example.com', '/id/123');
+
+        $this->assertEquals('http://example.com/id/123#foo', $form->getUri());
+    }
+
+    protected function createForm($form, $method = null, $host = null, $path = '/', $base = null)
     {
         $dom = new \DOMDocument();
         $dom->loadHTML('<html>'.$form.'</html>');
 
         $nodes = $dom->getElementsByTagName('input');
 
-        return new Form($nodes->item($nodes->length - 1), $method, $host, $path);
+        return new Form($nodes->item($nodes->length - 1), $method, $host, $path, $base);
     }
 }