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

Fix error state when setting existing language after non-existing #1

Closed
wants to merge 1 commit into from
Closed

Fix error state when setting existing language after non-existing #1

wants to merge 1 commit into from

Conversation

mermshaus
Copy link
Contributor

See GeSHi::set_language for reference.

$geshi->set_language('php');
    // $this->loaded_language is "...php.php"
$geshi->set_language('does-not-exist');
    // $this->loaded_language is still "...php.php"
    // $this->error is set to GESHI_ERROR_NO_SUCH_LANG
$geshi->set_language('php');
    // $this->loaded_language is still "...php.php"
    // and $this->error won't be reset to false because the line is below
    // if ($file_name == $this->loaded_language) { return; }

Because of the error state, no syntax highlighting for 'php' will take place until you do a hard reset via $geshi->set_language('php', true); or via $geshi->error = false; or until you load a different existing language.

GeSHi::parse_code:

        // Firstly, if there is an error, we won't highlight
        if ($this->error) {
            // ...
            return $result;
        }

I don’t suppose this is intentional.

This request contains one quick idea to fix this. It might not be a good one. For instance, I did not check the implications of moving $this->strict_mode = GESHI_NEVER;.

In any case, don’t feel obliged to merge. This is meant as a heads up.

Best

@BenBE
Copy link
Contributor

BenBE commented Nov 19, 2013

Hmmm, I'm not sure yet if the behaviour really is broken here for two reasons:

  1. It's kinda intensional as an optimization that it doesn't realod a language if this one is already loaded
  2. If there's an error when adding a language (without the second parameter true) than doing so from an error state still is an error.

But yet I see your point. Will think about it.

@mermshaus
Copy link
Contributor Author

mermshaus commented Nov 19, 2013

Here’s a complete example. The output for example 3 demonstrates the issue.

<?php

include __DIR__ . '/src/geshi.php';

$geshi = new GeSHi();

$geshi->set_language('php');
$geshi->set_source('<?php echo "Hello world!"');
echo '#1 ' . substr($geshi->parse_code(), 0, 45) . "\n";

$geshi->set_language('does-not-exist');
$geshi->set_source('Hello world!');
echo '#2 ' . substr($geshi->parse_code(), 0, 45) . "\n";

$geshi->set_language('php');
$geshi->set_source('<?php echo "Hello world!"');
echo '#3 ' . substr($geshi->parse_code(), 0, 45) . "\n";

$geshi->set_language('php', true); // <-- note the true
$geshi->set_source('<?php echo "Hello world!"');
echo '#4 ' . substr($geshi->parse_code(), 0, 45) . "\n";

// Output:
// #1 <pre class="php" style="font-family:monospace
// #2 <pre class="does-not-exist" style="font-famil
// #3 <pre class="does-not-exist" style="font-famil
// #4 <pre class="php" style="font-family:monospace

There are some inconsistencies in the handling of the involved state variables.

@cweiske
Copy link
Member

cweiske commented Mar 14, 2017

It's in master now, given that it at least improves the current situation - even if it's not perfect.

cweiske pushed a commit that referenced this pull request Mar 30, 2017
cweiske pushed a commit that referenced this pull request May 4, 2017
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

Successfully merging this pull request may close these issues.

None yet

3 participants