Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8816123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:32:39+00:00 2026-06-14T04:32:39+00:00

I’m having trouble in developing CKEditor plugins that insert uneditable contents into the text

  • 0

I’m having trouble in developing CKEditor plugins that insert uneditable contents into the text flow. I’ve been trying to utilize the range functions, but with little success as the documentation is less than stellar. So, given some text, lets say the plugin inserts “[[uneditable stuff]]” and then upon WYSIWYG display wraps that in a span so it can be styled in a color:

<p>This is some text[[uneditable stuff here]]</p>

When first inserting the uneditable stuff, we want the user to then be able to continue on typing or hitting Enter for a new line. The following code (which I got here: How to set cursor position to end of text in CKEditor?) works in Firefox but (naturally) not in IE9, 8, or 7:

var s = editor.getSelection();
editor.insertElement(e); // element 'e'= a span created earlier
var p = e.getParent();
s.selectElement(p);
var selected_ranges = s.getRanges();
selected_ranges[0].collapse(false);  //  false = to the end of the selected node
s.selectRanges(selected_ranges);  // putting the current selection there

So what I want to happen is that the cursor goes at position “^”:

<p>This is some text<span>[[uneditable stuff here]]</span>^</p>

If the new element is not at the end of the line, then after creating it, the cursor should go to here:

<p>This is some text<span>[[uneditable stuff here]]</span>^ with more text after the new element</p>

In FF, I can get the cursor at the end of the line though not at position after the new element. In IE, the cursor is still inside the new SPAN, which I see when I type and it is still in the span’s css color, and when switching to SOURCE view, the text is gone (because it’s an uneditable span).

I know there’s a range.setStartAfter method, but have been totally unable to make it work even in FF/Chrome.

Does anybody have a really good handle on using range and selection methods in CKEditor? I know I don’t!

Starting to think that just using editor.insertElement is wrong, and I should learn about the FakeElement (insertBogus?) functions, which I don’t understand, yet. Stock plugins for such as links and images don’t seem to have this problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:32:40+00:00Added an answer on June 14, 2026 at 4:32 am

    I had to do some sneaky stuff to solve this, but it did get solved: After creating the non-editable item (a span with attribute content-editable: false) I had to create a “dummy” span with text consisting of one space. So, I insert the real span, then the dummy. But only when creating a new item.

    So this goes in the “if not dealing with editing a selected item” section. Here, ‘a’ is the editor instance, ‘e’ is the desired non-editable item, ‘f’ is the dummy span.

    var e=new CKEDITOR.dom.element('span',a.document);
    e.setAttributes({// stuff to create our element});
    var f=new CKEDITOR.dom.element('span',a.document);
    f.setAttributes({
        'class':'dummyF'
    });
    f.setText(' '); // that's just one space
    
    // after section dealing with editing a selected item, in "else":
    var sel = a.getSelection(); // current cursor position
    a.insertElement(e); // the real new element
    if(CKEDITOR.env.ie || CKEDITOR.env.webkit){ // IE & Chrome like this way
        f.insertAfter(e);
        sel.selectElement(f);
    }
    else { //FF likes this way (to ensure cursor stays in the right place)
        f.insertAfter(e);
        var rangeObjForSelection = new CKEDITOR.dom.range( a.document );
        rangeObjForSelection.selectNodeContents( f );
        a.getSelection().selectRanges( [ rangeObjForSelection ] );
    }
    

    I have to admit that I don’t entirely understand my own code. I got there through hours of trial and error. Oh, and I had to add an htmlFilter Rule to get rid of left-over ‘f’ elements:

    e.addRules({
      // e is the htmlFilter: applied to editor data before/upon output
      elements:{
        span:function(s){ // 's' is any spans found in the editor
            if(s.attributes&&s.attributes['data-cke-myelement']) {
                //stuff to do with my element
            }
            else if(s.attributes['class']=='dummyF') { //CKEDITOR.env.ie&&
                // for dummy spans to deal with "can't type or hit enter after new element" problem
                realtext = new String(s.children[0]['value']);
                realtext.replace(/^&nbsp;/,'');
                s.children[0]['value'] = realtext;
                delete s.name;
            }
        }
      }
    });
    

    I also have to add that I don’t remember why I had to replace “nbsp” entities before deleting the span. But it works. And I don’t know why to delete you use “s.name” instead of just ‘s’.

    Hope that helps someone.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I'm trying to create an if statement in PHP that prevents a single post
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.