Example Fiddle

A quick demonstration of the problem and suggested fix are in the jsFiddle https://jsfiddle.net/jaydansand/suhqkhfo/.

Problem Description

Commit 6785848835db391ab7ffff2b7145e8d845815217 (from #2631934: Images or spans with class media-element are converted to "false" in WYSIWYG) breaks content that intermingles non-Media <IMG> embeds with Media embeds.

The error arises from an attempt at a backreference in a character class. As Regular-Expressions.info puts it:

Backreferences cannot be used inside character classes (ed: as the new REGEX attempts to do). The \1 in a regex like (a)[\1b] is either an error or a needlessly escaped literal 1. In JavaScript it's an octal escape.

Because [^\1]*? becomes "anything not matching character 0x01", it matches all of the content from the first <IMG> with a class attribute, all the way to the first match of the text "media-element".

After updating Media recently, we noticed some nodes losing some or all of their images (replaced by empty Media tokens), and random content, whenever the nodes were saved. We could see broken tokens being inserted when switching from WYSIWYG to source view. We isolated and tested the REGEX to confirm the issue on Regex101.com in both Firefox 49 and Chrome 54.

Suggested Fix

Since ' and " characters are invalid in class names, we don't need to check that the ending quote character matches the starting quote character: no other quote character is valid in the class name context.

So, let's replace the broken backreference with a simple character class: [^'"]. Attached is a patch that accomplishes that.

Comments

jay.dansand created an issue. See original summary.

jay.dansand’s picture

Issue summary: View changes
joseph.olstad’s picture

Status: Active » Needs review
joseph.olstad’s picture

OK thanks let's put it in dev branch

joseph.olstad’s picture

Status: Needs review » Fixed
sylus’s picture

I have traced an issue to this commit:

http://cgit.drupalcode.org/media/commit/?id=db5b708c50a43d2986d85037ebf8...

Immediately before this commit doing a fresh install of a distro all media images in wyswyg were working, then when leveraging this commit, some images but not all are no longer working. Seems like it is related to this issue:

https://www.drupal.org/node/2828665

jay.dansand’s picture

Thank you for mentioning an issue, but your referenced commit has nothing to do with the issue or patch discussed in this thread. Could you please either create a new issue or contribute to an existing, related one? Thanks!

sylus’s picture

Status: Fixed » Closed (fixed)

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