Bug 950076 - Add a fallible form of nsAString::Append and Replace and use it in nsTextFragment::AppendTo. r=bsmedberg, r=jst

This commit is contained in:
Alessio Placitelli 2014-03-19 13:05:02 -04:00
Родитель 6298745434
Коммит 1a0a307fb0
12 изменённых файлов: 147 добавлений и 23 удалений

Просмотреть файл

@ -201,6 +201,8 @@ public:
bool aNotify) MOZ_OVERRIDE;
virtual bool TextIsOnlyWhitespace() MOZ_OVERRIDE;
virtual void AppendTextTo(nsAString& aResult) MOZ_OVERRIDE;
virtual bool AppendTextTo(nsAString& aResult,
const mozilla::fallible_t&) MOZ_OVERRIDE NS_WARN_UNUSED_RESULT;
virtual nsIContent *GetBindingParent() const MOZ_OVERRIDE;
virtual nsXBLBinding *GetXBLBinding() const MOZ_OVERRIDE;
virtual void SetXBLBinding(nsXBLBinding* aBinding,

Просмотреть файл

@ -1247,8 +1247,8 @@ public:
/**
* Same as GetNodeTextContents but appends the result rather than sets it.
*/
static void AppendNodeTextContent(nsINode* aNode, bool aDeep,
nsAString& aResult);
static bool AppendNodeTextContent(nsINode* aNode, bool aDeep,
nsAString& aResult, const mozilla::fallible_t&);
/**
* Utility method that checks if a given node has any non-empty

Просмотреть файл

@ -539,6 +539,13 @@ public:
*/
virtual void AppendTextTo(nsAString& aResult) = 0;
/**
* Append the text content to aResult.
* NOTE: This asserts and returns for elements
*/
virtual bool AppendTextTo(nsAString& aResult,
const mozilla::fallible_t&) NS_WARN_UNUSED_RESULT = 0;
/**
* Check if this content is focusable and in the current tab order.
* Note: most callers should use nsIFrame::IsFocusable() instead as it

Просмотреть файл

@ -1927,6 +1927,16 @@ FragmentOrElement::AppendTextTo(nsAString& aResult)
NS_NOTREACHED("called FragmentOrElement::TextLength");
}
bool
FragmentOrElement::AppendTextTo(nsAString& aResult, const mozilla::fallible_t&)
{
// We can remove this assertion if it turns out to be useful to be able
// to depend on this appending nothing.
NS_NOTREACHED("called FragmentOrElement::TextLength");
return false;
}
uint32_t
FragmentOrElement::GetChildCount() const
{

Просмотреть файл

@ -4191,40 +4191,58 @@ nsContentUtils::SetNodeTextContent(nsIContent* aContent,
return rv;
}
static void AppendNodeTextContentsRecurse(nsINode* aNode, nsAString& aResult)
static bool
AppendNodeTextContentsRecurse(nsINode* aNode, nsAString& aResult,
const mozilla::fallible_t&)
{
for (nsIContent* child = aNode->GetFirstChild();
child;
child = child->GetNextSibling()) {
if (child->IsElement()) {
AppendNodeTextContentsRecurse(child, aResult);
bool ok = AppendNodeTextContentsRecurse(child, aResult,
mozilla::fallible_t());
if (!ok) {
return false;
}
}
else if (child->IsNodeOfType(nsINode::eTEXT)) {
child->AppendTextTo(aResult);
bool ok = child->AppendTextTo(aResult, mozilla::fallible_t());
if (!ok) {
return false;
}
}
}
return true;
}
/* static */
void
bool
nsContentUtils::AppendNodeTextContent(nsINode* aNode, bool aDeep,
nsAString& aResult)
nsAString& aResult,
const mozilla::fallible_t&)
{
if (aNode->IsNodeOfType(nsINode::eTEXT)) {
static_cast<nsIContent*>(aNode)->AppendTextTo(aResult);
return static_cast<nsIContent*>(aNode)->AppendTextTo(aResult,
mozilla::fallible_t());
}
else if (aDeep) {
AppendNodeTextContentsRecurse(aNode, aResult);
return AppendNodeTextContentsRecurse(aNode, aResult, mozilla::fallible_t());
}
else {
for (nsIContent* child = aNode->GetFirstChild();
child;
child = child->GetNextSibling()) {
if (child->IsNodeOfType(nsINode::eTEXT)) {
child->AppendTextTo(aResult);
bool ok = child->AppendTextTo(aResult, mozilla::fallible_t());
if (!ok) {
return false;
}
}
}
}
return true;
}
bool
@ -6573,7 +6591,7 @@ void
nsContentUtils::GetNodeTextContent(nsINode* aNode, bool aDeep, nsAString& aResult)
{
aResult.Truncate();
AppendNodeTextContent(aNode, aDeep, aResult);
AppendNodeTextContent(aNode, aDeep, aResult, mozilla::fallible_t());
}
void

