summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/editing/commands/apply_block_element_command.cc
blob: 492ac08996a3341ad20e346d90c15f8a2a1621aa (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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/*
 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved.
 * Copyright (C) 2010 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "third_party/blink/renderer/core/editing/commands/apply_block_element_command.h"

#include "third_party/blink/renderer/bindings/core/v8/exception_state.h"
#include "third_party/blink/renderer/core/dom/node_computed_style.h"
#include "third_party/blink/renderer/core/dom/text.h"
#include "third_party/blink/renderer/core/editing/commands/editing_commands_utilities.h"
#include "third_party/blink/renderer/core/editing/editing_utilities.h"
#include "third_party/blink/renderer/core/editing/selection_template.h"
#include "third_party/blink/renderer/core/editing/visible_position.h"
#include "third_party/blink/renderer/core/editing/visible_selection.h"
#include "third_party/blink/renderer/core/editing/visible_units.h"
#include "third_party/blink/renderer/core/html/html_br_element.h"
#include "third_party/blink/renderer/core/html/html_element.h"
#include "third_party/blink/renderer/core/html_names.h"
#include "third_party/blink/renderer/core/style/computed_style.h"

namespace blink {

using namespace HTMLNames;

ApplyBlockElementCommand::ApplyBlockElementCommand(
    Document& document,
    const QualifiedName& tag_name,
    const AtomicString& inline_style)
    : CompositeEditCommand(document),
      tag_name_(tag_name),
      inline_style_(inline_style) {}

ApplyBlockElementCommand::ApplyBlockElementCommand(
    Document& document,
    const QualifiedName& tag_name)
    : CompositeEditCommand(document), tag_name_(tag_name) {}

void ApplyBlockElementCommand::DoApply(EditingState* editing_state) {
  // ApplyBlockElementCommands are only created directly by editor commands'
  // execution, which updates layout before entering doApply().
  DCHECK(!GetDocument().NeedsLayoutTreeUpdate());

  if (!RootEditableElementOf(EndingSelection().Base()))
    return;

  VisiblePosition visible_end = EndingVisibleSelection().VisibleEnd();
  VisiblePosition visible_start = EndingVisibleSelection().VisibleStart();
  if (visible_start.IsNull() || visible_start.IsOrphan() ||
      visible_end.IsNull() || visible_end.IsOrphan())
    return;

  // When a selection ends at the start of a paragraph, we rarely paint
  // the selection gap before that paragraph, because there often is no gap.
  // In a case like this, it's not obvious to the user that the selection
  // ends "inside" that paragraph, so it would be confusing if Indent/Outdent
  // operated on that paragraph.
  // FIXME: We paint the gap before some paragraphs that are indented with left
  // margin/padding, but not others.  We should make the gap painting more
  // consistent and then use a left margin/padding rule here.
  if (visible_end.DeepEquivalent() != visible_start.DeepEquivalent() &&
      IsStartOfParagraph(visible_end)) {
    const Position& new_end =
        PreviousPositionOf(visible_end, kCannotCrossEditingBoundary)
            .DeepEquivalent();
    SelectionInDOMTree::Builder builder;
    builder.Collapse(visible_start.ToPositionWithAffinity());
    if (new_end.IsNotNull())
      builder.Extend(new_end);
    SetEndingSelection(SelectionForUndoStep::From(builder.Build()));
  }

  VisibleSelection selection =
      SelectionForParagraphIteration(EndingVisibleSelection());
  VisiblePosition start_of_selection = selection.VisibleStart();
  VisiblePosition end_of_selection = selection.VisibleEnd();
  DCHECK(!start_of_selection.IsNull());
  DCHECK(!end_of_selection.IsNull());
  ContainerNode* start_scope = nullptr;
  int start_index = IndexForVisiblePosition(start_of_selection, start_scope);
  ContainerNode* end_scope = nullptr;
  int end_index = IndexForVisiblePosition(end_of_selection, end_scope);

  FormatSelection(start_of_selection, end_of_selection, editing_state);
  if (editing_state->IsAborted())
    return;

  GetDocument().UpdateStyleAndLayoutIgnorePendingStylesheets();

  DCHECK_EQ(start_scope, end_scope);
  DCHECK_GE(start_index, 0);
  DCHECK_LE(start_index, end_index);
  if (start_scope == end_scope && start_index >= 0 &&
      start_index <= end_index) {
    VisiblePosition start(VisiblePositionForIndex(start_index, start_scope));
    VisiblePosition end(VisiblePositionForIndex(end_index, end_scope));
    if (start.IsNotNull() && end.IsNotNull()) {
      SetEndingSelection(SelectionForUndoStep::From(
          SelectionInDOMTree::Builder()
              .Collapse(start.ToPositionWithAffinity())
              .Extend(end.DeepEquivalent())
              .Build()));
    }
  }
}

static bool IsAtUnsplittableElement(const Position& pos) {
  Node* node = pos.AnchorNode();
  return node == RootEditableElementOf(pos) ||
         node == EnclosingNodeOfType(pos, &IsTableCell);
}

void ApplyBlockElementCommand::FormatSelection(
    const VisiblePosition& start_of_selection,
    const VisiblePosition& end_of_selection,
    EditingState* editing_state) {
  // Special case empty unsplittable elements because there's nothing to split
  // and there's nothing to move.
  const Position& caret_position =
      MostForwardCaretPosition(start_of_selection.DeepEquivalent());
  if (IsAtUnsplittableElement(caret_position)) {
    HTMLElement* blockquote = CreateBlockElement();
    InsertNodeAt(blockquote, caret_position, editing_state);
    if (editing_state->IsAborted())
      return;
    HTMLBRElement* placeholder = HTMLBRElement::Create(GetDocument());
    AppendNode(placeholder, blockquote, editing_state);
    if (editing_state->IsAborted())
      return;
    SetEndingSelection(SelectionForUndoStep::From(
        SelectionInDOMTree::Builder()
            .Collapse(Position::BeforeNode(*placeholder))
            .Build()));
    return;
  }

  HTMLElement* blockquote_for_next_indent = nullptr;
  VisiblePosition end_of_current_paragraph = EndOfParagraph(start_of_selection);
  const VisiblePosition& visible_end_of_last_paragraph =
      EndOfParagraph(end_of_selection);
  const Position& end_of_next_last_paragraph =
      EndOfParagraph(NextPositionOf(visible_end_of_last_paragraph))
          .DeepEquivalent();
  Position end_of_last_paragraph =
      visible_end_of_last_paragraph.DeepEquivalent();

  bool at_end = false;
  while (end_of_current_paragraph.DeepEquivalent() !=
             end_of_next_last_paragraph &&
         !at_end) {
    if (end_of_current_paragraph.DeepEquivalent() == end_of_last_paragraph)
      at_end = true;

    Position start, end;
    RangeForParagraphSplittingTextNodesIfNeeded(
        end_of_current_paragraph, end_of_last_paragraph, start, end);
    end_of_current_paragraph = CreateVisiblePosition(end);

    Node* enclosing_cell = EnclosingNodeOfType(start, &IsTableCell);
    PositionWithAffinity end_of_next_paragraph =
        EndOfNextParagrahSplittingTextNodesIfNeeded(
            end_of_current_paragraph, end_of_last_paragraph, start, end)
            .ToPositionWithAffinity();

    FormatRange(start, end, end_of_last_paragraph, blockquote_for_next_indent,
                editing_state);
    if (editing_state->IsAborted())
      return;

    // Don't put the next paragraph in the blockquote we just created for this
    // paragraph unless the next paragraph is in the same cell.
    if (enclosing_cell &&
        enclosing_cell !=
            EnclosingNodeOfType(end_of_next_paragraph.GetPosition(),
                                &IsTableCell))
      blockquote_for_next_indent = nullptr;

    // indentIntoBlockquote could move more than one paragraph if the paragraph
    // is in a list item or a table. As a result,
    // |endOfNextLastParagraph| could refer to a position no longer in the
    // document.
    if (end_of_next_last_paragraph.IsNotNull() &&
        !end_of_next_last_paragraph.IsConnected())
      break;
    // Sanity check: Make sure our moveParagraph calls didn't remove
    // endOfNextParagraph.anchorNode() If somehow, e.g. mutation
    // event handler, we did, return to prevent crashes.
    if (end_of_next_paragraph.IsNotNull() &&
        !end_of_next_paragraph.IsConnected())
      return;

    GetDocument().UpdateStyleAndLayoutIgnorePendingStylesheets();
    end_of_current_paragraph = CreateVisiblePosition(end_of_next_paragraph);
  }
}

static bool IsNewLineAtPosition(const Position& position) {
  Node* text_node = position.ComputeContainerNode();
  int offset = position.OffsetInContainerNode();
  if (!text_node || !text_node->IsTextNode() || offset < 0 ||
      offset >= static_cast<int>(ToText(text_node)->length()))
    return false;

  DummyExceptionStateForTesting exception_state;
  String text_at_position =
      ToText(text_node)->substringData(offset, 1, exception_state);
  if (exception_state.HadException())
    return false;

  return text_at_position[0] == '\n';
}

static const ComputedStyle* ComputedStyleOfEnclosingTextNode(
    const Position& position) {
  if (!position.IsOffsetInAnchor() || !position.ComputeContainerNode() ||
      !position.ComputeContainerNode()->IsTextNode())
    return nullptr;
  return position.ComputeContainerNode()->GetComputedStyle();
}

void ApplyBlockElementCommand::RangeForParagraphSplittingTextNodesIfNeeded(
    const VisiblePosition& end_of_current_paragraph,
    Position& end_of_last_paragraph,
    Position& start,
    Position& end) {
  start = StartOfParagraph(end_of_current_paragraph).DeepEquivalent();
  end = end_of_current_paragraph.DeepEquivalent();

  bool is_start_and_end_on_same_node = false;
  if (const ComputedStyle* start_style =
          ComputedStyleOfEnclosingTextNode(start)) {
    is_start_and_end_on_same_node =
        ComputedStyleOfEnclosingTextNode(end) &&
        start.ComputeContainerNode() == end.ComputeContainerNode();
    bool is_start_and_end_of_last_paragraph_on_same_node =
        ComputedStyleOfEnclosingTextNode(end_of_last_paragraph) &&
        start.ComputeContainerNode() ==
            end_of_last_paragraph.ComputeContainerNode();

    // Avoid obtanining the start of next paragraph for start
    // TODO(yosin) We should use |PositionMoveType::CodePoint| for
    // |previousPositionOf()|.
    if (start_style->PreserveNewline() && IsNewLineAtPosition(start) &&
        !IsNewLineAtPosition(
            PreviousPositionOf(start, PositionMoveType::kCodeUnit)) &&
        start.OffsetInContainerNode() > 0)
      start = StartOfParagraph(CreateVisiblePosition(PreviousPositionOf(
                                   end, PositionMoveType::kCodeUnit)))
                  .DeepEquivalent();

    // If start is in the middle of a text node, split.
    if (!start_style->CollapseWhiteSpace() &&
        start.OffsetInContainerNode() > 0) {
      int start_offset = start.OffsetInContainerNode();
      Text* start_text = ToText(start.ComputeContainerNode());
      SplitTextNode(start_text, start_offset);
      GetDocument().UpdateStyleAndLayoutTree();

      start = Position::FirstPositionInNode(*start_text);
      if (is_start_and_end_on_same_node) {
        DCHECK_GE(end.OffsetInContainerNode(), start_offset);
        end = Position(start_text, end.OffsetInContainerNode() - start_offset);
      }
      if (is_start_and_end_of_last_paragraph_on_same_node) {
        DCHECK_GE(end_of_last_paragraph.OffsetInContainerNode(), start_offset);
        end_of_last_paragraph =
            Position(start_text, end_of_last_paragraph.OffsetInContainerNode() -
                                     start_offset);
      }
    }
  }

  if (const ComputedStyle* end_style = ComputedStyleOfEnclosingTextNode(end)) {
    bool is_end_and_end_of_last_paragraph_on_same_node =
        ComputedStyleOfEnclosingTextNode(end_of_last_paragraph) &&
        end.AnchorNode() == end_of_last_paragraph.AnchorNode();
    // Include \n at the end of line if we're at an empty paragraph
    if (end_style->PreserveNewline() && start == end &&
        end.OffsetInContainerNode() <
            static_cast<int>(ToText(end.ComputeContainerNode())->length())) {
      int end_offset = end.OffsetInContainerNode();
      // TODO(yosin) We should use |PositionMoveType::CodePoint| for
      // |previousPositionOf()|.
      if (!IsNewLineAtPosition(
              PreviousPositionOf(end, PositionMoveType::kCodeUnit)) &&
          IsNewLineAtPosition(end))
        end = Position(end.ComputeContainerNode(), end_offset + 1);
      if (is_end_and_end_of_last_paragraph_on_same_node &&
          end.OffsetInContainerNode() >=
              end_of_last_paragraph.OffsetInContainerNode())
        end_of_last_paragraph = end;
    }

    // If end is in the middle of a text node, split.
    if (end_style->UserModify() != EUserModify::kReadOnly &&
        !end_style->CollapseWhiteSpace() && end.OffsetInContainerNode() &&
        end.OffsetInContainerNode() <
            static_cast<int>(ToText(end.ComputeContainerNode())->length())) {
      Text* end_container = ToText(end.ComputeContainerNode());
      SplitTextNode(end_container, end.OffsetInContainerNode());
      GetDocument().UpdateStyleAndLayoutTree();

      const Node* const previous_sibling_of_end =
          end_container->previousSibling();
      DCHECK(previous_sibling_of_end);
      if (is_start_and_end_on_same_node) {
        start = FirstPositionInOrBeforeNode(*previous_sibling_of_end);
      }
      if (is_end_and_end_of_last_paragraph_on_same_node) {
        if (end_of_last_paragraph.OffsetInContainerNode() ==
            end.OffsetInContainerNode()) {
          end_of_last_paragraph =
              LastPositionInOrAfterNode(*previous_sibling_of_end);
        } else {
          end_of_last_paragraph = Position(
              end_container, end_of_last_paragraph.OffsetInContainerNode() -
                                 end.OffsetInContainerNode());
        }
      }
      end = Position::LastPositionInNode(*previous_sibling_of_end);
    }
  }
}

VisiblePosition
ApplyBlockElementCommand::EndOfNextParagrahSplittingTextNodesIfNeeded(
    VisiblePosition& end_of_current_paragraph,
    Position& end_of_last_paragraph,
    Position& start,
    Position& end) {
  const VisiblePosition& end_of_next_paragraph =
      EndOfParagraph(NextPositionOf(end_of_current_paragraph));
  const Position& end_of_next_paragraph_position =
      end_of_next_paragraph.DeepEquivalent();
  const ComputedStyle* style =
      ComputedStyleOfEnclosingTextNode(end_of_next_paragraph_position);
  if (!style)
    return end_of_next_paragraph;

  Text* const end_of_next_paragraph_text =
      ToText(end_of_next_paragraph_position.ComputeContainerNode());
  if (!style->PreserveNewline() ||
      !end_of_next_paragraph_position.OffsetInContainerNode() ||
      !IsNewLineAtPosition(
          Position::FirstPositionInNode(*end_of_next_paragraph_text)))
    return end_of_next_paragraph;

  // \n at the beginning of the text node immediately following the current
  // paragraph is trimmed by moveParagraphWithClones. If endOfNextParagraph was
  // pointing at this same text node, endOfNextParagraph will be shifted by one
  // paragraph. Avoid this by splitting "\n"
  SplitTextNode(end_of_next_paragraph_text, 1);
  GetDocument().UpdateStyleAndLayoutIgnorePendingStylesheets();
  Text* const previous_text =
      end_of_next_paragraph_text->previousSibling() &&
              end_of_next_paragraph_text->previousSibling()->IsTextNode()
          ? ToText(end_of_next_paragraph_text->previousSibling())
          : nullptr;
  if (end_of_next_paragraph_text == start.ComputeContainerNode() &&
      previous_text) {
    DCHECK_LT(start.OffsetInContainerNode(),
              end_of_next_paragraph_position.OffsetInContainerNode());
    start = Position(previous_text, start.OffsetInContainerNode());
  }
  if (end_of_next_paragraph_text == end.ComputeContainerNode() &&
      previous_text) {
    DCHECK_LT(end.OffsetInContainerNode(),
              end_of_next_paragraph_position.OffsetInContainerNode());
    end = Position(previous_text, end.OffsetInContainerNode());
  }
  if (end_of_next_paragraph_text ==
      end_of_last_paragraph.ComputeContainerNode()) {
    if (end_of_last_paragraph.OffsetInContainerNode() <
        end_of_next_paragraph_position.OffsetInContainerNode()) {
      // We can only fix endOfLastParagraph if the previous node was still text
      // and hasn't been modified by script.
      if (previous_text && static_cast<unsigned>(
                               end_of_last_paragraph.OffsetInContainerNode()) <=
                               previous_text->length()) {
        end_of_last_paragraph = Position(
            previous_text, end_of_last_paragraph.OffsetInContainerNode());
      }
    } else {
      end_of_last_paragraph =
          Position(end_of_next_paragraph_text,
                   end_of_last_paragraph.OffsetInContainerNode() - 1);
    }
  }

  return CreateVisiblePosition(
      Position(end_of_next_paragraph_text,
               end_of_next_paragraph_position.OffsetInContainerNode() - 1));
}

HTMLElement* ApplyBlockElementCommand::CreateBlockElement() const {
  HTMLElement* element = CreateHTMLElement(GetDocument(), tag_name_);
  if (inline_style_.length())
    element->setAttribute(styleAttr, inline_style_);
  return element;
}

}  // namespace blink