Esempio n. 1
0
 /**
  * append() をテストします.
  * 以下を確認します.
  * 
  * - 任意のスカラー値および Node が追加できること
  * - null および None を指定した場合, 変化がない (追加されない) こと
  * - コンテナを追加した場合, 自身ではなくその子ノードが追加されること
  * 
  * @covers Peach\Markup\Container::append
  */
 public function testAppend()
 {
     $nList = new NodeList();
     $nList->append("foo");
     $nList->append("bar");
     $nList->append("baz");
     $test = $this->test;
     $obj = $this->object;
     $obj->append(null);
     $obj->append("TEXT");
     // (count: 1)
     $obj->append(new EmptyElement("test"));
     // (count: 2)
     $obj->append(None::getInstance());
     // added nothing (count: 2)
     $obj->append($nList);
     // added 3 nodes (count: 5)
     $obj->append(array("A", "B", array("C", "D")));
     // added 4 nodes (count: 9)
     $test->assertSame(9, count($obj->getChildNodes()));
 }
Esempio n. 2
0
 public static function convertToNodes($array, $depth = 0)
 {
     $nodelist = new NodeList();
     foreach ($array as $var) {
         $current = Node::autoCreate($var);
         if (isset($var->children)) {
             $current->setChildren(self::convertToNodes($var->children));
         }
         $nodelist->append($current);
     }
     return $nodelist;
 }
 /**
  * Find list of nodes with a CSS selector
  *
  * @param string $selector
  * @param int    $idx
  *
  * @return NodeList|Element|null
  */
 public function find($selector, $idx = null)
 {
     $elements = new NodeList();
     foreach ($this as $node) {
         foreach ($node->find($selector) as $res) {
             $elements->append($res);
         }
     }
     if (is_null($idx)) {
         return $elements;
     } else {
         if ($idx < 0) {
             $idx = count($elements) + $idx;
         }
     }
     return isset($elements[$idx]) ? $elements[$idx] : null;
 }
Esempio n. 4
0
 function &parse()
 {
     $until = func_get_args();
     $nodelist = new NodeList($this);
     while ($token = $this->tokenstream->next()) {
         //$token = $this->tokenstream->current();
         switch ($token->type) {
             case 'text':
                 $node = new TextNode($token->content, $token->position);
                 break;
             case 'variable':
                 $args = H2o_Parser::parseArguments($token->content, $token->position);
                 $variable = array_shift($args);
                 $filters = $args;
                 $node = new VariableNode($variable, $filters, $token->position);
                 break;
             case 'comment':
                 $node = new CommentNode($token->content);
                 break;
             case 'block':
                 if (in_array($token->content, $until)) {
                     $this->token = $token;
                     return $nodelist;
                 }
                 @(list($name, $args) = preg_split('/\\s+/', $token->content, 2));
                 $node = H2o::createTag($name, $args, $this, $token->position);
                 $this->token = $token;
         }
         $this->searching = join(',', $until);
         $this->first = false;
         $nodelist->append($node);
     }
     if ($until) {
         throw new TemplateSyntaxError('Unclose tag, expecting ' . $until[0]);
     }
     return $nodelist;
 }
Esempio n. 5
0
 /**
  * NodeList に含まれる各子ノードを handle することを確認します.
  * 
  * @covers Peach\Markup\DefaultContext::handleNodeList
  * @covers Peach\Markup\DefaultContext::formatChildNodes
  * @covers Peach\Markup\DefaultContext::breakCode
  */
 public function testHandleNodeList()
 {
     $node1 = new EmptyElement("empty");
     $node2 = new Text("Sample Text");
     $node3 = new ContainerElement("container");
     $node3->append("TEST");
     $nodeList = new NodeList();
     $nodeList->append($node1);
     $nodeList->append($node2);
     $nodeList->append($node3);
     $expected = implode("\r\n", array('<empty />', 'Sample Text', '<container>TEST</container>'));
     $this->object->handleNodeList($nodeList);
     $this->assertSame($expected, $this->object->getResult());
 }
Esempio n. 6
0
 /**
  * handleNodeList() のテストです.
  * 
  * @covers Peach\Markup\DebugContext::handleNodeList
  * @covers Peach\Markup\DebugContext::startNode
  * @covers Peach\Markup\DebugContext::handleContainer
  * @covers Peach\Markup\DebugContext::endNode
  */
 public function testHandleNodeList()
 {
     $expected = implode("\r\n", array("NodeList {", "    Comment {", "        Text", "    }", "    EmptyElement(img)", "    Text", "}")) . "\r\n";
     $nodeList = new NodeList();
     $comment = new Comment();
     $comment->append("This is test comment");
     $img = new EmptyElement("img");
     $img->setAttributes(array("src" => "test.jpg", "alt" => ""));
     $nodeList->append($comment);
     $nodeList->append($img);
     $nodeList->append("Test image");
     $context = $this->object;
     $this->expectOutputString($expected);
     $context->handleNodeList($nodeList);
     $this->assertSame($expected, $context->getResult());
 }
Esempio n. 7
0
 /**
  * この要素に子ノードを追加します.
  * 
  * @param  mixed $var 追加する値
  * @throws \InvalidArgumentException 指定されたノードの中にこのノードが存在している場合
  */
 public function append($var)
 {
     $this->childNodes->append($var);
 }