OSDN Git Service

Merge WebKit at r73109: Initial merge by git.
[android-x86/external-webkit.git] / WebCore / editing / ApplyStyleCommand.h
1 /*
2  * Copyright (C) 2005, 2006, 2008, 2009 Apple 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 #ifndef ApplyStyleCommand_h
27 #define ApplyStyleCommand_h
28
29 #include "CompositeEditCommand.h"
30 #include "HTMLElement.h"
31
32 namespace WebCore {
33
34 class CSSPrimitiveValue;
35 class HTMLElement;
36 class StyleChange;
37
38 enum ShouldIncludeTypingStyle {
39     IncludeTypingStyle,
40     IgnoreTypingStyle
41 };
42
43 class ApplyStyleCommand : public CompositeEditCommand {
44 public:
45     enum EPropertyLevel { PropertyDefault, ForceBlockProperties };
46     enum InlineStyleRemovalMode { RemoveIfNeeded, RemoveAlways, RemoveNone };
47     enum EAddStyledElement { AddStyledElement, DoNotAddStyledElement };
48     typedef bool (*IsInlineElementToRemoveFunction)(const Element*);
49
50     static PassRefPtr<ApplyStyleCommand> create(Document* document, CSSStyleDeclaration* style, EditAction action = EditActionChangeAttributes, EPropertyLevel level = PropertyDefault)
51     {
52         return adoptRef(new ApplyStyleCommand(document, style, action, level));
53     }
54     static PassRefPtr<ApplyStyleCommand> create(Document* document, CSSStyleDeclaration* style, const Position& start, const Position& end, EditAction action = EditActionChangeAttributes, EPropertyLevel level = PropertyDefault)
55     {
56         return adoptRef(new ApplyStyleCommand(document, style, start, end, action, level));
57     }
58     static PassRefPtr<ApplyStyleCommand> create(PassRefPtr<Element> element, bool removeOnly = false, EditAction action = EditActionChangeAttributes)
59     {
60         return adoptRef(new ApplyStyleCommand(element, removeOnly, action));
61     }
62     static PassRefPtr<ApplyStyleCommand> create(Document* document, CSSStyleDeclaration* style, IsInlineElementToRemoveFunction isInlineElementToRemoveFunction, EditAction action = EditActionChangeAttributes)
63     {
64         return adoptRef(new ApplyStyleCommand(document, style, isInlineElementToRemoveFunction, action));
65     }
66
67 private:
68     ApplyStyleCommand(Document*, CSSStyleDeclaration*, EditAction, EPropertyLevel);
69     ApplyStyleCommand(Document*, CSSStyleDeclaration*, const Position& start, const Position& end, EditAction, EPropertyLevel);
70     ApplyStyleCommand(PassRefPtr<Element>, bool removeOnly, EditAction);
71     ApplyStyleCommand(Document*, CSSStyleDeclaration*, bool (*isInlineElementToRemove)(const Element*), EditAction);
72
73     virtual void doApply();
74     virtual EditAction editingAction() const;
75
76     CSSMutableStyleDeclaration* style() const { return m_style.get(); }
77
78     // style-removal helpers
79     bool isStyledInlineElementToRemove(Element*) const;
80     bool removeStyleFromRunBeforeApplyingStyle(CSSMutableStyleDeclaration* style, RefPtr<Node>& runStart, RefPtr<Node>& runEnd);
81     bool removeInlineStyleFromElement(CSSMutableStyleDeclaration*, PassRefPtr<HTMLElement>, InlineStyleRemovalMode = RemoveIfNeeded, CSSMutableStyleDeclaration* extractedStyle = 0);
82     inline bool shouldRemoveInlineStyleFromElement(CSSMutableStyleDeclaration* style, HTMLElement* element) {return removeInlineStyleFromElement(style, element, RemoveNone);}
83     bool removeImplicitlyStyledElement(CSSMutableStyleDeclaration*, HTMLElement*, InlineStyleRemovalMode, CSSMutableStyleDeclaration* extractedStyle);
84     void replaceWithSpanOrRemoveIfWithoutAttributes(HTMLElement*&);
85     bool removeCSSStyle(CSSMutableStyleDeclaration*, HTMLElement*, InlineStyleRemovalMode = RemoveIfNeeded, CSSMutableStyleDeclaration* extractedStyle = 0);
86     HTMLElement* highestAncestorWithConflictingInlineStyle(CSSMutableStyleDeclaration*, Node*);
87     void applyInlineStyleToPushDown(Node*, CSSMutableStyleDeclaration *style);
88     void pushDownInlineStyleAroundNode(CSSMutableStyleDeclaration*, Node*);
89     void removeInlineStyle(PassRefPtr<CSSMutableStyleDeclaration>, const Position& start, const Position& end);
90     bool nodeFullySelected(Node*, const Position& start, const Position& end) const;
91     bool nodeFullyUnselected(Node*, const Position& start, const Position& end) const;
92
93     // style-application helpers
94     void applyBlockStyle(CSSMutableStyleDeclaration*);
95     void applyRelativeFontStyleChange(CSSMutableStyleDeclaration*);
96     void applyInlineStyle(CSSMutableStyleDeclaration*);
97     void fixRangeAndApplyInlineStyle(CSSMutableStyleDeclaration*, const Position& start, const Position& end);
98     void applyInlineStyleToNodeRange(CSSMutableStyleDeclaration*, Node* startNode, Node* pastEndNode);
99     void addBlockStyle(const StyleChange&, HTMLElement*);
100     void addInlineStyleIfNeeded(CSSMutableStyleDeclaration*, PassRefPtr<Node> start, PassRefPtr<Node> end, EAddStyledElement addStyledElement = AddStyledElement);
101     void splitTextAtStart(const Position& start, const Position& end);
102     void splitTextAtEnd(const Position& start, const Position& end);
103     void splitTextElementAtStart(const Position& start, const Position& end);
104     void splitTextElementAtEnd(const Position& start, const Position& end);
105     bool shouldSplitTextElement(Element* elem, CSSMutableStyleDeclaration*);
106     bool isValidCaretPositionInTextNode(const Position& position);
107     bool mergeStartWithPreviousIfIdentical(const Position& start, const Position& end);
108     bool mergeEndWithNextIfIdentical(const Position& start, const Position& end);
109     void cleanupUnstyledAppleStyleSpans(Node* dummySpanAncestor);
110
111     void surroundNodeRangeWithElement(PassRefPtr<Node> start, PassRefPtr<Node> end, PassRefPtr<Element>);
112     float computedFontSize(const Node*);
113     void joinChildTextNodes(Node*, const Position& start, const Position& end);
114
115     HTMLElement* splitAncestorsWithUnicodeBidi(Node*, bool before, int allowedDirection);
116     void removeEmbeddingUpToEnclosingBlock(Node* node, Node* unsplitAncestor);
117
118     void updateStartEnd(const Position& newStart, const Position& newEnd);
119     Position startPosition();
120     Position endPosition();
121
122     RefPtr<CSSMutableStyleDeclaration> m_style;
123     EditAction m_editingAction;
124     EPropertyLevel m_propertyLevel;
125     Position m_start;
126     Position m_end;
127     bool m_useEndingSelection;
128     RefPtr<Element> m_styledInlineElement;
129     bool m_removeOnly;
130     IsInlineElementToRemoveFunction m_isInlineElementToRemoveFunction;
131 };
132
133 bool isStyleSpan(const Node*);
134 PassRefPtr<HTMLElement> createStyleSpanElement(Document*);
135 RefPtr<CSSMutableStyleDeclaration> getPropertiesNotIn(CSSStyleDeclaration* styleWithRedundantProperties, CSSStyleDeclaration* baseStyle);
136
137 } // namespace WebCore
138
139 #endif