2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2006-03-30 12:03:04 +04:00
|
|
|
/*
|
2018-08-07 22:07:26 +03:00
|
|
|
* Storage of the attributes of a DOM node.
|
2006-03-30 12:03:04 +04:00
|
|
|
*/
|
|
|
|
|
2018-08-07 22:07:26 +03:00
|
|
|
#ifndef AttrArray_h___
|
|
|
|
#define AttrArray_h___
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2011-12-16 23:42:07 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2018-08-13 16:35:57 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
|
|
|
#include "mozilla/Span.h"
|
2016-07-23 00:11:41 +03:00
|
|
|
#include "mozilla/dom/BorrowedAttrInfo.h"
|
2011-12-16 23:42:07 +04:00
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsAttrName.h"
|
|
|
|
#include "nsAttrValue.h"
|
2012-07-14 03:29:14 +04:00
|
|
|
#include "nsCaseTreatment.h"
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2006-05-16 18:51:52 +04:00
|
|
|
class nsINode;
|
2004-01-15 20:07:27 +03:00
|
|
|
class nsIContent;
|
2004-01-26 22:22:05 +03:00
|
|
|
class nsMappedAttributes;
|
2004-04-13 01:56:09 +04:00
|
|
|
class nsHTMLStyleSheet;
|
2004-01-26 22:22:05 +03:00
|
|
|
class nsRuleWalker;
|
2008-01-09 12:38:28 +03:00
|
|
|
class nsMappedAttributeElement;
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2018-08-07 22:07:26 +03:00
|
|
|
class AttrArray
|
2004-01-15 20:07:27 +03:00
|
|
|
{
|
2016-07-23 00:11:41 +03:00
|
|
|
typedef mozilla::dom::BorrowedAttrInfo BorrowedAttrInfo;
|
2004-01-15 20:07:27 +03:00
|
|
|
public:
|
2018-08-13 16:35:57 +03:00
|
|
|
AttrArray() = default;
|
|
|
|
~AttrArray() = default;
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2013-11-05 15:53:57 +04:00
|
|
|
bool HasAttrs() const
|
|
|
|
{
|
2018-08-13 16:35:57 +03:00
|
|
|
return NonMappedAttrCount() || MappedAttrCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t AttrCount() const
|
|
|
|
{
|
|
|
|
return NonMappedAttrCount() + MappedAttrCount();
|
2013-11-05 15:53:57 +04:00
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
const nsAttrValue* GetAttr(nsAtom* aLocalName,
|
2013-05-11 02:57:58 +04:00
|
|
|
int32_t aNamespaceID = kNameSpaceID_None) const;
|
|
|
|
// As above but using a string attr name and always using
|
|
|
|
// kNameSpaceID_None. This is always case-sensitive.
|
|
|
|
const nsAttrValue* GetAttr(const nsAString& aName) const;
|
2012-07-14 03:29:14 +04:00
|
|
|
// Get an nsAttrValue by qualified name. Can optionally do
|
|
|
|
// ASCII-case-insensitive name matching.
|
|
|
|
const nsAttrValue* GetAttr(const nsAString& aName,
|
|
|
|
nsCaseTreatment aCaseSensitive) const;
|
2012-08-22 19:56:38 +04:00
|
|
|
const nsAttrValue* AttrAt(uint32_t aPos) const;
|
2015-07-25 08:57:13 +03:00
|
|
|
// SetAndSwapAttr swaps the current attribute value with aValue.
|
2017-05-19 00:09:01 +03:00
|
|
|
// If the attribute was unset, an empty value will be swapped into aValue
|
|
|
|
// and aHadValue will be set to false. Otherwise, aHadValue will be set to
|
|
|
|
// true.
|
2017-10-03 01:05:19 +03:00
|
|
|
nsresult SetAndSwapAttr(nsAtom* aLocalName, nsAttrValue& aValue,
|
2017-05-19 00:09:01 +03:00
|
|
|
bool* aHadValue);
|
|
|
|
nsresult SetAndSwapAttr(mozilla::dom::NodeInfo* aName, nsAttrValue& aValue,
|
|
|
|
bool* aHadValue);
|
2006-11-11 03:04:46 +03:00
|
|
|
|
|
|
|
// Remove the attr at position aPos. The value of the attr is placed in
|
|
|
|
// aValue; any value that was already in aValue is destroyed.
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult RemoveAttrAt(uint32_t aPos, nsAttrValue& aValue);
|
2005-12-30 23:12:35 +03:00
|
|
|
|
|
|
|
// Returns attribute name at given position, *not* out-of-bounds safe
|
2012-08-22 19:56:38 +04:00
|
|
|
const nsAttrName* AttrNameAt(uint32_t aPos) const;
|
2005-12-30 23:12:35 +03:00
|
|
|
|
2016-07-22 06:10:23 +03:00
|
|
|
// Returns the attribute info at a given position, *not* out-of-bounds safe
|
2016-07-23 00:11:41 +03:00
|
|
|
BorrowedAttrInfo AttrInfoAt(uint32_t aPos) const;
|
2016-07-22 06:10:23 +03:00
|
|
|
|
2005-12-30 23:25:54 +03:00
|
|
|
// Returns attribute name at given position or null if aPos is out-of-bounds
|
2012-08-22 19:56:38 +04:00
|
|
|
const nsAttrName* GetSafeAttrNameAt(uint32_t aPos) const;
|
2005-12-30 23:12:35 +03:00
|
|
|
|
2010-03-08 18:45:00 +03:00
|
|
|
const nsAttrName* GetExistingAttrNameFromQName(const nsAString& aName) const;
|
2017-10-03 01:05:19 +03:00
|
|
|
int32_t IndexOfAttr(nsAtom* aLocalName, int32_t aNamespaceID = kNameSpaceID_None) const;
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2017-05-19 00:09:01 +03:00
|
|
|
// SetAndSwapMappedAttr swaps the current attribute value with aValue.
|
|
|
|
// If the attribute was unset, an empty value will be swapped into aValue
|
|
|
|
// and aHadValue will be set to false. Otherwise, aHadValue will be set to
|
|
|
|
// true.
|
2017-10-03 01:05:19 +03:00
|
|
|
nsresult SetAndSwapMappedAttr(nsAtom* aLocalName, nsAttrValue& aValue,
|
2008-01-09 12:38:28 +03:00
|
|
|
nsMappedAttributeElement* aContent,
|
2017-05-19 00:09:01 +03:00
|
|
|
nsHTMLStyleSheet* aSheet,
|
|
|
|
bool* aHadValue);
|
2012-03-22 08:10:51 +04:00
|
|
|
nsresult SetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet) {
|
|
|
|
if (!mImpl || !mImpl->mMappedAttrs) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return DoSetMappedAttrStyleSheet(aSheet);
|
|
|
|
}
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2018-07-06 21:43:14 +03:00
|
|
|
// Update the rule mapping function on our mapped attributes, if we have any.
|
|
|
|
// We take a nsMappedAttributeElement, not a nsMapRuleToAttributesFunc,
|
|
|
|
// because the latter is defined in a header we can't include here.
|
|
|
|
nsresult UpdateMappedAttrRuleMapper(nsMappedAttributeElement& aElement)
|
|
|
|
{
|
|
|
|
if (!mImpl || !mImpl->mMappedAttrs) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return DoUpdateMappedAttrRuleMapper(aElement);
|
|
|
|
}
|
|
|
|
|
2008-02-03 02:41:24 +03:00
|
|
|
void Compact();
|
|
|
|
|
2013-06-23 16:03:39 +04:00
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
2012-02-16 03:40:45 +04:00
|
|
|
bool HasMappedAttrs() const
|
|
|
|
{
|
|
|
|
return MappedAttrCount();
|
|
|
|
}
|
2017-01-20 02:56:53 +03:00
|
|
|
const nsMappedAttributes* GetMapped() const;
|
2011-07-12 18:56:01 +04:00
|
|
|
|
2017-03-29 22:10:00 +03:00
|
|
|
// Force this to have mapped attributes, even if those attributes are empty.
|
|
|
|
nsresult ForceMapped(nsMappedAttributeElement* aContent, nsIDocument* aDocument);
|
|
|
|
|
|
|
|
// Clear the servo declaration block on the mapped attributes, if any
|
|
|
|
// Will assert off main thread
|
|
|
|
void ClearMappedServoStyle();
|
|
|
|
|
2017-04-20 22:57:48 +03:00
|
|
|
// Increases capacity (if necessary) to have enough space to accomodate the
|
2018-08-09 02:58:44 +03:00
|
|
|
// unmapped attributes of |aOther|.
|
|
|
|
nsresult EnsureCapacityToClone(const AttrArray& aOther);
|
2017-04-20 22:57:48 +03:00
|
|
|
|
2018-08-14 19:43:26 +03:00
|
|
|
struct InternalAttr
|
|
|
|
{
|
|
|
|
nsAttrName mName;
|
|
|
|
nsAttrValue mValue;
|
|
|
|
};
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
private:
|
2018-08-07 22:07:26 +03:00
|
|
|
AttrArray(const AttrArray& aOther) = delete;
|
|
|
|
AttrArray& operator=(const AttrArray& aOther) = delete;
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
uint32_t NonMappedAttrCount() const
|
|
|
|
{
|
|
|
|
return mImpl ? mImpl->mAttrCount : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t MappedAttrCount() const
|
|
|
|
{
|
|
|
|
return mImpl && mImpl->mMappedAttrs ? DoGetMappedAttrCount() : 0;
|
|
|
|
}
|
2004-08-10 20:16:58 +04:00
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
uint32_t DoGetMappedAttrCount() const;
|
2004-01-26 22:22:05 +03:00
|
|
|
|
2012-08-04 11:44:01 +04:00
|
|
|
// Returns a non-null zero-refcount object.
|
|
|
|
nsMappedAttributes*
|
|
|
|
GetModifiableMapped(nsMappedAttributeElement* aContent,
|
|
|
|
nsHTMLStyleSheet* aSheet,
|
2017-03-29 22:10:00 +03:00
|
|
|
bool aWillAddAttr,
|
|
|
|
int32_t aAttrCount = 1);
|
2004-01-26 22:22:05 +03:00
|
|
|
nsresult MakeMappedUnique(nsMappedAttributes* aAttributes);
|
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
bool GrowBy(uint32_t aGrowSize);
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
// Tries to create an attribute, growing the buffer if needed, with the given
|
|
|
|
// name and value.
|
|
|
|
//
|
|
|
|
// The value is moved from the argument.
|
|
|
|
//
|
|
|
|
// `Name` can be anything you construct a `nsAttrName` with (either an atom or
|
|
|
|
// a NodeInfo pointer).
|
|
|
|
template<typename Name>
|
|
|
|
nsresult AddNewAttribute(Name*, nsAttrValue&);
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2012-03-22 08:10:51 +04:00
|
|
|
/**
|
|
|
|
* Guts of SetMappedAttrStyleSheet for the rare case when we have mapped attrs
|
|
|
|
*/
|
|
|
|
nsresult DoSetMappedAttrStyleSheet(nsHTMLStyleSheet* aSheet);
|
|
|
|
|
2018-07-06 21:43:14 +03:00
|
|
|
/**
|
|
|
|
* Guts of UpdateMappedAttrRuleMapper for the case when we have mapped attrs.
|
|
|
|
*/
|
|
|
|
nsresult DoUpdateMappedAttrRuleMapper(nsMappedAttributeElement& aElement);
|
|
|
|
|
2018-08-16 13:45:50 +03:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
// Disable MSVC warning 'nonstandard extension used: zero-sized array in struct/union'
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4200)
|
|
|
|
#endif
|
2018-08-13 16:35:57 +03:00
|
|
|
class Impl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
constexpr static size_t AllocationSizeForAttributes(uint32_t aAttrCount)
|
|
|
|
{
|
|
|
|
return sizeof(Impl) + aAttrCount * sizeof(InternalAttr);
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::Span<const InternalAttr> NonMappedAttrs() const
|
|
|
|
{
|
|
|
|
return mozilla::MakeSpan(static_cast<const InternalAttr*>(mBuffer), mAttrCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::Span<InternalAttr> NonMappedAttrs()
|
|
|
|
{
|
|
|
|
return mozilla::MakeSpan(static_cast<InternalAttr*>(mBuffer), mAttrCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
Impl(const Impl&) = delete;
|
|
|
|
Impl(Impl&&) = delete;
|
|
|
|
~Impl();
|
|
|
|
|
2018-08-07 22:11:11 +03:00
|
|
|
uint32_t mAttrCount;
|
2018-08-13 16:35:57 +03:00
|
|
|
uint32_t mCapacity; // In number of InternalAttrs
|
|
|
|
|
|
|
|
// Manually refcounted.
|
2004-01-26 22:22:05 +03:00
|
|
|
nsMappedAttributes* mMappedAttrs;
|
2018-08-13 16:35:57 +03:00
|
|
|
|
|
|
|
// Allocated in the same buffer as `Impl`.
|
|
|
|
InternalAttr mBuffer[0];
|
2004-01-15 20:07:27 +03:00
|
|
|
};
|
2018-08-16 13:45:50 +03:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
2004-01-15 20:07:27 +03:00
|
|
|
|
2018-08-13 16:35:57 +03:00
|
|
|
mozilla::Span<InternalAttr> NonMappedAttrs()
|
|
|
|
{
|
|
|
|
return mImpl ? mImpl->NonMappedAttrs() : mozilla::Span<InternalAttr>();
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::Span<const InternalAttr> NonMappedAttrs() const
|
|
|
|
{
|
|
|
|
return mImpl ? mImpl->NonMappedAttrs() : mozilla::Span<const InternalAttr>();
|
|
|
|
}
|
|
|
|
|
|
|
|
mozilla::UniquePtr<Impl> mImpl;
|
2004-01-15 20:07:27 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|