|
1
|
|
|
<?php |
|
2
|
|
|
namespace Goetas\TwitalBundle\EventSubscriber; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
5
|
|
|
use Goetas\Twital\EventDispatcher\TemplateEvent; |
|
6
|
|
|
use Goetas\Twital\Twital; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* |
|
10
|
|
|
* @author Asmir Mustafic <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
*/ |
|
13
|
|
|
class WhiteSpaceRemovalSubscriber implements EventSubscriberInterface |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
protected $allowedTags = array(); |
|
17
|
|
|
|
|
18
|
|
|
protected $allowedNamespaces = array(); |
|
19
|
|
|
|
|
20
|
23 |
|
public function __construct(array $allowedTags = array(), array $allowedNamespaces = array()) |
|
21
|
|
|
{ |
|
22
|
23 |
|
$this->allowedTags = $allowedTags; |
|
23
|
23 |
|
$this->allowedNamespaces = $allowedNamespaces; |
|
24
|
23 |
|
} |
|
25
|
|
|
|
|
26
|
18 |
|
public static function getSubscribedEvents() |
|
27
|
|
|
{ |
|
28
|
|
|
return array( |
|
29
|
18 |
|
'compiler.post_load' => array('removeWhitespace', -10) |
|
30
|
|
|
); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
23 |
|
private function performXpathQuery(\DOMXPath $xp, $query, \DOMNode $ref) |
|
34
|
|
|
{ |
|
35
|
23 |
|
if (defined('HHVM_VERSION')) { |
|
36
|
|
|
return $xp->query($query, $ref); |
|
37
|
|
|
} else { |
|
38
|
23 |
|
return $xp->query($query, $ref, false); |
|
39
|
|
|
} |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
23 |
|
public function removeWhitespace(TemplateEvent $event) |
|
43
|
|
|
{ |
|
44
|
23 |
|
$doc = $event->getTemplate()->getDocument(); |
|
45
|
|
|
|
|
46
|
23 |
|
$xp = new \DOMXPath($doc); |
|
47
|
23 |
|
$xp->registerNamespace("t", Twital::NS); |
|
48
|
|
|
|
|
49
|
23 |
|
foreach ($this->performXpathQuery($xp, "//text()[ancestor::*[@t:trans or @t:trans-n]]", $doc) as $text) { |
|
50
|
12 |
|
if ($this->isAllowedNode($text->parentNode)) { |
|
51
|
5 |
|
$text->data = preg_replace('/\s+/', ' ', $text->data); |
|
52
|
|
|
|
|
53
|
5 |
|
if ($text->parentNode->childNodes->length === 1) { |
|
54
|
5 |
|
$trimmed = trim($text->data); |
|
55
|
5 |
|
if (!strlen($trimmed) && strlen($text->data)) { |
|
56
|
1 |
|
$trimmed = " "; |
|
57
|
|
|
} |
|
58
|
5 |
|
$text->data = $trimmed; |
|
59
|
2 |
|
} elseif ($text->parentNode->hasAttributeNs(Twital::NS, 'trans') || $text->parentNode->hasAttributeNs(Twital::NS, 'trans-n')) { |
|
60
|
2 |
|
if ($text->parentNode->firstChild === $text) { |
|
61
|
2 |
|
$text->data = ltrim($text->data); |
|
62
|
2 |
|
} elseif ($text->parentNode->lastChild === $text) { |
|
63
|
12 |
|
$text->data = rtrim($text->data); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
23 |
|
} |
|
69
|
|
|
|
|
70
|
12 |
|
private function isAllowedNode(\DOMElement $element) |
|
71
|
|
|
{ |
|
72
|
|
|
|
|
73
|
12 |
|
if ($this->allowedTags && !in_array($element->localName, $this->allowedTags)) { |
|
|
|
|
|
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
12 |
|
if ($this->allowedNamespaces && !in_array($element->namespaceURI, $this->allowedNamespaces)) { |
|
|
|
|
|
|
78
|
7 |
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
5 |
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.