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

"Preformatted" block autocompleting type declarations in pasted code #14092

Closed
koskila opened this issue Feb 25, 2019 · 17 comments
Closed

"Preformatted" block autocompleting type declarations in pasted code #14092

koskila opened this issue Feb 25, 2019 · 17 comments
Labels
Needs Testing Needs further testing to be confirmed. [Type] Bug An existing feature does not function as intended

Comments

@koskila
Copy link

koskila commented Feb 25, 2019

Describe the bug
"Preformatted" block is "autocompleting" or "autoclosing" type declarations in pasted code, because it interprets them as html tags.

To Reproduce
Steps to reproduce the behavior:

  1. Edit page
  2. Add Preformatted block
  3. Paste code, such as "DbContextOptions options"
  4. Modify the code as html
  5. Save
  6. Observe the "preformatted" block now having at the end.

Expected behavior
Type declarations should not be autoclosed - they are independent, and not html.

Screenshots
image
(Example with being closed at the end of the file.

Desktop (please complete the following information):

  • OS: [e.g. iOS]
  • Browser: Chrome Version 72.0.3626.109 (Official Build) (64-bit), but I suspect this might be partially caused by mobile usage as that's when I usually see this
  • WordPress 5.1

Smartphone (please complete the following information):

  • Device: Galaxy S9+
  • OS: Android 9 (4.9.112-15119402)
  • Browser Chrome
  • Version 72.0.3626.96

Let me know if you require any more information!

@talldan talldan added the [Type] Bug An existing feature does not function as intended label Mar 13, 2019
@talldan
Copy link
Contributor

talldan commented Mar 13, 2019

Hi @koskila, I had a quick test of this but wasn't able to reproduce the issue. Would be able to provide the snippet of code you're using to trigger the issue? Thanks.

I also wanted to check, when you mentioned 'Modify the code as html', were you using the option 'Edit as HTML' in the block menu? I was also wondering why you'd need to do that?

@talldan talldan added Needs Testing Needs further testing to be confirmed. [Block] Preformatted Affects the Preformatted Block - used for showing preformatted text labels Mar 13, 2019
@koskila
Copy link
Author

koskila commented Mar 19, 2019

@talldan - I need to edit blocks as html to add a parameter lang="" for the preformatted blocks, as there's no out-of-the-box block providing syntax highlighting. Modifying the blocks selecting "Edit as HTML" brings this issue up - not always, but often:
image

Removing the "closing tags" from the end sometimes is saved, sometimes not - I have no idea why it's not consistent...

@koskila
Copy link
Author

koskila commented Mar 19, 2019

Okay, so I tried again. Adding this to a block using "Edit as HTML":

<pre lang="csharp">
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
</pre>

Produces this:
image

And if I then refresh the editor, this is what the block has changed into:

<pre lang="csharp">services.AddSingleton<ihttpcontextaccessor ,="" httpcontextaccessor="">();
</ihttpcontextaccessor></pre>

@koskila
Copy link
Author

koskila commented Mar 22, 2019

Alright, the reason is found: Gutenberg and WP-GeSHi-Highlight have some pretty bad compatibility issues it seems. Using the classic editor works, but also after disabling the highlighting plugin Gutenberg doesn't mess up.

I just wish it was easier to find a well-working code highlighting plugin on WordPress... :) I'll resolve the case, since I don't think this should be fixed on Gutenberg's end.

@koskila koskila closed this as completed Mar 22, 2019
@koskila koskila reopened this Mar 22, 2019
@koskila
Copy link
Author

koskila commented Mar 22, 2019

Actually, scratch that. After opening the page in edit mode in another browser with WP-GeSHi-Highlight disabled, the issue is there again. So it's probably not that plugin, but rather Gutenberg.

Sorry for the back-and-forth. What an infuriating case. If you want any more info, let me know - would love to see this fixed one way or another.

image

@ellatrix
Copy link
Member

ellatrix commented Apr 4, 2019

@koskila The preformatted block is not a code editor. You should use the Code block instead. In the future, the code block have an option to set the language and have syntax highlighting.

@ellatrix ellatrix closed this as completed Apr 4, 2019
@koskila
Copy link
Author

koskila commented Apr 4, 2019

Thanks for the comments - it's not the preformatted block though, it's the custom html block (like shown in the screenshots). Even a way to disable the autocomplete (for custom html blocks) from messing up would be a perfectly valid solution.

@ellatrix
Copy link
Member

ellatrix commented Apr 4, 2019

Sorry, perhaps I'm having a hard time understanding the issue.

@ellatrix ellatrix reopened this Apr 4, 2019
@ellatrix ellatrix removed the [Block] Preformatted Affects the Preformatted Block - used for showing preformatted text label Apr 4, 2019
@koskila
Copy link
Author

