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

     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 "nsCOMPtr.h"                   // for already_AddRefed
    10 #include "nsTArray.h"                   // for nsTArray
    11 #include "nscore.h"                     // for nsAString, nsresult, nullptr
    13 class ChangeCSSInlineStyleTxn;
    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;
    29 class nsIDOMWindow;
    31 typedef void (*nsProcessValueFunc)(const nsAString * aInputString, nsAString & aOutputString,
    32                                    const char * aDefaultValueString,
    33                                    const char * aPrependString, const char* aAppendString);
    35 class nsHTMLCSSUtils
    36 {
    37 public:
    38   explicit nsHTMLCSSUtils(nsHTMLEditor* aEditor);
    39   ~nsHTMLCSSUtils();
    41   enum nsCSSEditableProperty {
    42     eCSSEditableProperty_NONE=0,
    43     eCSSEditableProperty_background_color,
    44     eCSSEditableProperty_background_image,
    45     eCSSEditableProperty_border,
    46     eCSSEditableProperty_caption_side,
    47     eCSSEditableProperty_color,
    48     eCSSEditableProperty_float,
    49     eCSSEditableProperty_font_family,
    50     eCSSEditableProperty_font_size,
    51     eCSSEditableProperty_font_style,
    52     eCSSEditableProperty_font_weight,
    53     eCSSEditableProperty_height,
    54     eCSSEditableProperty_list_style_type,
    55     eCSSEditableProperty_margin_left,
    56     eCSSEditableProperty_margin_right,
    57     eCSSEditableProperty_text_align,
    58     eCSSEditableProperty_text_decoration,
    59     eCSSEditableProperty_vertical_align,
    60     eCSSEditableProperty_whitespace,
    61     eCSSEditableProperty_width
    62   };
    64   enum StyleType { eSpecified, eComputed };
    67   struct CSSEquivTable {
    68     nsCSSEditableProperty cssProperty;
    69     nsProcessValueFunc processValueFunctor;
    70     const char * defaultValue;
    71     const char * prependValue;
    72     const char * appendValue;
    73     bool gettable;
    74     bool caseSensitiveValue;
    75   };
    77   /** answers true if the given combination element_name/attribute_name
    78     * has a CSS equivalence in this implementation
    79     *
    80     * @return               a boolean saying if the tag/attribute has a css equiv
    81     * @param aNode          [IN] a DOM node
    82     * @param aProperty      [IN] an atom containing a HTML tag name
    83     * @param aAttribute     [IN] a string containing the name of a HTML
    84     *                            attribute carried by the element above
    85     */
    86   bool IsCSSEditableProperty(nsIContent* aNode, nsIAtom* aProperty, const nsAString* aAttribute);
    87   bool IsCSSEditableProperty(nsIDOMNode* aNode, nsIAtom* aProperty, const nsAString* aAttribute);
    89   /** adds/remove a CSS declaration to the STYLE atrribute carried by a given element
    90     *
    91     * @param aElement       [IN] a DOM element
    92     * @param aProperty      [IN] an atom containing the CSS property to set
    93     * @param aValue         [IN] a string containing the value of the CSS property
    94     * @param aSuppressTransaction [IN] a boolean indicating, when true,
    95     *                                  that no transaction should be recorded
    96     */
    97   nsresult    SetCSSProperty(nsIDOMElement * aElement, nsIAtom * aProperty,
    98                              const nsAString & aValue,
    99                              bool aSuppressTransaction);
   100   nsresult    SetCSSPropertyPixels(nsIDOMElement *aElement, nsIAtom *aProperty,
   101                                    int32_t aIntValue, bool aSuppressTxn);
   102   nsresult    RemoveCSSProperty(nsIDOMElement * aElement, nsIAtom * aProperty,
   103                                 const nsAString & aPropertyValue, bool aSuppressTransaction);
   105   /** directly adds/remove a CSS declaration to the STYLE atrribute carried by
   106     * a given element without going through the txn manager
   107     *
   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
   111     */
   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 node (or its element
   120     * ancestor if it is not an element)
   121     *
   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
   125     */
   126   nsresult    GetSpecifiedProperty(nsIDOMNode *aNode, nsIAtom *aProperty,
   127                                    nsAString & aValue);
   128   nsresult    GetComputedProperty(nsIDOMNode *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
   133    *
   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
   138    */
   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
   143      *
   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
   147      */
   148   bool        IsCSSInvertable(nsIAtom * aProperty, const nsAString * aAttribute);
   150   /** Get the default browser background color if we need it for GetCSSBackgroundColorState
   151     *
   152     * @param aColor         [OUT] the default color as it is defined in prefs
   153     */
   154   void        GetDefaultBackgroundColor(nsAString & aColor);
   156   /** Get the default length unit used for CSS Indent/Outdent
   157     *
   158     * @param aLengthUnit    [OUT] the default length unit as it is defined in prefs
   159     */
   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
   164     *
   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
   170     */
   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 ?
   179     *
   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
   186     *
   187     * The nsIContent variant returns aIsSet instead of using an out parameter.
   188     */
   189   bool IsCSSEquivalentToHTMLInlineStyleSet(nsIContent* aContent,
   190                                            nsIAtom* aProperty,
   191                                            const nsAString* aAttribute,
   192                                            const nsAString& aValue,
   193                                            StyleType aStyleType);
   195   nsresult    IsCSSEquivalentToHTMLInlineStyleSet(nsIDOMNode * aNode,
   196                                                   nsIAtom * aHTMLProperty,
   197                                                   const nsAString * aAttribute,
   198                                                   bool & aIsSet,
   199                                                   nsAString & aValueString,
   200                                                   StyleType aStyleType);
   202   /** Adds to the node the CSS inline styles equivalent to the HTML style
   203     * and return the number of CSS properties set by the call
   204     *
   205     * @param aNode          [IN] a DOM node
   206     * @param aHTMLProperty  [IN] an atom containing an HTML property
   207     * @param aAttribute     [IN] a pointer to an attribute name or nullptr if irrelevant
   208     * @param aValue         [IN] the attribute value
   209     * @param aCount         [OUT] the number of CSS properties set by the call
   210     * @param aSuppressTransaction [IN] a boolean indicating, when true,
   211     *                                  that no transaction should be recorded
   212     *
   213     * aCount is returned by the dom::Element variant instead of being an out
   214     * parameter.
   215     */
   216   int32_t     SetCSSEquivalentToHTMLStyle(mozilla::dom::Element* aElement,
   217                                           nsIAtom* aProperty,
   218                                           const nsAString* aAttribute,
   219                                           const nsAString* aValue,
   220                                           bool aSuppressTransaction);
   221   nsresult    SetCSSEquivalentToHTMLStyle(nsIDOMNode * aNode,
   222                                           nsIAtom * aHTMLProperty,
   223                                           const nsAString * aAttribute,
   224                                           const nsAString * aValue,
   225                                           int32_t * aCount,
   226                                           bool aSuppressTransaction);
   228   /** removes from the node the CSS inline styles equivalent to the HTML style
   229     *
   230     * @param aNode          [IN] a DOM node
   231     * @param aHTMLProperty  [IN] an atom containing an HTML property
   232     * @param aAttribute     [IN] a pointer to an attribute name or nullptr if irrelevant
   233     * @param aValue         [IN] the attribute value
   234     * @param aSuppressTransaction [IN] a boolean indicating, when true,
   235     *                                  that no transaction should be recorded
   236     */
   237   nsresult    RemoveCSSEquivalentToHTMLStyle(nsIDOMNode * aNode,
   238                                              nsIAtom *aHTMLProperty,
   239                                              const nsAString *aAttribute,
   240                                              const nsAString *aValue,
   241                                              bool aSuppressTransaction);
   242   /** removes from the node the CSS inline styles equivalent to the HTML style
   243     *
   244     * @param aElement       [IN] a DOM Element (must not be null)
   245     * @param aHTMLProperty  [IN] an atom containing an HTML property
   246     * @param aAttribute     [IN] a pointer to an attribute name or nullptr if irrelevant
   247     * @param aValue         [IN] the attribute value
   248     * @param aSuppressTransaction [IN] a boolean indicating, when true,
   249     *                                  that no transaction should be recorded
   250     */
   251   nsresult    RemoveCSSEquivalentToHTMLStyle(mozilla::dom::Element* aElement,
   252                                              nsIAtom* aHTMLProperty,
   253                                              const nsAString* aAttribute,
   254                                              const nsAString* aValue,
   255                                              bool aSuppressTransaction);
   257   /** parses a "xxxx.xxxxxuuu" string where x is a digit and u an alpha char
   258     * we need such a parser because nsIDOMCSSStyleDeclaration::GetPropertyCSSValue() is not
   259     * implemented
   260     *
   261     * @param aString        [IN] input string to parse
   262     * @param aValue         [OUT] numeric part
   263     * @param aUnit          [OUT] unit part
   264     */
   265   void        ParseLength(const nsAString & aString, float * aValue, nsIAtom ** aUnit);
   267   /** sets the mIsCSSPrefChecked private member ; used as callback from observer when
   268     * the css pref state is changed
   269     *
   270     * @param aIsCSSPrefChecked [IN] the new boolean state for the pref
   271     */
   272   void        SetCSSEnabled(bool aIsCSSPrefChecked);
   274   /** retrieves the mIsCSSPrefChecked private member, true if the css pref is checked,
   275     * false if it is not
   276     *
   277     * @return                 the boolean value of the css pref
   278     */
   279   bool        IsCSSPrefChecked();
   281   /** ElementsSameStyle compares two elements and checks if they have the same
   282     * specified CSS declarations in the STYLE attribute 
   283     * The answer is always false if at least one of them carries an ID or a class
   284     *
   285     * @return                     true if the two elements are considered to have same styles
   286     * @param aFirstNode           [IN] a DOM node
   287     * @param aSecondNode          [IN] a DOM node
   288     */
   289   bool ElementsSameStyle(mozilla::dom::Element* aFirstNode,
   290                          mozilla::dom::Element* aSecondNode);
   291   bool ElementsSameStyle(nsIDOMNode *aFirstNode, nsIDOMNode *aSecondNode);
   293   /** get the specified inline styles (style attribute) for an element
   294     *
   295     * @param aElement        [IN] the element node
   296     * @param aCssDecl        [OUT] the CSS declaration corresponding to the style attr
   297     * @param aLength         [OUT] the number of declarations in aCssDecl
   298     */
   299   nsresult GetInlineStyles(mozilla::dom::Element* aElement,
   300                            nsIDOMCSSStyleDeclaration** aCssDecl,
   301                            uint32_t* aLength);
   302   nsresult GetInlineStyles(nsIDOMElement* aElement,
   303                            nsIDOMCSSStyleDeclaration** aCssDecl,
   304                            uint32_t* aLength);
   305 private:
   306   nsresult GetInlineStyles(nsISupports* aElement,
   307                            nsIDOMCSSStyleDeclaration** aCssDecl,
   308                            uint32_t* aLength);
   310 public:
   311   /** returns aNode itself if it is an element node, or the first ancestors being an element
   312     * node if aNode is not one itself
   313     *
   314     * @param aNode           [IN] a node
   315     * @param aElement        [OUT] the deepest element node containing aNode (possibly aNode itself)
   316     */
   317   mozilla::dom::Element* GetElementContainerOrSelf(nsINode* aNode);
   318   already_AddRefed<nsIDOMElement> GetElementContainerOrSelf(nsIDOMNode* aNode);
   320   /**
   321    * Gets the computed style for a given element.  Can return null.
   322    */
   323   already_AddRefed<nsComputedDOMStyle>
   324     GetComputedStyle(nsIDOMElement* aElement);
   325   already_AddRefed<nsComputedDOMStyle>
   326     GetComputedStyle(mozilla::dom::Element* aElement);
   329 private:
   331   /** retrieves the css property atom from an enum
   332     *
   333     * @param aProperty          [IN] the enum value for the property
   334     * @param aAtom              [OUT] the corresponding atom
   335     */
   336   void  GetCSSPropertyAtom(nsCSSEditableProperty aProperty, nsIAtom ** aAtom);
   338   /** retrieves the CSS declarations equivalent to a HTML style value for
   339     * a given equivalence table
   340     *
   341     * @param aPropertyArray     [OUT] the array of css properties
   342     * @param aValueArray        [OUT] the array of values for the css properties above
   343     * @param aEquivTable        [IN] the equivalence table
   344     * @param aValue             [IN] the HTML style value
   345     * @param aGetOrRemoveRequest [IN] a boolean value being true if the call to the current method
   346     *                                 is made for GetCSSEquivalentToHTMLInlineStyleSet or
   347     *                                 RemoveCSSEquivalentToHTMLInlineStyleSet
   348     */
   350   void      BuildCSSDeclarations(nsTArray<nsIAtom*> & aPropertyArray,
   351                                  nsTArray<nsString> & cssValueArray,
   352                                  const CSSEquivTable * aEquivTable,
   353                                  const nsAString * aValue,
   354                                  bool aGetOrRemoveRequest);
   356   /** retrieves the CSS declarations equivalent to the given HTML property/attribute/value
   357     * for a given node
   358     *
   359     * @param aNode              [IN] the DOM node
   360     * @param aHTMLProperty      [IN] an atom containing an HTML property
   361     * @param aAttribute         [IN] a pointer to an attribute name or nullptr if irrelevant
   362     * @param aValue             [IN] the attribute value
   363     * @param aPropertyArray     [OUT] the array of css properties
   364     * @param aValueArray        [OUT] the array of values for the css properties above
   365     * @param aGetOrRemoveRequest [IN] a boolean value being true if the call to the current method
   366     *                                 is made for GetCSSEquivalentToHTMLInlineStyleSet or
   367     *                                 RemoveCSSEquivalentToHTMLInlineStyleSet
   368     */
   369   void      GenerateCSSDeclarationsFromHTMLStyle(mozilla::dom::Element* aNode,
   370                                                  nsIAtom* aHTMLProperty,
   371                                                  const nsAString* aAttribute,
   372                                                  const nsAString* aValue,
   373                                                  nsTArray<nsIAtom*>& aPropertyArray,
   374                                                  nsTArray<nsString>& aValueArray,
   375                                                  bool aGetOrRemoveRequest);
   377   /** creates a Transaction for setting or removing a css property
   378     *
   379     * @param aElement           [IN] a DOM element
   380     * @param aProperty          [IN] a CSS property
   381     * @param aValue             [IN] the value to remove for this CSS property or the empty string if irrelevant
   382     * @param aTxn               [OUT] the created transaction
   383     * @param aRemoveProperty    [IN] true if we create a "remove" transaction, false for a "set"
   384     */
   385   nsresult    CreateCSSPropertyTxn(nsIDOMElement * aElement, 
   386                                    nsIAtom * aProperty,
   387                                    const nsAString & aValue,
   388                                    ChangeCSSInlineStyleTxn ** aTxn,
   389                                    bool aRemoveProperty);
   391   /** back-end for GetSpecifiedProperty and GetComputedProperty
   392    *
   393    * @param aNode               [IN] a DOM node
   394    * @param aProperty           [IN] a CSS property
   395    * @param aValue              [OUT] the retrieved value for this property
   396    * @param aStyleType          [IN] eSpecified or eComputed
   397    */
   398   nsresult GetCSSInlinePropertyBase(nsINode* aNode, nsIAtom* aProperty,
   399                                     nsAString& aValue, StyleType aStyleType);
   400   nsresult GetCSSInlinePropertyBase(nsIDOMNode* aNode, nsIAtom* aProperty,
   401                                     nsAString& aValue, StyleType aStyleType);
   404 private:
   405   nsHTMLEditor            *mHTMLEditor;
   406   bool                    mIsCSSPrefChecked; 
   407 };
   409 #define NS_EDITOR_INDENT_INCREMENT_IN        0.4134f
   410 #define NS_EDITOR_INDENT_INCREMENT_CM        1.05f
   411 #define NS_EDITOR_INDENT_INCREMENT_MM        10.5f
   412 #define NS_EDITOR_INDENT_INCREMENT_PT        29.76f
   413 #define NS_EDITOR_INDENT_INCREMENT_PC        2.48f
   414 #define NS_EDITOR_INDENT_INCREMENT_EM        3
   415 #define NS_EDITOR_INDENT_INCREMENT_EX        6
   416 #define NS_EDITOR_INDENT_INCREMENT_PX        40
   417 #define NS_EDITOR_INDENT_INCREMENT_PERCENT   4 
   419 #endif /* nsHTMLCSSUtils_h__ */

mercurial