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

Attribute Listener messes up array insert #92

Open
Jafa80 opened this issue Sep 6, 2024 · 2 comments
Open

Attribute Listener messes up array insert #92

Jafa80 opened this issue Sep 6, 2024 · 2 comments

Comments

@Jafa80
Copy link

Jafa80 commented Sep 6, 2024

Describe the bug
Wrapping custom Blot using json value to bold renders the json as string. This does not happen when not wrapped in bold.

The delta code which generates the Problem

Example:

{"ops": [{"insert": "test ", "attributes": {"bold": true}}, {"insert": {"mention": {"value": "custom-value"}}, "attributes": {"bold": true}}, {"insert": " test", "attributes": {"bold": true}}]}

The expected html output the delta should produce

Example:

<p><strong>test </strong><strong><mention>custom value</mention></strong><strong> test</strong></p>

Expected behavior
Bold attribute listener should not mess up the value. jsonInsert needs to process before the attributes

Additional context
To fix the problem I did following:

  1. overwrite bold attribute listener to prevent jsonInsert processing
class CustomBold extends InlineListener
{
    /**
     * {@inheritDoc}
     */
    public function process(Line $line)
    {
        if ($line->getAttribute('bold') && !$line->isJsonInsert()) {
            $this->updateInput($line, '<strong>'.$line->getUnsafeInput().'</strong>');
        }
    }
}

$lexer->overwriteListener(new Bold(), new CustomBold());
  1. add the attribute processor to custom Listener processor
public function process(Line $line)
    {
        $mention = $line->insertJsonKey('mention');
        if ($contractNumber) {
            $this->updateInput($line, '<span>'.$mention['value'].'</span>');
        }
        if ($line->getAttribute('bold')) {
            $this->updateInput($line, '<strong>'.$line->getInput().'</strong>');
        }
    }
@nadar
Copy link
Owner

nadar commented Sep 6, 2024

Would you like to provide a PR to fix this issue? Would be nice 👍

@Jafa80
Copy link
Author

Jafa80 commented Sep 6, 2024

Would you like to provide a PR to fix this issue? Would be nice 👍

This is my first interaction with the library, so it will take me a bit time to figure out the solution, to fix the root cause.
I would assume that there needs to be a prioritization on JsonInsert listeners being processed before attribute listeners as it works perfectly fine with my workaround. I will tinker more and see if I can wrap my head around processor behavior.

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

2 participants