Skip to content

Commit

Permalink
Merge pull request #50 from cidilabs/chuck/document-element-issues
Browse files Browse the repository at this point in the history
Updates ContentTooLong and NoHeadings rules to return empty string rather than all HTML
  • Loading branch information
atarisafari authored Jan 26, 2023
2 parents f7bf9f6 + 9144bf4 commit 9a176d9
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 37 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
}
],
"require": {
"php": ">=8.0",
"php": ">=7.4",
"guzzlehttp/guzzle": "^7.5.0",
"kaltura/api-client-library": "^17.2"
},
"scripts": {
"test": "./vendor/bin/phpunit tests"
"test": "./vendor/bin/phpunit tests --verbose"
},
"autoload": {
"classmap": [
Expand Down
68 changes: 36 additions & 32 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 20 additions & 1 deletion src/PhpAllyIssue.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class PhpAllyIssue implements \JsonSerializable
protected $ruleId;
protected $element;
protected $previewElement;
protected $metadata;

public function __construct($ruleId, DOMElement $element = null, DOMElement $previewElement = null, $metadata = null)
{
Expand Down Expand Up @@ -56,6 +57,14 @@ public function getHtml()
return '';
}

$metaStr = $this->getMetadata();
if ($metaStr) {
$metadata = \json_decode($metaStr, true);
if (!empty($metadata['isDocumentElement'])) {
return '';
}
}

return $this->element->ownerDocument->saveHTML($this->element);
}

Expand All @@ -64,8 +73,18 @@ public function getPreview()
if (!$this->previewElement) {
return '';
}

$metaStr = $this->getMetadata();
if ($metaStr) {
$metadata = \json_decode($metaStr, true);
if (!empty($metadata['isDocumentElement'])) {
return '';
}
}

$preview = $this->element->ownerDocument->saveHTML($this->previewElement);

return $this->element->ownerDocument->saveHTML($this->previewElement);
return str_replace('<?xml encoding="utf-8" ?>', '', $preview);
}

public function toArray(): array
Expand Down
2 changes: 1 addition & 1 deletion src/Rule/BaseRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BaseRule implements PhpAllyRuleInterface {
protected $errors = [];
protected $lang;
protected $strings = array('en' => '');
protected $options;

const ALT_TEXT_LENGTH_LIMIT = 150;
const DOC_LENGTH = 1500;
Expand Down Expand Up @@ -272,7 +273,6 @@ public function setIssue($element, $ruleId = null, $metadata = null)

$ruleId = str_replace(['CidiLabs\\PhpAlly\\Rule\\','App\\Rule\\'], '', $ruleId);
$previewElement = $this->previewElement;
$isDocumentElement = false;

if ($element) {
$elementClasses = $element->getAttribute('class');
Expand Down
3 changes: 2 additions & 1 deletion src/Rule/DocumentReadingDirection.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ public function check()
if ($element->getAttribute('dir') != 'rtl')
$this->setIssue($element);
}
$this->totalTests++;
}

$this->totalTests++;

return count($this->issues);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Rule/NoHeadings.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,10 @@ public function check()
return count($this->issues);
}
}

public function getPreviewElement(DOMElement $a = null)
{
return $a;
}

}
16 changes: 16 additions & 0 deletions tests/ContentTooLongTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,20 @@ public function testCheckSkipScriptTags()

$this->assertEquals(0, $rule->check(), 'Content Too Long should have no issues.');
}

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

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

$issues = $rule->getIssues();
$issue = reset($issues);

$this->assertEquals('', $issue->getHtml(), 'Content Too Long getHtml() returns an empty string.');
$this->assertEquals('', $issue->getPreview(), 'Content Too Long getHtml() returns an empty string.');
}
}

0 comments on commit 9a176d9

Please sign in to comment.