-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Codeblock upcast converter prevents from loading markers inside codeblock #9402
Comments
I did some quick checks in regards to this issue as it becomes PITA for me. For tl;dr skip to the bolded sentence at the end. Below, I describe two ideas, from which the final idea emerges. I leave the original ideas for entertainment and learning purposes, and also because this whole subject needs a little more research and manual tests (and coding), so I think it is good to have the whole thought process written down. The following snippet enables "back" the default text conversion (and conversion of other children, such as markers): This fixes my problems (i.e. markers now can be set in the middle of the code block) and also makes the code simpler. Setting the following data works correctly (there are no attributes on text): editor.setData( '<pre><code class="language-plaintext">Foo<strong>bar</strong>baz<a href="#">xyz</a></code></pre>' ); However, this scenario from tests fail: editor.setData( '<pre><code><p>Foo</p>\n<p>Bar</p></code></pre>' ); Because paragraphs break First idea is to try converting item-by-item in a different context (where everything's allowed) and then get text data and markers from it. Or, even better, get all the items that are allowed in the const holder = conversionApi.writer.createElement( '$clipboardHolder' );
conversionApi.convertChildren( viewChild, holder );
Array.from( conversionApi.writer.createRangeIn( holder ).getItems() ).forEach( item => {
if ( item.is( '$textProxy' ) || item.is( 'element', '$marker' ) ) {
writer.insert( item, codeBlock, 'end' );
}
} ); With that snippet, the following worked as expected: editor.setData( '<pre><code class="language-plaintext"><p>A<suggestion-start name="deletion:e7fed60586612bd21533e2cb5b8bcd4c8:u1"></suggestion-start>B</p>C<suggestion-end name="deletion:e7fed60586612bd21533e2cb5b8bcd4c8:u1"></suggestion-end>D</code></pre>' ) Three more notes here. First, as I wrote, using schema might be more future-proof. Second, if there is something like: <pre><code><allowed-in-code>Foo</allowed-in-code>Bar</code></pre> It won't work correctly ( Third, there might be actually attributes on text which are allowed everywhere, even in code block (real case). So some smart attributes stripping would be needed. The other idea was to set But then I decided to get the best of both worlds. Instead of using So, we still have a proper conversion, based on default converters and on schema. All elements and attributes that are not allowed should be removed. Most importantly, allowed elements and attributes (on text too) should be left. The final solution at this point is:
Of course, this will not work correctly in such edge case:
If Last two notes:
|
Fix (code-block): Markers created in/on code block element are now preserved after the document is loaded. Closes #9402.
📝 Provide detailed reproduction steps (if any)
editor.setData( editor.getData() )
).✔️ Expected result
Markers are retained.
❌ Actual result
Markers are gone.
📃 Other details
Additionally, it is impossible to upcast any "general" attributes that are set on
<code>
element /cc @Mgsy.Both problem happen because on upcast, converter for
<pre>
takes care of everything and it consumes<code>
element and does not fire conversion for its children. It is different than in image, where there is a converter for<figure>
but it fires conversion for<img>
and<img>
itself has its own converter.Additionally, the code block content is also converted in a custom way, to remove HTML tags. This is another blocker that prevents proper marker upcasting.
AFAIU (not tested), HTML coming from outside of the editor, which does not have
<pre>
will not be properly upcasted as well.The solution here is to:
<code>
that will mostly do what converter for<pre>
is doing at the moment. Converter for<pre>
should actually do almost nothing - see converter for<figure>
in image.Schema
. Actually, AFAICS, this is already done like that so I don't know why we have additional cleaning in the upcast converter. Maybe it is not needed?The text was updated successfully, but these errors were encountered: