2 namespace Civi\Angular
;
8 * Determine whether an HTML snippet remains consistent (through an
11 * Note: Variations in whitespace are permitted.
16 public function checkConsistentHtml($html) {
18 $recodedHtml = $this->recode($html);
20 catch (\Exception
$e) {
24 $htmlSig = preg_replace('/[ \t\r\n\/]+/', '', $this->cleanup($html));
25 $docSig = preg_replace('/[ \t\r\n\/]+/', '', $recodedHtml);
26 if ($htmlSig !== $docSig ||
empty($html) != empty($htmlSig)) {
33 * Parse an HTML snippet and re-encode is as HTML.
35 * This is useful for detecting cases where the parser or encoder
41 public function recode($html) {
42 $doc = \phpQuery
::newDocument("$html", 'text/html');
43 return $this->encode($doc);
47 * Encode a phpQueryObject as HTML.
49 * @param \phpQueryObject $doc
53 public function encode($doc) {
54 $doc->document
->formatOutput
= TRUE;
55 return $this->cleanup($doc->markupOuter());
58 protected function cleanup($html) {
59 $html = preg_replace_callback("/([\\-a-zA-Z0-9]+)=(')([^']*)(')/", array($this, 'cleanupAttribute'), $html);
60 $html = preg_replace_callback('/([\-a-zA-Z0-9]+)=(")([^"]*)(")/', array($this, 'cleanupAttribute'), $html);
64 protected function cleanupAttribute($matches) {
65 list ($full, $attr, $lquote, $value, $rquote) = $matches;
69 if (strpos($value, '%7B%7B') !== FALSE && strpos($value, '%7D%7D') !== FALSE) {
70 $value = urldecode($value);
75 $value = html_entity_decode($value);
79 return "$attr=$lquote$value$rquote";