1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/editor/libeditor/html/nsHTMLCSSUtils.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,419 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsHTMLCSSUtils_h__ 1.10 +#define nsHTMLCSSUtils_h__ 1.11 + 1.12 +#include "nsCOMPtr.h" // for already_AddRefed 1.13 +#include "nsTArray.h" // for nsTArray 1.14 +#include "nscore.h" // for nsAString, nsresult, nullptr 1.15 + 1.16 +class ChangeCSSInlineStyleTxn; 1.17 +class nsComputedDOMStyle; 1.18 +class nsIAtom; 1.19 +class nsIContent; 1.20 +class nsIDOMCSSStyleDeclaration; 1.21 +class nsIDOMElement; 1.22 +class nsIDOMNode; 1.23 +class nsINode; 1.24 +class nsString; 1.25 +namespace mozilla { 1.26 +namespace dom { 1.27 +class Element; 1.28 +} // namespace dom 1.29 +} // namespace mozilla 1.30 + 1.31 +class nsHTMLEditor; 1.32 +class nsIDOMWindow; 1.33 + 1.34 +typedef void (*nsProcessValueFunc)(const nsAString * aInputString, nsAString & aOutputString, 1.35 + const char * aDefaultValueString, 1.36 + const char * aPrependString, const char* aAppendString); 1.37 + 1.38 +class nsHTMLCSSUtils 1.39 +{ 1.40 +public: 1.41 + explicit nsHTMLCSSUtils(nsHTMLEditor* aEditor); 1.42 + ~nsHTMLCSSUtils(); 1.43 + 1.44 + enum nsCSSEditableProperty { 1.45 + eCSSEditableProperty_NONE=0, 1.46 + eCSSEditableProperty_background_color, 1.47 + eCSSEditableProperty_background_image, 1.48 + eCSSEditableProperty_border, 1.49 + eCSSEditableProperty_caption_side, 1.50 + eCSSEditableProperty_color, 1.51 + eCSSEditableProperty_float, 1.52 + eCSSEditableProperty_font_family, 1.53 + eCSSEditableProperty_font_size, 1.54 + eCSSEditableProperty_font_style, 1.55 + eCSSEditableProperty_font_weight, 1.56 + eCSSEditableProperty_height, 1.57 + eCSSEditableProperty_list_style_type, 1.58 + eCSSEditableProperty_margin_left, 1.59 + eCSSEditableProperty_margin_right, 1.60 + eCSSEditableProperty_text_align, 1.61 + eCSSEditableProperty_text_decoration, 1.62 + eCSSEditableProperty_vertical_align, 1.63 + eCSSEditableProperty_whitespace, 1.64 + eCSSEditableProperty_width 1.65 + }; 1.66 + 1.67 + enum StyleType { eSpecified, eComputed }; 1.68 + 1.69 + 1.70 + struct CSSEquivTable { 1.71 + nsCSSEditableProperty cssProperty; 1.72 + nsProcessValueFunc processValueFunctor; 1.73 + const char * defaultValue; 1.74 + const char * prependValue; 1.75 + const char * appendValue; 1.76 + bool gettable; 1.77 + bool caseSensitiveValue; 1.78 + }; 1.79 + 1.80 + /** answers true if the given combination element_name/attribute_name 1.81 + * has a CSS equivalence in this implementation 1.82 + * 1.83 + * @return a boolean saying if the tag/attribute has a css equiv 1.84 + * @param aNode [IN] a DOM node 1.85 + * @param aProperty [IN] an atom containing a HTML tag name 1.86 + * @param aAttribute [IN] a string containing the name of a HTML 1.87 + * attribute carried by the element above 1.88 + */ 1.89 + bool IsCSSEditableProperty(nsIContent* aNode, nsIAtom* aProperty, const nsAString* aAttribute); 1.90 + bool IsCSSEditableProperty(nsIDOMNode* aNode, nsIAtom* aProperty, const nsAString* aAttribute); 1.91 + 1.92 + /** adds/remove a CSS declaration to the STYLE atrribute carried by a given element 1.93 + * 1.94 + * @param aElement [IN] a DOM element 1.95 + * @param aProperty [IN] an atom containing the CSS property to set 1.96 + * @param aValue [IN] a string containing the value of the CSS property 1.97 + * @param aSuppressTransaction [IN] a boolean indicating, when true, 1.98 + * that no transaction should be recorded 1.99 + */ 1.100 + nsresult SetCSSProperty(nsIDOMElement * aElement, nsIAtom * aProperty, 1.101 + const nsAString & aValue, 1.102 + bool aSuppressTransaction); 1.103 + nsresult SetCSSPropertyPixels(nsIDOMElement *aElement, nsIAtom *aProperty, 1.104 + int32_t aIntValue, bool aSuppressTxn); 1.105 + nsresult RemoveCSSProperty(nsIDOMElement * aElement, nsIAtom * aProperty, 1.106 + const nsAString & aPropertyValue, bool aSuppressTransaction); 1.107 + 1.108 + /** directly adds/remove a CSS declaration to the STYLE atrribute carried by 1.109 + * a given element without going through the txn manager 1.110 + * 1.111 + * @param aElement [IN] a DOM element 1.112 + * @param aProperty [IN] a string containing the CSS property to set/remove 1.113 + * @param aValue [IN] a string containing the new value of the CSS property 1.114 + */ 1.115 + nsresult SetCSSProperty(nsIDOMElement * aElement, 1.116 + const nsAString & aProperty, 1.117 + const nsAString & aValue); 1.118 + nsresult SetCSSPropertyPixels(nsIDOMElement * aElement, 1.119 + const nsAString & aProperty, 1.120 + int32_t aIntValue); 1.121 + 1.122 + /** gets the specified/computed style value of a CSS property for a given node (or its element 1.123 + * ancestor if it is not an element) 1.124 + * 1.125 + * @param aNode [IN] a DOM node 1.126 + * @param aProperty [IN] an atom containing the CSS property to get 1.127 + * @param aPropertyValue [OUT] the retrieved value of the property 1.128 + */ 1.129 + nsresult GetSpecifiedProperty(nsIDOMNode *aNode, nsIAtom *aProperty, 1.130 + nsAString & aValue); 1.131 + nsresult GetComputedProperty(nsIDOMNode *aNode, nsIAtom *aProperty, 1.132 + nsAString & aValue); 1.133 + 1.134 + /** Removes a CSS property from the specified declarations in STYLE attribute 1.135 + ** and removes the node if it is an useless span 1.136 + * 1.137 + * @param aNode [IN] the specific node we want to remove a style from 1.138 + * @param aProperty [IN] the CSS property atom to remove 1.139 + * @param aPropertyValue [IN] the value of the property we have to rremove if the property 1.140 + * accepts more than one value 1.141 + */ 1.142 + nsresult RemoveCSSInlineStyle(nsIDOMNode * aNode, nsIAtom * aProperty, const nsAString & aPropertyValue); 1.143 + 1.144 + /** Answers true is the property can be removed by setting a "none" CSS value 1.145 + * on a node 1.146 + * 1.147 + * @return a boolean saying if the property can be remove by setting a "none" value 1.148 + * @param aProperty [IN] an atom containing a CSS property 1.149 + * @param aAttribute [IN] pointer to an attribute name or null if this information is irrelevant 1.150 + */ 1.151 + bool IsCSSInvertable(nsIAtom * aProperty, const nsAString * aAttribute); 1.152 + 1.153 + /** Get the default browser background color if we need it for GetCSSBackgroundColorState 1.154 + * 1.155 + * @param aColor [OUT] the default color as it is defined in prefs 1.156 + */ 1.157 + void GetDefaultBackgroundColor(nsAString & aColor); 1.158 + 1.159 + /** Get the default length unit used for CSS Indent/Outdent 1.160 + * 1.161 + * @param aLengthUnit [OUT] the default length unit as it is defined in prefs 1.162 + */ 1.163 + void GetDefaultLengthUnit(nsAString & aLengthUnit); 1.164 + 1.165 + /** returns the list of values for the CSS equivalences to 1.166 + * the passed HTML style for the passed node 1.167 + * 1.168 + * @param aNode [IN] a DOM node 1.169 + * @param aHTMLProperty [IN] an atom containing an HTML property 1.170 + * @param aAttribute [IN] a pointer to an attribute name or nullptr if irrelevant 1.171 + * @param aValueString [OUT] the list of css values 1.172 + * @param aStyleType [IN] eSpecified or eComputed 1.173 + */ 1.174 + nsresult GetCSSEquivalentToHTMLInlineStyleSet(nsINode* aNode, 1.175 + nsIAtom * aHTMLProperty, 1.176 + const nsAString * aAttribute, 1.177 + nsAString & aValueString, 1.178 + StyleType aStyleType); 1.179 + 1.180 + /** Does the node aNode (or his parent if it is not an element node) carries 1.181 + * the CSS equivalent styles to the HTML style for this node ? 1.182 + * 1.183 + * @param aNode [IN] a DOM node 1.184 + * @param aHTMLProperty [IN] an atom containing an HTML property 1.185 + * @param aAttribute [IN] a pointer to an attribute name or nullptr if irrelevant 1.186 + * @param aIsSet [OUT] a boolean being true if the css properties are set 1.187 + * @param aValueString [IN/OUT] the attribute value (in) the list of css values (out) 1.188 + * @param aStyleType [IN] eSpecified or eComputed 1.189 + * 1.190 + * The nsIContent variant returns aIsSet instead of using an out parameter. 1.191 + */ 1.192 + bool IsCSSEquivalentToHTMLInlineStyleSet(nsIContent* aContent, 1.193 + nsIAtom* aProperty, 1.194 + const nsAString* aAttribute, 1.195 + const nsAString& aValue, 1.196 + StyleType aStyleType); 1.197 + 1.198 + nsresult IsCSSEquivalentToHTMLInlineStyleSet(nsIDOMNode * aNode, 1.199 + nsIAtom * aHTMLProperty, 1.200 + const nsAString * aAttribute, 1.201 + bool & aIsSet, 1.202 + nsAString & aValueString, 1.203 + StyleType aStyleType); 1.204 + 1.205 + /** Adds to the node the CSS inline styles equivalent to the HTML style 1.206 + * and return the number of CSS properties set by the call 1.207 + * 1.208 + * @param aNode [IN] a DOM node 1.209 + * @param aHTMLProperty [IN] an atom containing an HTML property 1.210 + * @param aAttribute [IN] a pointer to an attribute name or nullptr if irrelevant 1.211 + * @param aValue [IN] the attribute value 1.212 + * @param aCount [OUT] the number of CSS properties set by the call 1.213 + * @param aSuppressTransaction [IN] a boolean indicating, when true, 1.214 + * that no transaction should be recorded 1.215 + * 1.216 + * aCount is returned by the dom::Element variant instead of being an out 1.217 + * parameter. 1.218 + */ 1.219 + int32_t SetCSSEquivalentToHTMLStyle(mozilla::dom::Element* aElement, 1.220 + nsIAtom* aProperty, 1.221 + const nsAString* aAttribute, 1.222 + const nsAString* aValue, 1.223 + bool aSuppressTransaction); 1.224 + nsresult SetCSSEquivalentToHTMLStyle(nsIDOMNode * aNode, 1.225 + nsIAtom * aHTMLProperty, 1.226 + const nsAString * aAttribute, 1.227 + const nsAString * aValue, 1.228 + int32_t * aCount, 1.229 + bool aSuppressTransaction); 1.230 + 1.231 + /** removes from the node the CSS inline styles equivalent to the HTML style 1.232 + * 1.233 + * @param aNode [IN] a DOM node 1.234 + * @param aHTMLProperty [IN] an atom containing an HTML property 1.235 + * @param aAttribute [IN] a pointer to an attribute name or nullptr if irrelevant 1.236 + * @param aValue [IN] the attribute value 1.237 + * @param aSuppressTransaction [IN] a boolean indicating, when true, 1.238 + * that no transaction should be recorded 1.239 + */ 1.240 + nsresult RemoveCSSEquivalentToHTMLStyle(nsIDOMNode * aNode, 1.241 + nsIAtom *aHTMLProperty, 1.242 + const nsAString *aAttribute, 1.243 + const nsAString *aValue, 1.244 + bool aSuppressTransaction); 1.245 + /** removes from the node the CSS inline styles equivalent to the HTML style 1.246 + * 1.247 + * @param aElement [IN] a DOM Element (must not be null) 1.248 + * @param aHTMLProperty [IN] an atom containing an HTML property 1.249 + * @param aAttribute [IN] a pointer to an attribute name or nullptr if irrelevant 1.250 + * @param aValue [IN] the attribute value 1.251 + * @param aSuppressTransaction [IN] a boolean indicating, when true, 1.252 + * that no transaction should be recorded 1.253 + */ 1.254 + nsresult RemoveCSSEquivalentToHTMLStyle(mozilla::dom::Element* aElement, 1.255 + nsIAtom* aHTMLProperty, 1.256 + const nsAString* aAttribute, 1.257 + const nsAString* aValue, 1.258 + bool aSuppressTransaction); 1.259 + 1.260 + /** parses a "xxxx.xxxxxuuu" string where x is a digit and u an alpha char 1.261 + * we need such a parser because nsIDOMCSSStyleDeclaration::GetPropertyCSSValue() is not 1.262 + * implemented 1.263 + * 1.264 + * @param aString [IN] input string to parse 1.265 + * @param aValue [OUT] numeric part 1.266 + * @param aUnit [OUT] unit part 1.267 + */ 1.268 + void ParseLength(const nsAString & aString, float * aValue, nsIAtom ** aUnit); 1.269 + 1.270 + /** sets the mIsCSSPrefChecked private member ; used as callback from observer when 1.271 + * the css pref state is changed 1.272 + * 1.273 + * @param aIsCSSPrefChecked [IN] the new boolean state for the pref 1.274 + */ 1.275 + void SetCSSEnabled(bool aIsCSSPrefChecked); 1.276 + 1.277 + /** retrieves the mIsCSSPrefChecked private member, true if the css pref is checked, 1.278 + * false if it is not 1.279 + * 1.280 + * @return the boolean value of the css pref 1.281 + */ 1.282 + bool IsCSSPrefChecked(); 1.283 + 1.284 + /** ElementsSameStyle compares two elements and checks if they have the same 1.285 + * specified CSS declarations in the STYLE attribute 1.286 + * The answer is always false if at least one of them carries an ID or a class 1.287 + * 1.288 + * @return true if the two elements are considered to have same styles 1.289 + * @param aFirstNode [IN] a DOM node 1.290 + * @param aSecondNode [IN] a DOM node 1.291 + */ 1.292 + bool ElementsSameStyle(mozilla::dom::Element* aFirstNode, 1.293 + mozilla::dom::Element* aSecondNode); 1.294 + bool ElementsSameStyle(nsIDOMNode *aFirstNode, nsIDOMNode *aSecondNode); 1.295 + 1.296 + /** get the specified inline styles (style attribute) for an element 1.297 + * 1.298 + * @param aElement [IN] the element node 1.299 + * @param aCssDecl [OUT] the CSS declaration corresponding to the style attr 1.300 + * @param aLength [OUT] the number of declarations in aCssDecl 1.301 + */ 1.302 + nsresult GetInlineStyles(mozilla::dom::Element* aElement, 1.303 + nsIDOMCSSStyleDeclaration** aCssDecl, 1.304 + uint32_t* aLength); 1.305 + nsresult GetInlineStyles(nsIDOMElement* aElement, 1.306 + nsIDOMCSSStyleDeclaration** aCssDecl, 1.307 + uint32_t* aLength); 1.308 +private: 1.309 + nsresult GetInlineStyles(nsISupports* aElement, 1.310 + nsIDOMCSSStyleDeclaration** aCssDecl, 1.311 + uint32_t* aLength); 1.312 + 1.313 +public: 1.314 + /** returns aNode itself if it is an element node, or the first ancestors being an element 1.315 + * node if aNode is not one itself 1.316 + * 1.317 + * @param aNode [IN] a node 1.318 + * @param aElement [OUT] the deepest element node containing aNode (possibly aNode itself) 1.319 + */ 1.320 + mozilla::dom::Element* GetElementContainerOrSelf(nsINode* aNode); 1.321 + already_AddRefed<nsIDOMElement> GetElementContainerOrSelf(nsIDOMNode* aNode); 1.322 + 1.323 + /** 1.324 + * Gets the computed style for a given element. Can return null. 1.325 + */ 1.326 + already_AddRefed<nsComputedDOMStyle> 1.327 + GetComputedStyle(nsIDOMElement* aElement); 1.328 + already_AddRefed<nsComputedDOMStyle> 1.329 + GetComputedStyle(mozilla::dom::Element* aElement); 1.330 + 1.331 + 1.332 +private: 1.333 + 1.334 + /** retrieves the css property atom from an enum 1.335 + * 1.336 + * @param aProperty [IN] the enum value for the property 1.337 + * @param aAtom [OUT] the corresponding atom 1.338 + */ 1.339 + void GetCSSPropertyAtom(nsCSSEditableProperty aProperty, nsIAtom ** aAtom); 1.340 + 1.341 + /** retrieves the CSS declarations equivalent to a HTML style value for 1.342 + * a given equivalence table 1.343 + * 1.344 + * @param aPropertyArray [OUT] the array of css properties 1.345 + * @param aValueArray [OUT] the array of values for the css properties above 1.346 + * @param aEquivTable [IN] the equivalence table 1.347 + * @param aValue [IN] the HTML style value 1.348 + * @param aGetOrRemoveRequest [IN] a boolean value being true if the call to the current method 1.349 + * is made for GetCSSEquivalentToHTMLInlineStyleSet or 1.350 + * RemoveCSSEquivalentToHTMLInlineStyleSet 1.351 + */ 1.352 + 1.353 + void BuildCSSDeclarations(nsTArray<nsIAtom*> & aPropertyArray, 1.354 + nsTArray<nsString> & cssValueArray, 1.355 + const CSSEquivTable * aEquivTable, 1.356 + const nsAString * aValue, 1.357 + bool aGetOrRemoveRequest); 1.358 + 1.359 + /** retrieves the CSS declarations equivalent to the given HTML property/attribute/value 1.360 + * for a given node 1.361 + * 1.362 + * @param aNode [IN] the DOM node 1.363 + * @param aHTMLProperty [IN] an atom containing an HTML property 1.364 + * @param aAttribute [IN] a pointer to an attribute name or nullptr if irrelevant 1.365 + * @param aValue [IN] the attribute value 1.366 + * @param aPropertyArray [OUT] the array of css properties 1.367 + * @param aValueArray [OUT] the array of values for the css properties above 1.368 + * @param aGetOrRemoveRequest [IN] a boolean value being true if the call to the current method 1.369 + * is made for GetCSSEquivalentToHTMLInlineStyleSet or 1.370 + * RemoveCSSEquivalentToHTMLInlineStyleSet 1.371 + */ 1.372 + void GenerateCSSDeclarationsFromHTMLStyle(mozilla::dom::Element* aNode, 1.373 + nsIAtom* aHTMLProperty, 1.374 + const nsAString* aAttribute, 1.375 + const nsAString* aValue, 1.376 + nsTArray<nsIAtom*>& aPropertyArray, 1.377 + nsTArray<nsString>& aValueArray, 1.378 + bool aGetOrRemoveRequest); 1.379 + 1.380 + /** creates a Transaction for setting or removing a css property 1.381 + * 1.382 + * @param aElement [IN] a DOM element 1.383 + * @param aProperty [IN] a CSS property 1.384 + * @param aValue [IN] the value to remove for this CSS property or the empty string if irrelevant 1.385 + * @param aTxn [OUT] the created transaction 1.386 + * @param aRemoveProperty [IN] true if we create a "remove" transaction, false for a "set" 1.387 + */ 1.388 + nsresult CreateCSSPropertyTxn(nsIDOMElement * aElement, 1.389 + nsIAtom * aProperty, 1.390 + const nsAString & aValue, 1.391 + ChangeCSSInlineStyleTxn ** aTxn, 1.392 + bool aRemoveProperty); 1.393 + 1.394 + /** back-end for GetSpecifiedProperty and GetComputedProperty 1.395 + * 1.396 + * @param aNode [IN] a DOM node 1.397 + * @param aProperty [IN] a CSS property 1.398 + * @param aValue [OUT] the retrieved value for this property 1.399 + * @param aStyleType [IN] eSpecified or eComputed 1.400 + */ 1.401 + nsresult GetCSSInlinePropertyBase(nsINode* aNode, nsIAtom* aProperty, 1.402 + nsAString& aValue, StyleType aStyleType); 1.403 + nsresult GetCSSInlinePropertyBase(nsIDOMNode* aNode, nsIAtom* aProperty, 1.404 + nsAString& aValue, StyleType aStyleType); 1.405 + 1.406 + 1.407 +private: 1.408 + nsHTMLEditor *mHTMLEditor; 1.409 + bool mIsCSSPrefChecked; 1.410 +}; 1.411 + 1.412 +#define NS_EDITOR_INDENT_INCREMENT_IN 0.4134f 1.413 +#define NS_EDITOR_INDENT_INCREMENT_CM 1.05f 1.414 +#define NS_EDITOR_INDENT_INCREMENT_MM 10.5f 1.415 +#define NS_EDITOR_INDENT_INCREMENT_PT 29.76f 1.416 +#define NS_EDITOR_INDENT_INCREMENT_PC 2.48f 1.417 +#define NS_EDITOR_INDENT_INCREMENT_EM 3 1.418 +#define NS_EDITOR_INDENT_INCREMENT_EX 6 1.419 +#define NS_EDITOR_INDENT_INCREMENT_PX 40 1.420 +#define NS_EDITOR_INDENT_INCREMENT_PERCENT 4 1.421 + 1.422 +#endif /* nsHTMLCSSUtils_h__ */