tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_detectindent.js (2195B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const TWO_SPACES_CODE = [
      7  "/*",
      8  " * tricky comment block",
      9  " */",
     10  "div {",
     11  "  color: red;",
     12  "  background: blue;",
     13  "}",
     14  "     ",
     15  "span {",
     16  "  padding-left: 10px;",
     17  "}",
     18 ].join("\n");
     19 
     20 const FOUR_SPACES_CODE = [
     21  "var obj = {",
     22  "    addNumbers: function() {",
     23  "        var x = 5;",
     24  "        var y = 18;",
     25  "        return x + y;",
     26  "    },",
     27  "   ",
     28  "    /*",
     29  "     * Do some stuff to two numbers",
     30  "     * ",
     31  "     * @param x",
     32  "     * @param y",
     33  "     * ",
     34  "     * @return the result of doing stuff",
     35  "     */",
     36  "    subtractNumbers: function(x, y) {",
     37  "        var x += 7;",
     38  "        var y += 18;",
     39  "        var result = x - y;",
     40  "        result %= 2;",
     41  "    }",
     42  "}",
     43 ].join("\n");
     44 
     45 const TABS_CODE = [
     46  "/*",
     47  " * tricky comment block",
     48  " */",
     49  "div {",
     50  "\tcolor: red;",
     51  "\tbackground: blue;",
     52  "}",
     53  "",
     54  "span {",
     55  "\tpadding-left: 10px;",
     56  "}",
     57 ].join("\n");
     58 
     59 const NONE_CODE = [
     60  "var x = 0;",
     61  "           // stray thing",
     62  "var y = 9;",
     63  "    ",
     64  "",
     65 ].join("\n");
     66 
     67 async function test() {
     68  waitForExplicitFinish();
     69 
     70  const { ed, win } = await setup();
     71  is(ed.getOption("indentUnit"), 2, "2 spaces before code added");
     72  is(ed.getOption("indentWithTabs"), false, "spaces is default");
     73 
     74  ed.setText(NONE_CODE);
     75  is(ed.getOption("indentUnit"), 2, "2 spaces after un-detectable code");
     76  is(
     77    ed.getOption("indentWithTabs"),
     78    false,
     79    "spaces still set after un-detectable code"
     80  );
     81 
     82  ed.setText(FOUR_SPACES_CODE);
     83  is(ed.getOption("indentUnit"), 4, "4 spaces detected in 4 space code");
     84  is(ed.getOption("indentWithTabs"), false, "spaces detected in 4 space code");
     85 
     86  ed.setText(TWO_SPACES_CODE);
     87  is(ed.getOption("indentUnit"), 2, "2 spaces detected in 2 space code");
     88  is(ed.getOption("indentWithTabs"), false, "spaces detected in 2 space code");
     89 
     90  ed.setText(TABS_CODE);
     91  is(ed.getOption("indentUnit"), 2, "2 space indentation unit");
     92  is(
     93    ed.getOption("indentWithTabs"),
     94    true,
     95    "tabs detected in majority tabs code"
     96  );
     97 
     98  teardown(ed, win);
     99 }