• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Element.h"
28 #include "FormatBlockCommand.h"
29 #include "Document.h"
30 #include "htmlediting.h"
31 #include "HTMLElement.h"
32 #include "HTMLNames.h"
33 #include "Range.h"
34 #include "visible_units.h"
35 
36 namespace WebCore {
37 
38 using namespace HTMLNames;
39 
40 static Node* enclosingBlockToSplitTreeTo(Node* startNode);
41 static bool isElementForFormatBlock(const QualifiedName& tagName);
isElementForFormatBlock(Node * node)42 static inline bool isElementForFormatBlock(Node* node)
43 {
44     return node->isElementNode() && isElementForFormatBlock(static_cast<Element*>(node)->tagQName());
45 }
46 
FormatBlockCommand(Document * document,const QualifiedName & tagName)47 FormatBlockCommand::FormatBlockCommand(Document* document, const QualifiedName& tagName)
48     : ApplyBlockElementCommand(document, tagName)
49     , m_didApply(false)
50 {
51 }
52 
formatSelection(const VisiblePosition & startOfSelection,const VisiblePosition & endOfSelection)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 
formatRange(const Position & start,const Position & end,const Position & endOfSelection,RefPtr<Element> & blockNode)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)
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 = lastPositionInNode(blockNode.get());
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 
elementForFormatBlockCommand(Range * range)98 Element* FormatBlockCommand::elementForFormatBlockCommand(Range* range)
99 {
100     if (!range)
101         return 0;
102 
103     ExceptionCode ec;
104     Node* commonAncestor = range->commonAncestorContainer(ec);
105     while (commonAncestor && !isElementForFormatBlock(commonAncestor))
106         commonAncestor = commonAncestor->parentNode();
107 
108     if (!commonAncestor)
109         return 0;
110 
111     Element* rootEditableElement = range->startContainer()->rootEditableElement();
112     if (!rootEditableElement || commonAncestor->contains(rootEditableElement))
113         return 0;
114 
115     return commonAncestor->isElementNode() ? toElement(commonAncestor) : 0;
116 }
117 
isElementForFormatBlock(const QualifiedName & tagName)118 bool isElementForFormatBlock(const QualifiedName& tagName)
119 {
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);
140         blockTags.add(pTag);
141         blockTags.add(preTag);
142         blockTags.add(sectionTag);
143     }
144     return blockTags.contains(tagName);
145 }
146 
enclosingBlockToSplitTreeTo(Node * startNode)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