tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / editing / EditingStyle.h
1 /*
2  * Copyright (C) 2010 Google 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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #ifndef EditingStyle_h
32 #define EditingStyle_h
33
34 #include "CSSPropertyNames.h"
35 #include "PlatformString.h"
36 #include "WritingDirection.h"
37 #include <wtf/Forward.h>
38 #include <wtf/RefCounted.h>
39 #include <wtf/RefPtr.h>
40 #include <wtf/Vector.h>
41
42 namespace WebCore {
43
44 class CSSStyleDeclaration;
45 class CSSComputedStyleDeclaration;
46 class CSSMutableStyleDeclaration;
47 class CSSPrimitiveValue;
48 class CSSValue;
49 class Document;
50 class Element;
51 class HTMLElement;
52 class Node;
53 class Position;
54 class QualifiedName;
55 class RenderStyle;
56 class StyledElement;
57 class VisibleSelection;
58
59 enum TriState { FalseTriState, TrueTriState, MixedTriState };
60
61 class EditingStyle : public RefCounted<EditingStyle> {
62 public:
63
64     enum PropertiesToInclude { AllProperties, OnlyEditingInheritableProperties, EditingPropertiesInEffect };
65     enum ShouldPreserveWritingDirection { PreserveWritingDirection, DoNotPreserveWritingDirection };
66     enum ShouldExtractMatchingStyle { ExtractMatchingStyle, DoNotExtractMatchingStyle };
67     static float NoFontDelta;
68
69     static PassRefPtr<EditingStyle> create()
70     {
71         return adoptRef(new EditingStyle());
72     }
73
74     static PassRefPtr<EditingStyle> create(Node* node, PropertiesToInclude propertiesToInclude = OnlyEditingInheritableProperties)
75     {
76         return adoptRef(new EditingStyle(node, propertiesToInclude));
77     }
78
79     static PassRefPtr<EditingStyle> create(const Position& position, PropertiesToInclude propertiesToInclude = OnlyEditingInheritableProperties)
80     {
81         return adoptRef(new EditingStyle(position, propertiesToInclude));
82     }
83
84     static PassRefPtr<EditingStyle> create(const CSSStyleDeclaration* style)
85     {
86         return adoptRef(new EditingStyle(style));
87     }
88
89     static PassRefPtr<EditingStyle> create(int propertyID, const String& value)
90     {
91         return adoptRef(new EditingStyle(propertyID, value));
92     }
93
94     ~EditingStyle();
95
96     CSSMutableStyleDeclaration* style() { return m_mutableStyle.get(); }
97     bool textDirection(WritingDirection&) const;
98     bool isEmpty() const;
99     void setStyle(PassRefPtr<CSSMutableStyleDeclaration>);
100     void overrideWithStyle(const CSSMutableStyleDeclaration*);
101     void clear();
102     PassRefPtr<EditingStyle> copy() const;
103     PassRefPtr<EditingStyle> extractAndRemoveBlockProperties();
104     PassRefPtr<EditingStyle> extractAndRemoveTextDirection();
105     void removeBlockProperties();
106     void removeStyleAddedByNode(Node*);
107     void removeStyleConflictingWithStyleOfNode(Node*);
108     void removeNonEditingProperties();
109     void collapseTextDecorationProperties();
110     enum ShouldIgnoreTextOnlyProperties { IgnoreTextOnlyProperties, DoNotIgnoreTextOnlyProperties };
111     TriState triStateOfStyle(EditingStyle*) const;
112     TriState triStateOfStyle(const VisibleSelection&) const;
113     bool conflictsWithInlineStyleOfElement(StyledElement* element) const { return conflictsWithInlineStyleOfElement(element, 0, 0); }
114     bool conflictsWithInlineStyleOfElement(StyledElement* element, EditingStyle* extractedStyle, Vector<CSSPropertyID>& conflictingProperties) const
115     {
116         return conflictsWithInlineStyleOfElement(element, extractedStyle, &conflictingProperties);
117     }
118     bool conflictsWithImplicitStyleOfElement(HTMLElement*, EditingStyle* extractedStyle = 0, ShouldExtractMatchingStyle = DoNotExtractMatchingStyle) const;
119     bool conflictsWithImplicitStyleOfAttributes(HTMLElement*) const;
120     bool extractConflictingImplicitStyleOfAttributes(HTMLElement*, ShouldPreserveWritingDirection, EditingStyle* extractedStyle,
121             Vector<QualifiedName>& conflictingAttributes, ShouldExtractMatchingStyle) const;
122     bool styleIsPresentInComputedStyleOfNode(Node*) const;
123
124     static bool elementIsStyledSpanOrHTMLEquivalent(const HTMLElement*);
125
126     void prepareToApplyAt(const Position&, ShouldPreserveWritingDirection = DoNotPreserveWritingDirection);
127     void mergeTypingStyle(Document*);
128     enum CSSPropertyOverrideMode { OverrideValues, DoNotOverrideValues };
129     void mergeInlineStyleOfElement(StyledElement*, CSSPropertyOverrideMode, PropertiesToInclude = AllProperties);
130     static PassRefPtr<EditingStyle> wrappingStyleForSerialization(Node* context, bool shouldAnnotate);
131     void mergeStyleFromRules(StyledElement*);
132     void mergeStyleFromRulesForSerialization(StyledElement*);
133     void removeStyleFromRulesAndContext(StyledElement*, Node* context);
134     void removePropertiesInElementDefaultStyle(Element*);
135     void forceInline();
136     int legacyFontSize(Document*) const;
137
138     float fontSizeDelta() const { return m_fontSizeDelta; }
139     bool hasFontSizeDelta() const { return m_fontSizeDelta != NoFontDelta; }
140     bool shouldUseFixedDefaultFontSize() const { return m_shouldUseFixedDefaultFontSize; }
141
142     static PassRefPtr<EditingStyle> styleAtSelectionStart(const VisibleSelection&, bool shouldUseBackgroundColorInEffect = false);
143 private:
144     EditingStyle();
145     EditingStyle(Node*, PropertiesToInclude);
146     EditingStyle(const Position&, PropertiesToInclude);
147     EditingStyle(const CSSStyleDeclaration*);
148     EditingStyle(int propertyID, const String& value);
149     void init(Node*, PropertiesToInclude);
150     void removeTextFillAndStrokeColorsIfNeeded(RenderStyle*);
151     void setProperty(int propertyID, const String& value, bool important = false);
152     void replaceFontSizeByKeywordIfPossible(RenderStyle*, CSSComputedStyleDeclaration*);
153     void extractFontSizeDelta();
154     TriState triStateOfStyle(CSSStyleDeclaration* styleToCompare, ShouldIgnoreTextOnlyProperties) const;
155     bool conflictsWithInlineStyleOfElement(StyledElement*, EditingStyle* extractedStyle, Vector<CSSPropertyID>* conflictingProperties) const;
156     void mergeInlineAndImplicitStyleOfElement(StyledElement*, CSSPropertyOverrideMode, PropertiesToInclude);
157     void mergeStyle(CSSMutableStyleDeclaration*, CSSPropertyOverrideMode);
158
159     RefPtr<CSSMutableStyleDeclaration> m_mutableStyle;
160     bool m_shouldUseFixedDefaultFontSize;
161     float m_fontSizeDelta;
162
163     friend class HTMLElementEquivalent;
164     friend class HTMLAttributeEquivalent;
165 };
166
167 class StyleChange {
168 public:
169     StyleChange(EditingStyle*, const Position&);
170
171     String cssStyle() const { return m_cssStyle; }
172     bool applyBold() const { return m_applyBold; }
173     bool applyItalic() const { return m_applyItalic; }
174     bool applyUnderline() const { return m_applyUnderline; }
175     bool applyLineThrough() const { return m_applyLineThrough; }
176     bool applySubscript() const { return m_applySubscript; }
177     bool applySuperscript() const { return m_applySuperscript; }
178     bool applyFontColor() const { return m_applyFontColor.length() > 0; }
179     bool applyFontFace() const { return m_applyFontFace.length() > 0; }
180     bool applyFontSize() const { return m_applyFontSize.length() > 0; }
181
182     String fontColor() { return m_applyFontColor; }
183     String fontFace() { return m_applyFontFace; }
184     String fontSize() { return m_applyFontSize; }
185
186     bool operator==(const StyleChange& other)
187     {
188         return m_cssStyle == other.m_cssStyle
189             && m_applyBold == other.m_applyBold
190             && m_applyItalic == other.m_applyItalic
191             && m_applyUnderline == other.m_applyUnderline
192             && m_applyLineThrough == other.m_applyLineThrough
193             && m_applySubscript == other.m_applySubscript
194             && m_applySuperscript == other.m_applySuperscript
195             && m_applyFontColor == other.m_applyFontColor
196             && m_applyFontFace == other.m_applyFontFace
197             && m_applyFontSize == other.m_applyFontSize;
198     }
199     bool operator!=(const StyleChange& other)
200     {
201         return !(*this == other);
202     }
203 private:
204     void extractTextStyles(Document*, CSSMutableStyleDeclaration*, bool shouldUseFixedFontDefaultSize);
205
206     String m_cssStyle;
207     bool m_applyBold;
208     bool m_applyItalic;
209     bool m_applyUnderline;
210     bool m_applyLineThrough;
211     bool m_applySubscript;
212     bool m_applySuperscript;
213     String m_applyFontColor;
214     String m_applyFontFace;
215     String m_applyFontSize;
216 };
217
218 // FIXME: Remove these functions or make them non-global to discourage using CSSStyleDeclaration directly.
219 int getIdentifierValue(CSSStyleDeclaration*, int propertyID);
220
221 } // namespace WebCore
222
223 #endif // EditingStyle_h