OSDN Git Service

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