Jump to content

Using the match callback for allowedContent/disallowedContent in CKEditor


Robin S
 Share

Recommended Posts

A pet hate of mine is when an editor uses a paragraph of bold text for what ought to be a heading. When I need to tidy up poorly formatted content like this I will quickly change such lines of text into the heading of the appropriate level, but that still results in markup like...

<h2><strong>Some heading text</strong></h2>

The <strong> has no business being there, but it's a bit of a hassle to remove it because you have to drag a selection around the exact text as opposed to just placing your cursor within the line. That gets tedious if you have a lot content to process.

I figured there has to be an easier way so started looking into the ACF (Advanced Content Filter) features of CKEditor. What I wanted is a rule that says "strong tags are disallowed specifically when they are within a heading tag". (I guess there could occasionally be a use case where it would be reasonable to have a strong tag within a heading tag, but it's so rare that I'm not bothered about it).

With the typical string format for allowedContent and disallowedContent there is no ability to disallow a specific tag only when it is within another specific tag - a tag is allowed everywhere or not at all. But I found there is an alternative object format for these rules that supports a callback function in the "match" property. So I was able to achieve my goal with the following in /site/modules/InputfieldCKEditor/config.js:

CKEDITOR.editorConfig = function(config) {
    config.disallowedContent = {
        // Rule for the <strong> element
        strong: {
            // Use "match" callback to determine if the element should be disallowed or not
            match: function(element) {
                // Heading tag names
                var headings = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
                // The parent of the element (if any)
                var parent = element.parent;
                if(typeof parent !== 'undefined') {
                    // If there is a parent, return true if its name is in the heading names array
                    return headings.indexOf(parent.name.toLowerCase()) !== -1;
                } else {
                    // There is no parent so the element is allowed
                    return false;
                }
            }
        }
    }
};

 

Another tip: if you want to debug your allowedContent or disallowedContent rules to make sure they are being parsed and applied successfully you can log the filter rules to the console. For convenience I used /site/modules/InputfieldCKEditor/config.js again.

// Get the CKEditor instance you want to debug
var editor = CKEDITOR.instances.Inputfield_body;
editor.on('instanceReady', function() {
    // Log the disallowed content rules
    console.log(editor.filter.disallowedContent);
});

 

  • Like 6
Link to comment
Share on other sites

Update...

The above works okay but it seems that the match callback only fires when CKEditor loads, so to be sure that any disallowed content resulting from the current page edit is removed you have to save the page twice.

After a bit more hunting I think I've found a better approach that uses CKEditor's DTD object. It's not quite as straightforward as you'd expect at first because element objects within the DTD object are not fully independent (e.g. CKEDITOR.dtd['h2'] seems to refer to the same object as CKEDITOR.dtd['p']). This SO post helped me find a solution.

In /site/modules/InputfieldCKEditor/config.js:

// For numbers 1 to 6
for(var i = 1; i <= 6; i++) {
    // Create the tag name from 'h' plus the number
    var tag = 'h' + i;
    // Create clone of DTD heading object so it can be modified individually
    CKEDITOR.dtd[tag] = Object.assign({}, CKEDITOR['dtd'][tag]);
    // Disallow strong element from being contained within heading element
    CKEDITOR.dtd[tag]['strong'] = 0;
}

 

  • Like 5
Link to comment
Share on other sites

  • 3 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...