8000 merged branch matthijsvandenbos/matthijsvandenbos/basetag-href-fix (P… · symfony/symfony@0c09b93 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0c09b93

Browse files
committed
merged branch matthijsvandenbos/matthijsvandenbos/basetag-href-fix (PR #7178)
This PR was submitted for the master branch but it was merged into the 2.1 branch instead (closes #7178). Commits ------- c41f640 [DomCrawler] Fixed handling absent href attribute in base tag Discussion ---------- [DomCrawler] Fixed handling absent href attribute in base tag | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | # Description The HTML5 spec states that the href attribute is optional for the base tag. The current code causes an exception on conforming HTML. Fixed the DomCrawler::addHtmlContent() method to support this. Added Unit Test to check for this situation. # Explanantion Currently, if the base tag doesn't have an href attribute, the uri for the DomCrawler gets set to an empty string. This is incorrect behaviour, especially because it breaks DomCrawler::links(). The Symfony\Component\DomCrawler\Link objects it creates, expect a non-empty string in their constructor arguments and throw an InvalidArgumentException. # References 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
2 parents ae5b94f + b1ea8e5 commit 0c09b93

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