Пример #1
0
 protected function addSiteId($siteId, $template)
 {
     $dom = TemplateHelper::loadTemplate($template);
     $xpath = new DOMXPath($dom);
     $query = '//*[namespace-uri() != "' . TemplateHelper::XMLNS_XSL . '"][not(ancestor::*[namespace-uri() != "' . TemplateHelper::XMLNS_XSL . '"])]';
     foreach ($xpath->query($query) as $element) {
         $element->setAttribute('data-s9e-mediaembed', $siteId);
     }
     return TemplateHelper::saveTemplate($dom);
 }
 public function normalizeValue($value)
 {
     return TemplateHelper::saveTemplate(TemplateHelper::loadTemplate($value));
 }
Пример #3
0
 /**
  * Parse given template and store the references it contains
  *
  * @param  string $template
  * @return void
  */
 protected function parseTemplate($template)
 {
     $this->references = ['anywhere' => [], 'asUrl' => [], 'inText' => []];
     preg_match_all($this->referencesRegexp, $template, $matches);
     foreach ($matches[0] as $match) {
         $key = trim($match, '\\${}');
         $this->references['anywhere'][$key] = $key;
     }
     $dom = TemplateHelper::loadTemplate($template);
     $xpath = new DOMXPath($dom);
     foreach ($xpath->query('//text()') as $node) {
         preg_match_all($this->referencesRegexp, $node->textContent, $matches);
         foreach ($matches[0] as $match) {
             $key = trim($match, '\\${}');
             $this->references['inText'][$key] = $key;
         }
     }
     foreach (TemplateHelper::getURLNodes($dom) as $node) {
         // We only bother with literal attributes that start with a capture
         if ($node instanceof DOMAttr && preg_match('(^(?:[$\\\\]\\d+|\\$\\{\\d+\\}))', trim($node->value), $m)) {
             $key = trim($m[0], '\\${}');
             $this->references['asUrl'][$key] = $key;
         }
     }
     $this->removeUnknownReferences();
 }
Пример #4
0
 /**
  * Check a given template for disallowed content
  *
  * @param  string $template Template
  * @param  Tag    $tag      Tag this template belongs to
  * @return void
  */
 public function checkTemplate($template, Tag $tag = null)
 {
     if ($this->disabled) {
         return;
     }
     if (!isset($tag)) {
         $tag = new Tag();
     }
     // Load the template into a DOMDocument
     $dom = TemplateHelper::loadTemplate($template);
     foreach ($this->collection as $check) {
         $check->check($dom->documentElement, $tag);
     }
 }
Пример #5
0
 /**
  * @testdox highlightNode() tests
  * @dataProvider getHighlights
  */
 public function testHighlightNode($query, $template, $expected)
 {
     $dom = TemplateHelper::loadTemplate($template);
     $xpath = new DOMXPath($dom);
     $this->assertSame($expected, TemplateHelper::highlightNode($xpath->query($query)->item(0), '<span style="background-color:#ff0">', '</span>'));
 }
Пример #6
0
 protected function parseTemplate($template)
 {
     $this->references = array('anywhere' => array(), 'asUrl' => array(), 'inText' => array());
     \preg_match_all($this->referencesRegexp, $template, $matches);
     foreach ($matches[0] as $match) {
         $key = \trim($match, '\\${}');
         $this->references['anywhere'][$key] = $key;
     }
     $dom = TemplateHelper::loadTemplate($template);
     $xpath = new DOMXPath($dom);
     foreach ($xpath->query('//text()') as $node) {
         \preg_match_all($this->referencesRegexp, $node->textContent, $matches);
         foreach ($matches[0] as $match) {
             $key = \trim($match, '\\${}');
             $this->references['inText'][$key] = $key;
         }
     }
     foreach (TemplateHelper::getURLNodes($dom) as $node) {
         if ($node instanceof DOMAttr && \preg_match('(^(?:[$\\\\]\\d+|\\$\\{\\d+\\}))', \trim($node->value), $m)) {
             $key = \trim($m[0], '\\${}');
             $this->references['asUrl'][$key] = $key;
         }
     }
     $this->removeUnknownReferences();
 }
Пример #7
0
 /**
  * Normalize a template
  *
  * @param  string $template Original template
  * @return string           Normalized template
  */
 public function normalizeTemplate($template)
 {
     $dom = TemplateHelper::loadTemplate($template);
     // We'll keep track of what normalizations have been applied
     $applied = [];
     // Apply all the normalizations until no more change is made or we've reached the maximum
     // number of loops
     $loops = 5;
     do {
         $old = $template;
         foreach ($this->collection as $k => $normalization) {
             if (isset($applied[$k]) && !empty($normalization->onlyOnce)) {
                 continue;
             }
             $normalization->normalize($dom->documentElement);
             $applied[$k] = 1;
         }
         $template = TemplateHelper::saveTemplate($dom);
     } while (--$loops && $template !== $old);
     return $template;
 }