public function enterTag(Tag $tag) { if ($tag->hasChilds() || $tag->hasContent()) { return; } if (in_array($tag->getTagName(), $this->autocloseTags)) { $tag->setFlag(Tag::FLAG_SELF_CLOSE); } }
public function enterTagAttributes(Tag $node) { $this->attrs = array(); $this->tag = $node; // Do not attempt to merge attributes if any attribute // name cannot be guessed at compile time. // // Else it may change the output (e.g. the unknown attribute // name could be 'class') foreach ($node->getAttributes() as $attr) { if (null === $this->getString($attr->getName())) { return false; } } }
protected function createNodes() { $node = new Tag(array(), 'div', array()); $this->assertFalse($node->hasChilds()); $nodeB = new Tag(array(), 'div', array()); $node->addChild($nodeB); $this->assertTrue($node->hasChilds()); $this->assertSame($node, $nodeB->getParent()); $nodeC = new Tag(array(), 'div', array()); $node->addChild($nodeC); $this->assertSame(2, count($node->getChilds())); $this->assertSame($node, $nodeC->getParent()); $this->assertSame(null, $nodeB->getPreviousSibling()); $this->assertSame($nodeC, $nodeB->getNextSibling()); $this->assertSame($nodeB, $nodeC->getPreviousSibling()); $this->assertSame(null, $nodeC->getNextSibling()); $nodeD = new Tag(array(), 'div', array()); $node->addChild($nodeD); $this->assertSame($nodeB, $nodeC->getPreviousSibling()); $this->assertSame($nodeD, $nodeC->getNextSibling()); return compact('node', 'nodeB', 'nodeC', 'nodeD'); }
public function leaveTag(Tag $node) { $this->undent()->write(')', $node->hasAttributes() || $node->hasChilds()); }
public function leaveTag(Tag $node) { if ($node->getFlags() & Tag::FLAG_SELF_CLOSE) { return; } $indent = $this->shouldIndentBeforeClose($node); $break = $this->shouldBreakAfterClose($node); if ($this->shouldBreakAfterOpen($node)) { $this->undent(); } $this->write(sprintf('</%s>', $node->getTagName()), $indent, $break); }
protected function parseTag($buf) { $arr = (array) $this->env->getOption('shortcut'); // if ( empty( $arr ) ) { // return parent::parseTag( $buf ); // } $shortcuts = array(); $prefixes[] = '\\%'; foreach ($arr as $s => $p) { if (!empty($p['tag'])) { $prefixes[] = '\\' . $s; $shortcuts[$s] = $p; } } //inject @include and @require $shortcuts['@'][] = 'include'; $shortcuts['@'][] = 'require'; $prefixes[] = '\\@'; $prefixes = implode('|', array_unique($prefixes)); // Dbg::emsgd($prefixes); return; $tagRegex = '/ (?P<shortcut>' . $prefixes . ')(?P<tag_name>[\\w:-]+) # explicit tag name ( %tagname ) | (?=[.#][\\w-]) # implicit div followed by class or id # ( .class or #id ) /xA'; //Dbg::emsgd($tagRegex); return; if ($buf->match($tagRegex, $match)) { $new_type = null; $tag_name = empty($match['tag_name']) ? 'div' : $match['tag_name']; if (!empty($shortcuts[$match['shortcut']])) { //@include adn @require if ('@' === $match['shortcut'] && ('require' === $match['tag_name'] || 'include' === $match['tag_name'])) { return $this->injectFile($buf, $match['tag_name']); } $params = $shortcuts[$match['shortcut']]; $tag_name = $params['tag']; //Dbg::emsgd($params['tag']); $name = new Text($match['pos'][0], $params['attr']); $value = new Text($match['pos'][1], $match['tag_name']); $new_type = new TagAttribute($match['pos'][0], $name, $value); } $attributes = $this->parseTagAttributesShortcut($buf); $flags = $this->parseTagFlags($buf); $node = new Tag($match['pos'][0], $tag_name, $attributes, $flags); if ($new_type !== null) { $node->addAttribute($new_type); } $buf->skipWs(); if (null !== ($nested = $this->parseNestableStatement($buf))) { if ($flags & Tag::FLAG_SELF_CLOSE) { $msg = 'Illegal nesting: nesting within a self-closing tag is illegal'; $this->syntaxError($buf, $msg); } $node->setContent($nested); } return $node; } else { return parent::parseTag($buf); } }
protected function parseTag($buf) { $tagRegex = '/ %(?P<tag_name>[\\w:-]+) # explicit tag name ( %tagname ) | (?=[.#][\\w-]) # implicit div followed by class or id # ( .class or #id ) /xA'; if ($buf->match($tagRegex, $match)) { $tag_name = empty($match['tag_name']) ? 'div' : $match['tag_name']; $attributes = $this->parseTagAttributes($buf); $flags = $this->parseTagFlags($buf); $node = new Tag($match['pos'][0], $tag_name, $attributes, $flags); $buf->skipWs(); if (null !== ($nested = $this->parseNestableStatement($buf))) { if ($flags & Tag::FLAG_SELF_CLOSE) { $msg = 'Illegal nesting: nesting within a self-closing tag is illegal'; $this->syntaxError($buf, $msg); } $node->setContent($nested); } return $node; } }
protected function renderDynamicAttributes(Tag $tag) { $list = array(); $n = 0; $this->raw(' <?php echo MtHaml\\Runtime::renderAttributes(array('); $this->setEchoMode(false); foreach ($tag->getAttributes() as $attr) { if (0 !== $n) { $this->raw(', '); } if ($attr instanceof TagAttributeInterpolation) { $this->raw('MtHaml\\Runtime\\AttributeInterpolation::create('); $attr->getValue()->accept($this); $this->raw(')'); } else { if ($attr instanceof TagAttributeList) { $this->raw('MtHaml\\Runtime\\AttributeList::create('); $attr->getValue()->accept($this); $this->raw(')'); } else { $this->raw('array('); $attr->getName()->accept($this); $this->raw(', '); if ($value = $attr->getValue()) { $attr->getValue()->accept($this); } else { $this->raw('TRUE'); } $this->raw(')'); } } ++$n; } $this->raw(')'); $this->setEchoMode(true); $this->raw(', '); $this->raw($this->stringLiteral($this->env->getOption('format'))); $this->raw(', '); $this->raw($this->stringLiteral($this->charset)); $this->raw('); ?>'); }
protected function renderDynamicAttributes(Tag $tag) { $this->raw(' '); foreach ($tag->getAttributes() as $attr) { $this->addDebugInfos($attr); break; } $this->raw('{{ mthaml_attributes(['); $this->setEchoMode(false); foreach (array_values($tag->getAttributes()) as $i => $attr) { if (0 !== $i) { $this->raw(', '); } if ($attr instanceof TagAttributeInterpolation) { $this->raw('mthaml_attribute_interpolation('); $attr->getValue()->accept($this); $this->raw(')'); } elseif ($attr instanceof TagAttributeList) { $this->raw('mthaml_attribute_list('); $attr->getValue()->accept($this); $this->raw(')'); } else { $this->raw('['); $attr->getName()->accept($this); $this->raw(', '); if ($attr->getValue()) { $attr->getValue()->accept($this); } else { $this->raw('true'); } $this->raw(']'); } } $this->raw(']'); $this->setEchoMode(true); $this->raw(', '); $this->raw($this->stringLiteral($this->env->getOption('format'))); $this->raw(', '); $this->raw($this->stringLiteral($this->charset)); $this->raw($this->env->getOption('enable_escaper') && $this->env->getOption('escape_attrs') ? '' : ', false'); $this->raw(')|raw }}'); }
public function __construct(array $position, $content) { parent::__construct($position, '', array()); $this->content = $content; }