Problem/Motivation

This is a problem in HEAD, with CKEditor 4.4.7, and still is a problem with #2521820: Update CKEditor library to 4.5.3, which updates CKEditor to 4.5.3.

Due to an automatically created <br>, the following CSS is not applying:

/**
 * While editing and whenever the caption is empty, show a placeholder.
 *
 * Based on http://codepen.io/flesler/pen/AEIFc.
 */
.caption > figcaption[contenteditable=true]:empty:before {
  content: attr(data-placeholder);
  font-style: italic;
}
Expected
Note the pesky <br>!
Actual
Note that I deleted the <br>.

Proposed resolution

TBD

Remaining tasks

Find the root cause.

User interface changes

API changes

None.

Data model changes

None.

Comments

Wim Leers created an issue. See original summary.

quicksketch’s picture

In Backdrop we did the following. The starting code should be the same in D8, the Firefox-specific code was added to fix this problem.

          // By default, the template of captioned widget has no
          // data-placeholder attribute. Note that it also must be done when
          // upcasting existing elements (see widgetDefinition.upcast).
          if (dialogReturnValues.attributes['data-has-caption']) {
            actualWidget.editables.caption.setAttribute('data-placeholder', placeholderText);

            // Firefox will add a <br> tag to a newly created DOM element with
            // no content. Remove this <br> if it is the only thing in the
            // caption. Our placeholder support requires the element be entirely
            // empty. See ckeditor-caption.css.
            var captionElement = actualWidget.editables.caption.$;
            if (captionElement.childNodes.length === 1 && captionElement.childNodes.item(0).nodeName === 'BR') {
              captionElement.removeChild(captionElement.childNodes.item(0));
            }
          }

See https://github.com/backdrop/backdrop/blob/1.x/core/modules/ckeditor/js/p...

adooo’s picture

Issue summary: View changes
Issue tags: +Barcelona2015

Swapped images in the summary to reflect the problem...
I'll try and fix this one!

adooo’s picture

Status: Active » Needs review
Issue tags: +Needs manual testing, +Novice
StatusFileSize
new1.16 KB
fmb’s picture

Status: Needs review » Reviewed & tested by the community

I followed these steps in Firefox 40.0 and Chromium 45.0:

  • create an article;
  • click on the "Image" Ckeditor button;
  • select an image, give it an alternative text, and check the "caption" checkbox;
  • click "Save" to insert the image.

Before applying the patch, the caption zone is empty. After applying it, it contains "Enter caption here".

I do not provide screenshots, as they would be identical to the ones provided in the description of this issue.

wim leers’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +JavaScript
+++ b/core/modules/ckeditor/js/plugins/drupalimagecaption/plugin.js
@@ -210,6 +210,15 @@
+              // Some browsers will add a <br> tag to a newly created DOM element with
+              // no content. Remove this <br> if it is the only thing in the
+              // caption. Our placeholder support requires the element be entirely

80 cols violation.


This needs review from at least a JS maintainer, and preferably from the CKEditor team. I've been told by them that the <br> is necessary for this to function correctly.

nod_’s picture

Oh wow, totally not comfortable reviewing that one. Need CK folks to have a look.

Trupti Bhosale’s picture

StatusFileSize
new309.91 KB
new313.69 KB

Verified the issue on Chrome and Firefox with the patch "placeholder-for-empty-caption-2563505-4.patch".
'Enter caption here' label is now visible for the image

Steps performed:
1.Create a CT Article
2.Click on Image ckeditor button
3.Select the checkbox 'caption'
4.Click Save to insert the image
5.Verify the label 'Enter caption here' is displayed

Attached snapshots for reference

Trupti Bhosale’s picture

Status: Needs review » Reviewed & tested by the community
wim leers’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: -Needs manual testing, -Novice

See #6.

javivf’s picture

Assigned: Unassigned » javivf

I'm working on this

javivf’s picture

Assigned: javivf » Unassigned
StatusFileSize
new1.16 KB
new1.32 KB

Fixed issue with the code style

duaelfr’s picture

I asked @reinmar to come review that issue so I hope he is going to have a moment to do that soon.

wim leers’s picture

#13: Great, I also pinged him on Skype :)

Reinmar’s picture

Hi guys!

I can't quickly check the patch, so I'm going to improvise and guess :). But first, a brief explanation what that <br> does there. We call it "br filler" or "bogus br" and it's the way how browsers originally made sure that empty block have non-zero height. If I remember correctly, even today there's no standardised line-height's value that would tell the browser that block should have height of a text. Some browsers (like Chrome iirc) have some non-standard property for that and some other will (FF guys are considering this), so depending on which browser you test it may seem that bogus brs are not needed any more. Nevertheless, those bogus brs are still required and are created either by browsers or by CKEditor.

If I can see correctly, the patch that was proposed removes automatically created bogus br upon creation of a caption. Things to consider:

  • Whether it affects the undo manager. I think it shouldn't, because this br seems to be removed while other operations are performed, so all of them should create one group.
  • What if bogus br will be created later. E.g. user may type something, press the enter key, delete everything and I think that at least on FF the editable will contain a bogus br. But perhaps it's fine for you that the caption is only appearing at the beginning.
  • Is bogus automatically created if you move selection out of the caption and back there?

These are the possible annoyances that came to my mind. There may be more cases like this. If that's fine for you, then the patch seems to be ok.

However, I proposed to Wim a more complicated, but complete solution. The idea was to polyfill the :empty selector on a nested editable (and perhaps the main one too). It could work like this:

  • Listen on editor#change.
  • Check whether current editable contains any content.
  • If "emptiness" changed remove/add a proper class to the editable.

You could then use such class to add the placeholder.

The tricky part here, as usual, is in the integration with the undo manager. Hard to say in advance how hard it will be. Despite that, I would like to see such plugin for CKEditor one day, so there's a chance that we'll work on it.

wim leers’s picture

So would you recommend going forward with the proposed patch for now, as an interim solution, until your "official solution" becomes available?

Reinmar’s picture

I haven't seen it live, but if you're satisfied with your tests, then yes – I would recommend proceeding.

duaelfr’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs subsystem maintainer review

Cool! Cool! Cool!
Thank you :)

  • webchick committed ae8bcad on 8.0.x
    Issue #2563505 by javivf, adooo, Wim Leers, Truptti, DuaelFr, Reinmar,...
duaelfr’s picture

Status: Reviewed & tested by the community » Fixed

Commited by @webchick on 8.0.x
Thank you all ;)

Status: Fixed » Closed (fixed)

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