jbowens / jBBCode

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

html code cant see in [code] bbcode #66

Closed morph192 closed 8 years ago

morph192 commented 8 years ago

Hello guys,

I used same code like from site :

$message = $_POST['message'];

//bbcode
$parser = new JBBCode\Parser();
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());

$builder = new JBBCode\CodeDefinitionBuilder('img', '<img style="max-width:700px;" src="{param}">');
$parser->addCodeDefinition($builder->build());
$builder = new JBBCode\CodeDefinitionBuilder('code', '<pre>{param}</pre>');
$builder->setParseContent(false);
$parser->addCodeDefinition($builder->build());

$parser->parse($message);
$mbb = $parser->getAsHtml();`

And when i insert some text in table i cant see HTML code.. Something like :

Result is only:Lorem..

Any help?

morph192 commented 8 years ago

Any help here?

shmax commented 8 years ago

What do you mean "insert some text in table"? Can you post a full sample?

morph192 commented 8 years ago

Sure , i use simple form like here for comments with this bbcode plugin... When someone try to add

[code]
 <div class="navigation">
  <li><a href="#">Test</a></li>
 </div>
[/code]

There is result only test , but cant see the full html for this bbcode :)

Sorry for my bad english

shmax commented 8 years ago

Ah, I see. Your problem in this case is the <pre> tag. All that does is specify a fixed-width font; if you want any html inside it to render as text, then you need to escape it. In PHP, you can do this with htmlentities. I don't see a default tag handler for [code], so you'll have to make one yourself:

class Code extends \JBBCode\CodeDefinition {
    public function __construct($useOption){
        parent::__construct($useOption);
        $this->setTagName("code");
        $this->setParseContent(false);
    }

    public function asHtml(\JBBCode\ElementNode $el){
        $content = $this->getContent($el);
        return "<pre>".htmlentities($content)."</pre>";
    }
}
$str = '[code]<div class="navigation"><li><a href="#">Test</a></li></div>[/code]';
$parser = new \JBBCode\Parser();
$parser->addCodeDefinitionSet(new \JBBCode\DefaultCodeDefinitionSet());
$parser->addCodeDefinition(new Code(false));

$parser->parse($str);
$mbb = $parser->getAsHtml();
echo $mbb;
morph192 commented 8 years ago

Hello mate, thanks for answer but still dont work :)

I use now:

//bbcode
$parser = new JBBCode\Parser();
$parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());

$builder = new JBBCode\CodeDefinitionBuilder('img', '<img style="max-width:700px;" src="{param}">');
$parser->addCodeDefinition($builder->build());
$parser = new \JBBCode\Parser();
$parser->addCodeDefinitionSet(new \JBBCode\DefaultCodeDefinitionSet());
$parser->addCodeDefinition(new Code(false));

$parser->parse($message);
$mbb = $parser->getAsHtml();

And still dont work and i added this function up.. It remove me like:

[code]
 <div class="nav">
  <li><a href="#">Lorem</a></li>
</div>
[/code]

It show only lorem now dont have and [code][/code] tags...

Sorry for my bad english

I just want to replace html tags in [code][/code] bbcode :)

shmax commented 8 years ago

Sorry, my mistake--forgot to include the <pre> tag. I've fixed the sample in my original comment.

morph192 commented 8 years ago

Hello , sorry for to many replies but this still dont work:

Now my code is like:

$message = $_POST['message'];

//bbcode

class Code extends \JBBCode\CodeDefinition {
    public function __construct($useOption){
        parent::__construct($useOption);
        $this->setTagName("code");
        $this->setParseContent(false);
    }

    public function asHtml(\JBBCode\ElementNode $el){
        $content = $this->getContent($el);
        return "<pre>".htmlentities($content)."</pre>";
    }
}

$parser = new \JBBCode\Parser();
$parser->addCodeDefinitionSet(new \JBBCode\DefaultCodeDefinitionSet());
$parser->addCodeDefinition(new Code(false));

$parser->parse($message);
$mbb = $parser->getAsHtml();

and still dont work what i need the mysql results is like:

<pre>
 Test
</pre>

From:

`[code]
<div class="nav">
 <li><a href="#">Test</a></li>
</div>
[/code]`

Any solution to solve this problem?

shmax commented 8 years ago

I just tried it on my machine and it works fine:

image

Maybe I don't understand what you're expecting. You say you are expecting

<pre>
 Test
</pre>

From

`[code]
<div class="nav">
 <li><a href="#">Test</a></li>
</div>
[/code]`

but that doesn't quite make sense. If the expected response you posted is meant to be what is visible at render time, then it's wrong because you wouldn't actually see <pre> rendered. If the expected response you posted is meant to represent the literal string output of the parser, then it is wrong as well, because it's missing all the markup.

Here is the literal string output from the parser when I run it:

<pre> &lt;div class=&quot;nav&quot;&gt;  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Test&lt;/a&gt;&lt;/li&gt; &lt;/div&gt; </pre>

I've already posted the rendered output. Do either of these match what you expect?

morph192 commented 8 years ago

I just need the full code from [code][/code] not just a part.. So if user want:

[code]
 <div class="nav">
   <li> <a href="#"> Test </a> </li>
 </div>
[/code]

The result in my database need to be :

<pre>
 <div class="nav">
   <li> <a href="#"> Test </a> </li>
 </div>
</pre>

I tryed all what u write but it dont want to work :/

shmax commented 8 years ago

oh, well first of all, you probably shouldn't store the parsed version in your database. Just store it with the bbcode tags intact, and parse them at render time. This way, if the functionality for those tags is ever modified or improved, you will always see the intended result when you render them.

Even if you DID want to store the parsed output in your database, again, you would still need to escape the html characters.

So store this in your database:

[code]
 <div class="nav">
   <li> <a href="#"> Test </a> </li>
 </div>
[/code]

and at render time, run it through your parser, which will send this to the browser:

<pre> &lt;div class=&quot;nav&quot;&gt;  &lt;li&gt;&lt;a href=&quot;#&quot;&gt;Test&lt;/a&gt;&lt;/li&gt; &lt;/div&gt; </pre>

and that will render like this:

image

shmax commented 8 years ago

@jbowens I think you can close this issue. We haven't heard from @morph192 again, and I don't think there's anything else to say on the subject.