Opened 3 years ago

Last modified 3 years ago

#14933 new Bug

Buggy behaviour when getting current selected node indexes — at Version 1

Reported by: Javier Lario Owned by:
Priority: Normal Milestone:
Component: Core : Selection Version: 4.5.11
Keywords: Selection Cc:

Description (last modified by Tade0)

I'm trying to integrate a speech plugin (based on another app of us) into CKEditor. The plugin is integrated seamlessly and correctly, props for the CKEditor team for that easyness.

Thing is we can dictate normal text and also use voice commands with the plugin, like the type of "Go to X", "Go to next phrase", etc. These commands make use of char index based selection , so we have 2 methods that extrapolate the DOM nodes based selection system of the CKEditor API to char index based selection.

Those functions are:

1. SETSELECTION

this function does not show buggy behaviour

textHelper.setSelectionRange = function (editor, range) {

        //"range" is our char index based range

        var startSetted = false,
            endSetted = false,
            counter = 0;
        var editable = editor.editable();
        var ckRange = editor.createRange();
        ckRange.selectNodeContents(editable);
        var walker = new CKEDITOR.dom.walker(ckRange);
        var node = null;
        while ((node = walker.next()) && (!startSetted || !endSetted)) {
            var nodeLength = 0;
            if (node.type == CKEDITOR.NODE_TEXT ) {
                nodeLength = node.getText().length;
            } else if (node.$.nodeName == 'BR') {
                nodeLength = 1;
            }
            if (nodeLength > 1 || !node.hasNext()) {
                if (!startSetted && counter + nodeLength >= range.start) {
                    ckRange.setStart(node, range.start - counter);
                    startSetted = true;
                }
                if (!endSetted && counter + nodeLength >= range.end) {
                    ckRange.setEnd(node, range.end - counter);
                    endSetted = true;
                }
            }
            counter += nodeLength;
        }
        ckRange.select();
        editor.focus();
    };

2. GETSELECTION

this function DOES show buggy behaviour

textHelper.getSelectionRange = function (editor) {
        
        var ckRange = editor.createRange();
        ckRange.selectNodeContents(editor.editable())
        var walker = new CKEDITOR.dom.walker(ckRange);

        var start = 0, end = 0,

        //This is where the buggy behaviour occurs, this indexes don't always match those of the node that is selected (or where the cursor is), in the editable part of the editor

        selectionRange = editor.getSelection(true).getRanges(true)[0],
        si = selectionRange.startContainer.getIndex(),
        ei = selectionRange.endContainer.getIndex();

        for (var i = 0; i <= ei && (node = walker.next()); i++) {
            var nodeLength = 0;
            if (node.type == CKEDITOR.NODE_TEXT ) nodeLength = node.getText().length;
            else if (node.$.nodeName == 'BR') nodeLength = 1;

            if (i < si || (i == si && node.$.nodeName == 'BR')) start += nodeLength;
            else if (i == si) start += selectionRange.startOffset;

            if (i < ei || (i == ei && node.$.nodeName == 'BR')) end += nodeLength;
            else if (i == ei) end += selectionRange.endOffset;
        }
        return new invoxmd.Range(start, end);
        
    };

Steps to reproduce

selectionRange = editor.getSelection(true).getRanges(true)[0], si = selectionRange.startContainer.getIndex(), ei = selectionRange.endContainer.getIndex();

Indexes don't always match those of the node of cursor/selection, speech behaviour then becomes buggy, text is inserted when it shouldn't, etc.

Expected result

Indexes always match those of the node of cursor/selection.

Actual result

Indexes don't always match those of the node of cursor/selection, speech behaviour then becomes buggy, text is inserted when it shouldn't, etc.

Other details (browser, OS, CKEditor version, installed plugins)

Chrome, Windows 10 x64, 4.5.11, full editor

Change History (1)

comment:1 Changed 3 years ago by Tade0

Description: modified (diff)
Note: See TracTickets for help on using tickets.
© 2003 – 2019 CKSource – Frederico Knabben. All rights reserved. | Terms of use | Privacy policy