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 "HTMLNames.h" 31 #include "bindings/v8/ExceptionState.h" 32 #include "core/dom/Text.h" 33 #include "core/editing/VisiblePosition.h" 34 #include "core/editing/VisibleUnits.h" 35 #include "core/editing/htmlediting.h" 36 #include "core/html/HTMLElement.h" 37 #include "core/rendering/RenderObject.h" 38 #include "core/rendering/style/RenderStyle.h" 39 40 namespace WebCore { 41 42 using namespace HTMLNames; 43 44 ApplyBlockElementCommand::ApplyBlockElementCommand(Document* document, const QualifiedName& tagName, const AtomicString& inlineStyle) 45 : CompositeEditCommand(document) 46 , m_tagName(tagName) 47 , m_inlineStyle(inlineStyle) 48 { 49 } 50 51 ApplyBlockElementCommand::ApplyBlockElementCommand(Document* document, const QualifiedName& tagName) 52 : CompositeEditCommand(document) 53 , m_tagName(tagName) 54 { 55 } 56 57 void ApplyBlockElementCommand::doApply() 58 { 59 if (!endingSelection().rootEditableElement()) 60 return; 61 62 VisiblePosition visibleEnd = endingSelection().visibleEnd(); 63 VisiblePosition visibleStart = endingSelection().visibleStart(); 64 if (visibleStart.isNull() || visibleStart.isOrphan() || visibleEnd.isNull() || visibleEnd.isOrphan()) 65 return; 66 67 // When a selection ends at the start of a paragraph, we rarely paint 68 // the selection gap before that paragraph, because there often is no gap. 69 // In a case like this, it's not obvious to the user that the selection 70 // ends "inside" that paragraph, so it would be confusing if Indent/Outdent 71 // operated on that paragraph. 72 // FIXME: We paint the gap before some paragraphs that are indented with left 73 // margin/padding, but not others. We should make the gap painting more consistent and 74 // then use a left margin/padding rule here. 75 if (visibleEnd != visibleStart && isStartOfParagraph(visibleEnd)) 76 setEndingSelection(VisibleSelection(visibleStart, visibleEnd.previous(CannotCrossEditingBoundary), endingSelection().isDirectional())); 77 78 VisibleSelection selection = selectionForParagraphIteration(endingSelection()); 79 VisiblePosition startOfSelection = selection.visibleStart(); 80 VisiblePosition endOfSelection = selection.visibleEnd(); 81 ASSERT(!startOfSelection.isNull()); 82 ASSERT(!endOfSelection.isNull()); 83 RefPtr<ContainerNode> startScope; 84 int startIndex = indexForVisiblePosition(startOfSelection, startScope); 85 RefPtr<ContainerNode> endScope; 86 int endIndex = indexForVisiblePosition(endOfSelection, endScope); 87 88 formatSelection(startOfSelection, endOfSelection); 89 90 document()->updateLayoutIgnorePendingStylesheets(); 91 92 ASSERT(startScope == endScope); 93 ASSERT(startIndex >= 0); 94 ASSERT(startIndex <= endIndex); 95 if (startScope == endScope && startIndex >= 0 && startIndex <= endIndex) { 96 VisiblePosition start(visiblePositionForIndex(startIndex, startScope.get())); 97 VisiblePosition end(visiblePositionForIndex(endIndex, endScope.get())); 98 if (start.isNotNull() && end.isNotNull()) 99 setEndingSelection(VisibleSelection(start, end, endingSelection().isDirectional())); 100 } 101 } 102 103 void ApplyBlockElementCommand::formatSelection(const VisiblePosition& startOfSelection, const VisiblePosition& endOfSelection) 104 { 105 // Special case empty unsplittable elements because there's nothing to split 106 // and there's nothing to move. 107 Position start = startOfSelection.deepEquivalent().downstream(); 108 if (isAtUnsplittableElement(start)) { 109 RefPtr<Element> blockquote = createBlockElement(); 110 insertNodeAt(blockquote, start); 111 RefPtr<Element> placeholder = createBreakElement(document()); 112 appendNode(placeholder, blockquote); 113 setEndingSelection(VisibleSelection(positionBeforeNode(placeholder.get()), DOWNSTREAM, endingSelection().isDirectional())); 114 return; 115 } 116 117 RefPtr<Element> blockquoteForNextIndent; 118 VisiblePosition endOfCurrentParagraph = endOfParagraph(startOfSelection); 119 VisiblePosition endAfterSelection = endOfParagraph(endOfParagraph(endOfSelection).next()); 120 m_endOfLastParagraph = endOfParagraph(endOfSelection).deepEquivalent(); 121 122 bool atEnd = false; 123 Position end; 124 while (endOfCurrentParagraph != endAfterSelection && !atEnd) { 125 if (endOfCurrentParagraph.deepEquivalent() == m_endOfLastParagraph) 126 atEnd = true; 127 128 rangeForParagraphSplittingTextNodesIfNeeded(endOfCurrentParagraph, start, end); 129 endOfCurrentParagraph = end; 130 131 Position afterEnd = end.next(); 132 Node* enclosingCell = enclosingNodeOfType(start, &isTableCell); 133 VisiblePosition endOfNextParagraph = endOfNextParagrahSplittingTextNodesIfNeeded(endOfCurrentParagraph, start, end); 134 135 formatRange(start, end, m_endOfLastParagraph, blockquoteForNextIndent); 136 137 // Don't put the next paragraph in the blockquote we just created for this paragraph unless 138 // the next paragraph is in the same cell. 139 if (enclosingCell && enclosingCell != enclosingNodeOfType(endOfNextParagraph.deepEquivalent(), &isTableCell)) 140 blockquoteForNextIndent = 0; 141 142 // indentIntoBlockquote could move more than one paragraph if the paragraph 143 // is in a list item or a table. As a result, endAfterSelection could refer to a position 144 // no longer in the document. 145 if (endAfterSelection.isNotNull() && !endAfterSelection.deepEquivalent().anchorNode()->inDocument()) 146 break; 147 // Sanity check: Make sure our moveParagraph calls didn't remove endOfNextParagraph.deepEquivalent().deprecatedNode() 148 // If somehow we did, return to prevent crashes. 149 if (endOfNextParagraph.isNotNull() && !endOfNextParagraph.deepEquivalent().anchorNode()->inDocument()) { 150 ASSERT_NOT_REACHED(); 151 return; 152 } 153 endOfCurrentParagraph = endOfNextParagraph; 154 } 155 } 156 157 static bool isNewLineAtPosition(const Position& position) 158 { 159 Node* textNode = position.containerNode(); 160 int offset = position.offsetInContainerNode(); 161 if (!textNode || !textNode->isTextNode() || offset < 0 || offset >= textNode->maxCharacterOffset()) 162 return false; 163 164 TrackExceptionState es; 165 String textAtPosition = toText(textNode)->substringData(offset, 1, es); 166 if (es.hadException()) 167 return false; 168 169 return textAtPosition[0] == '\n'; 170 } 171 172 static RenderStyle* renderStyleOfEnclosingTextNode(const Position& position) 173 { 174 if (position.anchorType() != Position::PositionIsOffsetInAnchor 175 || !position.containerNode() 176 || !position.containerNode()->isTextNode() 177 || !position.containerNode()->renderer()) 178 return 0; 179 return position.containerNode()->renderer()->style(); 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 bool isStartAndEndOnSameNode = false; 188 if (RenderStyle* startStyle = renderStyleOfEnclosingTextNode(start)) { 189 isStartAndEndOnSameNode = renderStyleOfEnclosingTextNode(end) && start.containerNode() == end.containerNode(); 190 bool isStartAndEndOfLastParagraphOnSameNode = renderStyleOfEnclosingTextNode(m_endOfLastParagraph) && start.containerNode() == m_endOfLastParagraph.containerNode(); 191 192 // Avoid obtanining the start of next paragraph for start 193 if (startStyle->preserveNewline() && isNewLineAtPosition(start) && !isNewLineAtPosition(start.previous()) && start.offsetInContainerNode() > 0) 194 start = startOfParagraph(end.previous()).deepEquivalent(); 195 196 // If start is in the middle of a text node, split. 197 if (!startStyle->collapseWhiteSpace() && start.offsetInContainerNode() > 0) { 198 int startOffset = start.offsetInContainerNode(); 199 Text* startText = start.containerText(); 200 splitTextNode(startText, startOffset); 201 start = firstPositionInNode(startText); 202 if (isStartAndEndOnSameNode) { 203 ASSERT(end.offsetInContainerNode() >= startOffset); 204 end = Position(startText, end.offsetInContainerNode() - startOffset); 205 } 206 if (isStartAndEndOfLastParagraphOnSameNode) { 207 ASSERT(m_endOfLastParagraph.offsetInContainerNode() >= startOffset); 208 m_endOfLastParagraph = Position(startText, m_endOfLastParagraph.offsetInContainerNode() - startOffset); 209 } 210 } 211 } 212 213 if (RenderStyle* endStyle = renderStyleOfEnclosingTextNode(end)) { 214 bool isEndAndEndOfLastParagraphOnSameNode = renderStyleOfEnclosingTextNode(m_endOfLastParagraph) && end.deprecatedNode() == m_endOfLastParagraph.deprecatedNode(); 215 // Include \n at the end of line if we're at an empty paragraph 216 if (endStyle->preserveNewline() && start == end && end.offsetInContainerNode() < end.containerNode()->maxCharacterOffset()) { 217 int endOffset = end.offsetInContainerNode(); 218 if (!isNewLineAtPosition(end.previous()) && isNewLineAtPosition(end)) 219 end = Position(end.containerText(), endOffset + 1); 220 if (isEndAndEndOfLastParagraphOnSameNode && end.offsetInContainerNode() >= m_endOfLastParagraph.offsetInContainerNode()) 221 m_endOfLastParagraph = end; 222 } 223 224 // If end is in the middle of a text node, split. 225 if (!endStyle->collapseWhiteSpace() && end.offsetInContainerNode() && end.offsetInContainerNode() < end.containerNode()->maxCharacterOffset()) { 226 RefPtr<Text> endContainer = end.containerText(); 227 splitTextNode(endContainer, end.offsetInContainerNode()); 228 if (isStartAndEndOnSameNode) 229 start = firstPositionInOrBeforeNode(endContainer->previousSibling()); 230 if (isEndAndEndOfLastParagraphOnSameNode) { 231 if (m_endOfLastParagraph.offsetInContainerNode() == end.offsetInContainerNode()) 232 m_endOfLastParagraph = lastPositionInOrAfterNode(endContainer->previousSibling()); 233 else 234 m_endOfLastParagraph = Position(endContainer, m_endOfLastParagraph.offsetInContainerNode() - end.offsetInContainerNode()); 235 } 236 end = lastPositionInNode(endContainer->previousSibling()); 237 } 238 } 239 } 240 241 VisiblePosition ApplyBlockElementCommand::endOfNextParagrahSplittingTextNodesIfNeeded(VisiblePosition& endOfCurrentParagraph, Position& start, Position& end) 242 { 243 VisiblePosition endOfNextParagraph = endOfParagraph(endOfCurrentParagraph.next()); 244 Position position = endOfNextParagraph.deepEquivalent(); 245 RenderStyle* style = renderStyleOfEnclosingTextNode(position); 246 if (!style) 247 return endOfNextParagraph; 248 249 RefPtr<Text> text = position.containerText(); 250 if (!style->preserveNewline() || !position.offsetInContainerNode() || !isNewLineAtPosition(firstPositionInNode(text.get()))) 251 return endOfNextParagraph; 252 253 // \n at the beginning of the text node immediately following the current paragraph is trimmed by moveParagraphWithClones. 254 // If endOfNextParagraph was pointing at this same text node, endOfNextParagraph will be shifted by one paragraph. 255 // Avoid this by splitting "\n" 256 splitTextNode(text, 1); 257 258 if (text == start.containerNode() && text->previousSibling() && text->previousSibling()->isTextNode()) { 259 ASSERT(start.offsetInContainerNode() < position.offsetInContainerNode()); 260 start = Position(toText(text->previousSibling()), start.offsetInContainerNode()); 261 } 262 if (text == end.containerNode() && text->previousSibling() && text->previousSibling()->isTextNode()) { 263 ASSERT(end.offsetInContainerNode() < position.offsetInContainerNode()); 264 end = Position(toText(text->previousSibling()), end.offsetInContainerNode()); 265 } 266 if (text == m_endOfLastParagraph.containerNode()) { 267 if (m_endOfLastParagraph.offsetInContainerNode() < position.offsetInContainerNode()) { 268 // We can only fix endOfLastParagraph if the previous node was still text and hasn't been modified by script. 269 if (text->previousSibling()->isTextNode() 270 && static_cast<unsigned>(m_endOfLastParagraph.offsetInContainerNode()) <= toText(text->previousSibling())->length()) 271 m_endOfLastParagraph = Position(toText(text->previousSibling()), m_endOfLastParagraph.offsetInContainerNode()); 272 } else 273 m_endOfLastParagraph = Position(text.get(), m_endOfLastParagraph.offsetInContainerNode() - 1); 274 } 275 276 return Position(text.get(), position.offsetInContainerNode() - 1); 277 } 278 279 PassRefPtr<Element> ApplyBlockElementCommand::createBlockElement() const 280 { 281 RefPtr<Element> element = createHTMLElement(document(), m_tagName); 282 if (m_inlineStyle.length()) 283 element->setAttribute(styleAttr, m_inlineStyle); 284 return element.release(); 285 } 286 287 } 288