/
HtmlLinkTag.php
144 lines (125 loc) · 4.22 KB
/
HtmlLinkTag.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
namespace APF\tools\link\taglib;
/**
* <!--
* This file is part of the adventure php framework (APF) published under
* http://adventure-php-framework.org.
*
* The APF is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The APF is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the APF. If not, see http://www.gnu.org/licenses/lgpl-3.0.txt.
* -->
*/
use APF\tools\link\LinkGenerator;
use APF\tools\link\Url;
use InvalidArgumentException;
/**
* @package APF\tools\link\taglib
* @class HtmlLinkTag
*
* This taglib generates a html link tag based on the LinkGenerationTag taglib.
*
* @author Werner Liemberger wpublicmail [at] gmail DOT com
* @version
* Version 0.1, 06.08.2011<br />
* Version 0.2, 22.11.2012 Werner Liemberger: add removed a:getstring and ignored href bug<br />
* Version 0.3, 30.11.2012 Werner Liemberger: add title:getstring and move $this->extractTagLibTags(); in onPareTime() to the beginning<br />
*/
class HtmlLinkTag extends LinkGenerationTag {
protected $attributeWhiteList = array(
'id',
'style',
'class',
'onabort',
'onclick',
'ondblclick',
'onmousedown',
'onmouseup',
'onmouseover',
'onmousemove',
'onmouseout',
'onkeypress',
'onkeydown',
'onkeyup',
'onblur',
'tabindex',
'dir',
'accesskey',
'title',
'coords',
self::HREF_ATTRIBUTE_NAME,
'hreflang',
'name',
'rel',
'rev',
'shape',
'target');
public function onParseTime() {
// generate URL using our parent implementation
$this->setAttribute(self::HREF_ATTRIBUTE_NAME, parent::transform());
// analyze tags which might add some attributes to this one.
$this->extractTagLibTags();
}
protected function getUrlParameters() {
// only non-white-list parameters are allowed as URL parameters
// to not interfere with HTML attributes
$attributes = array();
foreach ($this->getAttributes() as $key => $value) {
if (!in_array($key, $this->attributeWhiteList)) {
$attributes[$key] = $value;
}
}
return $attributes;
}
public function transform() {
// If no Content is set, this taglib tries to set the title as content.
// If this is also missing it throws an Exception. This exception is needed,
// because otherwise you will get an invalid html.
$content = $this->getContent();
if (empty($content)) {
$content = $this->attributeWhiteList['title'];
}
if (empty($content)) {
throw new InvalidArgumentException('No anchor text available!');
}
$href = $this->getAttribute(self::HREF_ATTRIBUTE_NAME);
if ($href === null) {
return '';
}
// if the current link is active, this taglib adds the css class active.
if ($this->isActive()) {
$class = $this->getAttribute('class');
if (empty($class)) {
$this->setAttribute('class', 'active');
} else {
$this->setAttribute('class', $class . ' active');
}
}
return '<a ' . $this->getAttributesAsString($this->getAttributes()) . '>' . $content . '</a>';
}
/**
* Checks if link is active (=we are already on the target page) or not.
*
* @return boolean
*
* @author Werner Liemberger wpublicmail [at] gmail DOT com
* @version
* Version 0.1, 30.11.2012<br />
*/
public function isActive() {
$currentUrl = LinkGenerator::generateUrl(Url::fromCurrent(true));
if (substr_count(str_replace('&', '&', $currentUrl), $this->getAttribute(self::HREF_ATTRIBUTE_NAME)) > 0) {
return true;
}
return false;
}
}