Bug 1247796. Use keyboardFocusIndicatorColor for ActiveBorder system color keyword...
[gecko.git] / editor / libeditor / nsHTMLCSSUtils.h
blobdd50952e0f0aeac37e474ef91d388b07e4c835a9
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsHTMLCSSUtils_h__
7 #define nsHTMLCSSUtils_h__
9 #include "ChangeStyleTxn.h" // for ChangeStyleTxn::EChangeType
10 #include "nsCOMPtr.h" // for already_AddRefed
11 #include "nsTArray.h" // for nsTArray
12 #include "nscore.h" // for nsAString, nsresult, nullptr
14 class nsComputedDOMStyle;
15 class nsIAtom;
16 class nsIContent;
17 class nsIDOMCSSStyleDeclaration;
18 class nsIDOMElement;
19 class nsIDOMNode;
20 class nsINode;
21 class nsString;
22 namespace mozilla {
23 namespace dom {
24 class Element;
25 } // namespace dom
26 } // namespace mozilla
28 class nsHTMLEditor;
30 typedef void (*nsProcessValueFunc)(const nsAString * aInputString, nsAString & aOutputString,
31 const char * aDefaultValueString,
32 const char * aPrependString, const char* aAppendString);
34 class nsHTMLCSSUtils
36 public:
37 explicit nsHTMLCSSUtils(nsHTMLEditor* aEditor);
38 ~nsHTMLCSSUtils();
40 enum nsCSSEditableProperty {
41 eCSSEditableProperty_NONE=0,
42 eCSSEditableProperty_background_color,
43 eCSSEditableProperty_background_image,
44 eCSSEditableProperty_border,
45 eCSSEditableProperty_caption_side,
46 eCSSEditableProperty_color,
47 eCSSEditableProperty_float,
48 eCSSEditableProperty_font_family,
49 eCSSEditableProperty_font_size,
50 eCSSEditableProperty_font_style,
51 eCSSEditableProperty_font_weight,
52 eCSSEditableProperty_height,
53 eCSSEditableProperty_list_style_type,
54 eCSSEditableProperty_margin_left,
55 eCSSEditableProperty_margin_right,
56 eCSSEditableProperty_text_align,
57 eCSSEditableProperty_text_decoration,
58 eCSSEditableProperty_vertical_align,
59 eCSSEditableProperty_whitespace,
60 eCSSEditableProperty_width
63 enum StyleType { eSpecified, eComputed };
66 struct CSSEquivTable {
67 nsCSSEditableProperty cssProperty;
68 nsProcessValueFunc processValueFunctor;
69 const char * defaultValue;
70 const char * prependValue;
71 const char * appendValue;
72 bool gettable;
73 bool caseSensitiveValue;
76 /** answers true if the given combination element_name/attribute_name
77 * has a CSS equivalence in this implementation
79 * @return a boolean saying if the tag/attribute has a css equiv
80 * @param aNode [IN] a DOM node
81 * @param aProperty [IN] an atom containing a HTML tag name
82 * @param aAttribute [IN] a string containing the name of a HTML
83 * attribute carried by the element above
85 bool IsCSSEditableProperty(nsINode* aNode, nsIAtom* aProperty,
86 const nsAString* aAttribute);
88 /** adds/remove a CSS declaration to the STYLE atrribute carried by a given element
90 * @param aElement [IN] a DOM element
91 * @param aProperty [IN] an atom containing the CSS property to set
92 * @param aValue [IN] a string containing the value of the CSS property
93 * @param aSuppressTransaction [IN] a boolean indicating, when true,
94 * that no transaction should be recorded
96 nsresult SetCSSProperty(mozilla::dom::Element& aElement, nsIAtom& aProperty,
97 const nsAString& aValue, bool aSuppressTxn = false);
98 nsresult SetCSSPropertyPixels(mozilla::dom::Element& aElement,
99 nsIAtom& aProperty, int32_t aIntValue);
100 nsresult RemoveCSSProperty(mozilla::dom::Element& aElement,
101 nsIAtom& aProperty,
102 const nsAString& aPropertyValue,
103 bool aSuppressTxn = false);
105 /** directly adds/remove a CSS declaration to the STYLE atrribute carried by
106 * a given element without going through the txn manager
108 * @param aElement [IN] a DOM element
109 * @param aProperty [IN] a string containing the CSS property to set/remove
110 * @param aValue [IN] a string containing the new value of the CSS property
112 nsresult SetCSSProperty(nsIDOMElement * aElement,
113 const nsAString & aProperty,
114 const nsAString & aValue);
115 nsresult SetCSSPropertyPixels(nsIDOMElement * aElement,
116 const nsAString & aProperty,
117 int32_t aIntValue);
119 /** Gets the specified/computed style value of a CSS property for a given
120 * node (or its element ancestor if it is not an element)
122 * @param aNode [IN] a DOM node
123 * @param aProperty [IN] an atom containing the CSS property to get
124 * @param aPropertyValue [OUT] the retrieved value of the property
126 nsresult GetSpecifiedProperty(nsINode& aNode, nsIAtom& aProperty,
127 nsAString& aValue);
128 nsresult GetComputedProperty(nsINode& aNode, nsIAtom& aProperty,
129 nsAString& aValue);
131 /** Removes a CSS property from the specified declarations in STYLE attribute
132 ** and removes the node if it is an useless span
134 * @param aNode [IN] the specific node we want to remove a style from
135 * @param aProperty [IN] the CSS property atom to remove
136 * @param aPropertyValue [IN] the value of the property we have to rremove if the property
137 * accepts more than one value
139 nsresult RemoveCSSInlineStyle(nsIDOMNode * aNode, nsIAtom * aProperty, const nsAString & aPropertyValue);
141 /** Answers true is the property can be removed by setting a "none" CSS value
142 * on a node
144 * @return a boolean saying if the property can be remove by setting a "none" value
145 * @param aProperty [IN] an atom containing a CSS property
146 * @param aAttribute [IN] pointer to an attribute name or null if this information is irrelevant
148 bool IsCSSInvertible(nsIAtom& aProperty, const nsAString* aAttribute);
150 /** Get the default browser background color if we need it for GetCSSBackgroundColorState
152 * @param aColor [OUT] the default color as it is defined in prefs
154 void GetDefaultBackgroundColor(nsAString & aColor);
156 /** Get the default length unit used for CSS Indent/Outdent
158 * @param aLengthUnit [OUT] the default length unit as it is defined in prefs
160 void GetDefaultLengthUnit(nsAString & aLengthUnit);
162 /** returns the list of values for the CSS equivalences to
163 * the passed HTML style for the passed node
165 * @param aNode [IN] a DOM node
166 * @param aHTMLProperty [IN] an atom containing an HTML property
167 * @param aAttribute [IN] a pointer to an attribute name or nullptr if irrelevant
168 * @param aValueString [OUT] the list of css values
169 * @param aStyleType [IN] eSpecified or eComputed
171 nsresult GetCSSEquivalentToHTMLInlineStyleSet(nsINode* aNode,
172 nsIAtom * aHTMLProperty,
173 const nsAString * aAttribute,
174 nsAString & aValueString,
175 StyleType aStyleType);
177 /** Does the node aNode (or his parent if it is not an element node) carries
178 * the CSS equivalent styles to the HTML style for this node ?
180 * @param aNode [IN] a DOM node
181 * @param aHTMLProperty [IN] an atom containing an HTML property
182 * @param aAttribute [IN] a pointer to an attribute name or nullptr if irrelevant
183 * @param aIsSet [OUT] a boolean being true if the css properties are set
184 * @param aValueString [IN/OUT] the attribute value (in) the list of css values (out)
185 * @param aStyleType [IN] eSpecified or eComputed
187 * The nsIContent variant returns aIsSet instead of using an out parameter.
189 bool IsCSSEquivalentToHTMLInlineStyleSet(nsINode* aContent,
190 nsIAtom* aProperty,
191 const nsAString* aAttribute,
192 const nsAString& aValue,
193 StyleType aStyleType);
195 bool IsCSSEquivalentToHTMLInlineStyleSet(nsINode* aContent,
196 nsIAtom* aProperty,
197 const nsAString* aAttribute,
198 nsAString& aValue,
199 StyleType aStyleType);
201 nsresult IsCSSEquivalentToHTMLInlineStyleSet(nsIDOMNode * aNode,
202 nsIAtom * aHTMLProperty,
203 const nsAString * aAttribute,
204 bool & aIsSet,
205 nsAString & aValueString,
206 StyleType aStyleType);
208 /** Adds to the node the CSS inline styles equivalent to the HTML style
209 * and return the number of CSS properties set by the call
211 * @param aNode [IN] a DOM node
212 * @param aHTMLProperty [IN] an atom containing an HTML property
213 * @param aAttribute [IN] a pointer to an attribute name or nullptr if irrelevant
214 * @param aValue [IN] the attribute value
215 * @param aCount [OUT] the number of CSS properties set by the call
216 * @param aSuppressTransaction [IN] a boolean indicating, when true,
217 * that no transaction should be recorded
219 * aCount is returned by the dom::Element variant instead of being an out
220 * parameter.
222 int32_t SetCSSEquivalentToHTMLStyle(mozilla::dom::Element* aElement,
223 nsIAtom* aProperty,
224 const nsAString* aAttribute,
225 const nsAString* aValue,
226 bool aSuppressTransaction);
227 nsresult SetCSSEquivalentToHTMLStyle(nsIDOMNode * aNode,
228 nsIAtom * aHTMLProperty,
229 const nsAString * aAttribute,
230 const nsAString * aValue,
231 int32_t * aCount,
232 bool aSuppressTransaction);
234 /** removes from the node the CSS inline styles equivalent to the HTML style
236 * @param aNode [IN] a DOM node
237 * @param aHTMLProperty [IN] an atom containing an HTML property
238 * @param aAttribute [IN] a pointer to an attribute name or nullptr if irrelevant
239 * @param aValue [IN] the attribute value
240 * @param aSuppressTransaction [IN] a boolean indicating, when true,
241 * that no transaction should be recorded
243 nsresult RemoveCSSEquivalentToHTMLStyle(nsIDOMNode * aNode,
244 nsIAtom *aHTMLProperty,
245 const nsAString *aAttribute,
246 const nsAString *aValue,
247 bool aSuppressTransaction);
248 /** removes from the node the CSS inline styles equivalent to the HTML style
250 * @param aElement [IN] a DOM Element (must not be null)
251 * @param aHTMLProperty [IN] an atom containing an HTML property
252 * @param aAttribute [IN] a pointer to an attribute name or nullptr if irrelevant
253 * @param aValue [IN] the attribute value
254 * @param aSuppressTransaction [IN] a boolean indicating, when true,
255 * that no transaction should be recorded
257 nsresult RemoveCSSEquivalentToHTMLStyle(mozilla::dom::Element* aElement,
258 nsIAtom* aHTMLProperty,
259 const nsAString* aAttribute,
260 const nsAString* aValue,
261 bool aSuppressTransaction);
263 /** parses a "xxxx.xxxxxuuu" string where x is a digit and u an alpha char
264 * we need such a parser because nsIDOMCSSStyleDeclaration::GetPropertyCSSValue() is not
265 * implemented
267 * @param aString [IN] input string to parse
268 * @param aValue [OUT] numeric part
269 * @param aUnit [OUT] unit part
271 void ParseLength(const nsAString & aString, float * aValue, nsIAtom ** aUnit);
273 /** sets the mIsCSSPrefChecked private member ; used as callback from observer when
274 * the css pref state is changed
276 * @param aIsCSSPrefChecked [IN] the new boolean state for the pref
278 void SetCSSEnabled(bool aIsCSSPrefChecked);
280 /** retrieves the mIsCSSPrefChecked private member, true if the css pref is checked,
281 * false if it is not
283 * @return the boolean value of the css pref
285 bool IsCSSPrefChecked();
287 /** ElementsSameStyle compares two elements and checks if they have the same
288 * specified CSS declarations in the STYLE attribute
289 * The answer is always false if at least one of them carries an ID or a class
291 * @return true if the two elements are considered to have same styles
292 * @param aFirstNode [IN] a DOM node
293 * @param aSecondNode [IN] a DOM node
295 bool ElementsSameStyle(mozilla::dom::Element* aFirstNode,
296 mozilla::dom::Element* aSecondNode);
297 bool ElementsSameStyle(nsIDOMNode *aFirstNode, nsIDOMNode *aSecondNode);
299 /** get the specified inline styles (style attribute) for an element
301 * @param aElement [IN] the element node
302 * @param aCssDecl [OUT] the CSS declaration corresponding to the style attr
303 * @param aLength [OUT] the number of declarations in aCssDecl
305 nsresult GetInlineStyles(mozilla::dom::Element* aElement,
306 nsIDOMCSSStyleDeclaration** aCssDecl,
307 uint32_t* aLength);
308 nsresult GetInlineStyles(nsIDOMElement* aElement,
309 nsIDOMCSSStyleDeclaration** aCssDecl,
310 uint32_t* aLength);
311 private:
312 nsresult GetInlineStyles(nsISupports* aElement,
313 nsIDOMCSSStyleDeclaration** aCssDecl,
314 uint32_t* aLength);
316 public:
317 /** returns aNode itself if it is an element node, or the first ancestors being an element
318 * node if aNode is not one itself
320 * @param aNode [IN] a node
321 * @param aElement [OUT] the deepest element node containing aNode (possibly aNode itself)
323 mozilla::dom::Element* GetElementContainerOrSelf(nsINode* aNode);
324 already_AddRefed<nsIDOMElement> GetElementContainerOrSelf(nsIDOMNode* aNode);
327 * Gets the computed style for a given element. Can return null.
329 already_AddRefed<nsComputedDOMStyle>
330 GetComputedStyle(mozilla::dom::Element* aElement);
333 private:
335 /** retrieves the css property atom from an enum
337 * @param aProperty [IN] the enum value for the property
338 * @param aAtom [OUT] the corresponding atom
340 void GetCSSPropertyAtom(nsCSSEditableProperty aProperty, nsIAtom ** aAtom);
342 /** retrieves the CSS declarations equivalent to a HTML style value for
343 * a given equivalence table
345 * @param aPropertyArray [OUT] the array of css properties
346 * @param aValueArray [OUT] the array of values for the css properties above
347 * @param aEquivTable [IN] the equivalence table
348 * @param aValue [IN] the HTML style value
349 * @param aGetOrRemoveRequest [IN] a boolean value being true if the call to the current method
350 * is made for GetCSSEquivalentToHTMLInlineStyleSet or
351 * RemoveCSSEquivalentToHTMLInlineStyleSet
354 void BuildCSSDeclarations(nsTArray<nsIAtom*> & aPropertyArray,
355 nsTArray<nsString> & cssValueArray,
356 const CSSEquivTable * aEquivTable,
357 const nsAString * aValue,
358 bool aGetOrRemoveRequest);
360 /** retrieves the CSS declarations equivalent to the given HTML property/attribute/value
361 * for a given node
363 * @param aNode [IN] the DOM node
364 * @param aHTMLProperty [IN] an atom containing an HTML property
365 * @param aAttribute [IN] a pointer to an attribute name or nullptr if irrelevant
366 * @param aValue [IN] the attribute value
367 * @param aPropertyArray [OUT] the array of css properties
368 * @param aValueArray [OUT] the array of values for the css properties above
369 * @param aGetOrRemoveRequest [IN] a boolean value being true if the call to the current method
370 * is made for GetCSSEquivalentToHTMLInlineStyleSet or
371 * RemoveCSSEquivalentToHTMLInlineStyleSet
373 void GenerateCSSDeclarationsFromHTMLStyle(mozilla::dom::Element* aNode,
374 nsIAtom* aHTMLProperty,
375 const nsAString* aAttribute,
376 const nsAString* aValue,
377 nsTArray<nsIAtom*>& aPropertyArray,
378 nsTArray<nsString>& aValueArray,
379 bool aGetOrRemoveRequest);
381 /** Creates a Transaction for setting or removing a CSS property. Never
382 * returns null.
384 * @param aElement [IN] a DOM element
385 * @param aProperty [IN] a CSS property
386 * @param aValue [IN] the value to set for this CSS property
387 * @param aChangeType [IN] eSet to set, eRemove to remove
389 already_AddRefed<mozilla::dom::ChangeStyleTxn>
390 CreateCSSPropertyTxn(mozilla::dom::Element& aElement,
391 nsIAtom& aProperty, const nsAString& aValue,
392 mozilla::dom::ChangeStyleTxn::EChangeType aChangeType);
394 /** back-end for GetSpecifiedProperty and GetComputedProperty
396 * @param aNode [IN] a DOM node
397 * @param aProperty [IN] a CSS property
398 * @param aValue [OUT] the retrieved value for this property
399 * @param aStyleType [IN] eSpecified or eComputed
401 nsresult GetCSSInlinePropertyBase(nsINode* aNode, nsIAtom* aProperty,
402 nsAString& aValue, StyleType aStyleType);
405 private:
406 nsHTMLEditor *mHTMLEditor;
407 bool mIsCSSPrefChecked;
410 #define NS_EDITOR_INDENT_INCREMENT_IN 0.4134f
411 #define NS_EDITOR_INDENT_INCREMENT_CM 1.05f
412 #define NS_EDITOR_INDENT_INCREMENT_MM 10.5f
413 #define NS_EDITOR_INDENT_INCREMENT_PT 29.76f
414 #define NS_EDITOR_INDENT_INCREMENT_PC 2.48f
415 #define NS_EDITOR_INDENT_INCREMENT_EM 3
416 #define NS_EDITOR_INDENT_INCREMENT_EX 6
417 #define NS_EDITOR_INDENT_INCREMENT_PX 40
418 #define NS_EDITOR_INDENT_INCREMENT_PERCENT 4
420 #endif /* nsHTMLCSSUtils_h__ */