michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * A struct that represents the value (type and actual data) of an michael@0: * attribute. michael@0: */ michael@0: michael@0: #ifndef nsAttrValue_h___ michael@0: #define nsAttrValue_h___ michael@0: michael@0: #include "nscore.h" michael@0: #include "nsStringGlue.h" michael@0: #include "nsStringBuffer.h" michael@0: #include "nsColor.h" michael@0: #include "nsCaseTreatment.h" michael@0: #include "nsMargin.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "SVGAttrValueWrapper.h" michael@0: #include "nsTArrayForwardDeclare.h" michael@0: #include "nsIAtom.h" michael@0: #include "mozilla/MemoryReporting.h" michael@0: #include "mozilla/dom/BindingDeclarations.h" michael@0: michael@0: class nsAString; michael@0: class nsIDocument; michael@0: class nsStyledElementNotElementCSSInlineStyle; michael@0: struct MiscContainer; michael@0: michael@0: namespace mozilla { michael@0: namespace css { michael@0: class StyleRule; michael@0: struct URLValue; michael@0: struct ImageValue; michael@0: } michael@0: } michael@0: michael@0: #define NS_ATTRVALUE_MAX_STRINGLENGTH_ATOM 12 michael@0: michael@0: #define NS_ATTRVALUE_BASETYPE_MASK (uintptr_t(3)) michael@0: #define NS_ATTRVALUE_POINTERVALUE_MASK (~NS_ATTRVALUE_BASETYPE_MASK) michael@0: michael@0: #define NS_ATTRVALUE_INTEGERTYPE_BITS 4 michael@0: #define NS_ATTRVALUE_INTEGERTYPE_MASK (uintptr_t((1 << NS_ATTRVALUE_INTEGERTYPE_BITS) - 1)) michael@0: #define NS_ATTRVALUE_INTEGERTYPE_MULTIPLIER (1 << NS_ATTRVALUE_INTEGERTYPE_BITS) michael@0: #define NS_ATTRVALUE_INTEGERTYPE_MAXVALUE ((1 << (31 - NS_ATTRVALUE_INTEGERTYPE_BITS)) - 1) michael@0: #define NS_ATTRVALUE_INTEGERTYPE_MINVALUE (-NS_ATTRVALUE_INTEGERTYPE_MAXVALUE - 1) michael@0: michael@0: #define NS_ATTRVALUE_ENUMTABLEINDEX_BITS (32 - 16 - NS_ATTRVALUE_INTEGERTYPE_BITS) michael@0: #define NS_ATTRVALUE_ENUMTABLE_VALUE_NEEDS_TO_UPPER (1 << (NS_ATTRVALUE_ENUMTABLEINDEX_BITS - 1)) michael@0: #define NS_ATTRVALUE_ENUMTABLEINDEX_MAXVALUE (NS_ATTRVALUE_ENUMTABLE_VALUE_NEEDS_TO_UPPER - 1) michael@0: #define NS_ATTRVALUE_ENUMTABLEINDEX_MASK \ michael@0: (uintptr_t((((1 << NS_ATTRVALUE_ENUMTABLEINDEX_BITS) - 1) &~ NS_ATTRVALUE_ENUMTABLE_VALUE_NEEDS_TO_UPPER))) michael@0: michael@0: /** michael@0: * A class used to construct a nsString from a nsStringBuffer (we might michael@0: * want to move this to nsString at some point). michael@0: * michael@0: * WARNING: Note that nsCheapString doesn't take an explicit length -- it michael@0: * assumes the string is maximally large, given the nsStringBuffer's storage michael@0: * size. This means the given string buffer *must* be sized exactly correctly michael@0: * for the string it contains (including one byte for a null terminator). If michael@0: * it has any unused storage space, then that will result in bogus characters michael@0: * at the end of our nsCheapString. michael@0: */ michael@0: class nsCheapString : public nsString { michael@0: public: michael@0: nsCheapString(nsStringBuffer* aBuf) michael@0: { michael@0: if (aBuf) michael@0: aBuf->ToString(aBuf->StorageSize()/sizeof(char16_t) - 1, *this); michael@0: } michael@0: }; michael@0: michael@0: class nsAttrValue { michael@0: friend struct MiscContainer; michael@0: public: michael@0: typedef nsTArray< nsCOMPtr > AtomArray; michael@0: michael@0: // This has to be the same as in ValueBaseType michael@0: enum ValueType { michael@0: eString = 0x00, // 00 michael@0: // 01 this value indicates an 'misc' struct michael@0: eAtom = 0x02, // 10 michael@0: eInteger = 0x03, // 0011 michael@0: eColor = 0x07, // 0111 michael@0: eEnum = 0x0B, // 1011 This should eventually die michael@0: ePercent = 0x0F, // 1111 michael@0: // Values below here won't matter, they'll be always stored in the 'misc' michael@0: // struct. michael@0: eCSSStyleRule = 0x10 michael@0: ,eURL = 0x11 michael@0: ,eImage = 0x12 michael@0: ,eAtomArray = 0x13 michael@0: ,eDoubleValue = 0x14 michael@0: ,eIntMarginValue = 0x15 michael@0: ,eSVGAngle = 0x16 michael@0: ,eSVGTypesBegin = eSVGAngle michael@0: ,eSVGIntegerPair = 0x17 michael@0: ,eSVGLength = 0x18 michael@0: ,eSVGLengthList = 0x19 michael@0: ,eSVGNumberList = 0x1A michael@0: ,eSVGNumberPair = 0x1B michael@0: ,eSVGPathData = 0x1C michael@0: ,eSVGPointList = 0x1D michael@0: ,eSVGPreserveAspectRatio = 0x1E michael@0: ,eSVGStringList = 0x1F michael@0: ,eSVGTransformList = 0x20 michael@0: ,eSVGViewBox = 0x21 michael@0: ,eSVGTypesEnd = eSVGViewBox michael@0: }; michael@0: michael@0: nsAttrValue(); michael@0: nsAttrValue(const nsAttrValue& aOther); michael@0: explicit nsAttrValue(const nsAString& aValue); michael@0: explicit nsAttrValue(nsIAtom* aValue); michael@0: nsAttrValue(mozilla::css::StyleRule* aValue, const nsAString* aSerialized); michael@0: explicit nsAttrValue(const nsIntMargin& aValue); michael@0: ~nsAttrValue(); michael@0: michael@0: inline const nsAttrValue& operator=(const nsAttrValue& aOther); michael@0: michael@0: static nsresult Init(); michael@0: static void Shutdown(); michael@0: michael@0: ValueType Type() const; michael@0: michael@0: void Reset(); michael@0: michael@0: void SetTo(const nsAttrValue& aOther); michael@0: void SetTo(const nsAString& aValue); michael@0: void SetTo(nsIAtom* aValue); michael@0: void SetTo(int16_t aInt); michael@0: void SetTo(int32_t aInt, const nsAString* aSerialized); michael@0: void SetTo(double aValue, const nsAString* aSerialized); michael@0: void SetTo(mozilla::css::StyleRule* aValue, const nsAString* aSerialized); michael@0: void SetTo(mozilla::css::URLValue* aValue, const nsAString* aSerialized); michael@0: void SetTo(const nsIntMargin& aValue); michael@0: void SetTo(const nsSVGAngle& aValue, const nsAString* aSerialized); michael@0: void SetTo(const nsSVGIntegerPair& aValue, const nsAString* aSerialized); michael@0: void SetTo(const nsSVGLength2& aValue, const nsAString* aSerialized); michael@0: void SetTo(const mozilla::SVGLengthList& aValue, michael@0: const nsAString* aSerialized); michael@0: void SetTo(const mozilla::SVGNumberList& aValue, michael@0: const nsAString* aSerialized); michael@0: void SetTo(const nsSVGNumberPair& aValue, const nsAString* aSerialized); michael@0: void SetTo(const mozilla::SVGPathData& aValue, const nsAString* aSerialized); michael@0: void SetTo(const mozilla::SVGPointList& aValue, const nsAString* aSerialized); michael@0: void SetTo(const mozilla::SVGAnimatedPreserveAspectRatio& aValue, michael@0: const nsAString* aSerialized); michael@0: void SetTo(const mozilla::SVGStringList& aValue, michael@0: const nsAString* aSerialized); michael@0: void SetTo(const mozilla::SVGTransformList& aValue, michael@0: const nsAString* aSerialized); michael@0: void SetTo(const nsSVGViewBox& aValue, const nsAString* aSerialized); michael@0: michael@0: /** michael@0: * Sets this object with the string or atom representation of aValue. michael@0: * michael@0: * After calling this method, this object will have type eString unless the michael@0: * type of aValue is eAtom, in which case this object will also have type michael@0: * eAtom. michael@0: */ michael@0: void SetToSerialized(const nsAttrValue& aValue); michael@0: michael@0: void SwapValueWith(nsAttrValue& aOther); michael@0: michael@0: void ToString(nsAString& aResult) const; michael@0: inline void ToString(mozilla::dom::DOMString& aResult) const; michael@0: michael@0: /** michael@0: * Returns the value of this object as an atom. If necessary, the value will michael@0: * first be serialised using ToString before converting to an atom. michael@0: */ michael@0: already_AddRefed GetAsAtom() const; michael@0: michael@0: // Methods to get value. These methods do not convert so only use them michael@0: // to retrieve the datatype that this nsAttrValue has. michael@0: inline bool IsEmptyString() const; michael@0: const nsCheapString GetStringValue() const; michael@0: inline nsIAtom* GetAtomValue() const; michael@0: inline int32_t GetIntegerValue() const; michael@0: bool GetColorValue(nscolor& aColor) const; michael@0: inline int16_t GetEnumValue() const; michael@0: inline float GetPercentValue() const; michael@0: inline AtomArray* GetAtomArrayValue() const; michael@0: inline mozilla::css::StyleRule* GetCSSStyleRuleValue() const; michael@0: inline mozilla::css::URLValue* GetURLValue() const; michael@0: inline mozilla::css::ImageValue* GetImageValue() const; michael@0: inline double GetDoubleValue() const; michael@0: bool GetIntMarginValue(nsIntMargin& aMargin) const; michael@0: michael@0: /** michael@0: * Returns the string corresponding to the stored enum value. michael@0: * michael@0: * @param aResult the string representing the enum tag michael@0: * @param aRealTag wheter we want to have the real tag or the saved one michael@0: */ michael@0: void GetEnumString(nsAString& aResult, bool aRealTag) const; michael@0: michael@0: // Methods to get access to atoms we may have michael@0: // Returns the number of atoms we have; 0 if we have none. It's OK michael@0: // to call this without checking the type first; it handles that. michael@0: uint32_t GetAtomCount() const; michael@0: // Returns the atom at aIndex (0-based). Do not call this with michael@0: // aIndex >= GetAtomCount(). michael@0: nsIAtom* AtomAt(int32_t aIndex) const; michael@0: michael@0: uint32_t HashValue() const; michael@0: bool Equals(const nsAttrValue& aOther) const; michael@0: // aCaseSensitive == eIgnoreCase means ASCII case-insenstive matching michael@0: bool Equals(const nsAString& aValue, nsCaseTreatment aCaseSensitive) const; michael@0: bool Equals(nsIAtom* aValue, nsCaseTreatment aCaseSensitive) const; michael@0: michael@0: /** michael@0: * Compares this object with aOther according to their string representation. michael@0: * michael@0: * For example, when called on an object with type eInteger and value 4, and michael@0: * given aOther of type eString and value "4", EqualsAsStrings will return michael@0: * true (while Equals will return false). michael@0: */ michael@0: bool EqualsAsStrings(const nsAttrValue& aOther) const; michael@0: michael@0: /** michael@0: * Returns true if this AttrValue is equal to the given atom, or is an michael@0: * array which contains the given atom. michael@0: */ michael@0: bool Contains(nsIAtom* aValue, nsCaseTreatment aCaseSensitive) const; michael@0: /** michael@0: * Returns true if this AttrValue is an atom equal to the given michael@0: * string, or is an array of atoms which contains the given string. michael@0: * This always does a case-sensitive comparison. michael@0: */ michael@0: bool Contains(const nsAString& aValue) const; michael@0: michael@0: void ParseAtom(const nsAString& aValue); michael@0: void ParseAtomArray(const nsAString& aValue); michael@0: void ParseStringOrAtom(const nsAString& aValue); michael@0: michael@0: /** michael@0: * Structure for a mapping from int (enum) values to strings. When you use michael@0: * it you generally create an array of them. michael@0: * Instantiate like this: michael@0: * EnumTable myTable[] = { michael@0: * { "string1", 1 }, michael@0: * { "string2", 2 }, michael@0: * { 0 } michael@0: * } michael@0: */ michael@0: struct EnumTable { michael@0: /** The string the value maps to */ michael@0: const char* tag; michael@0: /** The enum value that maps to this string */ michael@0: int16_t value; michael@0: }; michael@0: michael@0: /** michael@0: * Parse into an enum value. michael@0: * michael@0: * @param aValue the string to find the value for michael@0: * @param aTable the enumeration to map with michael@0: * @param aCaseSensitive specify if the parsing has to be case sensitive michael@0: * @param aDefaultValue if non-null, this function will always return true. michael@0: * Failure to parse aValue as one of the values in aTable will just michael@0: * cause aDefaultValue->value to be stored as the enumeration value. michael@0: * @return whether the enum value was found or not michael@0: */ michael@0: bool ParseEnumValue(const nsAString& aValue, michael@0: const EnumTable* aTable, michael@0: bool aCaseSensitive, michael@0: const EnumTable* aDefaultValue = nullptr); michael@0: michael@0: /** michael@0: * Parse a string into an integer. Can optionally parse percent (n%). michael@0: * This method explicitly sets a lower bound of zero on the element, michael@0: * whether it be percent or raw integer. michael@0: * michael@0: * @param aString the string to parse michael@0: * @return whether the value could be parsed michael@0: * michael@0: * @see http://www.whatwg.org/html/#rules-for-parsing-dimension-values michael@0: */ michael@0: bool ParseSpecialIntValue(const nsAString& aString); michael@0: michael@0: michael@0: /** michael@0: * Parse a string value into an integer. michael@0: * michael@0: * @param aString the string to parse michael@0: * @return whether the value could be parsed michael@0: */ michael@0: bool ParseIntValue(const nsAString& aString) { michael@0: return ParseIntWithBounds(aString, INT32_MIN, INT32_MAX); michael@0: } michael@0: michael@0: /** michael@0: * Parse a string value into an integer with minimum value and maximum value. michael@0: * michael@0: * @param aString the string to parse michael@0: * @param aMin the minimum value (if value is less it will be bumped up) michael@0: * @param aMax the maximum value (if value is greater it will be chopped down) michael@0: * @return whether the value could be parsed michael@0: */ michael@0: bool ParseIntWithBounds(const nsAString& aString, int32_t aMin, michael@0: int32_t aMax = INT32_MAX); michael@0: michael@0: /** michael@0: * Parse a string value into a non-negative integer. michael@0: * This method follows the rules for parsing non-negative integer from: michael@0: * http://dev.w3.org/html5/spec/infrastructure.html#rules-for-parsing-non-negative-integers michael@0: * michael@0: * @param aString the string to parse michael@0: * @return whether the value is valid michael@0: */ michael@0: bool ParseNonNegativeIntValue(const nsAString& aString); michael@0: michael@0: /** michael@0: * Parse a string value into a positive integer. michael@0: * This method follows the rules for parsing non-negative integer from: michael@0: * http://dev.w3.org/html5/spec/infrastructure.html#rules-for-parsing-non-negative-integers michael@0: * In addition of these rules, the value has to be greater than zero. michael@0: * michael@0: * This is generally used for parsing content attributes which reflecting IDL michael@0: * attributes are limited to only non-negative numbers greater than zero, see: michael@0: * http://dev.w3.org/html5/spec/common-dom-interfaces.html#limited-to-only-non-negative-numbers-greater-than-zero michael@0: * michael@0: * @param aString the string to parse michael@0: * @return whether the value was valid michael@0: */ michael@0: bool ParsePositiveIntValue(const nsAString& aString); michael@0: michael@0: /** michael@0: * Parse a string into a color. This implements what HTML5 calls the michael@0: * "rules for parsing a legacy color value". michael@0: * michael@0: * @param aString the string to parse michael@0: * @return whether the value could be parsed michael@0: */ michael@0: bool ParseColor(const nsAString& aString); michael@0: michael@0: /** michael@0: * Parse a string value into a double-precision floating point value. michael@0: * michael@0: * @param aString the string to parse michael@0: * @return whether the value could be parsed michael@0: */ michael@0: bool ParseDoubleValue(const nsAString& aString); michael@0: michael@0: /** michael@0: * Parse a lazy URI. This just sets up the storage for the URI; it michael@0: * doesn't actually allocate it. michael@0: */ michael@0: bool ParseLazyURIValue(const nsAString& aString); michael@0: michael@0: /** michael@0: * Parse a margin string of format 'top, right, bottom, left' into michael@0: * an nsIntMargin. michael@0: * michael@0: * @param aString the string to parse michael@0: * @return whether the value could be parsed michael@0: */ michael@0: bool ParseIntMarginValue(const nsAString& aString); michael@0: michael@0: /** michael@0: * Convert a URL nsAttrValue to an Image nsAttrValue. michael@0: * michael@0: * @param aDocument the document this nsAttrValue belongs to. michael@0: */ michael@0: void LoadImage(nsIDocument* aDocument); michael@0: michael@0: /** michael@0: * Parse a string into a CSS style rule. michael@0: * michael@0: * @param aString the style attribute value to be parsed. michael@0: * @param aElement the element the attribute is set on. michael@0: */ michael@0: bool ParseStyleAttribute(const nsAString& aString, michael@0: nsStyledElementNotElementCSSInlineStyle* aElement); michael@0: michael@0: size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; michael@0: michael@0: private: michael@0: // These have to be the same as in ValueType michael@0: enum ValueBaseType { michael@0: eStringBase = eString, // 00 michael@0: eOtherBase = 0x01, // 01 michael@0: eAtomBase = eAtom, // 10 michael@0: eIntegerBase = 0x03 // 11 michael@0: }; michael@0: michael@0: inline ValueBaseType BaseType() const; michael@0: inline bool IsSVGType(ValueType aType) const; michael@0: michael@0: /** michael@0: * Get the index of an EnumTable in the sEnumTableArray. michael@0: * If the EnumTable is not in the sEnumTableArray, it is added. michael@0: * michael@0: * @param aTable the EnumTable to get the index of. michael@0: * @return the index of the EnumTable. michael@0: */ michael@0: int16_t GetEnumTableIndex(const EnumTable* aTable); michael@0: michael@0: inline void SetPtrValueAndType(void* aValue, ValueBaseType aType); michael@0: void SetIntValueAndType(int32_t aValue, ValueType aType, michael@0: const nsAString* aStringValue); michael@0: void SetColorValue(nscolor aColor, const nsAString& aString); michael@0: void SetMiscAtomOrString(const nsAString* aValue); michael@0: void ResetMiscAtomOrString(); michael@0: void SetSVGType(ValueType aType, const void* aValue, michael@0: const nsAString* aSerialized); michael@0: inline void ResetIfSet(); michael@0: michael@0: inline void* GetPtr() const; michael@0: inline MiscContainer* GetMiscContainer() const; michael@0: inline int32_t GetIntInternal() const; michael@0: michael@0: // Clears the current MiscContainer. This will return null if there is no michael@0: // existing container. michael@0: MiscContainer* ClearMiscContainer(); michael@0: // Like ClearMiscContainer, except allocates a new container if one does not michael@0: // exist already. michael@0: MiscContainer* EnsureEmptyMiscContainer(); michael@0: bool EnsureEmptyAtomArray(); michael@0: already_AddRefed michael@0: GetStringBuffer(const nsAString& aValue) const; michael@0: // aStrict is set true if stringifying the return value equals with michael@0: // aValue. michael@0: int32_t StringToInteger(const nsAString& aValue, michael@0: bool* aStrict, michael@0: nsresult* aErrorCode, michael@0: bool aCanBePercent = false, michael@0: bool* aIsPercent = nullptr) const; michael@0: // Given an enum table and a particular entry in that table, return michael@0: // the actual integer value we should store. michael@0: int32_t EnumTableEntryToValue(const EnumTable* aEnumTable, michael@0: const EnumTable* aTableEntry); michael@0: michael@0: static nsTArray* sEnumTableArray; michael@0: michael@0: uintptr_t mBits; michael@0: }; michael@0: michael@0: inline const nsAttrValue& michael@0: nsAttrValue::operator=(const nsAttrValue& aOther) michael@0: { michael@0: SetTo(aOther); michael@0: return *this; michael@0: } michael@0: michael@0: inline nsIAtom* michael@0: nsAttrValue::GetAtomValue() const michael@0: { michael@0: NS_PRECONDITION(Type() == eAtom, "wrong type"); michael@0: return reinterpret_cast(GetPtr()); michael@0: } michael@0: michael@0: inline nsAttrValue::ValueBaseType michael@0: nsAttrValue::BaseType() const michael@0: { michael@0: return static_cast(mBits & NS_ATTRVALUE_BASETYPE_MASK); michael@0: } michael@0: michael@0: inline void* michael@0: nsAttrValue::GetPtr() const michael@0: { michael@0: NS_ASSERTION(BaseType() != eIntegerBase, michael@0: "getting pointer from non-pointer"); michael@0: return reinterpret_cast(mBits & NS_ATTRVALUE_POINTERVALUE_MASK); michael@0: } michael@0: michael@0: inline bool michael@0: nsAttrValue::IsEmptyString() const michael@0: { michael@0: return !mBits; michael@0: } michael@0: michael@0: inline void michael@0: nsAttrValue::ToString(mozilla::dom::DOMString& aResult) const michael@0: { michael@0: switch (Type()) { michael@0: case eString: michael@0: { michael@0: nsStringBuffer* str = static_cast(GetPtr()); michael@0: if (str) { michael@0: aResult.SetStringBuffer(str, str->StorageSize()/sizeof(char16_t) - 1); michael@0: } michael@0: // else aResult is already empty michael@0: return; michael@0: } michael@0: case eAtom: michael@0: { michael@0: nsIAtom *atom = static_cast(GetPtr()); michael@0: aResult.SetStringBuffer(atom->GetStringBuffer(), atom->GetLength()); michael@0: break; michael@0: } michael@0: default: michael@0: { michael@0: ToString(aResult.AsAString()); michael@0: } michael@0: } michael@0: } michael@0: michael@0: #endif