I'm not really sure which component this fits into so please move if I have this in the wrong place.

Add media and upload an image it adds this kind of token:

[[{"fid":"140","view_mode":"default","type":"media","attributes":{"alt":"Wide row 24" spacing left compared with 12" spacing right","title":"Wide row 24" spacing left compared with 12" spacing right","class":"media-element file-default"},"link_text":null}]]

Because some of those fields contain text with non-escaped double quotes in them it breaks the JSON and the image does not display.

Field values need to be properly escaped for JSON before being added.

Comments

mglaman’s picture

Status: Active » Postponed (maintainer needs more info)

I tested this using Panopoly 1.6 (TinyMCE) and Media 2.x checked out @ 1f46a9a. Quotes were being properly turned into HTML entity in tag

[[{"fid":"2","view_mode":"default","fields":{"format":"default","field_file_image_alt_text[und][0][value]":"\"FD\"S\"SD","field_file_image_title_text[und][0][value]":"\"F\"DD\"\""},"type":"media","attributes":{"alt":""FD"S"SD","title":""F"DD""","height":"270","width":"480","class":"media-element file-default"}}]]

Which hash are you checked out at or what is the version info? dev+##?

rooby’s picture

I'm currently using:
Media 7.x-2.0-alpha3+77-dev
File Entity 7.x-2.0-alpha3+17-dev
CKEditor 7.x-1.13+22-dev

Unfortunately I cannot update to latest dev because I am using the patch at #2164823: Fix for replacePlaceholderWithToken in media_wysiwyg.filter.js to fix another bug, so it's not viable to update until #2126755: Improve Media's WYSIWYG Macro handling is committed and then the CKEditor bug mentioned in #97 & #99 of that issue is also fixed.

If you're using TinyMCE and it works then maybe this is a CKEditor problem.

rooby’s picture

Status: Postponed (maintainer needs more info) » Active
mglaman’s picture

Sounds good! Was just trying to help triage the queue since it's pretty epic here ;). If I can spin up a CKEditor site I'll test, too based on what you've mentioned.

rooby’s picture

I've had a little look into this and the file is inserted into the WYSIWYG properly with HTML encoded quotes and displays its preview as it should, however after that when you save the page or click the "Switch to plain text editor" link it gets messed up - the HTML encoded text gets decoded into the token, then later when it tries to convert the token back to an image it gets stuck because the quote is not encoded for HTML or JSON.

This seems to be unrelated to input filters and appears to be a media module issue.

rooby’s picture

It seems to be in media_wysiwyg.filter.js the function:

    /**
     * Replaces the placeholders for html editing with the media tokens to store.
     * @param content
     */
    replacePlaceholderWithToken: function(content) {
      Drupal.media.filter.ensure_tagmap();
      // Convert all xhtml markup to html for reliable matching/replacing.
      content = content.replace(/[\s]\/\>/g, '>');

      // Re-build the macros in case any element has changed in the editor.
      // Wrap the content to be able to properly use replaceWith() and html().
      content =  $('<div>' + content + '</div>');
      $('.media-element', content).each(function(i, element) {
        $(this).replaceWith(function() {
          var markup = Drupal.media.filter.outerHTML($(this));
          var macro = Drupal.media.filter.create_macro($(this));
          // Store the macro => html for more efficient rendering in
          // replaceTokenWithPlaceholder().
          Drupal.settings.tagmap[macro] = markup;
          return macro;
        });
      });
      return content.html();
    },

When it calls $(this).replaceWith() on the media element the replaceWith() function decodes HTML entities.
It should also be noted that the return at the end of content.html() would also decode HTML entities even if replaceWith() didn't.

So then the HTML returned from replacePlaceholderWithToken() has decoded quotes and that's where the problem starts.

If they didn't get decoded when token replacement happens then it would successfully replace back to markup again later.

rooby’s picture

I should also mention that it isn't so much the jQuery functions that are the problem but the way the browser deals with the HTML.

dave reid’s picture

rooby’s picture

I had a little play with double HTML encoding things but there were other issues with that so I tried decoding the entities before using JSON.stringify() and that looked more promising.

An example of the kind of thing that might work here is in media_wysiwyg.filter.js in the extract_file_info() function change the line:

            file_info.attributes[a] = value;

to :

            file_info.attributes[a] = String(value).replace(/&quot;/g, '"');

To test add an image with a title that has a double quote in it.
Without this change it works fine on initial insertion of the media but after switching between plain text & wysiwyg modes the media breaks completely (staying as a token).
With this change you see the encoded quote as &quot; instead of " on initial insertion (regression) but then when after switching between plain text & wysiwyg modes everything works as expected.

Because of that little regression this still nees some work.
I also have only tested with ckeditor and only in firefox at this stage so I'm not sure yet what other issues it may cause to do this.

Hopefully I will have a bit of time to investigate further in the next couple of days.

rooby’s picture

Status: Active » Closed (duplicate)
Related issues: +#2126755: Improve Media's WYSIWYG Macro handling

Looks like this has already been fixed in http://cgit.drupalcode.org/media/commit/?id=72be229 from issue #2126755: Improve Media's WYSIWYG Macro handling with basically the same fix as I mentioned in #9.

Closing this as a duplicate.

rooby’s picture

Status: Closed (duplicate) » Active

Reopening as I have had a look at the solution that was committed in that other issue and it still has problems.

It has the problem I mentioned in #9, where upon initial insertion of the image the user can see excaped content like &quot; (for example if there is a quote in the title and a user hovers over the image in the WYSIWYG).

I'm not sure if that is a problem that needs fixing though because once you save the content that problem goes away.

The other problem with the committed solution is that it only works for the first quote in a string. If I have a title with 2 quotes things still break.

rooby’s picture

Status: Active » Needs review
StatusFileSize
new855 bytes

Here is a patch for latest dev.

I'm using an equivalent patch and it works however I am a little behind latest dev due to another bug in that version so I haven't actually tested this one with latest dev.

I'll test this one a bit later to be sure.

twod’s picture

StatusFileSize
new4.56 KB

I do not think that place in the code is the cause of the issue. I don't know why it's there either, the explanation offered by the comment seems completely backwards too. JSON has no issues with &quot;, but it could maybe have issues with \\". EDIT: (Heh, even this actual comment field has issues with rendering \\"...)

Either way, the code doesn't seem to hurt, so I left it in there when I made this patch. It properly escapes markup characters to their HTML entity equivalents in both attribute and field values when creating the macro, unescapes them when parsing the macro to create a placeholder image (and for altering the values in the dialog), and unescapes them when parsing the macro on the server.

It's pretty much impossible to use Media + Media WYSIWYG with markup-enabled fields on file types, and not have editors or browsers misinterpreting things, without this patch...

Editing macros by hand is a bit more difficult when you need to use &amp;amp; to mean &amp; and have it rendered as & in a title text for an image, but that's the way it has to be done to keep parsers sane.

Well, technically, &amp;amp; is a bad example, since just using a & in a macro won't mess things up, but you do need to use &lt;p&gt;...&lt;/p&gt; to have content rendered as a paragraph, and &amp;lt;p&amp;gt;...&amp;lt;/p&amp;gt; to have it render as the string <p>...</p>, but that example takes so long to write... Also, when editing a markup field using the file format select dialog, you won't have to think about double-escaping your markup. You won't even have to think about escaping your markup at all if you're using a WYSIWYG editor inside that dialog, and since we're talking about a WYSIWYG-related module, I don't see that as an issue. But I might as well mention it out in case anyone asks. ;)

twod’s picture

StatusFileSize
new4.56 KB

Apologies, the patch used to replace forward slashes as well but I changed my mind when doing so didn't add anything. I removed them from the lookup tables, but not from the RegExps...

rooby’s picture

I do not think that place in the code is the cause of the issue. I don't know why it's there either, the explanation offered by the comment seems completely backwards too. JSON has no issues with ", but it could maybe have issues with ". EDIT: (Heh, even this actual comment field has issues with rendering \\"...)

Yeah, the problem is not &quot; but the fact that &quot; gets converted to " before it gets json encoded (jQuery markup manipulation causes the browser to decode the html entities).

I agree that my solution was not optimal though, my knowledge of the media module is pretty limited and I have yet to have time to put into it, so I'm happy to be schooled on this :)

I will review your patch ASAP, thanks.

bkosborne’s picture

I came here a few months ago because I was having issues where a " character was being output as \". I think after reading this issue that's the same problem reported here?

Anyway, I found that on the latest dev of media, the problem I was experiencing doesn't exist anymore. I can safely use quotes in the alt/title field, and a custom field I have, and embed an image into CKEditor (with WYSIWYG module). Switching from CKEditor to plain text and back doesn't cause any issues, and saving it and editing it again is fine as well.

The commit that fixed it: http://cgit.drupalcode.org/media/commit/?id=ebf1d57
The issue for that commit: https://www.drupal.org/node/2307993

bkosborne’s picture

rooby’s picture

With the patch in #14 applied I get lots of errors like this:

Notice: Undefined index: fields in media_wysiwyg_token_to_markup() (line 122 of media/modules/media_wysiwyg/includes/media_wysiwyg.filter.inc).

rooby’s picture

Status: Needs review » Fixed

Agreed, this seems to no longer be an issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.