1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Dev\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Dev\CSSContentParser; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
|
8
|
|
|
class CSSContentParserTest extends SapphireTest |
9
|
|
|
{ |
10
|
|
|
public function testSelector2xpath() |
11
|
|
|
{ |
12
|
|
|
$parser = new CSSContentParser("<html><head><title>test</title></head><body><p>test</p></body></html>"); |
13
|
|
|
|
14
|
|
|
$this->assertEquals("//div[@id='UserProfile']//label", $parser->selector2xpath("div#UserProfile label")); |
15
|
|
|
$this->assertEquals("//div", $parser->selector2xpath("div")); |
16
|
|
|
$this->assertEquals("//div[contains(@class,'test')]", $parser->selector2xpath("div.test")); |
17
|
|
|
$this->assertEquals( |
18
|
|
|
"//*[@id='UserProfile']//div[contains(@class,'test')]//*[contains(@class,'other')]//div[@id='Item']", |
19
|
|
|
$parser->selector2xpath("#UserProfile div.test .other div#Item") |
20
|
|
|
); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function testGetBySelector() |
24
|
|
|
{ |
25
|
|
|
$parser = new CSSContentParser( |
26
|
|
|
<<<HTML |
27
|
|
|
<html> |
28
|
|
|
<head> |
29
|
|
|
<title>test</title> |
30
|
|
|
</head> |
31
|
|
|
<body> |
32
|
|
|
<div id="A" class="one two three"> |
33
|
|
|
<p class="other">result</p> |
34
|
|
|
</div> |
35
|
|
|
<p>test</p> |
36
|
|
|
</body> |
37
|
|
|
</html> |
38
|
|
|
HTML |
39
|
|
|
); |
40
|
|
|
|
41
|
|
|
$result = $parser->getBySelector('div.one'); |
42
|
|
|
$this->assertEquals("A", $result[0]['id'] . ''); |
43
|
|
|
$result = $parser->getBySelector('div.two'); |
44
|
|
|
$this->assertEquals("A", $result[0]['id'] . ''); |
45
|
|
|
$result = $parser->getBySelector('div.three'); |
46
|
|
|
$this->assertEquals("A", $result[0]['id'] . ''); |
47
|
|
|
|
48
|
|
|
$result = $parser->getBySelector('div#A p.other'); |
49
|
|
|
$this->assertEquals("result", $result[0] . ''); |
50
|
|
|
$result = $parser->getBySelector('#A .other'); |
51
|
|
|
$this->assertEquals("result", $result[0] . ''); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testXmlEntitiesDisabled() |
55
|
|
|
{ |
56
|
|
|
// CSSContentParser uses simplexml to parse html |
57
|
|
|
// Ensure XML entities are not substituted in to prevent XXE attacks |
58
|
|
|
$xml = '<!DOCTYPE html [<!ENTITY myentity "World">]><html><div>Hello &myentity;</div></html>'; |
59
|
|
|
$parser = new CSSContentParser($xml); |
60
|
|
|
$div = $parser->getBySelector('div')[0]->asXML(); |
61
|
|
|
$this->assertEquals('<div>Hello &myentity;</div>', $div); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|