Trying to extend the ordered list BBcode, running into errors

Nikolaos

Member
Hello,

I have made several edits to the BBcode related scripts in order to accommodate special ordered lists, such as:

I. foo
II. bar

But, I am running into trouble with the HTML interpretation.

Here is what I did in /library/XenForo/Html/Renderer/BbCode.php:, which is throwing the error:

PHP:
    protected $_handlers = array(
        'b'          => array('wrap' => '[B]%s[/B]'),
        'strong'     => array('wrap' => '[B]%s[/B]'),
        [...]
        'a'          => array('filterCallback' => array('$this', 'handleTagA')),
        'img'        => array('filterCallback' => array('$this', 'handleTagImg')),
        'ul'         => array('wrap' => "[LIST]\n%s\n[/LIST]", 'skipCss' => true),
        // Following line is edited
        'ol'         => array('filterCallback' => array('$this', 'handleTagOl')),
        'li'         => array('filterCallback' => array('$this', 'handleTagLi')),
        [...]

Here is the referenced handleTagOl:

PHP:
    public function handleTagOl($text, XenForo_Html_Tag $tag)
    {
        $listType = trim($tag->attribute('style'));
        if($listType)
        {
            if(preg_match('/^list-style-type: decimal/', $listType))
                $target = '1';
            elseif(preg_match('/^list-style-type: lower-alpha', $listType))
                $target = 'a';
            elseif(preg_match('/^list-style-type: upper-alpha', $listType))
                $target = 'A';
            elseif(preg_match('/^list-style-type: lower-roman', $listType))
                $target = 'i';
            elseif(preg_match('/^list-style-type: upper-roman', $listType))
                $target = 'I';
            return "[LIST=$target]{$text}[/LIST]";
        }
        else return "[LIST=1]{$text}[/LIST]";
    }

The error thrown (when trying to preview the post) is as follows:

The following error occurred:

Array to string conversion
  1. XenForo_Application::handlePhpError() in XenForo/Html/Renderer/BbCode.php at line 478
  2. XenForo_Html_Renderer_BbCode->handleTagOl() in XenForo/Html/Renderer/BbCode.php at line 259
  3. XenForo_Html_Renderer_BbCode->renderTag() in XenForo/Html/Renderer/BbCode.php at line 339
  4. XenForo_Html_Renderer_BbCode->renderChildren() in XenForo/Html/Renderer/BbCode.php at line 145
  5. XenForo_Html_Renderer_BbCode->renderTag() in XenForo/Html/Renderer/BbCode.php at line 120
  6. XenForo_Html_Renderer_BbCode->render() in XenForo/Html/Renderer/BbCode.php at line 92
  7. XenForo_Html_Renderer_BbCode::renderFromHtml() in XenForo/ControllerHelper/Editor.php at line 89
  8. XenForo_ControllerHelper_Editor->convertEditorHtmlToBbCode() in XenForo/ControllerPublic/Editor.php at line 46
  9. XenForo_ControllerPublic_Editor->actionToBbCode() in XenForo/FrontController.php at line 310
  10. XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 132
  11. XenForo_FrontController->run() in [removed]/forum/index.php at line 13

I don't see where it is going wrong. Could anyone enlighten me?
 
I had it print the contents of the attribute "style", and found that the problem is that the attribute itself has an attribute:

Code:
[15:37:25.654] PHP Array
(
    [style] => Array
        (
            [list-style-type] => upper-alpha
        )
 
)

I have no idea how to make the function retrieve "upper-alpha" from two layers of arrays. Could someone more experienced with PHP lend me a hand?

Thank you.
 
Hmm..
PHP:
  $listType = trim($tag->attribute('style')['list-style-type']);

or

PHP:
  $listType = trim($tag->attribute('style')['style']['list-style-type']);

?
 
Hmm..
PHP:
  $listType = trim($tag->attribute('style')['list-style-type']);

or

PHP:
  $listType = trim($tag->attribute('style')['style']['list-style-type']);

?
Hm, I'm afraid that neither of those work. They don't throw a forum error, but the preview simply doesn't load. Thanks for giving it a shot, though.

The class XenForo_Html_Tag is defined in /library/XenForo/Html/Tag.php. Does anyone know how to create a quick and dirty method to return the value of a nested array? To be frank, I wouldn't know how to do it even if the variables weren't protected - I simply haven't done this before.
 
Solved. I did this:

PHP:
    public function handleTagOl($text, XenForo_Html_Tag $tag)
    {
        $listStyle = $tag->attribute('style');

        if($listStyle)
        {
            if(preg_match('/^decimal/', $listStyle['list-style-type']))
                $target = '1';
            elseif(preg_match('/^lower-alpha/', $listStyle['list-style-type']))
                $target = 'a';
            elseif(preg_match('/^upper-alpha/', $listStyle['list-style-type']))
                $target = 'A';
            elseif(preg_match('/^lower-roman/', $listStyle['list-style-type']))
                $target = 'i';
            elseif(preg_match('/^upper-roman/', $listStyle['list-style-type']))
                $target = 'I';
            else $target = '1';
            return "[LIST=$target]{$text}[/LIST]";
        }
        else return "[LIST=1]{$text}[/LIST]";

    }
 
I hate to post in this thread again, for fear that it will be ignored, but I think that it would be profitable for any others who should try to do this in future if I post here.

Now, with various template edits, ending with the one above, I have managed to get the HTML to publish correctly. But, there is one final problem - the <li> element has a predefined list-style-type in the CSS:

Code:
.baseHtml ol li
    { list-style: decimal outside; }

Now, my question is as follows - what purpose does this rule serve in the big picture? What would be damaged if I simply erased it?

Thank you!
 
The above post was a bad idea. I replaced "decimal outside" with "inherit" instead of removing it.

The change works completely now. Anyone who wants to know how I did it can feel free to ask me for a log of all the final changes.
 
Top Bottom