1 /* 2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #include "config.h" 27 #include "HTMLNames.h" 28 #include "bindings/v8/ExceptionStatePlaceholder.h" 29 #include "core/dom/Document.h" 30 #include "core/dom/Element.h" 31 #include "core/dom/Range.h" 32 #include "core/editing/FormatBlockCommand.h" 33 #include "core/editing/VisibleUnits.h" 34 #include "core/editing/htmlediting.h" 35 36 namespace WebCore { 37 38 using namespace HTMLNames; 39 40 static Node* enclosingBlockToSplitTreeTo(Node* startNode); 41 static bool isElementForFormatBlock(const QualifiedName& tagName); 42 static inline bool isElementForFormatBlock(Node* node) 43 { 44 return node->isElementNode() && isElementForFormatBlock(toElement(node)->tagQName()); 45 } 46 47 FormatBlockCommand::FormatBlockCommand(Document* document, const QualifiedName& tagName) 48 : ApplyBlockElementCommand(document, tagName) 49 , m_didApply(false) 50 { 51 } 52 53 void FormatBlockCommand::formatSelection(const VisiblePosition& startOfSelection, const VisiblePosition& endOfSelection) 54 { 55 if (!isElementForFormatBlock(tagName())) 56 return; 57 ApplyBlockElementCommand::formatSelection(startOfSelection, endOfSelection); 58 m_didApply = true; 59 } 60 61 void FormatBlockCommand::formatRange(const Position& start, const Position& end, const Position& endOfSelection, RefPtr<Element>& blockNode) 62 { 63 Node* nodeToSplitTo = enclosingBlockToSplitTreeTo(start.deprecatedNode()); 64 RefPtr<Node> outerBlock = (start.deprecatedNode() == nodeToSplitTo) ? start.deprecatedNode() : splitTreeToNode(start.deprecatedNode(), nodeToSplitTo); 65 RefPtr<Node> nodeAfterInsertionPosition = outerBlock; 66 67 RefPtr<Range> range = Range::create(document(), start, endOfSelection); 68 Element* refNode = enclosingBlockFlowElement(end); 69 Element* root = editableRootForPosition(start); 70 // Root is null for elements with contenteditable=false. 71 if (!root || !refNode) 72 return; 73 if (isElementForFormatBlock(refNode->tagQName()) && start == startOfBlock(start) 74 && (end == endOfBlock(end) || isNodeVisiblyContainedWithin(refNode, range.get())) 75 && refNode != root && !root->isDescendantOf(refNode)) { 76 // Already in a block element that only contains the current paragraph 77 if (refNode->hasTagName(tagName())) 78 return; 79 nodeAfterInsertionPosition = refNode; 80 } 81 82 if (!blockNode) { 83 // Create a new blockquote and insert it as a child of the root editable element. We accomplish 84 // this by splitting all parents of the current paragraph up to that point. 85 blockNode = createBlockElement(); 86 insertNodeBefore(blockNode, nodeAfterInsertionPosition); 87 } 88 89 Position lastParagraphInBlockNode = blockNode->lastChild() ? positionAfterNode(blockNode->lastChild()) : Position(); 90 bool wasEndOfParagraph = isEndOfParagraph(lastParagraphInBlockNode); 91 92 moveParagraphWithClones(start, end, blockNode.get(), outerBlock.get()); 93 94 if (wasEndOfParagraph && !isEndOfParagraph(lastParagraphInBlockNode) && !isStartOfParagraph(lastParagraphInBlockNode)) 95 insertBlockPlaceholder(lastParagraphInBlockNode); 96 } 97 98 Element* FormatBlockCommand::elementForFormatBlockCommand(Range* range) 99 { 100 if (!range) 101 return 0; 102 103 Node* commonAncestor = range->commonAncestorContainer(IGNORE_EXCEPTION); 104 while (commonAncestor && !isElementForFormatBlock(commonAncestor)) 105 commonAncestor = commonAncestor->parentNode(); 106 107 if (!commonAncestor) 108 return 0; 109 110 Element* rootEditableElement = range->startContainer()->rootEditableElement(); 111 if (!rootEditableElement || commonAncestor->contains(rootEditableElement)) 112 return 0; 113 114 return commonAncestor->isElementNode() ? toElement(commonAncestor) : 0; 115 } 116 117 bool isElementForFormatBlock(const QualifiedName& tagName) 118 { 119 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, blockTags, ()); 120 if (blockTags.isEmpty()) { 121 blockTags.add(addressTag); 122 blockTags.add(articleTag); 123 blockTags.add(asideTag); 124 blockTags.add(blockquoteTag); 125 blockTags.add(ddTag); 126 blockTags.add(divTag); 127 blockTags.add(dlTag); 128 blockTags.add(dtTag); 129 blockTags.add(footerTag); 130 blockTags.add(h1Tag); 131 blockTags.add(h2Tag); 132 blockTags.add(h3Tag); 133 blockTags.add(h4Tag); 134 blockTags.add(h5Tag); 135 blockTags.add(h6Tag); 136 blockTags.add(headerTag); 137 blockTags.add(hgroupTag); 138 blockTags.add(mainTag); 139 blockTags.add(navTag); 140 blockTags.add(pTag); 141 blockTags.add(preTag); 142 blockTags.add(sectionTag); 143 } 144 return blockTags.contains(tagName); 145 } 146 147 Node* enclosingBlockToSplitTreeTo(Node* startNode) 148 { 149 Node* lastBlock = startNode; 150 for (Node* n = startNode; n; n = n->parentNode()) { 151 if (!n->rendererIsEditable()) 152 return lastBlock; 153 if (isTableCell(n) || n->hasTagName(bodyTag) || !n->parentNode() || !n->parentNode()->rendererIsEditable() || isElementForFormatBlock(n)) 154 return n; 155 if (isBlock(n)) 156 lastBlock = n; 157 if (isListElement(n)) 158 return n->parentNode()->rendererIsEditable() ? n->parentNode() : n; 159 } 160 return lastBlock; 161 } 162 163 } 164