Пример #1
0
 /**
  * Convert HTML DOM to a template node.
  * @param \simple_html_dom_node $node DOM node.
  * @return TemplateNode Template node.
  */
 public function convert(\simple_html_dom_node $node)
 {
     if ($node->tag === 'text' or $node->tag === 'unknown') {
         return new TextNode($node->innertext);
     } else {
         if ($node->tag === 'comment') {
             if (preg_match('/^<!-- *\\{(.*)\\} *-->$/ms', $node->innertext, $matches) === 1) {
                 return new PhpNode($matches[1], true);
             }
             return new TextNode('');
         } else {
             $output = new HtmlNode($node->tag);
             foreach ($node->attr as $name => $value) {
                 if (preg_match('/^{(.*)}$/', $value, $matches) === 1) {
                     $value = new PhpNode($matches[1]);
                 } else {
                     if ($value !== true) {
                         $value = new TextNode($value);
                     } else {
                         $value = null;
                     }
                 }
                 if (strpos($name, ':') === false) {
                     $output->setAttribute($name, $value);
                 } else {
                     //           list($ns, $name) = explode(':', $name, 2);
                     if ($value === true) {
                         $value = null;
                     }
                     $output->addMacro($name, $value);
                 }
             }
             foreach ($node->nodes as $child) {
                 $output->append($this->convert($child));
             }
             return $output;
         }
     }
 }