Skip to content

Commit

Permalink
Fixed handling absent href attribute in base tag
Browse files Browse the repository at this point in the history
The HTML5 spec states that the href attribute is optional for the
base tag. Fixed the DomCrawler::addHtmlContent() method to support this

See here and here:
http://www.w3.org/TR/html-markup/base.html#base.attrs.href
http://www.whatwg.org/specs/web-apps/current-work/multipage/semantics.html#the-base-element
  • Loading branch information
mvdbos authored and fabpot committed Feb 26, 2013
1 parent ae5b94f commit b1ea8e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Symfony/Component/DomCrawler/Crawler.php
Expand Up @@ -145,8 +145,9 @@ public function addHtmlContent($content, $charset = 'UTF-8')

$base = $this->filterXPath('descendant-or-self::base')->extract(array('href'));

if (count($base)) {
$this->uri = current($base);
$baseHref = current($base);
if (count($base) && !empty($baseHref)) {

This comment has been minimized.

Copy link
@staabm

staabm Mar 3, 2013

Contributor

Would't just !empty work already?

$this->uri = $baseHref;
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
Expand Up @@ -80,6 +80,18 @@ public function testAddHtmlContentCharset()
$this->assertEquals('Tiếng Việt', $crawler->filterXPath('//div')->text());
}

/**

This comment has been minimized.

Copy link
@staabm

staabm Mar 3, 2013

Contributor

The name of the test does not reflect the actual problem, because as stated in the commit message, the base tag is valid without the href

* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
*/
public function testAddHtmlContentInvalidBaseTag()
{
$crawler = new Crawler(null, 'http://symfony.com');

$crawler->addHtmlContent('<html><head><base target="_top"></head><a href="/contact"></a></html>', 'UTF-8');

$this->assertEquals('http://symfony.com/contact', current($crawler->filterXPath('//a')->links())->getUri(), '->addHtmlContent() correctly handles a non-existent base tag href attribute');
}

/**
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
*/
Expand Down

0 comments on commit b1ea8e5

Please sign in to comment.