editor/libeditor/html/nsHTMLCSSUtils.h

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

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

mercurial