Texy\HtmlElement::insert PHP Method

insert() public method

Inserts child node.
public insert ( $index, $child, $replace = FALSE ) : self
return self
    public function insert($index, $child, $replace = FALSE)
    {
        if ($child instanceof self || is_string($child)) {
            if ($index === NULL) {
                // append
                $this->children[] = $child;
            } else {
                // insert or replace
                array_splice($this->children, (int) $index, $replace ? 1 : 0, [$child]);
            }
        } else {
            throw new \InvalidArgumentException('Child node must be scalar or HtmlElement object.');
        }
        return $this;
    }

Usage Example

示例#1
0
 /**
  * @return void
  */
 public function process(Texy\BlockParser $parser, $content, Texy\HtmlElement $el)
 {
     if ($parser->isIndented()) {
         $parts = preg_split('#(\\n(?! )|\\n{2,})#', $content, -1, PREG_SPLIT_NO_EMPTY);
     } else {
         $parts = preg_split('#(\\n{2,})#', $content, -1, PREG_SPLIT_NO_EMPTY);
     }
     foreach ($parts as $s) {
         $s = trim($s);
         if ($s === '') {
             continue;
         }
         // try to find modifier
         $mod = NULL;
         if ($mx = Regexp::match($s, '#' . Texy\Patterns::MODIFIER_H . '(?=\\n|\\z)#sUm', Regexp::OFFSET_CAPTURE)) {
             list($mMod) = $mx[1];
             $s = trim(substr_replace($s, '', $mx[0][1], strlen($mx[0][0])));
             if ($s === '') {
                 continue;
             }
             $mod = new Texy\Modifier();
             $mod->setProperties($mMod);
         }
         $res = $this->texy->invokeAroundHandlers('paragraph', $parser, [$s, $mod]);
         if ($res) {
             $el->insert(NULL, $res);
         }
     }
 }