Skip to content

Commit a10a618

Browse files
authored
Merge pull request #23 from AlanFCMV/iframe-detected
Add IframeNotHandled rule
2 parents 03111f6 + b66170a commit a10a618

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

src/Rule/IframeNotHandled.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace CidiLabs\PhpAlly\Rule;
4+
5+
class IframeNotHandled extends BaseRule
6+
{
7+
private $regex = array(
8+
'@youtube\.com/embed/([^"\&\? ]+)@i',
9+
'@youtube\.com/v/([^"\&\? ]+)@i',
10+
'@youtube\.com/watch\?v=([^"\&\? ]+)@i',
11+
'@youtube\.com/\?v=([^"\&\? ]+)@i',
12+
'@youtu\.be/([^"\&\? ]+)@i',
13+
'@youtu\.be/v/([^"\&\? ]+)@i',
14+
'@youtu\.be/watch\?v=([^"\&\? ]+)@i',
15+
'@youtu\.be/\?v=([^"\&\? ]+)@i',
16+
'@youtube-nocookie\.com/embed/([^"\&\? ]+)@i',
17+
'@vimeo\.com/[^0-9]*([0-9]{7,9})@i',
18+
'/(kaltura)/',
19+
'/(dailymotion)/',
20+
);
21+
22+
public function id()
23+
{
24+
return self::class;
25+
}
26+
27+
public function check()
28+
{
29+
foreach ($this->getAllElements('iframe') as $iframe) {
30+
$matches = false;
31+
32+
foreach ($this->regex as $search) {
33+
if (preg_match($search, trim($iframe->getAttribute('src')))) {
34+
$matches = true;
35+
break;
36+
}
37+
}
38+
39+
if (!$matches) {
40+
$this->setIssue($iframe);
41+
}
42+
}
43+
44+
return count($this->issues);
45+
}
46+
}

src/rules.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"CidiLabs\\PhpAlly\\Rule\\FontIsNotUsed",
1414
"CidiLabs\\PhpAlly\\Rule\\HeadersHaveText",
1515
"CidiLabs\\PhpAlly\\Rule\\HeadingsInOrder",
16+
"CidiLabs\\PhpAlly\\Rule\\IframeNotHandled",
1617
"CidiLabs\\PhpAlly\\Rule\\ImageAltIsDifferent",
1718
"CidiLabs\\PhpAlly\\Rule\\ImageAltIsTooLong",
1819
"CidiLabs\\PhpAlly\\Rule\\ImageAltNotEmptyInAnchor",

tests/IframeNotHandledTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
use CidiLabs\PhpAlly\Rule\IframeNotHandled;
4+
5+
class IframeNotHandledTest extends PhpAllyTestCase {
6+
public function testCheckEmpty()
7+
{
8+
$html = '<div></div>';
9+
$dom = new \DOMDocument('1.0', 'utf-8');
10+
$dom->loadHTML($html);
11+
$rule = new IframeNotHandled($dom);
12+
13+
$this->assertEquals(0, $rule->check(), 'IframeNotHandled should have no issues.');
14+
}
15+
16+
public function testCheckTrueIframe()
17+
{
18+
$html = '<div><iframe src="https://www.thisisatest.com/"></iframe></div>';
19+
$dom = new \DOMDocument('1.0', 'utf-8');
20+
$dom->loadHTML($html);
21+
$rule = new IframeNotHandled($dom);
22+
23+
$this->assertEquals(1, $rule->check(), 'IframeNotHandled should have one issue.');
24+
}
25+
26+
public function testCheckFalseIframe()
27+
{
28+
$html = '<div>
29+
<iframe src="https://www.dailymotion.com/us"></iframe>
30+
<iframe src="https://www.youtube.com/watch?v=1xZxxVlu7BM"></iframe>
31+
<iframe src="https://vimeo.com/205755088"></iframe>
32+
<iframe src="https://cdnapisec.kaltura.com/p/4183983"></iframe>
33+
</div>';
34+
$dom = new \DOMDocument('1.0', 'utf-8');
35+
$dom->loadHTML($html);
36+
$rule = new IframeNotHandled($dom);
37+
38+
$this->assertEquals(0, $rule->check(), 'IframeNotHandled should have no issues.');
39+
}
40+
}

0 commit comments

Comments
 (0)