Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
NoScriptJsDisabledContentRule
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 1
 test
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace pvc\html\rules\content_rules\permitted;
4
5use pvc\html\rules\AbstractRule;
6use pvc\interfaces\html\content_model\ContentCategory;
7use pvc\interfaces\html\content_model\ContentPermission;
8use pvc\interfaces\html\dom\DomNodeInterface;
9use pvc\interfaces\html\rules\ContentRuleInterface;
10
11/**
12 * TODO: major difficulty: no way to know the content model until the
13 * page is served and we can test whether javascript has been enabled.....
14 */
15class NoScriptJsDisabledContentRule extends AbstractRule implements ContentRuleInterface
16{
17    public function test(DomNodeInterface $content): ContentPermission
18    {
19        /**
20         * if content appears inside head element, then it must be meta, link,
21         * or style tag
22         */
23        $nameEqualsHead = function (DomNodeInterface $node) { return $node->getDomElement()->hasName('head'); };
24
25        if ($content->hasParentWith($nameEqualsHead)) {
26            $contentName = $content->getDomElement()->getName();
27            if (in_array($contentName,['link', 'style', 'meta'])) return ContentPermission::GRANTED;
28        } else {
29            /**
30             * if it is not inside the head element, then it must be in the body element
31             */
32            if ($content->getContentModel()->hasCategory(ContentCategory::Transparent) &&
33                ($content->getDomElement()->getName() !== 'noscript')) return ContentPermission::GRANTED;
34        }
35        return ContentPermission::DENIED;
36    }
37}