summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/editing/commands/apply_block_element_command_test.cc
blob: 0630b0654e59235acf94cade637edb0cbcd10c66 (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
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/qualified_name.h"
#include "third_party/blink/renderer/core/editing/commands/format_block_command.h"
#include "third_party/blink/renderer/core/editing/commands/indent_outdent_command.h"
#include "third_party/blink/renderer/core/editing/frame_selection.h"
#include "third_party/blink/renderer/core/editing/position.h"
#include "third_party/blink/renderer/core/editing/selection_template.h"
#include "third_party/blink/renderer/core/editing/testing/editing_test_base.h"
#include "third_party/blink/renderer/core/editing/visible_selection.h"
#include "third_party/blink/renderer/core/html/html_head_element.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/platform/bindings/exception_state.h"

#include <memory>

namespace blink {

class ApplyBlockElementCommandTest : public EditingTestBase {};

// This is a regression test for https://crbug.com/639534
TEST_F(ApplyBlockElementCommandTest, selectionCrossingOverBody) {
  GetDocument().head()->insertAdjacentHTML(
      "afterbegin",
      "<style> .CLASS13 { -webkit-user-modify: read-write; }</style></head>",
      ASSERT_NO_EXCEPTION);
  GetDocument().body()->insertAdjacentHTML(
      "afterbegin",
      "\n<pre><var id='va' class='CLASS13'>\nC\n</var></pre><input />",
      ASSERT_NO_EXCEPTION);
  GetDocument().body()->insertAdjacentText("beforebegin", "foo",
                                           ASSERT_NO_EXCEPTION);

  GetDocument().body()->setContentEditable("false", ASSERT_NO_EXCEPTION);
  GetDocument().setDesignMode("on");
  GetDocument().UpdateStyleAndLayout();
  Selection().SetSelection(
      SelectionInDOMTree::Builder()
          .SetBaseAndExtent(
              Position(GetDocument().documentElement(), 1),
              Position(GetDocument().getElementById("va")->firstChild(), 2))
          .Build(),
      SetSelectionOptions());

  auto* command = MakeGarbageCollected<FormatBlockCommand>(
      GetDocument(), html_names::kFooterTag);
  command->Apply();

  EXPECT_EQ(
      "<body contenteditable=\"false\">\n"
      "<pre><var id=\"va\" class=\"CLASS13\">\nC\n</var></pre><input></body>",
      GetDocument().documentElement()->InnerHTMLAsString());
}

// This is a regression test for https://crbug.com/660801
TEST_F(ApplyBlockElementCommandTest, visibilityChangeDuringCommand) {
  GetDocument().head()->insertAdjacentHTML(
      "afterbegin", "<style>li:first-child { visibility:visible; }</style>",
      ASSERT_NO_EXCEPTION);
  SetBodyContent("<ul style='visibility:hidden'><li>xyz</li></ul>");
  GetDocument().setDesignMode("on");

  UpdateAllLifecyclePhasesForTest();
  Selection().SetSelection(
      SelectionInDOMTree::Builder()
          .Collapse(Position(GetDocument().QuerySelector("li"), 0))
          .Build(),
      SetSelectionOptions());

  auto* command = MakeGarbageCollected<IndentOutdentCommand>(
      GetDocument(), IndentOutdentCommand::kIndent);
  command->Apply();

  EXPECT_EQ(
      "<head><style>li:first-child { visibility:visible; }</style></head>"
      "<body><ul style=\"visibility:hidden\"><ul></ul><li>xyz</li></ul></body>",
      GetDocument().documentElement()->InnerHTMLAsString());
}

// This is a regression test for https://crbug.com/712510
TEST_F(ApplyBlockElementCommandTest, IndentHeadingIntoBlockquote) {
  SetBodyContent(
      "<div contenteditable=\"true\">"
      "<h6><button><table></table></button></h6>"
      "<object></object>"
      "</div>");
  Element* button = GetDocument().QuerySelector("button");
  Element* object = GetDocument().QuerySelector("object");
  Selection().SetSelection(SelectionInDOMTree::Builder()
                               .Collapse(Position(button, 0))
                               .Extend(Position(object, 0))
                               .Build(),
                           SetSelectionOptions());

  auto* command = MakeGarbageCollected<IndentOutdentCommand>(
      GetDocument(), IndentOutdentCommand::kIndent);
  command->Apply();

  // This only records the current behavior, which can be wrong.
  EXPECT_EQ(
      "<div contenteditable=\"true\">"
      "<blockquote style=\"margin: 0 0 0 40px; border: none; padding: 0px;\">"
      "<h6><button></button></h6>"
      "<h6><button><table></table></button></h6>"
      "</blockquote>"
      "<h6><button></button></h6><br>"
      "<object></object>"
      "</div>",
      GetDocument().body()->InnerHTMLAsString());
}

// This is a regression test for https://crbug.com/806525
TEST_F(ApplyBlockElementCommandTest, InsertPlaceHolderAtDisconnectedPosition) {
  GetDocument().setDesignMode("on");
  InsertStyleElement(".input:nth-of-type(2n+1) { visibility:collapse; }");
  Selection().SetSelection(
      SetSelectionTextToBody(
          "^<input><input class=\"input\" style=\"position:absolute\">|"),
      SetSelectionOptions());
  auto* command = MakeGarbageCollected<FormatBlockCommand>(GetDocument(),
                                                           html_names::kPreTag);
  // Crash happens here.
  EXPECT_FALSE(command->Apply());
  EXPECT_EQ(
      "<pre>|<input></pre><input class=\"input\" style=\"position:absolute\">",
      GetSelectionTextFromBody());
}

// https://crbug.com/873084
TEST_F(ApplyBlockElementCommandTest, FormatBlockCrossingUserModifyBoundary) {
  InsertStyleElement("*{-webkit-user-modify:read-write}");
  Selection().SetSelection(
      SetSelectionTextToBody(
          "^<b style=\"-webkit-user-modify:read-only\"><button></button></b>|"),
      SetSelectionOptions());
  auto* command = MakeGarbageCollected<FormatBlockCommand>(GetDocument(),
                                                           html_names::kPreTag);
  // Shouldn't crash here.
  EXPECT_FALSE(command->Apply());
  EXPECT_EQ(
      "^<b style=\"-webkit-user-modify:read-only\"><button>|</button></b>",
      GetSelectionTextFromBody());
}

// https://crbug.com/873084
TEST_F(ApplyBlockElementCommandTest,
       FormatBlockWithTableCrossingUserModifyBoundary) {
  InsertStyleElement("*{-webkit-user-modify:read-write}");
  Selection().SetSelection(
      SetSelectionTextToBody("^<table></table>"
                             "<kbd "
                             "style=\"-webkit-user-modify:read-only\"><button><"
                             "/button></kbd>|"),
      SetSelectionOptions());
  auto* command = MakeGarbageCollected<FormatBlockCommand>(GetDocument(),
                                                           html_names::kPreTag);
  // Shouldn't crash here.
  EXPECT_FALSE(command->Apply());
  EXPECT_EQ(
      "<table>^</table>"
      "<kbd style=\"-webkit-user-modify:read-only\"><button>|</button></kbd>",
      GetSelectionTextFromBody());
}

}  // namespace blink