Passed
Push — master ( 62942b...a1d089 )
by Josh
02:55
created

SetRelNoreferrerOnTargetedLinks::linkTargetCanAccessOpener()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 15
ccs 6
cts 6
cp 1
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2020 The s9e authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Configurator\TemplateNormalizations;
9
10
use DOMElement;
11
12
/**
13
* Add rel="noreferrer" on links that open in a new context that would allow window.opener to be
14
* accessed.
15
*
16
* @link https://mathiasbynens.github.io/rel-noopener/
17
* @link https://wiki.whatwg.org/wiki/Links_to_Unrelated_Browsing_Contexts
18
*/
19
class SetRelNoreferrerOnTargetedLinks extends AddAttributeValueToElements
20
{
21
	/**
22
	* {@inheritdoc}
23
	*/
24 7
	public function __construct(string $query = '//a[@target] | //area[@target]', string $attrName = 'rel', string $value = 'noreferrer')
25
	{
26 7
		parent::__construct($query, $attrName, $value);
27
	}
28
29
	/**
30
	* {@inheritdoc}
31
	*/
32 6
	protected function normalizeElement(DOMElement $element): void
33
	{
34 6
		if (!preg_match('(\\bno(?:open|referr)er\\b)', $element->getAttribute('rel')))
35
		{
36 4
			parent::normalizeElement($element);
37
		}
38
	}
39
}