summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/sourceeditor/test/browser_editor_basic.js
blob: 9373990bf78d3d3f4c62a7723294fa027c7ad2f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

async function test() {
  waitForExplicitFinish();
  const { ed, win } = await setup();
  // appendTo
  const cmFrame = win.document.querySelector("iframe");
  const cmStyle = cmFrame.contentDocument.getElementById("cmBaseStyle");
  ok(~cmStyle.innerHTML.indexOf(".CodeMirror"), "correct iframe is there");

  // getOption/setOption
  ok(ed.getOption("styleActiveLine"), "getOption works");
  ed.setOption("styleActiveLine", false);
  ok(!ed.getOption("styleActiveLine"), "setOption works");

  // Language modes
  is(ed.getMode(), Editor.modes.text, "getMode");
  ed.setMode(Editor.modes.js);
  is(ed.getMode(), Editor.modes.js, "setMode");

  // Content
  is(ed.getText(), "Hello.", "getText");
  ed.setText("Hi.\nHow are you?");
  is(ed.getText(), "Hi.\nHow are you?", "setText");
  is(ed.getText(1), "How are you?", "getText(num)");
  is(ed.getText(5), "", "getText(num) when num is out of scope");

  ed.replaceText("YOU", { line: 1, ch: 8 }, { line: 1, ch: 11 });
  is(ed.getText(1), "How are YOU?", "replaceText(str, from, to)");
  ed.replaceText("you?", { line: 1, ch: 8 });
  is(ed.getText(1), "How are you?", "replaceText(str, from)");
  ed.replaceText("Hello.");
  is(ed.getText(), "Hello.", "replaceText(str)");

  ed.insertText(", sir/madam", { line: 0, ch: 5 });
  is(ed.getText(), "Hello, sir/madam.", "insertText");

  // Add-ons
  ed.extend({ whoami: () => "Anton", whereami: () => "Mozilla" });
  is(ed.whoami(), "Anton", "extend/1");
  is(ed.whereami(), "Mozilla", "extend/2");

  // Line classes
  ed.setText("Hello!\nHow are you?");
  ok(!ed.hasLineClass(0, "test"), "no test line class");
  ed.addLineClass(0, "test");
  ok(ed.hasLineClass(0, "test"), "test line class is there");
  ed.removeLineClass(0, "test");
  ok(!ed.hasLineClass(0, "test"), "test line class is gone");

  // Font size
  const size = ed.getFontSize();
  is("number", typeof size, "we have the default font size");
  ed.setFontSize(ed.getFontSize() + 1);
  is(ed.getFontSize(), size + 1, "new font size was set");

  info("Check that we display unicode values for non-printable characters");
  ed.setText("> \u202e \u2066 - \u2069 \u2066 <");

  const doc = win.document.querySelector("iframe").contentWindow.document;
  const nonPrintableCharElements = Array.from(
    doc.querySelectorAll(".cm-non-printable-char")
  );

  Assert.deepEqual(
    nonPrintableCharElements.map(el => el.textContent),
    ["\\u202e", "\\u2066", "\\u2069", "\\u2066"],
    "non printable chars are displayed as expected"
  );

  teardown(ed, win);
}