8000 Crawler default namespace fix · sroze/symfony@cfff054 · GitHub
[go: up one dir, main page]

Skip to content

Commit cfff054

Browse files
ChrisCfabpot
authored andcommitted
Crawler default namespace fix
1 parent aff0594 commit cfff054

File tree

3 files changed

+29
-1
lines changed
Expand file tree

3 files changed

+29
-1
lines changed

src/Symfony/Component/DomCrawler/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ CHANGELOG
33

44
2.4.0
55
-----
6-
6+
7+
* `Crawler::addXmlContent()` removes the default document namespace again if it's an only namespace.
78
* added support for automatic discovery and explicit registration of document
89
namespaces for `Crawler::filterXPath()` and `Crawler::filter()`
910
* improved content type guessing in `Crawler::addContent()`

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ public function addHtmlContent($content, $charset = 'UTF-8')
200200
*/
201201
public function addXmlContent($content, $charset = 'UTF-8')
202202
{
203+
// remove the default namespace if it's the only namespace to make XPath expressions simpler
204+
if (!preg_match('/xmlns:/', $content)) {
205+
$content = str_replace('xmlns', 'ns', $content);
206+
}
207+
203208
$current = libxml_use_internal_errors(true);
204209
$disableEntities = libxml_disable_entity_loader(true);
205210

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,28 @@ public function testFilterWithMultipleNamespaces()
469469
$this->assertSame('widescreen', $crawler->text());
470470
}
471471

472+
public function testFilterWithDefaultNamespaceOnly()
473+
{
474+
$crawler = new Crawler('<?xml version="1.0" encoding="UTF-8"?>
475+
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
476+
<url>
477+
<loc>http://localhost/foo</loc>
478+
<changefreq>weekly</changefreq>
479+
<priority>0.5</priority>
480+
<lastmod>2012-11-16</lastmod>
481+
</url>
482+
<url>
483+
<loc>http://localhost/bar</loc>
484+
<changefreq>weekly</changefreq>
485+
<priority>0.5</priority>
486+
<lastmod>2012-11-16</lastmod>
487+
</url>
488+
</urlset>
489+
');
490+
491+
$this->assertEquals(2, $crawler->filter('url')->count());
492+
}
493+
472494
public function testSelectLink()
473495
{
474496
$crawler = $this->createTestCrawler();

0 commit comments

Comments
 (0)
0