<commit_before>define(["mocha/mocha", "chai", "jquery", "wed/wed", "wed/domutil", "rangy",
       "wed/key_constants"],
function (mocha, chai, $, wed, domutil, rangy, key_constants) {
    var options = {
        schema: 'test/tei-simplified-rng.js',
        mode: {
            path: 'wed/modes/generic/generic',
            options: {
                meta: 'wed/modes/generic/metas/tei_meta',
                // This option is for testing only, not to be used as
                // part of normal operations. It may change at any
                // time, without warning, and without documentation.
                __test: {
                    no_element_decoration: ["term"]
                }
            }
        }
    };
    var assert = chai.assert;

    var wedroot = $("#wedframe-invisible").contents().find("#wedroot").get(0);
    var $wedroot = $(wedroot);
    var source = "../../test-files/wed_test_data/source_converted.xml";

    function caretCheck(editor, container, offset, msg) {
        assert.equal(editor._raw_caret[0], container, msg + " (container)");
        assert.equal(editor._raw_caret[1], offset, msg + " (offset)");
    }

    function dataCaretCheck(editor, container, offset, msg) {
        var data_caret = editor.getDataCaret();
        assert.equal(data_caret[0], container, msg + " (container)");
        assert.equal(data_caret[1], offset, msg + " (offset)");
    }


    function firstGUI($container) {
        return $container.children("._gui").get(0);
    }

    function lastGUI($container) {
        return $container.children("._gui").last().get(0);
    }

    function firstPH($container) {
        return $container.children("._placeholder").get(0).childNodes[0];
    }

    function lastPH($container) {
        return $container.children("._placeholder").last().
            get(0).childNodes[0];
    }


    describe("wed", function () {
        var editor;
        beforeEach(function (done) {
            $wedroot.empty();
            require(["requirejs/text!" + source], function(data) {
                $wedroot.append(data);
                editor = new wed.Editor();
                editor.addEventListener("initialized", function () {
                    done();
                });
                editor.init(wedroot, options);
            });
        });

        afterEach(function () {
            if (editor)
                editor.destroy();
            editor = undefined;
        });

        it("starts with an undefined caret", function () {
            assert.equal(editor.getCaret(), undefined, "no caret");
        });

        it("clicking moves the caret", function (done) {
            editor.whenCondition(
                "first-validation-complete",
                function () {
                // Text node inside title.
                var initial = $(editor.gui_root).find(".title").
                    get(0).childNodes[1];
                editor.setCaret(initial,
                                initial.nodeValue.length);
                editor.moveCaretRight();
                // It is now inside the final gui element.
                caretCheck(editor, lastGUI($(initial.parentNode)),
                           0, "initial caret position");

                // We used to check for the existence of a fake
                // caret. However, not all browsers require the
                // fake caret to exist in this context. (Chrome
                // does, Firefox does not. Subject to change with
                // new versions.) There's no point in check
                // it. However, the fake caret should definitely
                // be off by the time we finish the test.
                //
                // Fake caret must exist
                // assert.equal(editor._$fake_caret.parent().length,
                // 1, "fake caret existence");

                // We have to set the selection manually and
                // generate a click event because just generating
                // the event won't move the caret.
                var r = rangy.createRange();
                r.setStart(initial, 0);
                rangy.getSelection(editor.my_window).setSingleRange(r);
                var ev = $.Event("mouseup", { target: initial });
                $(initial.parentNode).trigger(ev);

                // We need to do this because setting the caret
                // through a click is not instantaneous. wed
                // internally sets a timeout of 0 length to deal with
                // browser incompatibilities. We need to do the same
                // so that wed's timeout runs before we query the
                // value.
                editor.my_window.setTimeout(function () {
                    // In text, no fake caret
                    assert.equal(editor._$fake_caret.parent().length, 0);
                    caretCheck(editor, initial, 0, "final caret position");

                    done();
                }, 1);
            });
        });

        it("typing text works", function (done) {
            editor.whenCondition(
                "first-validation-complete",
                function () {
                // Text node inside title.
                var initial = $(editor.gui_root).find(".title").
                    get(0).childNodes[1];
                var parent = initial.parentNode;
                editor.setCaret(initial, 0);

                // There was a version of wed which would fail this
                // test. The fake caret would be inserted inside the
                // text node, which would throw off the
                // nodeToPath/pathToNode calculations.

                // Synthetic event
                var event = new $.Event("keydown");
                key_constants.SPACE.setEventToMatch(event);
                event.type = "keypress";
                editor.$gui_root.trigger(event);
                editor._syncDisplay();
                assert.equal(initial.nodeValue, " abcd");
                assert.equal(parent.childNodes.length, 3);

                event = new $.Event("keydown");
                key_constants.SPACE.setEventToMatch(event);
                event.type = "keypress";
                editor.$gui_root.trigger(event);
                editor._syncDisplay();
                assert.equal(initial.nodeValue, "  abcd");
                assert.equal(parent.childNodes.length, 3);

                // This is where wed used to fail.
                event = new $.Event("keydown");
                key_constants.SPACE.setEventToMatch(event);
                event.type = "keypress";
                editor.$gui_root.trigger(event);
                editor._syncDisplay();
                assert.equal(initial.nodeValue, "   abcd");
                assert.equal(parent.childNodes.length, 3);
                done();
            });
        });

        describe("moveCaretRight", function () {
            it("works even if there is no caret defined", function () {
                assert.equal(editor.getCaret(), undefined, "no caret");
                editor.moveCaretRight();
                assert.equal(editor.getCaret(), undefined, "no caret");
            });

            it("moves right into gui elements",
               function (done) {
                editor.whenCondition(
                    "first-validation-complete",
                    function () {
                    var initial = editor.gui_root.childNodes[0];
                    editor.setCaret(initial, 0);
                    caretCheck(editor, initial, 0, "initial");
                    editor.moveCaretRight();
                    var first_gui = firstGUI($(initial));
                    // It is now located inside the text inside
                    // the label which marks the start of the TEI
                    // element.
                    caretCheck(editor, first_gui, 0, "moved once");
                    editor.moveCaretRight();
<commit_msg>Added tests for contextual menus.<commit_after>define(["mocha/mocha", "chai", "jquery", "wed/wed", "wed/domutil", "rangy",
       "wed/key_constants"],
function (mocha, chai, $, wed, domutil, rangy, key_constants) {
    var options = {
        schema: 'test/tei-simplified-rng.js',
        mode: {
            path: 'wed/modes/generic/generic',
            options: {
                meta: 'wed/modes/generic/metas/tei_meta',
                // This option is for testing only, not to be used as
                // part of normal operations. It may change at any
                // time, without warning, and without documentation.
                __test: {
                    no_element_decoration: ["term"]
                }
            }
        }
    };
    var assert = chai.assert;

    var wedroot = $("#wedframe-invisible").contents().find("#wedroot").get(0);
    var $wedroot = $(wedroot);
    var source = "../../test-files/wed_test_data/source_converted.xml";

    function caretCheck(editor, container, offset, msg) {
        assert.equal(editor._raw_caret[0], container, msg + " (container)");
        assert.equal(editor._raw_caret[1], offset, msg + " (offset)");
    }

    function dataCaretCheck(editor, container, offset, msg) {
        var data_caret = editor.getDataCaret();
        assert.equal(data_caret[0], container, msg + " (container)");
        assert.equal(data_caret[1], offset, msg + " (offset)");
    }


    function firstGUI($container) {
        return $container.children("._gui").get(0);
    }

    function lastGUI($container) {
        return $container.children("._gui").last().get(0);
    }

    function firstPH($container) {
        return $container.children("._placeholder").get(0).childNodes[0];
    }

    function lastPH($container) {
        return $container.children("._placeholder").last().
            get(0).childNodes[0];
    }


    describe("wed", function () {
        var editor;
        beforeEach(function (done) {
            $wedroot.empty();
            require(["requirejs/text!" + source], function(data) {
                $wedroot.append(data);
                editor = new wed.Editor();
                editor.addEventListener("initialized", function () {
                    done();
                });
                editor.init(wedroot, options);
            });
        });

        afterEach(function () {
            if (editor)
                editor.destroy();
            editor = undefined;
        });

        it("starts with an undefined caret", function () {
            assert.equal(editor.getCaret(), undefined, "no caret");
        });

        it("clicking moves the caret", function (done) {
            editor.whenCondition(
                "first-validation-complete",
                function () {
                // Text node inside title.
                var initial = $(editor.gui_root).find(".title").
                    get(0).childNodes[1];
                editor.setCaret(initial,
                                initial.nodeValue.length);
                editor.moveCaretRight();
                // It is now inside the final gui element.
                caretCheck(editor, lastGUI($(initial.parentNode)),
                           0, "initial caret position");

                // We used to check for the existence of a fake
                // caret. However, not all browsers require the
                // fake caret to exist in this context. (Chrome
                // does, Firefox does not. Subject to change with
                // new versions.) There's no point in check
                // it. However, the fake caret should definitely
                // be off by the time we finish the test.
                //
                // Fake caret must exist
                // assert.equal(editor._$fake_caret.parent().length,
                // 1, "fake caret existence");

                // We have to set the selection manually and
                // generate a click event because just generating
                // the event won't move the caret.
                var r = rangy.createRange();
                r.setStart(initial, 0);
                rangy.getSelection(editor.my_window).setSingleRange(r);
                var ev = $.Event("mouseup", { target: initial });
                $(initial.parentNode).trigger(ev);

                // We need to do this because setting the caret
                // through a click is not instantaneous. wed
                // internally sets a timeout of 0 length to deal with
                // browser incompatibilities. We need to do the same
                // so that wed's timeout runs before we query the
                // value.
                editor.my_window.setTimeout(function () {
                    // In text, no fake caret
                    assert.equal(editor._$fake_caret.parent().length, 0);
                    caretCheck(editor, initial, 0, "final caret position");

                    done();
                }, 1);
            });
        });

        it("typing text works", function (done) {
            editor.whenCondition(
                "first-validation-complete",
                function () {
                // Text node inside title.
                var initial = $(editor.gui_root).find(".title").
                    get(0).childNodes[1];
                var parent = initial.parentNode;
                editor.setCaret(initial, 0);

                // There was a version of wed which would fail this
                // test. The fake caret would be inserted inside the
                // text node, which would throw off the
                // nodeToPath/pathToNode calculations.

                // Synthetic event
                var event = new $.Event("keydown");
                key_constants.SPACE.setEventToMatch(event);
                event.type = "keypress";
                editor.$gui_root.trigger(event);
                editor._syncDisplay();
                assert.equal(initial.nodeValue, " abcd");
                assert.equal(parent.childNodes.length, 3);

                event = new $.Event("keydown");
                key_constants.SPACE.setEventToMatch(event);
                event.type = "keypress";
                editor.$gui_root.trigger(event);
                editor._syncDisplay();
                assert.equal(initial.nodeValue, "  abcd");
                assert.equal(parent.childNodes.length, 3);

                // This is where wed used to fail.
                event = new $.Event("keydown");
                key_constants.SPACE.setEventToMatch(event);
                event.type = "keypress";
                editor.$gui_root.trigger(event);
                editor._syncDisplay();
                assert.equal(initial.nodeValue, "   abcd");
                assert.equal(parent.childNodes.length, 3);
                done();
            });
        });

        it("does not bring up a contextual menu when there is no caret",
           function (done) {
            editor.whenCondition(
                "first-validation-complete",
                function () {
                var initial = $(editor.gui_root).find(".title").
                    get(0).childNodes[1];
                assert.isUndefined(editor.getCaret());
                var event = new $.Event("contextmenu");
                editor.$gui_root.trigger(event);
                assert.equal(editor.menu_layer.childNodes.length, 0);
                done();
            });
        });

        it("brings up a contextual menu when there is a caret",
           function (done) {
            editor.whenCondition(
                "first-validation-complete",
                function () {
                var initial = $(editor.gui_root).find(".title").
                    get(0).childNodes[1];
                editor.setCaret(initial, 0);
                var event = new $.Event("contextmenu");
                editor.$gui_root.trigger(event);
                assert.equal(editor.menu_layer.childNodes.length, 1);
                done();
            });
        });

        describe("moveCaretRight", function () {
            it("works even if there is no caret defined", function () {
                assert.equal(editor.getCaret(), undefined, "no caret");
                editor.moveCaretRight();
                assert.equal(editor.getCaret(), undefined, "no caret");
            });

            it("moves right into gui elements",
               function (done) {
                editor.whenCondition(
                    "first-validation-complete",
                    function () {
                    var initial = editor.gui_root.childNodes[0];
                    editor.setCaret(initial, 0);
                    caretCheck(editor, initial, 0, "initial");
                    editor.moveCaretRight();
                    var first_gui = firstGUI($(initial));
                    // It is now located inside the text inside
                    // the label which marks the start of the TEI
                    // element.
                    caretCheck(editor, first_gui, 0, "moved once");
                    editor.moveCaretRight();

{'fraction_non_alphanumeric': 0.11524293983511665, 'fraction_numerical': 0.0039174413845524175, 'mean_word_length': 1.4645533141210374, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3066236', 'n_tokens_mistral': 4420, 'n_tokens_neox': 4079, 'n_words': 1065}