| Conditions | 9 |
| Paths | 13 |
| Total Lines | 36 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 23 |
| CRAP Score | 9 |
| Changes | 0 | ||
| 1 | <?php |
||
| 19 | 18 | public function relativeToAbsolute(ElementFinder $finder, string $affectedUrl = '') { |
|
| 20 | |||
| 21 | 18 | $affected = new Uri($affectedUrl); |
|
| 22 | |||
| 23 | 18 | $srcElements = $finder->element('//*[@src] | //*[@href] | //form[@action]'); |
|
| 24 | 18 | $baseUrl = $finder->value('//base/@href')->getFirst(); |
|
| 25 | |||
| 26 | 18 | foreach ($srcElements as $element) { |
|
| 27 | 17 | $attributeName = 'href'; |
|
| 28 | |||
| 29 | 17 | if ($element->hasAttribute('action') === true and $element->tagName === 'form') { |
|
| 30 | 2 | $attributeName = 'action'; |
|
| 31 | 17 | } else if ($element->hasAttribute('src') === true) { |
|
| 32 | 4 | $attributeName = 'src'; |
|
| 33 | 4 | } |
|
| 34 | |||
| 35 | 17 | $relative = $element->getAttribute($attributeName); |
|
| 36 | |||
| 37 | # don`t change javascript in href |
||
| 38 | 17 | if (preg_match('!^\s*javascript\s*:\s*!', $relative)) { |
|
| 39 | 1 | continue; |
|
| 40 | } |
||
| 41 | |||
| 42 | 16 | if (parse_url($relative) === false) { |
|
| 43 | 1 | continue; |
|
| 44 | } |
||
| 45 | |||
| 46 | 15 | if ($baseUrl !== null and !preg_match('!^(/|http)!i', $relative)) { |
|
| 47 | 2 | $relative = UriResolver::resolve(new Uri($baseUrl), new Uri($relative)); |
|
| 48 | 2 | } |
|
| 49 | |||
| 50 | 15 | $url = UriResolver::resolve($affected, new Uri($relative)); |
|
| 51 | 15 | $element->setAttribute($attributeName, (string) $url); |
|
| 52 | 18 | } |
|
| 53 | |||
| 54 | 18 | } |
|
| 55 | |||
| 56 | } |