Skip to content

Commit e3754a3

Browse files
authored
Merge pull request #41 from cidilabs/skipScripts
skipping html with script tags for issues that mark entire document a…
2 parents 7b73743 + 9965a8b commit e3754a3

File tree

4 files changed

+61
-34
lines changed

4 files changed

+61
-34
lines changed

src/Rule/ContentTooLong.php

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,30 @@ public function check()
2222
{
2323
$pageText = '';
2424
$wordCount = 0;
25-
foreach ($this->getAllElements(null, 'text') as $element) {
26-
$text = $element->nodeValue;
27-
28-
if($text != null){
29-
$pageText = $pageText . $text;
30-
}
31-
$this->totalTests++;
32-
}
33-
$wordCount = str_word_count($pageText);
34-
35-
if($wordCount > $this->maxWordCount) {
36-
/*
37-
Since this rule sets the document element as the issue
38-
we set this flag here so we can process it accordingly in UDOIT.
39-
*/
40-
$metadata = array('isDocumentElement' => true);
41-
$this->setIssue($this->dom->documentElement, null, json_encode($metadata));
42-
}
43-
44-
return count($this->issues);
25+
26+
// Ignore html with script tags
27+
if (count($this->getAllElements('script')) === 0) {
28+
foreach ($this->getAllElements(null, 'text') as $element) {
29+
$text = $element->nodeValue;
30+
31+
if($text != null){
32+
$pageText = $pageText . $text;
33+
}
34+
$this->totalTests++;
35+
}
36+
$wordCount = str_word_count($pageText);
37+
38+
if($wordCount > $this->maxWordCount) {
39+
/*
40+
Since this rule sets the document element as the issue
41+
we set this flag here so we can process it accordingly in UDOIT.
42+
*/
43+
$metadata = array('isDocumentElement' => true);
44+
$this->setIssue($this->dom->documentElement, null, json_encode($metadata));
45+
}
46+
47+
return count($this->issues);
48+
}
4549
}
4650

4751
public function getPreviewElement(DOMElement $a = null)

src/Rule/NoHeadings.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,25 @@ public function id()
2020

2121
public function check()
2222
{
23-
$document_string = $this->dom->textContent;
24-
25-
if (strlen($document_string) > $this->minDocLengthForHeaders){
26-
if (!$this->getAllElements('h1')
27-
&& !$this->getAllElements('h2')
28-
&& !$this->getAllElements('h3')
29-
&& !$this->getAllElements('h4')
30-
&& !$this->getAllElements('h5')
31-
&& !$this->getAllElements('h6')) {
32-
$this->setIssue($this->dom->documentElement);
33-
}
34-
}
35-
$this->totalTests++;
23+
// Ignore html with script tags
24+
if (count($this->getAllElements('script')) === 0) {
25+
$document_string = $this->dom->textContent;
26+
27+
if (strlen($document_string) > $this->minDocLengthForHeaders){
28+
if (!$this->getAllElements('h1')
29+
&& !$this->getAllElements('h2')
30+
&& !$this->getAllElements('h3')
31+
&& !$this->getAllElements('h4')
32+
&& !$this->getAllElements('h5')
33+
&& !$this->getAllElements('h6')) {
34+
$this->setIssue($this->dom->documentElement);
35+
}
36+
}
37+
$this->totalTests++;
3638

3739

38-
return count($this->issues);
40+
return count($this->issues);
41+
}
3942
}
4043

4144
}

tests/ContentTooLongTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,14 @@ public function testCheckFalseMultipleElements()
4242

4343
$this->assertEquals(1, $rule->check(), 'Content Too Long should have one issue.');
4444
}
45+
46+
public function testCheckSkipScriptTags()
47+
{
48+
$html = file_get_contents(__DIR__ . '/../tests/testFiles/ContentTooLongValidPage.html');
49+
$dom = new \DOMDocument('1.0', 'utf-8');
50+
$dom->loadHTML($html);
51+
$rule = new ContentTooLong($dom);
52+
53+
$this->assertEquals(0, $rule->check(), 'Content Too Long should have no issues.');
54+
}
4555
}

tests/NoHeadingsTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,14 @@ public function testCheckFalse()
3939

4040
$this->assertEquals(1, $rule->check(), 'No Headings Test should have one issue.');
4141
}
42+
43+
public function testCheckSkipScriptTags()
44+
{
45+
$html = file_get_contents(__DIR__ . '/../tests/testFiles/ContentTooLongScript.html');
46+
$dom = new \DOMDocument('1.0', 'utf-8');
47+
$dom->loadHTML($html);
48+
$rule = new NoHeadings($dom);
49+
50+
$this->assertEquals(0, $rule->check(), 'No Headings Test should have one issue.');
51+
}
4252
}

0 commit comments

Comments
 (0)