koskila commented Apr 5, 2019

No worries - so am I! But my understanding is, that whenever I want to have something like this in a custom HTML block:

<pre>
var list = new List<string>();
</pre>

It'll get turned to this:

<pre>
var list = new List<string></string>();
</pre>

Or occasionally this:

<pre>
var list = new List<string>();
</string>
</pre>

And it gets even weirder with Dictionaries and such:

<pre>
var dict = new Dictionary<string,Object>();
</pre>

Will become:

<pre>
var dict = new Dictionary<string ='',object =''>();
</string></object>
</pre>

(Note: even casing changes!)

@koskila
Copy link
Author

koskila commented Apr 5, 2019

Oh, and as a side note - really looking forward to the changes to code-block you mentioned! Syntax highlighting is a very welcome new feature. Much appreciated :)

@talldan
Copy link
Contributor

talldan commented Apr 10, 2019

@koskila Right, I understand now. The issue is that the Custom HTML block does some HTML validation and automatic fixing when it sees invalid html. When it sees <string> it considers it an unterminated html tag and so adds a closing tag. It'll also change the casing as you mentioned.

The solution (if you want to use the Custom HTML block instead of the Code block) is to escape the code you're trying to add first. Basically turn this:

<pre>
var list = new List<string>();
</pre>

into this:

<pre>
var list = new List&lt;string&gt;();
</pre>

There are some online tools you can use to escape content, like this one: https://www.freeformatter.com/html-escape.html

This tool might also be useful - http://hilite.me/ - it converts code into syntax highlighted html snippet that you can insert into your post (and does the escaping as part of that).

I would just double-check to make sure there are no malicious scripts or anything like that are being added to the HTML! (not sure, but Gutenberg might strip those out anyway).

I'll close the issue now as I think there are some good solutions, and hopefully at some point syntax highlighting will appear in the code block.

@talldan talldan closed this as completed Apr 10, 2019
@koskila
Copy link
Author

koskila commented May 5, 2019

@talldan, I appreciate the response, but encoding the html does not help. See - this:
image

Becomes this:
image

I'd love to see the syntax highlighting appear, as this renders Gutenberg really difficult to use for sharing code snippets. I could upload everything to GitHub as Gists and then embed them, but that's a really overengineered problem of pasting example code - sometimes just one line - to a blog entry.

I'd like for the issue to be reopened as there's no good workaround (apart from not using Gutenberg but embedding the scripts instead) available, please.

@talldan
Copy link
Contributor

talldan commented May 6, 2019

@koskila - please could you provide reproduction steps and let us know which version you're using? I'm unable to reproduce the issue. Here's an example of what I see with the custom HTML block:

Editor - Custom HTML block content
Screen Shot 2019-05-06 at 1 00 06 pm

Editor - Custom HTML block preview
Screen Shot 2019-05-06 at 1 00 13 pm

Post preview
Screen Shot 2019-05-06 at 1 00 20 pm

@talldan
Copy link
Contributor

talldan commented May 6, 2019

As for reopening—if we can find a bug with the Custom HTML block, then I'm happy to open this as a bug. At the moment I'm unable to reproduce an issue. We'll need some reproduction steps in the latest version of Gutenberg (it could be that there was an issue in an older version of the editor that's since been fixed).

There's already a separate open issue for syntax highlighting in the code block that you can follow - #10423

There isn't a designated developer for the issue yet, but there's already been some discussion and a few proof of concepts.

@koskila
Copy link
Author

koskila commented May 8, 2019

Thanks for the reference to #10423, I subscribed to the notifications as I'd really love to see this functionality.

An example where this issue happens:
image
image

That html produces this after publishing:
image

The article is here, but I'll need to revert the changes as the output is confusing to readers:
https://www.koskila.net/help-my-asp-net-mvc-textbox-date-format-is-wrong/

@koskila
Copy link
Author

koskila commented May 8, 2019

I tested replacing all of the characters as well:
image
image

And the output:
image

I'm running 5.1.1, as I push the updates to the site during weekends when there's less traffic.

@talldan
Copy link
Contributor

talldan commented May 9, 2019

I still couldn't reproduce this using the same text in your post. I think there are two possibilities:

  • This is a bug that's since been fixed (WordPress 5.2 was released yesterday, so there's a chance that might resolve the matter) 🤞
  • The theme or a plugin are causing an issue

If it can be isolated to a theme or plugin issue, it would be worth following up with the developer. I'm happy to test with the theme you're using if it's freely available.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Testing Needs further testing to be confirmed. [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests

3 participants