Просмотреть файл

@ -996,6 +996,12 @@ nsGenericDOMDataNode::AppendTextTo(nsAString& aResult)
mText.AppendTo(aResult);
}
bool
nsGenericDOMDataNode::AppendTextTo(nsAString& aResult, const mozilla::fallible_t&)
{
return mText.AppendTo(aResult, mozilla::fallible_t());
}
already_AddRefed<nsIAtom>
nsGenericDOMDataNode::GetCurrentValueAtom()
{

Просмотреть файл

@ -146,6 +146,8 @@ public:
bool aNotify) MOZ_OVERRIDE;
virtual bool TextIsOnlyWhitespace() MOZ_OVERRIDE;
virtual void AppendTextTo(nsAString& aResult) MOZ_OVERRIDE;
virtual bool AppendTextTo(nsAString& aResult,
const mozilla::fallible_t&) MOZ_OVERRIDE NS_WARN_UNUSED_RESULT;
virtual void DestroyContent() MOZ_OVERRIDE;
virtual void SaveSubtreeState() MOZ_OVERRIDE;

Просмотреть файл

@ -125,10 +125,27 @@ public:
* Append the contents of this string fragment to aString
*/
void AppendTo(nsAString& aString) const {
if (!AppendTo(aString, mozilla::fallible_t())) {
NS_ABORT_OOM(GetLength());
}
}
/**
* Append the contents of this string fragment to aString
* @return false if an out of memory condition is detected, true otherwise
*/
bool AppendTo(nsAString& aString,
const mozilla::fallible_t&) const NS_WARN_UNUSED_RESULT {
if (mState.mIs2b) {
aString.Append(m2b, mState.mLength);
bool ok = aString.Append(m2b, mState.mLength, mozilla::fallible_t());
if (!ok) {
return false;
}
return true;
} else {
AppendASCIItoUTF16(Substring(m1b, mState.mLength), aString);
return AppendASCIItoUTF16(Substring(m1b, mState.mLength), aString,
mozilla::fallible_t());
}
}
@ -138,10 +155,31 @@ public:
* @param aLength the length of the substring
*/
void AppendTo(nsAString& aString, int32_t aOffset, int32_t aLength) const {
if (!AppendTo(aString, aOffset, aLength, mozilla::fallible_t())) {
NS_ABORT_OOM(aLength);
}
}
/**
* Append a substring of the contents of this string fragment to aString.
* @param aString the string in which to append
* @param aOffset where to start the substring in this text fragment
* @param aLength the length of the substring
* @return false if an out of memory condition is detected, true otherwise
*/
bool AppendTo(nsAString& aString, int32_t aOffset, int32_t aLength,
const mozilla::fallible_t&) const NS_WARN_UNUSED_RESULT
{
if (mState.mIs2b) {
aString.Append(m2b + aOffset, aLength);
bool ok = aString.Append(m2b + aOffset, aLength, mozilla::fallible_t());
if (!ok) {
return false;
}
return true;
} else {
AppendASCIItoUTF16(Substring(m1b + aOffset, aLength), aString);
return AppendASCIItoUTF16(Substring(m1b + aOffset, aLength), aString,
mozilla::fallible_t());
}
}

Просмотреть файл

@ -514,7 +514,8 @@ txXPathNodeUtils::appendNodeValue(const txXPathNode& aNode, nsAString& aResult)
if (aNode.isDocument() ||
aNode.mNode->IsElement() ||
aNode.mNode->IsNodeOfType(nsINode::eDOCUMENT_FRAGMENT)) {
nsContentUtils::AppendNodeTextContent(aNode.mNode, true, aResult);
nsContentUtils::AppendNodeTextContent(aNode.mNode, true, aResult,
mozilla::fallible_t());
return;
}

Просмотреть файл

@ -568,14 +568,21 @@ mozInlineSpellWordUtil::BuildSoftText()
}
}
}
if (firstOffsetInNode < lastOffsetInNode) {
int32_t len = lastOffsetInNode - firstOffsetInNode;
mSoftTextDOMMapping.AppendElement(
DOMTextMapping(NodeOffset(node, firstOffsetInNode), mSoftText.Length(), len));
textFragment->AppendTo(mSoftText, firstOffsetInNode, len);
bool ok = textFragment->AppendTo(mSoftText, firstOffsetInNode, len,
mozilla::fallible_t());
if (!ok) {
// probably out of memory, remove from mSoftTextDOMMapping
mSoftTextDOMMapping.RemoveElementAt(mSoftTextDOMMapping.Length() - 1);
exit = true;
}
}
firstOffsetInNode = 0;
}

