2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2013 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef EditingStyle_h
33 #define EditingStyle_h
35 #include "CSSPropertyNames.h"
36 #include "CSSValueKeywords.h"
37 #include "core/editing/WritingDirection.h"
38 #include "platform/heap/Handle.h"
39 #include "wtf/Forward.h"
40 #include "wtf/RefCounted.h"
41 #include "wtf/RefPtr.h"
42 #include "wtf/TriState.h"
43 #include "wtf/Vector.h"
44 #include "wtf/text/WTFString.h"
48 class CSSStyleDeclaration;
49 class CSSComputedStyleDeclaration;
50 class CSSPrimitiveValue;
55 class MutableStylePropertySet;
60 class StylePropertySet;
61 class VisibleSelection;
63 class EditingStyle : public RefCounted<EditingStyle> {
66 enum PropertiesToInclude { AllProperties, OnlyEditingInheritableProperties, EditingPropertiesInEffect };
67 enum ShouldPreserveWritingDirection { PreserveWritingDirection, DoNotPreserveWritingDirection };
68 enum ShouldExtractMatchingStyle { ExtractMatchingStyle, DoNotExtractMatchingStyle };
69 static float NoFontDelta;
71 static PassRefPtr<EditingStyle> create()
73 return adoptRef(new EditingStyle());
76 static PassRefPtr<EditingStyle> create(Node* node, PropertiesToInclude propertiesToInclude = OnlyEditingInheritableProperties)
78 return adoptRef(new EditingStyle(node, propertiesToInclude));
81 static PassRefPtr<EditingStyle> create(const Position& position, PropertiesToInclude propertiesToInclude = OnlyEditingInheritableProperties)
83 return adoptRef(new EditingStyle(position, propertiesToInclude));
86 static PassRefPtr<EditingStyle> create(const StylePropertySet* style)
88 return adoptRef(new EditingStyle(style));
91 static PassRefPtr<EditingStyle> create(CSSPropertyID propertyID, const String& value)
93 return adoptRef(new EditingStyle(propertyID, value));
98 MutableStylePropertySet* style() { return m_mutableStyle.get(); }
99 bool textDirection(WritingDirection&) const;
100 bool isEmpty() const;
101 void overrideWithStyle(const StylePropertySet*);
103 PassRefPtr<EditingStyle> copy() const;
104 PassRefPtr<EditingStyle> extractAndRemoveBlockProperties();
105 PassRefPtr<EditingStyle> extractAndRemoveTextDirection();
106 void removeBlockProperties();
107 void removeStyleAddedByNode(Node*);
108 void removeStyleConflictingWithStyleOfNode(Node*);
109 void collapseTextDecorationProperties();
110 enum ShouldIgnoreTextOnlyProperties { IgnoreTextOnlyProperties, DoNotIgnoreTextOnlyProperties };
111 TriState triStateOfStyle(EditingStyle*) const;
112 TriState triStateOfStyle(const VisibleSelection&) const;
113 bool conflictsWithInlineStyleOfElement(Element* element) const { return conflictsWithInlineStyleOfElement(element, 0, 0); }
114 bool conflictsWithInlineStyleOfElement(Element* element, EditingStyle* extractedStyle, Vector<CSSPropertyID>& conflictingProperties) const
116 return conflictsWithInlineStyleOfElement(element, extractedStyle, &conflictingProperties);
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;
124 static bool elementIsStyledSpanOrHTMLEquivalent(const HTMLElement*);
126 void prepareToApplyAt(const Position&, ShouldPreserveWritingDirection = DoNotPreserveWritingDirection);
127 void mergeTypingStyle(Document*);
128 enum CSSPropertyOverrideMode { OverrideValues, DoNotOverrideValues };
129 void mergeInlineStyleOfElement(Element*, CSSPropertyOverrideMode, PropertiesToInclude = AllProperties);
130 static PassRefPtr<EditingStyle> wrappingStyleForSerialization(Node* context, bool shouldAnnotate);
131 void mergeStyleFromRules(Element*);
132 void mergeStyleFromRulesForSerialization(Element*);
133 void removeStyleFromRulesAndContext(Element*, Node* context);
134 void removePropertiesInElementDefaultStyle(Element*);
136 int legacyFontSize(Document*) const;
138 float fontSizeDelta() const { return m_fontSizeDelta; }
139 bool hasFontSizeDelta() const { return m_fontSizeDelta != NoFontDelta; }
141 static PassRefPtr<EditingStyle> styleAtSelectionStart(const VisibleSelection&, bool shouldUseBackgroundColorInEffect = false);
142 static WritingDirection textDirectionForSelection(const VisibleSelection&, EditingStyle* typingStyle, bool& hasNestedOrMultipleEmbeddings);
145 EditingStyle(Node*, PropertiesToInclude);
146 EditingStyle(const Position&, PropertiesToInclude);
147 explicit EditingStyle(const StylePropertySet*);
148 EditingStyle(CSSPropertyID, const String& value);
149 void init(Node*, PropertiesToInclude);
150 void removeTextFillAndStrokeColorsIfNeeded(RenderStyle*);
151 void setProperty(CSSPropertyID, const String& value, bool important = false);
152 void replaceFontSizeByKeywordIfPossible(RenderStyle*, CSSComputedStyleDeclaration*);
153 void extractFontSizeDelta();
154 TriState triStateOfStyle(CSSStyleDeclaration* styleToCompare, ShouldIgnoreTextOnlyProperties) const;
155 bool conflictsWithInlineStyleOfElement(Element*, EditingStyle* extractedStyle, Vector<CSSPropertyID>* conflictingProperties) const;
156 void mergeInlineAndImplicitStyleOfElement(Element*, CSSPropertyOverrideMode, PropertiesToInclude);
157 void mergeStyle(const StylePropertySet*, CSSPropertyOverrideMode);
159 RefPtrWillBePersistent<MutableStylePropertySet> m_mutableStyle;
160 bool m_shouldUseFixedDefaultFontSize;
161 float m_fontSizeDelta;
163 friend class HTMLElementEquivalent;
164 friend class HTMLAttributeEquivalent;
171 , m_applyItalic(false)
172 , m_applyUnderline(false)
173 , m_applyLineThrough(false)
174 , m_applySubscript(false)
175 , m_applySuperscript(false)
178 StyleChange(EditingStyle*, const Position&);
180 String cssStyle() const { return m_cssStyle; }
181 bool applyBold() const { return m_applyBold; }
182 bool applyItalic() const { return m_applyItalic; }
183 bool applyUnderline() const { return m_applyUnderline; }
184 bool applyLineThrough() const { return m_applyLineThrough; }
185 bool applySubscript() const { return m_applySubscript; }
186 bool applySuperscript() const { return m_applySuperscript; }
187 bool applyFontColor() const { return m_applyFontColor.length() > 0; }
188 bool applyFontFace() const { return m_applyFontFace.length() > 0; }
189 bool applyFontSize() const { return m_applyFontSize.length() > 0; }
191 String fontColor() { return m_applyFontColor; }
192 String fontFace() { return m_applyFontFace; }
193 String fontSize() { return m_applyFontSize; }
195 bool operator==(const StyleChange& other)
197 return m_cssStyle == other.m_cssStyle
198 && m_applyBold == other.m_applyBold
199 && m_applyItalic == other.m_applyItalic
200 && m_applyUnderline == other.m_applyUnderline
201 && m_applyLineThrough == other.m_applyLineThrough
202 && m_applySubscript == other.m_applySubscript
203 && m_applySuperscript == other.m_applySuperscript
204 && m_applyFontColor == other.m_applyFontColor
205 && m_applyFontFace == other.m_applyFontFace
206 && m_applyFontSize == other.m_applyFontSize;
208 bool operator!=(const StyleChange& other)
210 return !(*this == other);
213 void extractTextStyles(Document*, MutableStylePropertySet*, bool shouldUseFixedFontDefaultSize);
218 bool m_applyUnderline;
219 bool m_applyLineThrough;
220 bool m_applySubscript;
221 bool m_applySuperscript;
222 String m_applyFontColor;
223 String m_applyFontFace;
224 String m_applyFontSize;
227 // FIXME: Remove these functions or make them non-global to discourage using CSSStyleDeclaration directly.
228 CSSValueID getIdentifierValue(CSSStyleDeclaration*, CSSPropertyID);
229 CSSValueID getIdentifierValue(StylePropertySet*, CSSPropertyID);
231 } // namespace WebCore
233 #endif // EditingStyle_h