From ce94155998ab64d442463cd38faad62b94ae4a79 Mon Sep 17 00:00:00 2001 From: John Schoenick Date: Tue, 1 Jul 2014 13:35:21 -0700 Subject: [PATCH] Bug 1076587 - Allow nsAttrValueOrString helper to accept potentially-null pointers r=bz --- content/base/src/nsAttrValueOrString.cpp | 5 +++++ content/base/src/nsAttrValueOrString.h | 16 ++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/content/base/src/nsAttrValueOrString.cpp b/content/base/src/nsAttrValueOrString.cpp index f5719017cbee..08183d0705ca 100644 --- a/content/base/src/nsAttrValueOrString.cpp +++ b/content/base/src/nsAttrValueOrString.cpp @@ -12,6 +12,11 @@ nsAttrValueOrString::String() const return *mStringPtr; } + if (!mAttrValue) { + mStringPtr = &mCheapString; + return *mStringPtr; + } + if (mAttrValue->Type() == nsAttrValue::eString) { mCheapString = mAttrValue->GetStringValue(); mStringPtr = &mCheapString; diff --git a/content/base/src/nsAttrValueOrString.h b/content/base/src/nsAttrValueOrString.h index 28f3b683f908..139a6f06f4b4 100644 --- a/content/base/src/nsAttrValueOrString.h +++ b/content/base/src/nsAttrValueOrString.h @@ -8,6 +8,9 @@ * because constructing an nsAttrValue from an nsAString can be expensive when * the buffer of the string is not shared. * + * This treats nsAttrValueOrString(nullptr) as the empty string, + * to help with contexts where a null pointer denotes an empty value. + * * Since a raw pointer to the passed-in string is kept, this class should only * be used on the stack. */ @@ -26,12 +29,25 @@ public: , mStringPtr(&aValue) , mCheapString(nullptr) { } + + explicit nsAttrValueOrString(const nsAString* aValue) + : mAttrValue(nullptr) + , mStringPtr(aValue) + , mCheapString(nullptr) + { } + explicit nsAttrValueOrString(const nsAttrValue& aValue) : mAttrValue(&aValue) , mStringPtr(nullptr) , mCheapString(nullptr) { } + explicit nsAttrValueOrString(const nsAttrValue* aValue) + : mAttrValue(aValue) + , mStringPtr(nullptr) + , mCheapString(nullptr) + { } + /** * Returns a reference to the string value of the contents of this object. *