@@ -4,13 +4,44 @@ Keyboard = Cypress.Keyboard
4
4
Promise = Cypress .Promise
5
5
$selection = require (" ../../../../../src/dom/selection" )
6
6
7
+ # # trim new lines at the end of innerText
8
+ # # due to changing browser versions implementing
9
+ # # this differently
10
+ trimInnerText = ($el ) ->
11
+ _ .trimEnd ($el .get (0 ).innerText , " \n " )
12
+
7
13
describe " src/cy/commands/actions/type" , ->
8
14
before ->
9
15
cy
10
16
.visit (" /fixtures/dom.html" )
11
17
.then (win) ->
12
18
@body = win .document .body .outerHTML
13
19
20
+ el = cy .$$ (' [contenteditable]:first' ).get (0 )
21
+
22
+ innerHtml = el .innerHTML
23
+
24
+ # # by default... the last new line by itself
25
+ # # will only ever count as a single new line...
26
+ # # but new lines above it will count as 2 new lines...
27
+ # # so by adding "3" new lines, the last counts as 1
28
+ # # and the first 2 count as 2...
29
+ el .innerHTML = ' <div><br></div>' .repeat (3 )
30
+
31
+ # # browsers changed their implementation
32
+ # # of the number of newlines that <div><br></div>
33
+ # # create. newer versions of chrome set 2 new lines
34
+ # # per set - whereas older ones create only 1 new line.
35
+ # # so we grab the current sets for the assertion later
36
+ # # so this test is browser version agnostic
37
+ newLines = el .innerText
38
+
39
+ # # disregard the last new line, and divide by 2...
40
+ # # this tells us how many multiples of new lines
41
+ # # the browser inserts for new lines other than
42
+ # # the last new line
43
+ @multiplierNumNewLines = (newLines .length - 1 ) / 2
44
+
14
45
beforeEach ->
15
46
doc = cy .state (" document" )
16
47
@@ -1038,29 +1069,29 @@ describe "src/cy/commands/actions/type", ->
1038
1069
cy .$$ (' [contenteditable]:first' ).get (0 ).innerHTML = ' <div>foo</div>'
1039
1070
cy .get (" [contenteditable]:first" )
1040
1071
.type (" bar" ).then ($div) ->
1041
- expect ($div . get ( 0 ). innerText ).to .eql (" foobar\n " )
1072
+ expect (trimInnerText ( $div) ).to .eql (" foobar" )
1042
1073
expect ($div .get (0 ).textContent ).to .eql (" foobar" )
1043
1074
expect ($div .get (0 ).innerHTML ).to .eql (" <div>foobar</div>" )
1044
1075
1045
1076
it " can type into [contenteditable] with existing <p>" , ->
1046
1077
cy .$$ (' [contenteditable]:first' ).get (0 ).innerHTML = ' <p>foo</p>'
1047
1078
cy .get (" [contenteditable]:first" )
1048
1079
.type (" bar" ).then ($div) ->
1049
- expect ($div . get ( 0 ). innerText ).to .eql (" foobar\n\n " )
1080
+ expect (trimInnerText ( $div) ).to .eql (" foobar" )
1050
1081
expect ($div .get (0 ).textContent ).to .eql (" foobar" )
1051
1082
expect ($div .get (0 ).innerHTML ).to .eql (" <p>foobar</p>" )
1052
1083
1053
1084
it " collapses selection to start on {leftarrow}" , ->
1054
1085
cy .$$ (' [contenteditable]:first' ).get (0 ).innerHTML = ' <div>bar</div>'
1055
1086
cy .get (" [contenteditable]:first" )
1056
1087
.type (" {selectall}{leftarrow}foo" ).then ($div) ->
1057
- expect ($div . get ( 0 ). innerText ).to .eql (" foobar\n " )
1088
+ expect (trimInnerText ( $div) ).to .eql (" foobar" )
1058
1089
1059
1090
it " collapses selection to end on {rightarrow}" , ->
1060
1091
cy .$$ (' [contenteditable]:first' ).get (0 ).innerHTML = ' <div>bar</div>'
1061
1092
cy .get (" [contenteditable]:first" )
1062
1093
.type (" {selectall}{leftarrow}foo{selectall}{rightarrow}baz" ).then ($div) ->
1063
- expect ($div . get ( 0 ). innerText ).to .eql (" foobarbaz\n " )
1094
+ expect (trimInnerText ( $div) ).to .eql (" foobarbaz" )
1064
1095
1065
1096
it " can remove a placeholder <br>" , ->
1066
1097
cy .$$ (' [contenteditable]:first' ).get (0 ).innerHTML = ' <div><br></div>'
@@ -1451,7 +1482,7 @@ describe "src/cy/commands/actions/type", ->
1451
1482
1452
1483
cy .get (" [contenteditable]:first" )
1453
1484
.type (" {home}11{uparrow}{home}22{uparrow}{home}33" ).then ($div) ->
1454
- expect ($div . get ( 0 ). innerText ).to .eql (" 33foo\n 22bar\n 11baz\n " )
1485
+ expect (trimInnerText ( $div) ).to .eql (" 33foo\n 22bar\n 11baz" )
1455
1486
1456
1487
context " {end}" , ->
1457
1488
it " sets which and keyCode to 35 and does not fire keypress events" , (done ) ->
@@ -1501,7 +1532,7 @@ describe "src/cy/commands/actions/type", ->
1501
1532
1502
1533
cy .get (" [contenteditable]:first" )
1503
1534
.type (" {end}11{uparrow}{end}22{uparrow}{end}33" ).then ($div) ->
1504
- expect ($div . get ( 0 ). innerText ).to .eql (" foo33\n bar22\n baz11\n " )
1535
+ expect (trimInnerText ( $div) ).to .eql (" foo33\n bar22\n baz11" )
1505
1536
1506
1537
context " {uparrow}" , ->
1507
1538
beforeEach ->
@@ -1540,7 +1571,7 @@ describe "src/cy/commands/actions/type", ->
1540
1571
1541
1572
cy .get (" [contenteditable]:first" )
1542
1573
.type (" {leftarrow}{leftarrow}{uparrow}11{uparrow}22{downarrow}{downarrow}33" ).then ($div) ->
1543
- expect ($div . get ( 0 ). innerText ).to .eql (" foo22\n b11ar\n baz33\n " )
1574
+ expect (trimInnerText ( $div) ).to .eql (" foo22\n b11ar\n baz33" )
1544
1575
1545
1576
it " uparrow ignores current selection" , ->
1546
1577
ce = cy .$$ (' [contenteditable]:first' ).get (0 )
@@ -1556,7 +1587,7 @@ describe "src/cy/commands/actions/type", ->
1556
1587
1557
1588
cy .get (" [contenteditable]:first" )
1558
1589
.type (" {uparrow}11" ).then ($div) ->
1559
- expect ($div . get ( 0 ). innerText ).to .eql (" 11foo\n bar\n baz\n " )
1590
+ expect (trimInnerText ( $div) ).to .eql (" 11foo\n bar\n baz" )
1560
1591
1561
1592
it " up and down arrow on textarea" , ->
1562
1593
cy .$$ (' textarea:first' ).get (0 ).value = ' foo\n bar\n baz'
@@ -1570,7 +1601,6 @@ describe "src/cy/commands/actions/type", ->
1570
1601
.type (' {uparrow}{uparrow}' )
1571
1602
.should (' have.value' , ' 14' )
1572
1603
1573
-
1574
1604
context " {downarrow}" , ->
1575
1605
beforeEach ->
1576
1606
cy .$$ (" #comments" ).val (" foo\n bar\n baz" )
@@ -1626,7 +1656,7 @@ describe "src/cy/commands/actions/type", ->
1626
1656
1627
1657
cy .get (" [contenteditable]:first" )
1628
1658
.type (" {downarrow}22" ).then ($div) ->
1629
- expect ($div . get ( 0 ). innerText ).to .eql (" foo\n 22bar\n baz\n " )
1659
+ expect (trimInnerText ( $div) ).to .eql (" foo\n 22bar\n baz" )
1630
1660
1631
1661
context " {selectall}{del}" , ->
1632
1662
it " can select all the text and delete" , ->
@@ -1688,14 +1718,16 @@ describe "src/cy/commands/actions/type", ->
1688
1718
it " inserts new line into [contenteditable] " , ->
1689
1719
cy .get (" #input-types [contenteditable]:first" ).invoke (" text" , " foo" )
1690
1720
.type (" bar{enter}baz{enter}{enter}{enter}quux" ).then ($div) ->
1691
- expect ($div .get (0 ).innerText ).to .eql (" foobar\n baz\n\n\n quux\n " )
1721
+ conditionalNewLines = " \n\n " .repeat (@multiplierNumNewLines )
1722
+
1723
+ expect (trimInnerText ($div)).to .eql (" foobar\n baz#{ conditionalNewLines} \n quux" )
1692
1724
expect ($div .get (0 ).textContent ).to .eql (" foobarbazquux" )
1693
1725
expect ($div .get (0 ).innerHTML ).to .eql (" foobar<div>baz</div><div><br></div><div><br></div><div>quux</div>" )
1694
1726
1695
1727
it " inserts new line into [contenteditable] from midline" , ->
1696
1728
cy .get (" #input-types [contenteditable]:first" ).invoke (" text" , " foo" )
1697
1729
.type (" bar{leftarrow}{enter}baz{leftarrow}{enter}quux" ).then ($div) ->
1698
- expect ($div . get ( 0 ). innerText ).to .eql (" fooba\n ba\n quuxzr\n " )
1730
+ expect (trimInnerText ( $div) ).to .eql (" fooba\n ba\n quuxzr" )
1699
1731
expect ($div .get (0 ).textContent ).to .eql (" foobabaquuxzr" )
1700
1732
expect ($div .get (0 ).innerHTML ).to .eql (" fooba<div>ba</div><div>quuxzr</div>" )
1701
1733
@@ -2312,7 +2344,6 @@ describe "src/cy/commands/actions/type", ->
2312
2344
.then ->
2313
2345
expect (changed).to .eql 0
2314
2346
2315
-
2316
2347
describe " caret position" , ->
2317
2348
2318
2349
it " respects being formatted by input event handlers"
@@ -2380,32 +2411,35 @@ describe "src/cy/commands/actions/type", ->
2380
2411
el .innerHTML = ' start' +
2381
2412
' <div>middle</div>' +
2382
2413
' <div>end</div>'
2414
+
2383
2415
cy .get (' [contenteditable]:first' )
2384
2416
# # move cursor to beginning of div
2385
2417
.type (' {selectall}{leftarrow}' )
2386
- .type (' {rightarrow}' .repeat (14 )+ ' [_I_]' ).then ->
2387
- expect (cy . $$ ( ' [contenteditable]:first ' ). get ( 0 ). innerText ). to .eql (' start\n middle\n e[_I_]nd\n ' )
2418
+ .type (' {rightarrow}' .repeat (14 )+ ' [_I_]' ).then ($el) ->
2419
+ expect (trimInnerText ($el)). to .eql (' start\n middle\n e[_I_]nd' )
2388
2420
2389
2421
it " can wrap cursor to prev line in [contenteditable] with {leftarrow}" , ->
2390
2422
$el = cy .$$ (' [contenteditable]:first' )
2391
2423
el = $el .get (0 )
2424
+
2392
2425
el .innerHTML = ' start' +
2393
2426
' <div>middle</div>' +
2394
2427
' <div>end</div>'
2395
- cy .get (' [contenteditable]:first' ).type (' {leftarrow}' .repeat (12 )+ ' [_I_]' ).then ->
2396
- expect (cy .$$ (' [contenteditable]:first' ).get (0 ).innerText ).to .eql (' star[_I_]t\n middle\n end\n ' )
2397
2428
2429
+ cy .get (' [contenteditable]:first' ).type (' {leftarrow}' .repeat (12 )+ ' [_I_]' ).then ($el) ->
2430
+ expect (trimInnerText ($el)).to .eql (' star[_I_]t\n middle\n end' )
2398
2431
2399
2432
it " can wrap cursor to next line in [contenteditable] with {rightarrow} and empty lines" , ->
2400
2433
$el = cy .$$ (' [contenteditable]:first' )
2401
2434
el = $el .get (0 )
2402
- el .innerHTML = ' <div><br></div>' .repeat (4 )+
2403
- ' <div>end</div>'
2435
+ el .innerHTML = ' <div><br></div>' .repeat (4 ) + ' <div>end</div>'
2436
+
2437
+ newLines = " \n\n\n " .repeat (@multiplierNumNewLines )
2404
2438
2405
2439
cy .get (' [contenteditable]:first' )
2406
2440
.type (' {selectall}{leftarrow}' )
2407
- # .type('foobar'+'{rightarrow}'.repeat(6)+'[_I_]').then ->
2408
- # expect(cy.$$('[contenteditable]:first').get(0).innerText). to.eql(' foobar\n\n\n\ nen[_I_]d\n' )
2441
+ .type (' foobar' + ' {rightarrow}' .repeat (6 )+ ' [_I_]' ).then ->
2442
+ expect (trimInnerText ($el)). to .eql (" foobar#{ newLines } \ n en[_I_]d" )
2409
2443
2410
2444
it " can use {rightarrow} and nested elements" , ->
2411
2445
$el = cy .$$ (' [contenteditable]:first' )
@@ -2415,19 +2449,19 @@ describe "src/cy/commands/actions/type", ->
2415
2449
cy .get (' [contenteditable]:first' )
2416
2450
.type (' {selectall}{leftarrow}' )
2417
2451
.type (' {rightarrow}' .repeat (3 )+ ' [_I_]' ).then ->
2418
- expect (cy . $$ ( ' [contenteditable]:first ' ). get ( 0 ). innerText ). to .eql (' sta[_I_]rt\n ' )
2452
+ expect (trimInnerText ($el)). to .eql (' sta[_I_]rt' )
2419
2453
2420
2454
it " enter and \\ n should act the same for [contenteditable]" , ->
2421
-
2422
2455
cleanseText = (text ) ->
2423
- text .replace (/ / g , ' ' )
2456
+ # # non breaking white space
2457
+ text .split (' \u 00a0' ).join (' ' )
2424
2458
2425
2459
expectMatchInnerText = ($el , innerText ) ->
2426
- expect (cleanseText ($el . get ( 0 ). innerText )).to .eql (innerText)
2460
+ expect (cleanseText (trimInnerText ( $el) )).to .eql (innerText)
2427
2461
2428
2462
# # NOTE: this may only pass in Chrome since the whitespace may be different in other browsers
2429
2463
# # even if actual and expected appear the same.
2430
- expected = " {\n foo: 1\n bar: 2\n baz: 3\n }\n "
2464
+ expected = " {\n foo: 1\n bar: 2\n baz: 3\n }"
2431
2465
cy .get (' [contenteditable]:first' )
2432
2466
.invoke (' html' , ' <div><br></div>' )
2433
2467
.type (' {{}{enter} foo: 1{enter} bar: 2{enter} baz: 3{enter}}' )
@@ -2438,7 +2472,6 @@ describe "src/cy/commands/actions/type", ->
2438
2472
.should ($el) ->
2439
2473
expectMatchInnerText ($el, expected)
2440
2474
2441
-
2442
2475
it " enter and \\ n should act the same for textarea" , ->
2443
2476
expected = " {\n foo: 1\n bar: 2\n baz: 3\n }"
2444
2477
cy .get (' textarea:first' )
@@ -2449,8 +2482,6 @@ describe "src/cy/commands/actions/type", ->
2449
2482
.type (' {{}\n foo: 1\n bar: 2\n baz: 3\n }' )
2450
2483
.should (' have.prop' , ' value' , expected)
2451
2484
2452
-
2453
-
2454
2485
describe " {enter}" , ->
2455
2486
beforeEach ->
2456
2487
@$forms = cy .$$ (" #form-submits" )
0 commit comments