0

Let's say I have a block markup of:

<div class="block">
    <a href="/">
        <div class="divInside"></div>
        <img src="/bla" />
        <p>Parargraph</p>
    </a>
</div>

HTML5: states that the <a> element "may be wrapped around entire paragraphs, lists, tables, and so forth, even entire sections, so long as there is no interactive content within (e.g. buttons or other links)".

So what I have here is perfectly valid is it not ? Why does ckeditor keep reformatting my source, example, the above, once I change beween the rich editor and source mode does the following:

<div class="block">
    <div class="divInside">
        &nbsp;
    </div>
    <a href="/"><img src="/bla" /> </a>
    <p>
        <a href="/">Parargraph</a>
    </p>
    <a href="/"> </a>
</div>

I have no problem with what it does to the empty div, but what's doing with the anchors is very annoying.

Is there a way to disable this?

Francois Borgies
  • 2,378
  • 31
  • 38
Shannon Hochkins
  • 11,763
  • 15
  • 62
  • 95
  • CKEditor lacks support for block level links that were introduced in HTML5, because it was a pretty drastic change. For the same reason there's no DTD for HTML5 - it could not be created. CKEditor parses and processes content based on DTD-like object (`CKEDITOR.dtd`) and therefore, without deep architectural change, it's not able to support both - inline and block links (so called transparent elements). This change will be made of course, but we need time. – Reinmar Jul 02 '14 at 07:06
  • See my previous answer http://stackoverflow.com/questions/16591205/is-ckeditor-4-really-ready-for-html5/16591582#16591582 – oleq Jul 02 '14 at 08:58
  • @oleq See my answer I have posted, I have came up with a solution that works just fine! It basically tells ckeditor to not validate anchors. – Shannon Hochkins Jul 02 '14 at 21:43
  • possible duplicate of [How to configure CKEditor to allow html block-level tags to be wrapped in an anchor tag](http://stackoverflow.com/questions/19825802/how-to-configure-ckeditor-to-allow-html-block-level-tags-to-be-wrapped-in-an-anc) – authentictech Sep 21 '15 at 19:07

1 Answers1

1

For future people coming to this with the same issue, I did come up with a solution but I'm not overly happy about it as there should be an option for this as it's PERFECTLY valid in HTML5

In your config.js file, add in the following config setting:

config.protectedSource.push(/[\r|\n]|(<a([^*>]+>)|<\/a>)/g);

Here's a demo of the regex working: DEMO

Basically all we're doing here is:

  1. Finds new lines \r
  2. Finds new rows \n
  3. Finds any opening anchors: <a followed by an text until we come to > - (<a([^*>]+>)
  4. Finds closing anchor tags <\/a>

This regex will stop validing the found results, I'm sure there's a better way to do this as I'm not regexPERT (<-- good at puns though!)

Hope this helps

Shannon Hochkins
  • 11,763
  • 15
  • 62
  • 95
  • But using that regexp for the protectedSource, doesn't mean that it's impossible to edit the links in the editor? – AlfonsoML Jul 02 '14 at 13:45
  • According to their docs yes, but I also added `config.allowedContent = true` which seems to not filter the html. @AlfonsoML – Shannon Hochkins Jul 02 '14 at 21:47
  • allowedContent disables the ACF filter, so CKEditor won't try to strip out any element or attribute that can't understand. But I don't think that it's related at all to this question, my comment is: with that protectedSource, can you edit an existing link in the editor? – AlfonsoML Jul 03 '14 at 07:28
  • Yes I can still edit it just fine, I don't know if I've somehow destroyed the functionality with combined tags, however for me, this is working exactly as I want it to @AlfonsoML – Shannon Hochkins Jul 08 '14 at 00:11
  • 1
    I really can't understand that. I've created this fiddle just to test and as I expected, while in WYSIWYG mode the editor doens't recognize the link and so a user won't be able to edit them. http://jsfiddle.net/DEHwL/ I've added a test with inline and another with a framed editor and both of them have the same behavior. – AlfonsoML Jul 08 '14 at 09:40
  • I see what you mean, seeing as I very rarely actually edit links using the editor, I didn't realise that the functionality to edit them with the UI no longer works. Can you think of any other way to fix this problem? @AlfonsoML I was merely using this fix so that I could stop reformatting of the code when swapping between modes. – Shannon Hochkins Jul 09 '14 at 02:56
  • Any real fix means changing core parts of CKEditor, so if you don't edit links your fix is perfect. But other people might want to edit anything and in that case they must be aware of this downside of using protectedSource – AlfonsoML Jul 09 '14 at 08:39