1 /* 2 * Copyright (C) 2006, 2008 Apple Inc. All rights reserved. 3 * Copyright (C) 2010 Google Inc. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 18 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include "config.h" 28 #include "core/editing/ApplyBlockElementCommand.h" 29 30 #include "bindings/v8/ExceptionState.h" 31 #include "core/HTMLNames.h" 32 #include "core/dom/NodeRenderStyle.h" 33 #include "core/dom/Text.h" 34 #include "core/editing/VisiblePosition.h" 35 #include "core/editing/VisibleUnits.h" 36 #include "core/editing/htmlediting.h" 37 #include "core/html/HTMLElement.h" 38 #include "core/rendering/RenderObject.h" 39 #include "core/rendering/style/RenderStyle.h" 40 41 namespace WebCore { 42 43 using namespace HTMLNames; 44 45 ApplyBlockElementCommand::ApplyBlockElementCommand(Document& document, const QualifiedName& tagName, const AtomicString& inlineStyle) 46 : CompositeEditCommand(document) 47 , m_tagName(tagName) 48 , m_inlineStyle(inlineStyle) 49 { 50 } 51 52 ApplyBlockElementCommand::ApplyBlockElementCommand(Document& document, const QualifiedName& tagName) 53 : CompositeEditCommand(document) 54 , m_tagName(tagName) 55 { 56 } 57 58 void ApplyBlockElementCommand::doApply() 59 { 60 if (!endingSelection().rootEditableElement()) 61 return; 62 63 VisiblePosition visibleEnd = endingSelection().visibleEnd(); 64 VisiblePosition visibleStart = endingSelection().visibleStart(); 65 if (visibleStart.isNull() || visibleStart.isOrphan() || visibleEnd.isNull() || visibleEnd.isOrphan()) 66 return; 67 68 // When a selection ends at the start of a paragraph, we rarely paint 69 // the selection gap before that paragraph, because there often is no gap. 70 // In a case like this, it's not obvious to the user that the selection 71 // ends "inside" that paragraph, so it would be confusing if Indent/Outdent 72 // operated on that paragraph. 73 // FIXME: We paint the gap before some paragraphs that are indented with left 74 // margin/padding, but not others. We should make the gap painting more consistent and 75 // then use a left margin/padding rule here. 76 if (visibleEnd != visibleStart && isStartOfParagraph(visibleEnd)) { 77 VisibleSelection newSelection(visibleStart, visibleEnd.previous(CannotCrossEditingBoundary), endingSelection().isDirectional()); 78 if (newSelection.isNone()) 79 return; 80 setEndingSelection(newSelection); 81 } 82 83 VisibleSelection selection = selectionForParagraphIteration(endingSelection()); 84 VisiblePosition startOfSelection = selection.visibleStart(); 85 VisiblePosition endOfSelection = selection.visibleEnd(); 86 ASSERT(!startOfSelection.isNull()); 87 ASSERT(!endOfSelection.isNull()); 88 RefPtrWillBeRawPtr<ContainerNode> startScope = nullptr; 89 int startIndex = indexForVisiblePosition(startOfSelection, startScope); 90 RefPtrWillBeRawPtr<ContainerNode> endScope = nullptr; 91 int endIndex = indexForVisiblePosition(endOfSelection, endScope); 92 93 formatSelection(startOfSelection, endOfSelection); 94 95 document().updateLayoutIgnorePendingStylesheets(); 96 97 ASSERT(startScope == endScope); 98 ASSERT(startIndex >= 0); 99 ASSERT(startIndex <= endIndex); 100 if (startScope == endScope && startIndex >= 0 && startIndex <= endIndex) { 101 VisiblePosition start(visiblePositionForIndex(startIndex, startScope.get())); 102 VisiblePosition end(visiblePositionForIndex(endIndex, endScope.get())); 103 if (start.isNotNull() && end.isNotNull()) 104 setEndingSelection(VisibleSelection(start, end, endingSelection().isDirectional())); 105 } 106 } 107 108 void ApplyBlockElementCommand::formatSelection(const VisiblePosition& startOfSelection, const VisiblePosition& endOfSelection) 109 { 110 // Special case empty unsplittable elements because there's nothing to split 111 // and there's nothing to move. 112 Position start = startOfSelection.deepEquivalent().downstream(); 113 if (isAtUnsplittableElement(start)) { 114 RefPtrWillBeRawPtr<Element> blockquote = createBlockElement(); 115 insertNodeAt(blockquote, start); 116 RefPtrWillBeRawPtr<Element> placeholder = createBreakElement(document()); 117 appendNode(placeholder, blockquote); 118 setEndingSelection(VisibleSelection(positionBeforeNode(placeholder.get()), DOWNSTREAM, endingSelection().isDirectional())); 119 return; 120 } 121 122 RefPtrWillBeRawPtr<Element> blockquoteForNextIndent = nullptr; 123 VisiblePosition endOfCurrentParagraph = endOfParagraph(startOfSelection); 124 VisiblePosition endOfLastParagraph = endOfParagraph(endOfSelection); 125 VisiblePosition endAfterSelection = endOfParagraph(endOfLastParagraph.next()); 126 m_endOfLastParagraph = endOfLastParagraph.deepEquivalent(); 127 128 bool atEnd = false; 129 Position end; 130 while (endOfCurrentParagraph != endAfterSelection && !atEnd) { 131 if (endOfCurrentParagraph.deepEquivalent() == m_endOfLastParagraph) 132 atEnd = true; 133 134 rangeForParagraphSplittingTextNodesIfNeeded(endOfCurrentParagraph, start, end); 135 endOfCurrentParagraph = VisiblePosition(end); 136 137 Node* enclosingCell = enclosingNodeOfType(start, &isTableCell); 138 VisiblePosition endOfNextParagraph = endOfNextParagrahSplittingTextNodesIfNeeded(endOfCurrentParagraph, start, end); 139 140 formatRange(start, end, m_endOfLastParagraph, blockquoteForNextIndent); 141 142 // Don't put the next paragraph in the blockquote we just created for this paragraph unless 143 // the next paragraph is in the same cell. 144 if (enclosingCell && enclosingCell != enclosingNodeOfType(endOfNextParagraph.deepEquivalent(), &isTableCell)) 145 blockquoteForNextIndent = nullptr; 146 147 // indentIntoBlockquote could move more than one paragraph if the paragraph 148 // is in a list item or a table. As a result, endAfterSelection could refer to a position 149 // no longer in the document. 150 if (endAfterSelection.isNotNull() && !endAfterSelection.deepEquivalent().inDocument()) 151 break; 152 // Sanity check: Make sure our moveParagraph calls didn't remove endOfNextParagraph.deepEquivalent().deprecatedNode() 153 // If somehow, e.g. mutation event handler, we did, return to prevent crashes. 154 if (endOfNextParagraph.isNotNull() && !endOfNextParagraph.deepEquivalent().inDocument()) 155 return; 156 endOfCurrentParagraph = endOfNextParagraph; 157 } 158 } 159 160 static bool isNewLineAtPosition(const Position& position) 161 { 162 Node* textNode = position.containerNode(); 163 int offset = position.offsetInContainerNode(); 164 if (!textNode || !textNode->isTextNode() || offset < 0 || offset >= textNode->maxCharacterOffset()) 165 return false; 166 167 TrackExceptionState exceptionState; 168 String textAtPosition = toText(textNode)->substringData(offset, 1, exceptionState); 169 if (exceptionState.hadException()) 170 return false; 171 172 return textAtPosition[0] == '\n'; 173 } 174 175 static RenderStyle* renderStyleOfEnclosingTextNode(const Position& position) 176 { 177 if (position.anchorType() != Position::PositionIsOffsetInAnchor || !position.containerNode() || !position.containerNode()->isTextNode()) 178 return 0; 179 return position.containerNode()->renderStyle(); 180 } 181 182 void ApplyBlockElementCommand::rangeForParagraphSplittingTextNodesIfNeeded(const VisiblePosition& endOfCurrentParagraph, Position& start, Position& end) 183 { 184 start = startOfParagraph(endOfCurrentParagraph).deepEquivalent(); 185 end = endOfCurrentParagraph.deepEquivalent(); 186 187 document().updateRenderTreeIfNeeded(); 188 189 bool isStartAndEndOnSameNode = false; 190 if (RenderStyle* startStyle = renderStyleOfEnclosingTextNode(start)) { 191 isStartAndEndOnSameNode = renderStyleOfEnclosingTextNode(end) && start.containerNode() == end.containerNode(); 192 bool isStartAndEndOfLastParagraphOnSameNode = renderStyleOfEnclosingTextNode(m_endOfLastParagraph) && start.containerNode() == m_endOfLastParagraph.containerNode(); 193 194 // Avoid obtanining the start of next paragraph for start 195 if (startStyle->preserveNewline() && isNewLineAtPosition(start) && !isNewLineAtPosition(start.previous()) && start.offsetInContainerNode() > 0) 196 start = startOfParagraph(VisiblePosition(end.previous())).deepEquivalent(); 197 198 // If start is in the middle of a text node, split. 199 if (!startStyle->collapseWhiteSpace() && start.offsetInContainerNode() > 0) { 200 int startOffset = start.offsetInContainerNode(); 201 Text* startText = start.containerText(); 202 splitTextNode(startText, startOffset); 203 start = firstPositionInNode(startText); 204 if (isStartAndEndOnSameNode) { 205 ASSERT(end.offsetInContainerNode() >= startOffset); 206 end = Position(startText, end.offsetInContainerNode() - startOffset); 207 } 208 if (isStartAndEndOfLastParagraphOnSameNode) { 209 ASSERT(m_endOfLastParagraph.offsetInContainerNode() >= startOffset); 210 m_endOfLastParagraph = Position(startText, m_endOfLastParagraph.offsetInContainerNode() - startOffset); 211 } 212 } 213 } 214 215 document().updateRenderTreeIfNeeded(); 216 217 if (RenderStyle* endStyle = renderStyleOfEnclosingTextNode(end)) { 218 bool isEndAndEndOfLastParagraphOnSameNode = renderStyleOfEnclosingTextNode(m_endOfLastParagraph) && end.deprecatedNode() == m_endOfLastParagraph.deprecatedNode(); 219 // Include \n at the end of line if we're at an empty paragraph 220 if (endStyle->preserveNewline() && start == end && end.offsetInContainerNode() < end.containerNode()->maxCharacterOffset()) { 221 int endOffset = end.offsetInContainerNode(); 222 if (!isNewLineAtPosition(end.previous()) && isNewLineAtPosition(end)) 223 end = Position(end.containerText(), endOffset + 1); 224 if (isEndAndEndOfLastParagraphOnSameNode && end.offsetInContainerNode() >= m_endOfLastParagraph.offsetInContainerNode()) 225 m_endOfLastParagraph = end; 226 } 227 228 // If end is in the middle of a text node, split. 229 if (!endStyle->collapseWhiteSpace() && end.offsetInContainerNode() && end.offsetInContainerNode() < end.containerNode()->maxCharacterOffset()) { 230 RefPtrWillBeRawPtr<Text> endContainer = end.containerText(); 231 splitTextNode(endContainer, end.offsetInContainerNode()); 232 if (isStartAndEndOnSameNode) 233 start = firstPositionInOrBeforeNode(endContainer->previousSibling()); 234 if (isEndAndEndOfLastParagraphOnSameNode) { 235 if (m_endOfLastParagraph.offsetInContainerNode() == end.offsetInContainerNode()) 236 m_endOfLastParagraph = lastPositionInOrAfterNode(endContainer->previousSibling()); 237 else 238 m_endOfLastParagraph = Position(endContainer, m_endOfLastParagraph.offsetInContainerNode() - end.offsetInContainerNode()); 239 } 240 end = lastPositionInNode(endContainer->previousSibling()); 241 } 242 } 243 } 244 245 VisiblePosition ApplyBlockElementCommand::endOfNextParagrahSplittingTextNodesIfNeeded(VisiblePosition& endOfCurrentParagraph, Position& start, Position& end) 246 { 247 VisiblePosition endOfNextParagraph = endOfParagraph(endOfCurrentParagraph.next()); 248 Position position = endOfNextParagraph.deepEquivalent(); 249 RenderStyle* style = renderStyleOfEnclosingTextNode(position); 250 if (!style) 251 return endOfNextParagraph; 252 253 RefPtrWillBeRawPtr<Text> text = position.containerText(); 254 if (!style->preserveNewline() || !position.offsetInContainerNode() || !isNewLineAtPosition(firstPositionInNode(text.get()))) 255 return endOfNextParagraph; 256 257 // \n at the beginning of the text node immediately following the current paragraph is trimmed by moveParagraphWithClones. 258 // If endOfNextParagraph was pointing at this same text node, endOfNextParagraph will be shifted by one paragraph. 259 // Avoid this by splitting "\n" 260 splitTextNode(text, 1); 261 262 if (text == start.containerNode() && text->previousSibling() && text->previousSibling()->isTextNode()) { 263 ASSERT(start.offsetInContainerNode() < position.offsetInContainerNode()); 264 start = Position(toText(text->previousSibling()), start.offsetInContainerNode()); 265 } 266 if (text == end.containerNode() && text->previousSibling() && text->previousSibling()->isTextNode()) { 267 ASSERT(end.offsetInContainerNode() < position.offsetInContainerNode()); 268 end = Position(toText(text->previousSibling()), end.offsetInContainerNode()); 269 } 270 if (text == m_endOfLastParagraph.containerNode()) { 271 if (m_endOfLastParagraph.offsetInContainerNode() < position.offsetInContainerNode()) { 272 // We can only fix endOfLastParagraph if the previous node was still text and hasn't been modified by script. 273 if (text->previousSibling()->isTextNode() 274 && static_cast<unsigned>(m_endOfLastParagraph.offsetInContainerNode()) <= toText(text->previousSibling())->length()) 275 m_endOfLastParagraph = Position(toText(text->previousSibling()), m_endOfLastParagraph.offsetInContainerNode()); 276 } else 277 m_endOfLastParagraph = Position(text.get(), m_endOfLastParagraph.offsetInContainerNode() - 1); 278 } 279 280 return VisiblePosition(Position(text.get(), position.offsetInContainerNode() - 1)); 281 } 282 283 PassRefPtrWillBeRawPtr<Element> ApplyBlockElementCommand::createBlockElement() const 284 { 285 RefPtrWillBeRawPtr<Element> element = createHTMLElement(document(), m_tagName); 286 if (m_inlineStyle.length()) 287 element->setAttribute(styleAttr, m_inlineStyle); 288 return element.release(); 289 } 290 291 void ApplyBlockElementCommand::trace(Visitor* visitor) 292 { 293 visitor->trace(m_endOfLastParagraph); 294 CompositeEditCommand::trace(visitor); 295 } 296 297 } 298