2 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
28 #include "FormatBlockCommand.h"
30 #include "htmlediting.h"
31 #include "HTMLElement.h"
32 #include "HTMLNames.h"
34 #include "visible_units.h"
38 using namespace HTMLNames;
40 static Node* enclosingBlockToSplitTreeTo(Node* startNode);
41 static bool isElementForFormatBlock(const QualifiedName& tagName);
42 static inline bool isElementForFormatBlock(Node* node)
44 return node->isElementNode() && isElementForFormatBlock(static_cast<Element*>(node)->tagQName());
47 FormatBlockCommand::FormatBlockCommand(Document* document, const QualifiedName& tagName)
48 : ApplyBlockElementCommand(document, tagName)
53 void FormatBlockCommand::formatSelection(const VisiblePosition& startOfSelection, const VisiblePosition& endOfSelection)
55 if (!isElementForFormatBlock(tagName()))
57 ApplyBlockElementCommand::formatSelection(startOfSelection, endOfSelection);
61 void FormatBlockCommand::formatRange(const Position& start, const Position& end, const Position& endOfSelection, RefPtr<Element>& blockNode)
63 Node* nodeToSplitTo = enclosingBlockToSplitTreeTo(start.deprecatedNode());
64 RefPtr<Node> outerBlock = (start.deprecatedNode() == nodeToSplitTo) ? start.deprecatedNode() : splitTreeToNode(start.deprecatedNode(), nodeToSplitTo);
65 RefPtr<Node> nodeAfterInsertionPosition = outerBlock;
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.
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()))
79 nodeAfterInsertionPosition = refNode;
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);
89 Position lastParagraphInBlockNode = blockNode->lastChild() ? positionAfterNode(blockNode->lastChild()) : Position();
90 bool wasEndOfParagraph = isEndOfParagraph(lastParagraphInBlockNode);
92 moveParagraphWithClones(start, end, blockNode.get(), outerBlock.get());
94 if (wasEndOfParagraph && !isEndOfParagraph(lastParagraphInBlockNode) && !isStartOfParagraph(lastParagraphInBlockNode))
95 insertBlockPlaceholder(lastParagraphInBlockNode);
98 Element* FormatBlockCommand::elementForFormatBlockCommand(Range* range)
104 Node* commonAncestor = range->commonAncestorContainer(ec);
105 while (commonAncestor && !isElementForFormatBlock(commonAncestor))
106 commonAncestor = commonAncestor->parentNode();
111 Element* rootEditableElement = range->startContainer()->rootEditableElement();
112 if (!rootEditableElement || commonAncestor->contains(rootEditableElement))
115 return commonAncestor->isElementNode() ? toElement(commonAncestor) : 0;
118 bool isElementForFormatBlock(const QualifiedName& tagName)
120 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, blockTags, ());
121 if (blockTags.isEmpty()) {
122 blockTags.add(addressTag);
123 blockTags.add(articleTag);
124 blockTags.add(asideTag);
125 blockTags.add(blockquoteTag);
126 blockTags.add(ddTag);
127 blockTags.add(divTag);
128 blockTags.add(dlTag);
129 blockTags.add(dtTag);
130 blockTags.add(footerTag);
131 blockTags.add(h1Tag);
132 blockTags.add(h2Tag);
133 blockTags.add(h3Tag);
134 blockTags.add(h4Tag);
135 blockTags.add(h5Tag);
136 blockTags.add(h6Tag);
137 blockTags.add(headerTag);
138 blockTags.add(hgroupTag);
139 blockTags.add(navTag);
141 blockTags.add(preTag);
142 blockTags.add(sectionTag);
144 return blockTags.contains(tagName);
147 Node* enclosingBlockToSplitTreeTo(Node* startNode)
149 Node* lastBlock = startNode;
150 for (Node* n = startNode; n; n = n->parentNode()) {
151 if (!n->rendererIsEditable())
153 if (isTableCell(n) || n->hasTagName(bodyTag) || !n->parentNode() || !n->parentNode()->rendererIsEditable() || isElementForFormatBlock(n))
157 if (isListElement(n))
158 return n->parentNode()->rendererIsEditable() ? n->parentNode() : n;