Skip to content

Commit

Permalink
Merge pull request #41 from cidilabs/skipScripts
Browse files Browse the repository at this point in the history
skipping html with script tags for issues that mark entire document a…
  • Loading branch information
cidilabs authored Nov 22, 2022
2 parents 7b73743 + 9965a8b commit e3754a3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 34 deletions.
44 changes: 24 additions & 20 deletions src/Rule/ContentTooLong.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,30 @@ public function check()
{
$pageText = '';
$wordCount = 0;
foreach ($this->getAllElements(null, 'text') as $element) {
$text = $element->nodeValue;

if($text != null){
$pageText = $pageText . $text;
}
$this->totalTests++;
}
$wordCount = str_word_count($pageText);

if($wordCount > $this->maxWordCount) {
/*
Since this rule sets the document element as the issue
we set this flag here so we can process it accordingly in UDOIT.
*/
$metadata = array('isDocumentElement' => true);
$this->setIssue($this->dom->documentElement, null, json_encode($metadata));
}

return count($this->issues);

// Ignore html with script tags
if (count($this->getAllElements('script')) === 0) {
foreach ($this->getAllElements(null, 'text') as $element) {
$text = $element->nodeValue;

if($text != null){
$pageText = $pageText . $text;
}
$this->totalTests++;
}
$wordCount = str_word_count($pageText);

if($wordCount > $this->maxWordCount) {
/*
Since this rule sets the document element as the issue
we set this flag here so we can process it accordingly in UDOIT.
*/
$metadata = array('isDocumentElement' => true);
$this->setIssue($this->dom->documentElement, null, json_encode($metadata));
}

return count($this->issues);
}
}

public function getPreviewElement(DOMElement $a = null)
Expand Down
31 changes: 17 additions & 14 deletions src/Rule/NoHeadings.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,25 @@ public function id()

public function check()
{
$document_string = $this->dom->textContent;

if (strlen($document_string) > $this->minDocLengthForHeaders){
if (!$this->getAllElements('h1')
&& !$this->getAllElements('h2')
&& !$this->getAllElements('h3')
&& !$this->getAllElements('h4')
&& !$this->getAllElements('h5')
&& !$this->getAllElements('h6')) {
$this->setIssue($this->dom->documentElement);
}
}
$this->totalTests++;
// Ignore html with script tags
if (count($this->getAllElements('script')) === 0) {
$document_string = $this->dom->textContent;

if (strlen($document_string) > $this->minDocLengthForHeaders){
if (!$this->getAllElements('h1')
&& !$this->getAllElements('h2')
&& !$this->getAllElements('h3')
&& !$this->getAllElements('h4')
&& !$this->getAllElements('h5')
&& !$this->getAllElements('h6')) {
$this->setIssue($this->dom->documentElement);
}
}
$this->totalTests++;


return count($this->issues);
return count($this->issues);
}
}

}
10 changes: 10 additions & 0 deletions tests/ContentTooLongTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,14 @@ public function testCheckFalseMultipleElements()

$this->assertEquals(1, $rule->check(), 'Content Too Long should have one issue.');
}

public function testCheckSkipScriptTags()
{
$html = file_get_contents(__DIR__ . '/../tests/testFiles/ContentTooLongValidPage.html');
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->loadHTML($html);
$rule = new ContentTooLong($dom);

$this->assertEquals(0, $rule->check(), 'Content Too Long should have no issues.');
}
}
10 changes: 10 additions & 0 deletions tests/NoHeadingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,14 @@ public function testCheckFalse()

$this->assertEquals(1, $rule->check(), 'No Headings Test should have one issue.');
}

public function testCheckSkipScriptTags()
{
$html = file_get_contents(__DIR__ . '/../tests/testFiles/ContentTooLongScript.html');
$dom = new \DOMDocument('1.0', 'utf-8');
$dom->loadHTML($html);
$rule = new NoHeadings($dom);

$this->assertEquals(0, $rule->check(), 'No Headings Test should have one issue.');
}
}

0 comments on commit e3754a3

Please sign in to comment.