Hey all I made a plugin that is intended to edit the HTML element of the current selection, or the common parent if in the selection there are partial HTML tags, for example if the data in my instance of CKEditor is:
if I trigger the plugin after having selected "Hey " or "you!" it will return respectively the first or the second span element, if I trigger the plugin with a mixed selection, for example "ey y" it will return the common ancestor p element.
In order to do that I have setup the code in my plugin like this:
which works just fine for the above cases, however if my selection is null (for example I have my cursor sitting between the "e" and the "y" of "Hey"), or if I select the partial content of a single HTML element (for example "He"), the selection doesn't trigger (rightfully) the if sentence, but when I try to call getCommonAncestor() on its range what is returned to me is not an HTML Element (or Node) but a DOM Object on which I cannot use any of the methods I could use in the other cases.
In particular if I try to use getAttribute() or setAttribute() (or any other method that is usable on Elements) the plugin crashes.
So in order to make this plugin work the user has to select either the full contents of an HTML element or the partial (or full) contents of subsequent sibgling elements, otherwise the plugin will crash, what's worse is that I cannot even seem to find a way to alert the user of an invalid selection as (!selection2) will never be triggered because even in the last case it's an object (and I don't think there are any methods of the likes of isElement() to define wether an object is also an HTML element or not)
As usual any help is very much appreciated.
<p><span id="1">Hey </span><span id="2">you!</span></p>
if I trigger the plugin after having selected "Hey " or "you!" it will return respectively the first or the second span element, if I trigger the plugin with a mixed selection, for example "ey y" it will return the common ancestor p element.
In order to do that I have setup the code in my plugin like this:
selection1 = this._.editor.getSelection(); if ( selection1.getType() == CKEDITOR.SELECTION_ELEMENT ) { selection2 = selection1.getSelectedElement(); } else { //editing the selection's common ancestor node and also trying to get its attributes var range = selection1.getRanges( true )[0]; range.shrink( CKEDITOR.SHRINK_TEXT ); selection2 = range.getCommonAncestor(); }
which works just fine for the above cases, however if my selection is null (for example I have my cursor sitting between the "e" and the "y" of "Hey"), or if I select the partial content of a single HTML element (for example "He"), the selection doesn't trigger (rightfully) the if sentence, but when I try to call getCommonAncestor() on its range what is returned to me is not an HTML Element (or Node) but a DOM Object on which I cannot use any of the methods I could use in the other cases.
In particular if I try to use getAttribute() or setAttribute() (or any other method that is usable on Elements) the plugin crashes.
So in order to make this plugin work the user has to select either the full contents of an HTML element or the partial (or full) contents of subsequent sibgling elements, otherwise the plugin will crash, what's worse is that I cannot even seem to find a way to alert the user of an invalid selection as (!selection2) will never be triggered because even in the last case it's an object (and I don't think there are any methods of the likes of isElement() to define wether an object is also an HTML element or not)
As usual any help is very much appreciated.