MyBB Community Forums

Full Version: MyCode parse error ( [font="droid sans", sans-serif] )
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is what gets displayed when you select and copy text from mybb and paste it in new thread / reply like I've just done


i.e
[font="droid sans", sans-serif]

This gets parsed just fine in Sceditor but appears broken in showthread

To replicate,
Select and copy text from any comment, paste it in the reply (not quick reply) then post
Which version of MyBB and theme/template are you using? And what was the web browser / OS when you achieved that?
Might be mistaken, but this looks like the issue I posted about back in April.  And can be duplicated on this board:

And can be duplicated on this board

Link to original post:

https://community.mybb.com/thread-223638...pid1346298

cheers...
(2020-09-27, 11:16 AM)nixer55 Wrote: [ -> ]Might be mistaken, but this looks like the issue I posted about back in April.

It definitely seems related, although the issue you posted about seems broader: SCEditor adding extraneous tags, particularly "font" but also "color" and "size".

The more particular issue of this thread is that sometimes those added "font" tags include a comma-separated list of fonts, including at least one font enclosed in double-quote marks. The MyBB parser doesn't handle that situation (the double-quotes within - rather than simply enclosing - the comma-separated list are the problem), and thus it doesn't render the tag but instead displays it literally, making the post less readable.

I've put together a fix in the form of a very simple plugin which replaces those double-quotes with single-quotes prior to MyBB parsing font tags.

If anybody wants to test it, feel free to download the attached file to your inc/plugins directory and then activate the Fix Unrendered Font Tags plugin via the ACP.
Thanks @ Laird for the plugin
I'd already created a temporary fix using Mycode (Admin > Configuration > Mycode)

Title: Font Fix
Description: Fix Sceditor - Mycode issue
Regular Expression: \[font="droid sans", sans-serif\](.*?)\[/font\]
Replacement: <span style="font-family: droid sans, sans-serif;" class="mycode_font">$1</span>

This might be custom to my forum (font style)
You're welcome. And I've just updated the file in my above post to version 1.0.1 to handle nested font tags.

Yes, your regex looks like it will work for your custom forum and font style, however, it might not catch the scenario where somebody copy-n-pastes text from an external site into SCEditor, in which case different fonts - some enclosed in double-quotes - might be listed in the font tag generated by SCEditor. It also, as best I can tell, would not - as initially the case with my plugin - handle nested font tags which list those fonts specific to your forum.
It looks like different copy/paste behaviors between desktop browsers and mobile browsers, Google Chrome especially.
The addition of tags should be just disabled imo.
I agree to an extent, Omar, but that only prevents the problem of extraneous tags occurring in posts from that point onwards - it doesn't have any effect on those posts which have already been posted, so there's still a use case for my plugin, and perhaps even for incorporating its code into the core.

I say "to an extent" because sometimes when rich-format text is pasted into the editor, it is necessary that font, size, and/or color tags be added so that the formatting is preserved. In this case, we should simply ensure that if the added font tag contains a list of fonts, any individual fonts in that list are not enclosed in double quotes but rather in single quotes.

I'm not sure that I'll have any success but I'll see if I can work out why SCEditor is adding extraneous tags and how to stop it - noyle seems to have given us a good clue - as well as how to ensure that it uses single quotes instead of double quotes in the list of fonts in those font tags that can't be eliminated.

OK, I've done a bunch of debugging and testing, and here are the results. Noyle, you're probably way ahead of me on this, so please let me know how this compares to your own findings.

