Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foreground color is set to background color if sequence lists background first #17

Open
kiler129 opened this issue Jul 14, 2017 · 2 comments

Comments

@kiler129
Copy link

kiler129 commented Jul 14, 2017

I came across a weird behavior while using the library - if background color (e.g. 42 - green) is specified before foreground (e.g. 30 - black) the render will contain black text on black background. Weirdly enough this happens only for separated sequences (e.g. ^[[0m^[[42m^[[30m) and not while parsing their short forms (e.g. ^[[42;30m).

I used following example to demonstrate the problem (also attached):
Download raw

^[[30;42mWORKS1^[[0m
^[[0m^[[42m^[[30mDOESNT1^[[0m
^[[0m^[[30m^[[42mWORKS2^[[0m
^[[42;30mWORKS1^[[0m

Rendering in GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)
bash-render

HTML from ansi-to-html v1.1.3

<?php
$output = (new AnsiToHtmlConverter())->convert($test);
<span style="background-color: green; color: black">WORKS1</span><span style="background-color: black; color: white"><br />
</span><span style="background-color: black; color: black">DOESNT1</span><span style="background-color: black; color: white"><br />
</span><span style="background-color: green; color: white">WORKS2</span><span style="background-color: black; color: white"><br />
</span><span style="background-color: green; color: black">WORKS1</span><span style="background-color: black; color: white"><br />
</span>
@ellisgl
Copy link

ellisgl commented Aug 1, 2018

^[[0m is reset, so the colors should back to white-on-black, then convert at the next sequence...

@fctr
Copy link

fctr commented Feb 20, 2020

    public function convert($text)
    {
        // remove cursor movement sequences
        $text = preg_replace('#\e\[(K|s|u|2J|2K|\d+(A|B|C|D|E|F|G|J|K|S|T)|\d+;\d+(H|f))#', '', $text);
        // remove character set sequences
        $text = preg_replace('#\e(\(|\))(A|B|[0-2])#', '', $text);

        $text = htmlspecialchars($text, PHP_VERSION_ID >= 50400 ? ENT_QUOTES | ENT_SUBSTITUTE : ENT_QUOTES, $this->charset);

        // carriage return
        $text = preg_replace('#^.*\r(?!\n)#m', '', $text);

        // Add the follow line
        $text = preg_replace('/m\e\[/', ';', $text);


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants