Search code examples
widgetckeditorckeditor4.x

How to make an a-tag editable in custom CKEditor widget?


I have created a custom faq widget for the CKEditor 4.5. The plugin.js file looks like this. Basically the widget adds a div to render a FAQ entry using Twitter Bootstrap's collapse feature.

CKEDITOR.plugins.add('faq', {
    requires: 'widget',
    icons: 'faq',

    init: function (editor) {
        var id = Math.random().toString(36).substr(2);
        editor.widgets.add('faq', {

            button: 'Create FAQ entries',

            template: '<div class="faq"><a class="faq-question" data-toggle="collapse" href="#faq-' + id +
                        '" aria-expanded="true" aria-controls="faq-' + id + '"><p>Question ...?</p></a>' +
                        '<div class="collapse in faq-answer" id="faq-' + id +'">Answer ...</div></div>',

            editables: {
                question: {
                    selector: '.faq-question'
                },
                answer: {
                    selector: '.faq-answer'
                }
            },

            allowedContent: 'div(!faq); a(!faq-question); div(!faq-answer); a',
            requiredContent: 'div(!faq)',

            upcast: function (element) {
                return element.name == 'div' && element.hasClass('faq');
            }
        });
    }
});

Editing the answer field works fine, but within the CKEditor I can not edit the question.

Screenshot of a FAQ entry in the CKEditor

The generated source code in the editor looks like this:

<div class="faq">
    <a aria-controls="faq-fk7stx54sfpnl8fr" aria-expanded="true" class="faq-question" data-toggle="collapse" href="#faq-fk7stx54sfpnl8fr">Question ...?</a>    
    <div class="collapse in faq-answer" id="faq-fk7stx54sfpnl8fr"><p>Answer ...</p></div>
</div>

Solution

  • Ok, sometines writing about it helps to solve it ;-)

    I had to add the <a> tag to the list of editable elements like this:

    CKEDITOR.dtd.$editable['a'] = 1;
    

    at top of the plugins.js file