thunderer/Shortcode

Bug with shortcodes with long text replace

Jonatanmdez opened this issue · 4 comments

Hi,

I'm having a problem with this amazing library. I use the shortcodes for really big replaces (Tables, imgs, etc).

The code: http://pastebin.com/NGWtVa4x (I couldn't insert in github with format)

The output: http://pastebin.com/27dQxrcL

As you can see, only works fine in the first shortcode. The second only first line. And , if you append more shortcodes, ignore them.

What's going on? I'm using dev-master version in composer.

Hi @Jonatanmdez, thanks for the kind words and reporting this issue. I was able to reproduce it in the much shorter code:

<?php
require_once __DIR__ . '/vendor/autoload.php';

use Thunder\Shortcode\HandlerContainer\HandlerContainer;
use Thunder\Shortcode\Parser\RegularParser;
use Thunder\Shortcode\Processor\Processor;
use Thunder\Shortcode\Shortcode\ShortcodeInterface;

$handlers = new HandlerContainer();
foreach(['keyword-ranking-evolution','keyword-ranking-status'] as $shortcode){
    $handlers->add($shortcode, function(ShortcodeInterface $s){
        return 'Inicio: Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
    });
}

$processor = new Processor(new RegularParser(), $handlers);
echo $processor->process('[keyword-ranking-status] [keyword-ranking-status]');

This bug is related to shortcode replacement logic - if the length of the replacements is greater than the length of the input string then replacements are applied only up to the input string's length. I fixed this bug by recalculating text length after each replacement. You can see it in the latest master commit d1bfba4 . I'll tag v0.6.1 after Travis will confirm that master is green.

Wow!!Very fast!

Now is working perfect. Thanks !

@Jonatanmdez master is green, v0.6.1 was tagged. Could you please check if it fixes your problem and close this issue?

Woah, you're really fast too! 😄