initial import
[vuplus_webkit] / 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
58 enum TriState { FalseTriState, TrueTriState, MixedTriState };
59
60 class EditingStyle : public RefCounted<EditingStyle> {
61 public:
62
63     enum PropertiesToInclude { AllProperties, OnlyEditingInheritableProperties, EditingInheritablePropertiesAndBackgroundColorInEffect };
64     enum ShouldPreserveWritingDirection { PreserveWritingDirection, DoNotPreserveWritingDirection };
65     enum ShouldExtractMatchingStyle { ExtractMatchingStyle, DoNotExtractMatchingStyle };
66     static float NoFontDelta;
67
68     static PassRefPtr<EditingStyle> create()
69     {
70         return adoptRef(new EditingStyle());
71     }
72
73     static PassRefPtr<EditingStyle> create(Node* node, PropertiesToInclude propertiesToInclude = OnlyEditingInheritableProperties)
74     {
75         return adoptRef(new EditingStyle(node, propertiesToInclude));
76     }
77
78     static PassRefPtr<EditingStyle> create(const Position& position, PropertiesToInclude propertiesToInclude = OnlyEditingInheritableProperties)
79     {
80         return adoptRef(new EditingStyle(position, propertiesToInclude));
81     }
82
83     static PassRefPtr<EditingStyle> create(const CSSStyleDeclaration* style)
84     {
85         return adoptRef(new EditingStyle(style));
86     }
87
88     static PassRefPtr<EditingStyle> create(int propertyID, const String& value)
89     {
90         return adoptRef(new EditingStyle(propertyID, value));
91     }
92
93     ~EditingStyle();
94
95     CSSMutableStyleDeclaration* style() { return m_mutableStyle.get(); }
96     bool textDirection(WritingDirection&) const;
97     bool isEmpty() const;
98     void setStyle(PassRefPtr<CSSMutableStyleDeclaration>);
99     void overrideWithStyle(const CSSMutableStyleDeclaration*);
100     void clear();
101     PassRefPtr<EditingStyle> copy() const;
102     PassRefPtr<EditingStyle> extractAndRemoveBlockProperties();
103     PassRefPtr<EditingStyle> extractAndRemoveTextDirection();
104     void removeBlockProperties();
105     void removeStyleAddedByNode(Node*);
106     void removeStyleConflictingWithStyleOfNode(Node*);
107     void removeNonEditingProperties();
108     void collapseTextDecorationProperties();
109     enum ShouldIgnoreTextOnlyProperties { IgnoreTextOnlyProperties, DoNotIgnoreTextOnlyProperties };
110     TriState triStateOfStyle(CSSStyleDeclaration*, ShouldIgnoreTextOnlyProperties = DoNotIgnoreTextOnlyProperties) const;
111     bool conflictsWithInlineStyleOfElement(StyledElement* element) const { return conflictsWithInlineStyleOfElement(element, 0, 0); }
112     bool conflictsWithInlineStyleOfElement(StyledElement* element, EditingStyle* extractedStyle, Vector<CSSPropertyID>& conflictingProperties) const
113     {
114         return conflictsWithInlineStyleOfElement(element, extractedStyle, &conflictingProperties);
115     }
116     bool conflictsWithImplicitStyleOfElement(HTMLElement*, EditingStyle* extractedStyle = 0, ShouldExtractMatchingStyle = DoNotExtractMatchingStyle) const;
117     bool conflictsWithImplicitStyleOfAttributes(HTMLElement*) const;
118     bool extractConflictingImplicitStyleOfAttributes(HTMLElement*, ShouldPreserveWritingDirection, EditingStyle* extractedStyle,
119             Vector<QualifiedName>& conflictingAttributes, ShouldExtractMatchingStyle) const;
120     bool styleIsPresentInComputedStyleOfNode(Node*) const;
121
122     static bool elementIsStyledSpanOrHTMLEquivalent(const HTMLElement*);
123
124     void prepareToApplyAt(const Position&, ShouldPreserveWritingDirection = DoNotPreserveWritingDirection);
125     void mergeTypingStyle(Document*);
126     void mergeInlineStyleOfElement(StyledElement*);
127     void mergeStyleFromRules(StyledElement*);
128     void mergeStyleFromRulesForSerialization(StyledElement*);
129     void removeStyleFromRulesAndContext(StyledElement*, Node* context);
130     void removePropertiesInElementDefaultStyle(Element*);
131     void forceInline();
132
133     float fontSizeDelta() const { return m_fontSizeDelta; }
134     bool hasFontSizeDelta() const { return m_fontSizeDelta != NoFontDelta; }
135     bool shouldUseFixedDefaultFontSize() const { return m_shouldUseFixedDefaultFontSize; }
136
137 private:
138     EditingStyle();
139     EditingStyle(Node*, PropertiesToInclude);
140     EditingStyle(const Position&, PropertiesToInclude);
141     EditingStyle(const CSSStyleDeclaration*);
142     EditingStyle(int propertyID, const String& value);
143     void init(Node*, PropertiesToInclude);
144     void removeTextFillAndStrokeColorsIfNeeded(RenderStyle*);
145     void setProperty(int propertyID, const String& value, bool important = false);
146     void replaceFontSizeByKeywordIfPossible(RenderStyle*, CSSComputedStyleDeclaration*);
147     void extractFontSizeDelta();
148     bool conflictsWithInlineStyleOfElement(StyledElement*, EditingStyle* extractedStyle, Vector<CSSPropertyID>* conflictingProperties) const;
149     void mergeStyle(CSSMutableStyleDeclaration*);
150
151     RefPtr<CSSMutableStyleDeclaration> m_mutableStyle;
152     bool m_shouldUseFixedDefaultFontSize;
153     float m_fontSizeDelta;
154
155     friend class HTMLElementEquivalent;
156     friend class HTMLAttributeEquivalent;
157 };
158
159 class StyleChange {
160 public:
161     StyleChange(EditingStyle*, const Position&);
162
163     String cssStyle() const { return m_cssStyle; }
164     bool applyBold() const { return m_applyBold; }
165     bool applyItalic() const { return m_applyItalic; }
166     bool applyUnderline() const { return m_applyUnderline; }
167     bool applyLineThrough() const { return m_applyLineThrough; }
168     bool applySubscript() const { return m_applySubscript; }
169     bool applySuperscript() const { return m_applySuperscript; }
170     bool applyFontColor() const { return m_applyFontColor.length() > 0; }
171     bool applyFontFace() const { return m_applyFontFace.length() > 0; }
172     bool applyFontSize() const { return m_applyFontSize.length() > 0; }
173
174     String fontColor() { return m_applyFontColor; }
175     String fontFace() { return m_applyFontFace; }
176     String fontSize() { return m_applyFontSize; }
177
178     bool operator==(const StyleChange& other)
179     {
180         return m_cssStyle == other.m_cssStyle
181             && m_applyBold == other.m_applyBold
182             && m_applyItalic == other.m_applyItalic
183             && m_applyUnderline == other.m_applyUnderline
184             && m_applyLineThrough == other.m_applyLineThrough
185             && m_applySubscript == other.m_applySubscript
186             && m_applySuperscript == other.m_applySuperscript
187             && m_applyFontColor == other.m_applyFontColor
188             && m_applyFontFace == other.m_applyFontFace
189             && m_applyFontSize == other.m_applyFontSize;
190     }
191     bool operator!=(const StyleChange& other)
192     {
193         return !(*this == other);
194     }
195 private:
196     void extractTextStyles(Document*, CSSMutableStyleDeclaration*, bool shouldUseFixedFontDefaultSize);
197
198     String m_cssStyle;
199     bool m_applyBold;
200     bool m_applyItalic;
201     bool m_applyUnderline;
202     bool m_applyLineThrough;
203     bool m_applySubscript;
204     bool m_applySuperscript;
205     String m_applyFontColor;
206     String m_applyFontFace;
207     String m_applyFontSize;
208 };
209
210 // FIXME: Remove these functions or make them non-global to discourage using CSSStyleDeclaration directly.
211 int getIdentifierValue(CSSStyleDeclaration*, int propertyID);
212 enum LegacyFontSizeMode { AlwaysUseLegacyFontSize, UseLegacyFontSizeOnlyIfPixelValuesMatch };
213 int legacyFontSizeFromCSSValue(Document*, CSSPrimitiveValue*, bool shouldUseFixedFontDefaultSize, LegacyFontSizeMode);
214 bool hasTransparentBackgroundColor(CSSStyleDeclaration*);
215 PassRefPtr<CSSValue> backgroundColorInEffect(Node*);
216
217 } // namespace WebCore
218
219 #endif // EditingStyle_h