summaryrefslogtreecommitdiff
path: root/tests/frontend/specs/indentation.js
blob: 71aafc7acfaca7d2d7b49c8e5e5f1f32dda3f316 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
describe("indentation button", function(){
  //create a new pad before each test run
  beforeEach(function(cb){
    helper.newPad(cb);
    this.timeout(60000);
  });

 it("indent text with keypress", function(done){
    var inner$ = helper.padInner$;
    var chrome$ = helper.padChrome$;

    //get the first text element out of the inner iframe
    var $firstTextElement = inner$("div").first();

    //select this text element
    $firstTextElement.sendkeys('{selectall}');

    if(inner$(window)[0].bowser.firefox || inner$(window)[0].bowser.modernIE){ // if it's a mozilla or IE
      var evtType = "keypress";
    }else{
      var evtType = "keydown";
    }

    var e = inner$.Event(evtType);
    e.keyCode = 9; // tab :|
    inner$("#innerdocbody").trigger(e);

    helper.waitFor(function(){
      return inner$("div").first().find("ul li").length === 1;
    }).done(done);
  });

  it("indent text with button", function(done){
    var inner$ = helper.padInner$;
    var chrome$ = helper.padChrome$;

    var $indentButton = chrome$(".buttonicon-indent");
    $indentButton.click();

    helper.waitFor(function(){
      return inner$("div").first().find("ul li").length === 1;
    }).done(done);
  });

  it("keeps the indent on enter for the new line", function(done){
    var inner$ = helper.padInner$;
    var chrome$ = helper.padChrome$;

    var $indentButton = chrome$(".buttonicon-indent");
    $indentButton.click();

    //type a bit, make a line break and type again
    var $firstTextElement = inner$("div span").first();
    $firstTextElement.sendkeys('line 1');
    $firstTextElement.sendkeys('{enter}');
    $firstTextElement.sendkeys('line 2');
    $firstTextElement.sendkeys('{enter}');

    helper.waitFor(function(){
      return inner$("div span").first().text().indexOf("line 2") === -1;
    }).done(function(){
      var $newSecondLine = inner$("div").first().next();
      var hasULElement = $newSecondLine.find("ul li").length === 1;

      expect(hasULElement).to.be(true);
      expect($newSecondLine.text()).to.be("line 2");
      done();
    });
  });

  it("indents text with spaces on enter if previous line ends with ':', '[', '(', or '{'", function(done){
    var inner$ = helper.padInner$;
    var chrome$ = helper.padChrome$;

    //type a bit, make a line break and type again
    var $firstTextElement = inner$("div").first();
    $firstTextElement.sendkeys("line with ':'{enter}");
    $firstTextElement.sendkeys("line with '['{enter}");
    $firstTextElement.sendkeys("line with '('{enter}");
    $firstTextElement.sendkeys("line with '{{}'{enter}");

    helper.waitFor(function(){
      return inner$("div span").first().text().indexOf("line with '{'") === -1;
    }).done(function(){
      // we validate bottom to top for easier implementation

      // curly braces
      var $lineWithCurlyBraces = inner$("div").first().next().next().next();
      $lineWithCurlyBraces.sendkeys('{{}');
      pressEnter(); // cannot use sendkeys('{enter}') here, browser does not read the command properly
      var $lineAfterCurlyBraces = inner$("div").first().next().next().next().next();
      expect($lineAfterCurlyBraces.text()).to.match(/\s{4}/); // tab === 4 spaces

      // parenthesis
      var $lineWithParenthesis = inner$("div").first().next().next();
      $lineWithParenthesis.sendkeys('(');
      pressEnter();
      var $lineAfterParenthesis = inner$("div").first().next().next().next();
      expect($lineAfterParenthesis.text()).to.match(/\s{4}/);

      // bracket
      var $lineWithBracket = inner$("div").first().next();
      $lineWithBracket.sendkeys('[');
      pressEnter();
      var $lineAfterBracket = inner$("div").first().next().next();
      expect($lineAfterBracket.text()).to.match(/\s{4}/);

      // colon
      var $lineWithColon = inner$("div").first();
      $lineWithColon.sendkeys(':');
      pressEnter();
      var $lineAfterColon = inner$("div").first().next();
      expect($lineAfterColon.text()).to.match(/\s{4}/);

      done();
    });
  });

  it("appends indentation to the indent of previous line if previous line ends with ':', '[', '(', or '{'", function(done){
    var inner$ = helper.padInner$;
    var chrome$ = helper.padChrome$;

    //type a bit, make a line break and type again
    var $firstTextElement = inner$("div").first();
    $firstTextElement.sendkeys("  line with some indentation and ':'{enter}");
    $firstTextElement.sendkeys("line 2{enter}");

    helper.waitFor(function(){
      return inner$("div span").first().text().indexOf("line 2") === -1;
    }).done(function(){
      var $lineWithColon = inner$("div").first();
      $lineWithColon.sendkeys(':');
      pressEnter();
      var $lineAfterColon = inner$("div").first().next();
      expect($lineAfterColon.text()).to.match(/\s{6}/); // previous line indentation + regular tab (4 spaces)

      done();
    });
  });

  /*

  it("makes text indented and outdented", function() {

    //get the inner iframe
    var $inner = testHelper.$getPadInner();

    //get the first text element out of the inner iframe
    var firstTextElement = $inner.find("div").first();

    //select this text element
    testHelper.selectText(firstTextElement[0], $inner);

    //get the indentation button and click it
    var $indentButton = testHelper.$getPadChrome().find(".buttonicon-indent");
    $indentButton.click();

    //ace creates a new dom element when you press a button, so just get the first text element again
    var newFirstTextElement = $inner.find("div").first();

    // is there a list-indent class element now?
    var firstChild = newFirstTextElement.children(":first");
    var isUL = firstChild.is('ul');

    //expect it to be the beginning of a list
    expect(isUL).to.be(true);

    var secondChild = firstChild.children(":first");
    var isLI = secondChild.is('li');
    //expect it to be part of a list
    expect(isLI).to.be(true);

    //indent again
    $indentButton.click();

    var newFirstTextElement = $inner.find("div").first();

    // is there a list-indent class element now?
    var firstChild = newFirstTextElement.children(":first");
    var hasListIndent2 = firstChild.hasClass('list-indent2');

    //expect it to be part of a list
    expect(hasListIndent2).to.be(true);

    //make sure the text hasn't changed
    expect(newFirstTextElement.text()).to.eql(firstTextElement.text());


    // test outdent

    //get the unindentation button and click it twice
    var $outdentButton = testHelper.$getPadChrome().find(".buttonicon-outdent");
    $outdentButton.click();
    $outdentButton.click();

    //ace creates a new dom element when you press a button, so just get the first text element again
    var newFirstTextElement = $inner.find("div").first();

    // is there a list-indent class element now?
    var firstChild = newFirstTextElement.children(":first");
    var isUL = firstChild.is('ul');

    //expect it not to be the beginning of a list
    expect(isUL).to.be(false);

    var secondChild = firstChild.children(":first");
    var isLI = secondChild.is('li');
    //expect it to not be part of a list
    expect(isLI).to.be(false);

    //make sure the text hasn't changed
    expect(newFirstTextElement.text()).to.eql(firstTextElement.text());


    // Next test tests multiple line indentation

    //select this text element
    testHelper.selectText(firstTextElement[0], $inner);

    //indent twice
    $indentButton.click();
    $indentButton.click();

    //get the first text element out of the inner iframe
    var firstTextElement = $inner.find("div").first();

    //select this text element
    testHelper.selectText(firstTextElement[0], $inner);

    /* this test creates the below content, both should have double indentation
    line1
    line2


    firstTextElement.sendkeys('{rightarrow}'); // simulate a keypress of enter
    firstTextElement.sendkeys('{enter}'); // simulate a keypress of enter
    firstTextElement.sendkeys('line 1'); // simulate writing the first line
    firstTextElement.sendkeys('{enter}'); // simulate a keypress of enter
    firstTextElement.sendkeys('line 2'); // simulate writing the second line

    //get the second text element out of the inner iframe
    setTimeout(function(){ // THIS IS REALLY BAD
      var secondTextElement = $('iframe').contents().find('iframe').contents().find('iframe').contents().find('body > div').get(1); // THIS IS UGLY

      // is there a list-indent class element now?
      var firstChild = secondTextElement.children(":first");
      var isUL = firstChild.is('ul');

      //expect it to be the beginning of a list
      expect(isUL).to.be(true);

      var secondChild = secondChild.children(":first");
      var isLI = secondChild.is('li');
      //expect it to be part of a list
      expect(isLI).to.be(true);

      //get the first text element out of the inner iframe
      var thirdTextElement = $('iframe').contents().find('iframe').contents().find('iframe').contents().find('body > div').get(2); // THIS IS UGLY TOO

      // is there a list-indent class element now?
      var firstChild = thirdTextElement.children(":first");
      var isUL = firstChild.is('ul');

      //expect it to be the beginning of a list
      expect(isUL).to.be(true);

      var secondChild = firstChild.children(":first");
      var isLI = secondChild.is('li');

      //expect it to be part of a list
      expect(isLI).to.be(true);
    },1000);
  });*/

});

function pressEnter(){
  var inner$ = helper.padInner$;
  if(inner$(window)[0].bowser.firefox || inner$(window)[0].bowser.modernIE){ // if it's a mozilla or IE
    var evtType = "keypress";
  }else{
    var evtType = "keydown";
  }
  var e = inner$.Event(evtType);
  e.keyCode = 13; // enter :|
  inner$("#innerdocbody").trigger(e);
}