Просмотреть файл

@ -415,8 +415,12 @@ class nsTSubstring_CharT
*/
void NS_FASTCALL Replace( index_type cutStart, size_type cutLength, char_type c );
bool NS_FASTCALL Replace( index_type cutStart, size_type cutLength, char_type c, const mozilla::fallible_t&) NS_WARN_UNUSED_RESULT;
void NS_FASTCALL Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length = size_type(-1) );
bool NS_FASTCALL Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length, const mozilla::fallible_t&) NS_WARN_UNUSED_RESULT;
void Replace( index_type cutStart, size_type cutLength, const self_type& str ) { Replace(cutStart, cutLength, str.Data(), str.Length()); }
bool Replace( index_type cutStart, size_type cutLength, const self_type& str, const mozilla::fallible_t&) NS_WARN_UNUSED_RESULT
{ return Replace(cutStart, cutLength, str.Data(), str.Length(), mozilla::fallible_t()); }
void NS_FASTCALL Replace( index_type cutStart, size_type cutLength, const substring_tuple_type& tuple );
void NS_FASTCALL ReplaceASCII( index_type cutStart, size_type cutLength, const char* data, size_type length = size_type(-1) );
@ -428,7 +432,10 @@ class nsTSubstring_CharT
void ReplaceLiteral( index_type cutStart, size_type cutLength, const char_type (&str)[N] ) { ReplaceLiteral(cutStart, cutLength, str, N - 1); }
void Append( char_type c ) { Replace(mLength, 0, c); }
bool Append( char_type c, const mozilla::fallible_t&) NS_WARN_UNUSED_RESULT { return Replace(mLength, 0, c, mozilla::fallible_t()); }
void Append( const char_type* data, size_type length = size_type(-1) ) { Replace(mLength, 0, data, length); }
bool Append( const char_type* data, size_type length, const mozilla::fallible_t&) NS_WARN_UNUSED_RESULT
{ return Replace(mLength, 0, data, length, mozilla::fallible_t()); }
#if defined(CharT_is_PRUnichar) && defined(MOZ_USE_CHAR16_WRAPPER)
void Append( char16ptr_t data, size_type length = size_type(-1) ) { Append(static_cast<const char16_t*>(data), length); }

Просмотреть файл

@ -484,9 +484,30 @@ nsTSubstring_CharT::Replace( index_type cutStart, size_type cutLength, char_type
mData[cutStart] = c;
}
bool
nsTSubstring_CharT::Replace( index_type cutStart, size_type cutLength, char_type c, const mozilla::fallible_t& )
{
cutStart = XPCOM_MIN(cutStart, Length());
if (!ReplacePrep(cutStart, cutLength, 1))
return false;
mData[cutStart] = c;
return true;
}
void
nsTSubstring_CharT::Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length )
{
if (!Replace(cutStart, cutLength, data, length, mozilla::fallible_t()))
{
NS_ABORT_OOM(Length() - cutLength + 1);
}
}
bool
nsTSubstring_CharT::Replace( index_type cutStart, size_type cutLength, const char_type* data, size_type length, const mozilla::fallible_t& )
{
// unfortunately, some callers pass null :-(
if (!data)
@ -501,15 +522,20 @@ nsTSubstring_CharT::Replace( index_type cutStart, size_type cutLength, const cha
if (IsDependentOn(data, data + length))
{
nsTAutoString_CharT temp(data, length);
Replace(cutStart, cutLength, temp);
return;
return Replace(cutStart, cutLength, temp, mozilla::fallible_t());
}
}
cutStart = XPCOM_MIN(cutStart, Length());
if (ReplacePrep(cutStart, cutLength, length) && length > 0)
bool ok = ReplacePrep(cutStart, cutLength, length);
if (!ok)
return false;
if (length > 0)
char_traits::copy(mData + cutStart, data, length);
return true;
}
void
@ -517,7 +543,7 @@ nsTSubstring_CharT::ReplaceASCII( index_type cutStart, size_type cutLength, cons
{
if (length == size_type(-1))
length = strlen(data);
// A Unicode string can't depend on an ASCII string buffer,
// so this dependence check only applies to CStrings.
#ifdef CharT_is_char