1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Oskar van Velden <[email protected]> |
4
|
|
|
* @author Rik van der Kemp <[email protected]> |
5
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Zicht\Bundle\UrlBundle\Aliasing\Mapper; |
9
|
|
|
|
10
|
|
|
use Zicht\Bundle\UrlBundle\Url\Rewriter; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class HtmlMapper |
14
|
|
|
* |
15
|
|
|
* Helper to map urls in an HTML string from internal to public aliasing or vice versa. |
16
|
|
|
* |
17
|
|
|
* @package Zicht\Bundle\UrlBundle\Aliasing |
18
|
|
|
*/ |
19
|
|
|
class HtmlMapper implements UrlMapperInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Map of [elementName => [attr1, attr2]] where URL's may occur |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $htmlAttributes; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* HtmlMapper constructor. |
30
|
|
|
*/ |
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
$this->htmlAttributes = [ |
34
|
|
|
'a' => ['href', 'data-href'], |
35
|
|
|
'area' => ['href', 'data-href'], |
36
|
|
|
'option' => ['data-href'], |
37
|
|
|
'iframe' => ['src'], |
38
|
|
|
'form' => ['action'], |
39
|
|
|
'meta' => ['content'], |
40
|
|
|
'link' => ['href'] |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @{inheritDoc} |
47
|
|
|
*/ |
48
|
|
|
public function supports($contentType) |
49
|
|
|
{ |
50
|
|
|
return $contentType == 'text/html'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @{inheritDoc} |
56
|
|
|
*/ |
57
|
|
|
public function processAliasing($html, $mode, Rewriter $rewriter) |
58
|
|
|
{ |
59
|
|
|
$map = []; |
60
|
|
|
|
61
|
|
|
foreach ($this->htmlAttributes as $tagName => $attributes) { |
62
|
|
|
$pattern = sprintf('!(<%s\b[^>]+\b(?:%s)=")([^"]+)(")!', $tagName, join('|', $attributes)); |
63
|
|
|
if (preg_match_all($pattern, $html, $matches, PREG_SET_ORDER)) { |
64
|
|
|
foreach ($matches as $match) { |
65
|
|
|
$map[$match[2]][]= $match; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $rewriter->rewriteMatches($html, $mode, $map); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Merges given html attributes with the default attributes. |
75
|
|
|
* |
76
|
|
|
* See for a default defined set DependencyInjection/Configuration.php |
77
|
|
|
* |
78
|
|
|
* @param array $attributes |
79
|
|
|
*/ |
80
|
|
|
public function addAttributes($attributes) |
81
|
|
|
{ |
82
|
|
|
$this->htmlAttributes = array_merge_recursive( |
83
|
|
|
$this->htmlAttributes, |
84
|
|
|
$attributes |
85
|
|
|
); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|