I have a situation where I need users to be able to select and act on, elements like lists so that it's possible to detect if a child/descendant like <li> is selected or if the parent like <ul> is selected. Users would switch to the parent using the elementspath plugin / bottom bar tag breadcrumbs.
For example on a document like this:
Users must be able to target the ul element. I tried the following, but GetSelectedElement() always returns null!
This is how I can get the first child of the selection, but I need to retrieve the parent!
How would I gain access to the parent element?
For example on a document like this:
<ul> <li>Something</li> <li>Something</li> </ul>
Users must be able to target the ul element. I tried the following, but GetSelectedElement() always returns null!
var selection = editor.getSelection(); var test = selection.GetSelectedElement(); debug(test); // This is null, I expected it to be ul
This is how I can get the first child of the selection, but I need to retrieve the parent!
var selection = editor.getSelection(); var element = selection.getStartElement(); debug(element); // This returns the first LI element
How would I gain access to the parent element?
Re: GetSelectedElement returns null, can I get selected pare
Is there a workaround? This is a critical function.
Re: GetSelectedElement returns null in IE and Chrome, BUG?
I do believe it's a confirmed bug. No proposed workarounds yet.
Customer and Community Manager, CKSource
Follow us on: Facebook, Twitter, LinkedIn
If you think you found a bug in CKEditor, read this!
Re: GetSelectedElement returns null in IE and Chrome, BUG?
Also, is there a way to I could "vote up" the bug to bump it's priority?
Re: GetSelectedElement returns null in IE and Chrome, BUG?
Given a node, you can easily get its parent: http://docs.cksource.com/ckeditor_api/s ... #getParent
now you can check if it's the node that you want, or if you want to keep on searching or anything else.
Re: GetSelectedElement returns null in IE and Chrome, BUG?
Great suggestion but the problem is that GetStartElement() is not consistent either!
)
For example (Chrome):
user has a table that contains many lists (it happens
user wants to target ul and clicks ul (the elementspath)
-> the first LI is selected, we can select the parent and everything is ok, right?
if the user wants to target the table row they click on the "tr" element
-> a table > tbody > tr > td > br element is returned - one that doesn't even exist in the DOM!
--> how do we find out what he wanted to select?
if the user clicks on the table element, the same nonexistant br is returned
if the user clicks the tbody element, the table element is returned.
I'm getting dizzy.
I'm thinking if this won't be fixed I'll have a look if I can fix it myself or something, but if I cant I have no idea what to do. The bug is 13 months old (previous comment is equally old) with a priority of Normal so I'm a bit afraid...
Re: GetSelectedElement returns null in IE and Chrome, BUG?
You'll have to dig a little into the code to understand it better unless you can find someone else explaining better how to handle the selection and your current problems.
Re: GetSelectedElement returns null in IE and Chrome, BUG?
The API
defines that it returns a {CKEDITOR.dom.element}, which I thought could be a representation of most, if not any DOM element? I fail to see how an image would be of that type and a paragraph wouldn't.
I did do a little digging and where the elementpaths handles clicks events I find access to the element/object like I would want GetSelectedElement() to do:
When I debug the element variable it's almost what I would want GetSelectedElement() to return. On Firefox it seems to be the reference to the actual DOM element and on Chrome it seems to be a CKEDITOR.dom.element, like HTMLParagraphElement. Both would work in my case as I could work with that, but I'm a little hesitant to go poke around the source. I think a workaround would be to set a silly global variable like var __ElementpathClicked = element; there and then in my code check if that is set, read the value and then unset it... seems like a hack and I really want to avoid them if a better solution is available, so I'll wait a while for replies.
Re: GetSelectedElement returns null in IE and Chrome, BUG?
You can ask the browser to select a node and it might react in different ways, it can select just that node correctly, or move the boundaries to some other node, it depends on each browser and the type of selection that you want to perform (it's not the same to select a <p> than a <tr>, also empty nodes are problematic for Opera and Webkit IIRC)
In a general way when you have a selection marked by the pipes as this
Which is the "selected element"?
is not the p because it's not fully selected, and it's not the span because there's also text selected beyond its boundaries.
So the correct statement is that there's a range selected with two boundary points, and you can use them to check the selection according to your needs.
Re: GetSelectedElement returns null in IE and Chrome, BUG?
I think I get it now, thank you for taking the time to explain it to me and sorry for being so slow! My problem in thinking was that I wasn't really thinking about it as a selection, rather that I had just clicked on an element so I should gain access to that targeted element. I think I'll try my "hack" approach to get to my goal, at least until issue 7928 is closed.