Cheetah
AddChLinksClass.php
Go to the documentation of this file.
1 <?php
2 
4 {
5  public $name = 'AddChLinksClass';
6  protected $class = CH_WSB_LINK_CLASS;
7 
8  public function preFilter($sHtml, $config, $context)
9  {
10  if (false === strstr($sHtml, '<a '))
11  return $sHtml;
12 
13  $sId = 'ch-links-' . md5(microtime());
14  $dom = new DOMDocument();
15  @$dom->loadHTML('<?xml encoding="UTF-8"><div id="' . $sId . '">' . $sHtml . '</div>');
16  $xpath = new DOMXpath($dom);
17 
18  $oLinks = $xpath->evaluate('//a');
19  for ($i = 0; $i < $oLinks->length; $i++) {
20  $oLink = $oLinks->item($i);
21 
22  $sClasses = $oLink->getAttribute('class');
23  if (!$sClasses || false === strpos($sClasses, $this->class))
24  $sClasses = ($sClasses ? $sClasses . ' ' : '') . $this->class;
25 
26  $oLink->removeAttribute('class');
27  $oLink->setAttribute("class", $sClasses);
28  }
29 
30  if (false === ($s = $dom->saveXML($dom->getElementById($sId)))) // in case of error return original string
31  return $sHtml;
32 
33  return mb_substr($s, 52, -6); // strip added tags
34  }
35 }
36 
$config
$config
Definition: Filter.ExtractStyleBlocks.txt:33
php
CH_WSB_LINK_CLASS
const CH_WSB_LINK_CLASS
Definition: utils.inc.php:12
HTMLPurifier_Filter
Definition: HTMLPurifier.standalone.php:5134
$s
$s
Definition: embed.php:13
$sId
$sId
Definition: actions.inc.php:8