1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Netscape Public License
6 * Version 1.1 (the "License"); you may not use this file except in
7 * compliance with the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/NPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Daniel Glazman <glazman@netscape.com>, foo@bar.org
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the NPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the NPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #ifndef nsHTMLCSSUtils_h__
40 #define nsHTMLCSSUtils_h__
44 #include "nsIDOMViewCSS.h"
45 #include "nsIDOMNode.h"
46 #include "nsIDOMElement.h"
47 #include "nsIHTMLEditor.h"
48 #include "ChangeCSSInlineStyleTxn.h"
49 #include "nsEditProperty.h"
50 #include "nsIDOMCSSStyleDeclaration.h"
52 #define SPECIFIED_STYLE_TYPE 1
53 #define COMPUTED_STYLE_TYPE 2
57 typedef void (*nsProcessValueFunc
)(const nsAString
* aInputString
, nsAString
& aOutputString
,
58 const char * aDefaultValueString
,
59 const char * aPrependString
, const char* aAppendString
);
67 enum nsCSSEditableProperty
{
68 eCSSEditableProperty_NONE
=0,
69 eCSSEditableProperty_background_color
,
70 eCSSEditableProperty_background_image
,
71 eCSSEditableProperty_border
,
72 eCSSEditableProperty_caption_side
,
73 eCSSEditableProperty_color
,
74 eCSSEditableProperty_float
,
75 eCSSEditableProperty_font_family
,
76 eCSSEditableProperty_font_size
,
77 eCSSEditableProperty_font_style
,
78 eCSSEditableProperty_font_weight
,
79 eCSSEditableProperty_height
,
80 eCSSEditableProperty_list_style_type
,
81 eCSSEditableProperty_margin_left
,
82 eCSSEditableProperty_margin_right
,
83 eCSSEditableProperty_text_align
,
84 eCSSEditableProperty_text_decoration
,
85 eCSSEditableProperty_vertical_align
,
86 eCSSEditableProperty_whitespace
,
87 eCSSEditableProperty_width
91 struct CSSEquivTable
{
92 nsCSSEditableProperty cssProperty
;
93 nsProcessValueFunc processValueFunctor
;
94 const char * defaultValue
;
95 const char * prependValue
;
96 const char * appendValue
;
98 PRBool caseSensitiveValue
;
102 nsresult
Init(nsHTMLEditor
* aEditor
);
104 /** answers true if the given combination element_name/attribute_name
105 * has a CSS equivalence in this implementation
107 * @return a boolean saying if the tag/attribute has a css equiv
108 * @param aNode [IN] a DOM node
109 * @param aProperty [IN] an atom containing a HTML tag name
110 * @param aAttribute [IN] a string containing the name of a HTML attribute carried by the element above
112 PRBool
IsCSSEditableProperty(nsIDOMNode
* aNode
, nsIAtom
* aProperty
, const nsAString
* aAttribute
);
114 /** adds/remove a CSS declaration to the STYLE atrribute carried by a given element
116 * @param aElement [IN] a DOM element
117 * @param aProperty [IN] an atom containing the CSS property to set
118 * @param aValue [IN] a string containing the value of the CSS property
119 * @param aSuppressTransaction [IN] a boolean indicating, when true,
120 * that no transaction should be recorded
122 nsresult
SetCSSProperty(nsIDOMElement
* aElement
, nsIAtom
* aProperty
,
123 const nsAString
& aValue
,
124 PRBool aSuppressTransaction
);
125 nsresult
RemoveCSSProperty(nsIDOMElement
* aElement
, nsIAtom
* aProperty
,
126 const nsAString
& aPropertyValue
, PRBool aSuppressTransaction
);
128 /** directly adds/remove a CSS declaration to the STYLE atrribute carried by
129 * a given element without going through the txn manager
131 * @param aElement [IN] a DOM element
132 * @param aProperty [IN] a string containing the CSS property to set/remove
133 * @param aValue [IN] a string containing the new value of the CSS property
135 nsresult
SetCSSProperty(nsIDOMElement
* aElement
,
136 const nsAString
& aProperty
,
137 const nsAString
& aValue
);
138 nsresult
RemoveCSSProperty(nsIDOMElement
* aElement
,
139 const nsAString
& aProperty
);
141 /** gets the specified/computed style value of a CSS property for a given node (or its element
142 * ancestor if it is not an element)
144 * @param aNode [IN] a DOM node
145 * @param aProperty [IN] an atom containing the CSS property to get
146 * @param aPropertyValue [OUT] the retrieved value of the property
148 nsresult
GetSpecifiedProperty(nsIDOMNode
*aNode
, nsIAtom
*aProperty
,
150 nsresult
GetComputedProperty(nsIDOMNode
*aNode
, nsIAtom
*aProperty
,
153 /** Removes a CSS property from the specified declarations in STYLE attribute
154 ** and removes the node if it is an useless span
156 * @param aNode [IN] the specific node we want to remove a style from
157 * @param aProperty [IN] the CSS property atom to remove
158 * @param aPropertyValue [IN] the value of the property we have to rremove if the property
159 * accepts more than one value
161 nsresult
RemoveCSSInlineStyle(nsIDOMNode
* aNode
, nsIAtom
* aProperty
, const nsAString
& aPropertyValue
);
163 /** Answers true is the property can be removed by setting a "none" CSS value
166 * @return a boolean saying if the property can be remove by setting a "none" value
167 * @param aProperty [IN] an atom containing a CSS property
168 * @param aAttribute [IN] pointer to an attribute name or null if this information is irrelevant
170 PRBool
IsCSSInvertable(nsIAtom
* aProperty
, const nsAString
* aAttribute
);
172 /** Get the default browser background color if we need it for GetCSSBackgroundColorState
174 * @param aColor [OUT] the default color as it is defined in prefs
176 nsresult
GetDefaultBackgroundColor(nsAString
& aColor
);
178 /** Get the default length unit used for CSS Indent/Outdent
180 * @param aLengthUnit [OUT] the default length unit as it is defined in prefs
182 nsresult
GetDefaultLengthUnit(nsAString
& aLengthUnit
);
184 /** asnwers true if the element aElement carries an ID or a class
186 * @param aElement [IN] a DOM element
187 * @param aReturn [OUT] the boolean answer
189 nsresult
HasClassOrID(nsIDOMElement
* aElement
, PRBool
& aReturn
);
191 /** returns the list of values for the CSS equivalences to
192 * the passed HTML style for the passed node
194 * @param aNode [IN] a DOM node
195 * @param aHTMLProperty [IN] an atom containing an HTML property
196 * @param aAttribute [IN] a pointer to an attribute name or nsnull if irrelevant
197 * @param aValueString [OUT] the list of css values
198 * @param aStyleType [IN] SPECIFIED_STYLE_TYPE to query the specified style values
199 COMPUTED_STYLE_TYPE to query the computed style values
201 nsresult
GetCSSEquivalentToHTMLInlineStyleSet(nsIDOMNode
* aNode
,
202 nsIAtom
* aHTMLProperty
,
203 const nsAString
* aAttribute
,
204 nsAString
& aValueString
,
207 /** Does the node aNode (or his parent if it is not an element node) carries
208 * the CSS equivalent styles to the HTML style for this node ?
210 * @param aNode [IN] a DOM node
211 * @param aHTMLProperty [IN] an atom containing an HTML property
212 * @param aAttribute [IN] a pointer to an attribute name or nsnull if irrelevant
213 * @param aIsSet [OUT] a boolean being true if the css properties are set
214 * @param aValueString [IN/OUT] the attribute value (in) the list of css values (out)
215 * @param aStyleType [IN] SPECIFIED_STYLE_TYPE to query the specified style values
216 COMPUTED_STYLE_TYPE to query the computed style values
218 nsresult
IsCSSEquivalentToHTMLInlineStyleSet(nsIDOMNode
* aNode
,
219 nsIAtom
* aHTMLProperty
,
220 const nsAString
* aAttribute
,
222 nsAString
& aValueString
,
225 /** Adds to the node the CSS inline styles equivalent to the HTML style
226 * and return the number of CSS properties set by the call
228 * @param aNode [IN] a DOM node
229 * @param aHTMLProperty [IN] an atom containing an HTML property
230 * @param aAttribute [IN] a pointer to an attribute name or nsnull if irrelevant
231 * @param aValue [IN] the attribute value
232 * @param aCount [OUT] the number of CSS properties set by the call
233 * @param aSuppressTransaction [IN] a boolean indicating, when true,
234 * that no transaction should be recorded
236 nsresult
SetCSSEquivalentToHTMLStyle(nsIDOMNode
* aNode
,
237 nsIAtom
* aHTMLProperty
,
238 const nsAString
* aAttribute
,
239 const nsAString
* aValue
,
241 PRBool aSuppressTransaction
);
243 /** removes from the node the CSS inline styles equivalent to the HTML style
245 * @param aNode [IN] a DOM node
246 * @param aHTMLProperty [IN] an atom containing an HTML property
247 * @param aAttribute [IN] a pointer to an attribute name or nsnull if irrelevant
248 * @param aValue [IN] the attribute value
249 * @param aSuppressTransaction [IN] a boolean indicating, when true,
250 * that no transaction should be recorded
252 nsresult
RemoveCSSEquivalentToHTMLStyle(nsIDOMNode
* aNode
,
253 nsIAtom
*aHTMLProperty
,
254 const nsAString
*aAttribute
,
255 const nsAString
*aValue
,
256 PRBool aSuppressTransaction
);
258 /** parses a "xxxx.xxxxxuuu" string where x is a digit and u an alpha char
259 * we need such a parser because nsIDOMCSSStyleDeclaration::GetPropertyCSSValue() is not
262 * @param aString [IN] input string to parse
263 * @param aValue [OUT] numeric part
264 * @param aUnit [OUT] unit part
266 void ParseLength(const nsAString
& aString
, float * aValue
, nsIAtom
** aUnit
);
268 /** sets the mIsCSSPrefChecked private member ; used as callback from observer when
269 * the css pref state is changed
271 * @param aIsCSSPrefChecked [IN] the new boolean state for the pref
273 nsresult
SetCSSEnabled(PRBool aIsCSSPrefChecked
);
275 /** retrieves the mIsCSSPrefChecked private member, true if the css pref is checked,
278 * @return the boolean value of the css pref
280 PRBool
IsCSSPrefChecked();
282 /** ElementsSameStyle compares two elements and checks if they have the same
283 * specified CSS declarations in the STYLE attribute
284 * The answer is always false if at least one of them carries an ID or a class
286 * @return true if the two elements are considered to have same styles
287 * @param aFirstNode [IN] a DOM node
288 * @param aSecondNode [IN] a DOM node
290 PRBool
ElementsSameStyle(nsIDOMNode
*aFirstNode
, nsIDOMNode
*aSecondNode
);
292 /** get the specified inline styles (style attribute) for an element
294 * @param aElement [IN] the element node
295 * @param aCssDecl [OUT] the CSS declaration corresponding to the style attr
296 * @param aLength [OUT] the number of declarations in aCssDecl
298 nsresult
GetInlineStyles(nsIDOMElement
* aElement
, nsIDOMCSSStyleDeclaration
** aCssDecl
,
301 /** returns aNode itself if it is an element node, or the first ancestors being an element
302 * node if aNode is not one itself
304 * @param aNode [IN] a node
305 * @param aElement [OUT] the deepest element node containing aNode (possibly aNode itself)
307 nsresult
GetElementContainerOrSelf(nsIDOMNode
* aNode
, nsIDOMElement
** aElement
);
309 /** Gets the default DOMView for a given node
311 * @param aNode the node we want the default DOMView for
312 * @param aViewCSS [OUT] the default DOMViewCSS
314 nsresult
GetDefaultViewCSS(nsIDOMNode
* aNode
, nsIDOMViewCSS
** aViewCSS
);
319 /** retrieves the css property atom from an enum
321 * @param aProperty [IN] the enum value for the property
322 * @param aAtom [OUT] the corresponding atom
324 void GetCSSPropertyAtom(nsCSSEditableProperty aProperty
, nsIAtom
** aAtom
);
326 /** retrieves the CSS declarations equivalent to a HTML style value for
327 * a given equivalence table
329 * @param aPropertyArray [OUT] the array of css properties
330 * @param aValueArray [OUT] the array of values for the css properties above
331 * @param aEquivTable [IN] the equivalence table
332 * @param aValue [IN] the HTML style value
333 * @param aGetOrRemoveRequest [IN] a boolean value being true if the call to the current method
334 * is made for GetCSSEquivalentToHTMLInlineStyleSet or
335 * RemoveCSSEquivalentToHTMLInlineStyleSet
338 void BuildCSSDeclarations(nsVoidArray
& aPropertyArray
,
339 nsStringArray
& cssValueArray
,
340 const CSSEquivTable
* aEquivTable
,
341 const nsAString
* aValue
,
342 PRBool aGetOrRemoveRequest
);
344 /** retrieves the CSS declarations equivalent to the given HTML property/attribute/value
347 * @param aNode [IN] the DOM node
348 * @param aHTMLProperty [IN] an atom containing an HTML property
349 * @param aAttribute [IN] a pointer to an attribute name or nsnull if irrelevant
350 * @param aValue [IN] the attribute value
351 * @param aPropertyArray [OUT] the array of css properties
352 * @param aValueArray [OUT] the array of values for the css properties above
353 * @param aGetOrRemoveRequest [IN] a boolean value being true if the call to the current method
354 * is made for GetCSSEquivalentToHTMLInlineStyleSet or
355 * RemoveCSSEquivalentToHTMLInlineStyleSet
357 void GenerateCSSDeclarationsFromHTMLStyle(nsIDOMNode
* aNode
,
358 nsIAtom
* aHTMLProperty
,
359 const nsAString
*aAttribute
,
360 const nsAString
*aValue
,
361 nsVoidArray
& aPropertyArray
,
362 nsStringArray
& aValueArray
,
363 PRBool aGetOrRemoveRequest
);
365 /** creates a Transaction for setting or removing a css property
367 * @param aElement [IN] a DOM element
368 * @param aProperty [IN] a CSS property
369 * @param aValue [IN] the value to remove for this CSS property or the empty string if irrelevant
370 * @param aTxn [OUT] the created transaction
371 * @param aRemoveProperty [IN] true if we create a "remove" transaction, false for a "set"
373 nsresult
CreateCSSPropertyTxn(nsIDOMElement
* aElement
,
375 const nsAString
& aValue
,
376 ChangeCSSInlineStyleTxn
** aTxn
,
377 PRBool aRemoveProperty
);
379 /** back-end for GetSpecifiedProperty and GetComputedProperty
381 * @param aNode [IN] a DOM node
382 * @param aProperty [IN] a CSS property
383 * @param aValue [OUT] the retrieved value for this property
384 * @param aViewCSS [IN] the ViewCSS we need in case we query computed styles
385 * @param aStyleType [IN] SPECIFIED_STYLE_TYPE to query the specified style values
386 COMPUTED_STYLE_TYPE to query the computed style values
388 nsresult
GetCSSInlinePropertyBase(nsIDOMNode
* aNode
, nsIAtom
* aProperty
,
390 nsIDOMViewCSS
* aViewCSS
,
395 nsHTMLEditor
*mHTMLEditor
;
396 PRBool mIsCSSPrefChecked
;
400 nsresult
NS_NewHTMLCSSUtils(nsHTMLCSSUtils
** aInstancePtrResult
);
402 #define NS_EDITOR_INDENT_INCREMENT_IN 0.4134f
403 #define NS_EDITOR_INDENT_INCREMENT_CM 1.05f
404 #define NS_EDITOR_INDENT_INCREMENT_MM 10.5f
405 #define NS_EDITOR_INDENT_INCREMENT_PT 29.76f
406 #define NS_EDITOR_INDENT_INCREMENT_PC 2.48f
407 #define NS_EDITOR_INDENT_INCREMENT_EM 3
408 #define NS_EDITOR_INDENT_INCREMENT_EX 6
409 #define NS_EDITOR_INDENT_INCREMENT_PX 40
410 #define NS_EDITOR_INDENT_INCREMENT_PERCENT 4
412 #endif /* nsHTMLCSSUtils_h__ */