8000 Fixed handling absent href attribute in base tag · combro2k/symfony@b1ea8e5 · GitHub
[go: up one dir, main page]

Skip to content

Commit b1ea8e5

Browse files
mvdbosfabpot
authored andcommitted
Fixed handling absent href attribute in base tag
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
1 parent ae5b94f commit b1ea8e5

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,9 @@ public function addHtmlContent($content, $charset = 'UTF-8')
145145

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

148-
if (count($base)) {
149-
$this->uri = current($base);
148+
$baseHref = current($base);
149+
if (count($base) && !empty($baseHref)) {
150+
$this->uri = $baseHref;
150151
}
151152
}
152153

src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,18 @@ public function testAddHtmlContentCharset()
8080
$this->assertEquals('Tiếng Việt', $crawler->filterXPath('//div')->text());
8181
}
8282

83+
/**
84+
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
85+
*/
86+
public function testAddHtmlContentInvalidBaseTag()
87+
{
88+
$crawler = new Crawler(null, 'http://symfony.com');
89+
90+
$crawler->addHtmlContent('<html><head><base target="_top"></head><a href="/contact"></a></html>', 'UTF-8');
91+
92+
$this->assertEquals('http://symfony.com/contact', current($crawler->filterXPath('//a')->links())->getUri(), '->addHtmlContent() correctly handles a non-existent base tag href attribute');
93+
}
94+
8395
/**
8496
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
8597
*/

0 commit comments

Comments
 (0)
0