-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from AlanFCMV/iframe-detected
Add IframeNotHandled rule
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace CidiLabs\PhpAlly\Rule; | ||
|
||
class IframeNotHandled extends BaseRule | ||
{ | ||
private $regex = array( | ||
'@youtube\.com/embed/([^"\&\? ]+)@i', | ||
'@youtube\.com/v/([^"\&\? ]+)@i', | ||
'@youtube\.com/watch\?v=([^"\&\? ]+)@i', | ||
'@youtube\.com/\?v=([^"\&\? ]+)@i', | ||
'@youtu\.be/([^"\&\? ]+)@i', | ||
'@youtu\.be/v/([^"\&\? ]+)@i', | ||
'@youtu\.be/watch\?v=([^"\&\? ]+)@i', | ||
'@youtu\.be/\?v=([^"\&\? ]+)@i', | ||
'@youtube-nocookie\.com/embed/([^"\&\? ]+)@i', | ||
'@vimeo\.com/[^0-9]*([0-9]{7,9})@i', | ||
'/(kaltura)/', | ||
'/(dailymotion)/', | ||
); | ||
|
||
public function id() | ||
{ | ||
return self::class; | ||
} | ||
|
||
public function check() | ||
{ | ||
foreach ($this->getAllElements('iframe') as $iframe) { | ||
$matches = false; | ||
|
||
foreach ($this->regex as $search) { | ||
if (preg_match($search, trim($iframe->getAttribute('src')))) { | ||
$matches = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!$matches) { | ||
$this->setIssue($iframe); | ||
} | ||
} | ||
|
||
return count($this->issues); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
use CidiLabs\PhpAlly\Rule\IframeNotHandled; | ||
|
||
class IframeNotHandledTest extends PhpAllyTestCase { | ||
public function testCheckEmpty() | ||
{ | ||
$html = '<div></div>'; | ||
$dom = new \DOMDocument('1.0', 'utf-8'); | ||
$dom->loadHTML($html); | ||
$rule = new IframeNotHandled($dom); | ||
|
||
$this->assertEquals(0, $rule->check(), 'IframeNotHandled should have no issues.'); | ||
} | ||
|
||
public function testCheckTrueIframe() | ||
{ | ||
$html = '<div><iframe src="https://www.thisisatest.com/"></iframe></div>'; | ||
$dom = new \DOMDocument('1.0', 'utf-8'); | ||
$dom->loadHTML($html); | ||
$rule = new IframeNotHandled($dom); | ||
|
||
$this->assertEquals(1, $rule->check(), 'IframeNotHandled should have one issue.'); | ||
} | ||
|
||
public function testCheckFalseIframe() | ||
{ | ||
$html = '<div> | ||
<iframe src="https://www.dailymotion.com/us"></iframe> | ||
<iframe src="https://www.youtube.com/watch?v=1xZxxVlu7BM"></iframe> | ||
<iframe src="https://vimeo.com/205755088"></iframe> | ||
<iframe src="https://cdnapisec.kaltura.com/p/4183983"></iframe> | ||
</div>'; | ||
$dom = new \DOMDocument('1.0', 'utf-8'); | ||
$dom->loadHTML($html); | ||
$rule = new IframeNotHandled($dom); | ||
|
||
$this->assertEquals(0, $rule->check(), 'IframeNotHandled should have no issues.'); | ||
} | ||
} |