Bug 834877 part 4. Add overloads of GetAttr() and GetId() that take a DOMString. r=peterv

This commit is contained in:
Boris Zbarsky 2013-01-29 09:42:14 -05:00
parent 634d722804
commit fdc89414dd
4 changed files with 65 additions and 22 deletions

View File

@ -45,6 +45,8 @@
#include "nsISMILAttr.h"
#include "nsClientRect.h"
#include "nsEvent.h"
#include "nsAttrValue.h"
#include "mozilla/dom/BindingDeclarations.h"
class nsIDOMEventListener;
class nsIFrame;
@ -461,7 +463,7 @@ public:
nsresult SetParsedAttr(int32_t aNameSpaceID, nsIAtom* aName, nsIAtom* aPrefix,
nsAttrValue& aParsedValue, bool aNotify);
virtual bool GetAttr(int32_t aNameSpaceID, nsIAtom* aName,
nsAString& aResult) const;
nsAString& aResult) const;
virtual bool HasAttr(int32_t aNameSpaceID, nsIAtom* aName) const;
// aCaseSensitive == eIgnoreCaase means ASCII case-insensitive matching.
virtual bool AttrValueIs(int32_t aNameSpaceID, nsIAtom* aName,
@ -517,6 +519,23 @@ private:
const MappedAttributeEntry* const aMaps[],
uint32_t aMapCount);
protected:
inline bool GetAttr(int32_t aNameSpaceID, nsIAtom* aName,
mozilla::dom::DOMString& aResult) const
{
NS_ASSERTION(nullptr != aName, "must have attribute name");
NS_ASSERTION(aNameSpaceID != kNameSpaceID_Unknown,
"must have a real namespace ID!");
MOZ_ASSERT(aResult.HasStringBuffer() && aResult.StringBufferLength() == 0,
"Should have empty string coming in");
const nsAttrValue* val = mAttrsAndChildren.GetAttr(aName, aNameSpaceID);
if (val) {
val->ToString(aResult);
}
// else DOMString comes pre-emptied.
return val != nullptr;
}
public:
void GetTagName(nsAString& aTagName) const
{
@ -526,6 +545,10 @@ public:
{
GetAttr(kNameSpaceID_None, nsGkAtoms::id, aId);
}
void GetId(mozilla::dom::DOMString& aId) const
{
GetAttr(kNameSpaceID_None, nsGkAtoms::id, aId);
}
void SetId(const nsAString& aId)
{
SetAttr(kNameSpaceID_None, nsGkAtoms::id, aId, true);

View File

@ -1961,23 +1961,10 @@ bool
Element::GetAttr(int32_t aNameSpaceID, nsIAtom* aName,
nsAString& aResult) const
{
NS_ASSERTION(nullptr != aName, "must have attribute name");
NS_ASSERTION(aNameSpaceID != kNameSpaceID_Unknown,
"must have a real namespace ID!");
const nsAttrValue* val = mAttrsAndChildren.GetAttr(aName, aNameSpaceID);
if (!val) {
// Since we are returning a success code we'd better do
// something about the out parameters (someone may have
// given us a non-empty string).
aResult.Truncate();
return false;
}
val->ToString(aResult);
return true;
DOMString str;
bool haveAttr = GetAttr(aNameSpaceID, aName, str);
str.ToString(aResult);
return haveAttr;
}
bool

View File

@ -20,9 +20,10 @@
#include "nsCOMPtr.h"
#include "SVGAttrValueWrapper.h"
#include "nsTArrayForwardDeclare.h"
#include "nsIAtom.h"
#include "mozilla/dom/BindingDeclarations.h"
class nsAString;
class nsIAtom;
class nsIDocument;
class nsStyledElementNotElementCSSInlineStyle;
struct MiscContainer;
@ -159,6 +160,8 @@ public:
void SwapValueWith(nsAttrValue& aOther);
void ToString(nsAString& aResult) const;
inline void ToString(mozilla::dom::DOMString& aResult) const;
/**
* Returns the value of this object as an atom. If necessary, the value will
* first be serialised using ToString before converting to an atom.
@ -464,4 +467,30 @@ nsAttrValue::IsEmptyString() const
return !mBits;
}
inline void
nsAttrValue::ToString(mozilla::dom::DOMString& aResult) const
{
switch (Type()) {
case eString:
{
nsStringBuffer* str = static_cast<nsStringBuffer*>(GetPtr());
if (str) {
aResult.SetStringBuffer(str, str->StorageSize()/sizeof(PRUnichar) - 1);
}
// else aResult is already empty
return;
}
case eAtom:
{
nsIAtom *atom = static_cast<nsIAtom*>(GetPtr());
aResult.SetStringBuffer(atom->GetStringBuffer(), atom->GetLength());
break;
}
default:
{
ToString(aResult.AsAString());
}
}
}
#endif

View File

@ -40,15 +40,15 @@ interface nsIAtom : nsISupports
%{C++
// note this is NOT virtual so this won't muck with the vtable!
inline bool Equals(const nsAString& aString) {
inline bool Equals(const nsAString& aString) const {
return aString.Equals(nsDependentString(mString, mLength));
}
inline const PRUnichar* GetUTF16String() {
inline const PRUnichar* GetUTF16String() const {
return mString;
}
inline const uint32_t GetLength() {
inline const uint32_t GetLength() const {
return mLength;
}
@ -56,6 +56,10 @@ interface nsIAtom : nsISupports
nsStringBuffer::FromData(mString)->ToString(mLength, aBuf);
}
inline nsStringBuffer* GetStringBuffer() const {
return nsStringBuffer::FromData(mString);
}
/**
* A hashcode that is better distributed than the actual atom
* pointer, for use in situations that need a well-distributed