jbowens / jBBCode

A lightweight but extensible BBCode parser
http://jbbcode.com
MIT License
164 stars 32 forks source link

Strange Bug [Hope find a solution] #1

Closed ghost closed 12 years ago

ghost commented 12 years ago

Hi, i made 2 custom class code definitions, but the problem came when this two tags used in nested each other.

If color tag put in size tag, color won't be parsed, so it just produced like this :

xzc[color=#ff99ff]dsfd[/color]sfs

Sorry about my english. Thank you.

class Size extends JBBCode\CodeDefinition {

    public function __construct()
    {
        parent::__construct();
        $this->setTagName("size");
        $this->setUseOption(true);
        $this->setParseContent(true);           
    }

    public function asHtml( JBBCode\ElementNode $el )
    {
        $ems=array(
            0.75
            ,0.95
            ,1.05
            ,1.45
            ,1.5
            ,2
            ,3
        );

        $content = "";
        foreach( $el->getChildren() as $child )
            $content .= $child->getAsBBCode();

        $attribute=abs(intval($el->getAttribute()));
        $attribute--;
        $em=isset($ems[$attribute]) ? $ems[$attribute] : $ems[2];

        return '<span style="font-size:'.$em.'em !important;">'.$content.'</span>';
    }
}

class Color extends JBBCode\CodeDefinition {

    public function __construct()
    {
        parent::__construct();
        $this->setTagName("color");
        $this->setUseOption(true);
        $this->setParseContent(true);
    }

    public function asHtml( JBBCode\ElementNode $el )
    {
        $content = "";
        foreach( $el->getChildren() as $child )
            $content .= $child->getAsBBCode();

        $color=$el->getAttribute();

        return '<span style="color:'.$color.' !important;">'.$content.'</span>';
    }
}
jbowens commented 12 years ago

In your for loops within your CodeDefinitions, try using

$child->getAsHTML(); instead of $child->getAsBBCode();

This will ensure that children of the element get parsed if they're supposed to be parsed. The getAsBBCode() method returns a bbcode representation (without parsing) no matter what value is given to the definition through setParseContent.

ghost commented 12 years ago

Hi, Thank you for reply.

It works now, Thank you very much...