jbowens / jBBCode

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

Parsing table newlines issue #69

Open pedjas opened 8 years ago

pedjas commented 8 years ago

I have set definitions to parse BBC for table as:

    //table
    $builder = new \JBBCode\CodeDefinitionBuilder('table', '<table><tbody>{param}</tbody></table>');
    array_push($this->definitions, $builder->build());

    //thead
    $builder = new \JBBCode\CodeDefinitionBuilder('th', '<th>{param}</th>');
    array_push($this->definitions, $builder->build());

    //tr
    $builder = new \JBBCode\CodeDefinitionBuilder('tr', '<tr>{param}</tr>');
    array_push($this->definitions, $builder->build());

    //td
    $builder = new \JBBCode\CodeDefinitionBuilder('td', '<td>{param}</td>');
    array_push($this->definitions, $builder->build());

It forks fine except it requires all BBC in a table to be in single line. If any part of table is split to new line, empty line will be inserted on top of rendered TABLE.

When dealing with tables it is necessary to split table to several lines and even make indentation for elements to make it readable.

Is there a way to clean all new lines and spaces outside of TR and TD tags within a TABLE?

DaSourcerer commented 8 years ago

Well, I guess you could implement a NodeVisitor that is removing all leading and trailing whitespaces via trim() in the TextNodes of your table. In general it is not a good idea to implement tables in BBCode, though.

pedjas commented 8 years ago

Well it is necessity. The same issue is with lists or other HTML tags that have inner structure.

Thing is trim should not be executed always. IT should be optional. I guess good solution would be to provide events so we can add our own code to handle such situations for BBC that needs it.

Art4 commented 8 years ago

For complex tags like [list] or [table] I would recommend to create a own CodeDefinition instead of using CodeDefinitionBuilder.

Here you can see my implementation for [list]: https://github.com/youthweb/bbcode-parser/blob/master/src/Definition/ListDefinition.php

Hope this will help you.