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

SetRelNoreferrerOnTargetedLinks   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 4
c 1
b 0
f 0
dl 0
loc 18
ccs 5
cts 5
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizeElement() 0 5 2
A __construct() 0 3 1
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
}