I think that there are three distinct - though related - problems raised in this thread and the one to which nixer55 linked above:
  1. The 'redundant tags on paste of unformatted text' bug.

    Description: In WYSIWYG mode, on pasting unformatted text into the editor which was cut/copied from elsewhere within the editor, some browsers (e.g., Chrome and Microsoft Edge) generate redundant font+size+color attributes in the paste data whose values reflect the editor's default style (based on the default stylesheet jscripts/sceditor/styles/jquery.sceditor.mybb.css). Other browsers (e.g., Firefox) don't, and are not subject to this bug. This confirms noyle's observation/finding above. It is also the bug to which Omar's suggestion above applies: let's disable the addition of these tags. And it is the first aspect of the bug identified in the post to which nixer55 linked above.

    How to reproduce: In Chrome or Microsoft Edge, bring up a new editor window through a page such as a New Reply page. Deselect Source mode if it is selected, such that the editor is in WYSIWYG mode. Now, type some text into it, such as "Blah". Do not style or format that text in any way. Now, select it all, cut it, then immediately paste it, and switch to Source mode. You will see that it has had font, color, and size MyCode tags added to it even though it was unformatted and unstyled prior to being pasted, and thus should not have had any MyCode tags applied to it.

    Impact: Mild-moderate. Not detectable in WYSIWYG mode (except in the form of bug #2 below); irritating when switching to Source mode.

  2. The 'reduced size on paste of unformatted text' bug.

    Description: As above, but with the added observation that under those same prone browsers (e.g., Chrome and Microsoft Edge), when the unformatted text cut/copied from elsewhere in the editor in WYSIWYG mode is pasted back into it, its default size of 14px (redundantly generated in the pasted data by these browsers) as styled by the default stylesheet (jscripts/sceditor/styles/jquery.sceditor.mybb.css) does not match any of the sizes supported by MyBB's customisation of SCEditor, such that the editor picks the "best fit" out of those sizes, which is in fact smaller than 14px. Again, other browsers (e.g., Firefox) aren't affected, again confirming noyle's observation/finding above. This is the second aspect of the bug identified in the post to which nixer55 linked above.

    How to reproduce: As above, but note that before switching to Source mode, the pasted text "Blah" is smaller than it was when it was cut. Also note that after switching to Source mode, the default attribute of the size tag is "small", rather than the size specified by default stylesheet, "14px" (and thus infer that "small" and "14px" are different font sizes).

    Impact: Mild-moderate. It is annoying in WYSIWYG mode to have to re-select such pasted text and click the "Remove Formatting" button to get it back to its original (unformatted) size.

  3. The 'double quotes in font lists' bug.

    Description: Sometimes, in scenarios the criteria of which I haven't yet rigorously identified, when pasting text into the WYSIWYG editor, its specified font is actually a comma-separated list of two or more fonts, and at least one of the fonts in that comma-separated list is enclosed in double quotes. When the post is posted, the associated MyBB font tag(s) is/are not rendered by MyBB's PHP parser, because that parser doesn't support double-quotes within the comma-separated list of fonts of a MyCode font tag. (This is the bug which my plugin above addresses for posts which have already been posted).

    How to reproduce: In any browser, bring up a new editor window through a page such as a New Reply page. Select Source mode. Paste the following MyCode into the editor, after first replacing the curly braces with square braces (since I am posting this on a MyBB forum, I can't paste valid MyCode tags as they would then be rendered):

    {font=Test, "Test two"}Blah{/font}

    Switch out of Source mode and then back into it. Confirm that the code has not changed. Then submit or preview the post. Notice that in the rendered post, the raw font tag shows up unrendered.

    Impact: Moderate-severe. There can at times be so many of these unrendered font tags that the post once posted is effectively unreadable.

A working solution and a potential but flawed solution:

Let's start with bug #3, the 'double quotes in font lists' bug, because its solution is simplest with fewest potential side effects. Borrowing from and porting into Javascript the PHP code of my plugin above, we can simply replace in the file jscript/bbcodes_sceditor.js the following code from within the "format" method relating to the "font" bbcode:

				return '[font=' + this.stripQuotes(font) + ']' + content + '[/font]';

with this:

				if (font[0] == '"' && font[font.length-1] == '"' && (font.match(/"/g) || []).length == 2) {
					font = font.substr(1, font.length-2);
				}
				font = font.replace(/"/g, "'");
				return '[font=' + font + ']' + content + '[/font]';

Now, when text is pasted into the editor in WYSIWYG mode, or when switching into Source mode, double quotes within lists of fonts are replaced with single quotes. Problem solved.



Bugs #1 and #2 are closely related in that they stem from browsers redundantly setting values for style attributes in pasted text that are already at their default values. The "obvious" fix is to again edit appropriate sections of jscript/bbcodes_sceditor.js such that default values of those size/font/color attributes are detected and ignored, and so that no tag is thus added in those scenarios. I have tested this approach - for size, font, and color tags - and it does work on a default MyBB installation, resolving both bugs. The problem, however, is that it involves hard-coding those default style values into this Javascript file based on those in the default stylesheet, jscripts/sceditor/styles/jquery.sceditor.mybb.css, whereas, as Hovatek's scenario above demonstrates, those default stylesheet values might be changed (customised) by themes. That is to say that, for example, the default font-family specified in jscripts/sceditor/styles/jquery.sceditor.mybb.css is 'Tahoma, Verdana, Arial, Sans-Serif', whereas for Hovatek's forum it appears instead to have been customised to '"droid sans", sans-serif'. Detecting and ignoring in Javascript the default font-family for the editor as specified in jscripts/sceditor/styles/jquery.sceditor.mybb.css would thus fail on Hovatek's forum, because his theme has overridden that default font-family.

So, I wonder whether anybody has any good suggestions for how to more generally resolve bugs #1 and #2?
I have same issue with mybb 1.8.24.
Need to be fix.