0

Good day.

I have this code:

<div class="align-center">
 <a href="#" class="client">
  <div><img src="./client0.png" alt="" /></div>
  <p class="italic comm"> Test Test Test Test</p>
</a>
</div>

I paste this code in a textarea ckeditor, but after pressing the tab "source" and back to view page I get changed code:

<div class="align-center">
 <div>
 <a class="client" href="#"><img alt="" src="./client0.png" /></a>
 </div>    
 <p class="italic comm"><a class="client" href="#"> Test Test Test Test</a></p>
</div>

Please tell me how to stop changing the code?

eLRuLL
  • 18,488
  • 9
  • 73
  • 99
Alex N
  • 1,111
  • 4
  • 13
  • 20
  • The input is not a valid markup snippet. It's missing a ``. – Matt Ball Jun 10 '13 at 04:22
  • Matt is right, and the edtitor gets confused with this code. – orezvani Jun 10 '13 at 04:27
  • @MattBall I was wrong when I wrote the code in question, now I updated my question – Alex N Jun 10 '13 at 04:36
  • @emab now I updated my question – Alex N Jun 10 '13 at 04:36
  • 1
    ckeditor right now does not support `a` to have block elements inside, even if this is valid for html5 (`a` acts transparent in html5). That's why it rewrites your code. – t.niese Jun 10 '13 at 04:38
  • @t.niese this can be turned off or changed? – Alex N Jun 10 '13 at 04:44
  • You want to disable code correction completely? – orezvani Jun 10 '13 at 04:50
  • @AlexN afaik it is still a pending bug report, so i would say right now it is not possible to have an `a` containing block elements. But I'll check if it is still true as soon as possible to verify my statement. – t.niese Jun 10 '13 at 04:51
  • @t.niese which version html need for ckeditor ? – Alex N Jun 10 '13 at 05:12
  • Nope - you can't easily fix this - that's why it is still a pending issue. You declare links as block elements (by modifying [DTD](https://github.com/ckeditor/ckeditor-dev/blob/master/core/dtd.js)), but it may have unpredictable results. – Reinmar Jun 10 '13 at 07:59

1 Answers1

2

The reason for such behavior is that your markup is invalid.

HTML 4.01 specification (also xHTML 2.0) doesn't allow block elements like <div> inside <a> (see this answer). CKEditor follows those specs so, of course, it also finds your code invalid. Still, CKEditor's parser tries to fix your HTML and the result is what you call "changed code".

Your code could be valid in HTML5 though. CKEditor doesn't support HTML5 DTD yet because it's complex and dynamic as opposed to HTML 4.01 (xHTML 2.0).

At the moment, the only solution is to change your code.

Community
  • 1
  • 1
oleq
  • 15,697
  • 1
  • 38
  • 65