2008-05-15 09:15:33 +02:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Dev\Tests;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\CSSContentParser;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-10-14 03:30:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class CSSContentParserTest extends SapphireTest
|
|
|
|
{
|
|
|
|
public function testSelector2xpath()
|
|
|
|
{
|
|
|
|
$parser = new CSSContentParser("<html><head><title>test</title></head><body><p>test</p></body></html>");
|
2008-05-15 09:15:33 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals("//div[@id='UserProfile']//label", $parser->selector2xpath("div#UserProfile label"));
|
|
|
|
$this->assertEquals("//div", $parser->selector2xpath("div"));
|
|
|
|
$this->assertEquals("//div[contains(@class,'test')]", $parser->selector2xpath("div.test"));
|
|
|
|
$this->assertEquals(
|
|
|
|
"//*[@id='UserProfile']//div[contains(@class,'test')]//*[contains(@class,'other')]//div[@id='Item']",
|
|
|
|
$parser->selector2xpath("#UserProfile div.test .other div#Item")
|
|
|
|
);
|
|
|
|
}
|
2008-05-15 09:15:33 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testGetBySelector()
|
|
|
|
{
|
|
|
|
$parser = new CSSContentParser(
|
|
|
|
<<<HTML
|
2008-05-15 09:15:33 +02:00
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>test</title>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<div id="A" class="one two three">
|
|
|
|
<p class="other">result</p>
|
|
|
|
</div>
|
|
|
|
<p>test</p>
|
|
|
|
</body>
|
2008-10-17 01:39:02 +02:00
|
|
|
</html>
|
2008-05-15 09:15:33 +02:00
|
|
|
HTML
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2008-05-15 09:15:33 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $parser->getBySelector('div.one');
|
2018-01-16 19:39:30 +01:00
|
|
|
$this->assertEquals("A", $result[0]['id'] . '');
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $parser->getBySelector('div.two');
|
2018-01-16 19:39:30 +01:00
|
|
|
$this->assertEquals("A", $result[0]['id'] . '');
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $parser->getBySelector('div.three');
|
2018-01-16 19:39:30 +01:00
|
|
|
$this->assertEquals("A", $result[0]['id'] . '');
|
2008-05-15 09:15:33 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$result = $parser->getBySelector('div#A p.other');
|
|
|
|
$this->assertEquals("result", $result[0] . '');
|
|
|
|
$result = $parser->getBySelector('#A .other');
|
|
|
|
$this->assertEquals("result", $result[0] . '');
|
|
|
|
}
|
2021-02-11 01:05:10 +01:00
|
|
|
|
|
|
|
public function testXmlEntitiesDisabled()
|
|
|
|
{
|
|
|
|
// CSSContentParser uses simplexml to parse html
|
|
|
|
// Ensure XML entities are not substituted in to prevent XXE attacks
|
|
|
|
$xml = '<!DOCTYPE html [<!ENTITY myentity "World">]><html><div>Hello &myentity;</div></html>';
|
|
|
|
$parser = new CSSContentParser($xml);
|
|
|
|
$div = $parser->getBySelector('div')[0]->asXML();
|
|
|
|
$this->assertEquals('<div>Hello &myentity;</div>', $div);
|
|
|
|
}
|
2008-10-17 01:39:02 +02:00
|
|
|
}
|