Revised implementation to better manage the embedded text fragment; implement revised nsITextContent API

This commit is contained in:
kipp%netscape.com 1999-08-27 21:41:13 +00:00
parent c6628f4c30
commit 7ce7393125
2 changed files with 102 additions and 12 deletions

View File

@ -159,6 +159,8 @@ public:
// Implementation for nsITextContent
NS_IMETHOD GetText(const nsTextFragment*& aFragmentsResult,
PRInt32& aNumFragmentsResult);
NS_IMETHOD GetTextLength(PRInt32* aLengthResult);
NS_IMETHOD CopyText(nsString& aResult);
NS_IMETHOD SetText(const PRUnichar* aBuffer,
PRInt32 aLength,
PRBool aNotify);
@ -169,6 +171,8 @@ public:
//----------------------------------------
void ValidateTextFragment();
void ToCString(nsString& aBuf, PRInt32 aOffset, PRInt32 aLen) const;
// Up pointer to the real content object that we are
@ -390,9 +394,8 @@ nsAttributeContent::GetRangeList(nsVoidArray*& aResult) const
// Implementation of the nsITextContent interface
nsresult
nsAttributeContent::GetText(const nsTextFragment*& aFragmentsResult,
PRInt32& aNumFragmentsResult)
void
nsAttributeContent::ValidateTextFragment()
{
if (nsnull != mContent) {
nsAutoString result;
@ -401,17 +404,56 @@ nsAttributeContent::GetText(const nsTextFragment*& aFragmentsResult,
PRUnichar * text = result.ToNewUnicode();
mText.SetTo(text, result.Length());
delete text;
}
else {
mText.SetTo("", 0);
}
}
nsresult
nsAttributeContent::GetText(const nsTextFragment*& aFragmentsResult,
PRInt32& aNumFragmentsResult)
{
ValidateTextFragment();
if (nsnull != mContent) {
aFragmentsResult = &mText;
aNumFragmentsResult = 1;
return NS_OK;
}
// XXX is this a good idea, or should we just return an empty
// fragment with no data in it?
return NS_ERROR_FAILURE;
}
nsresult
nsAttributeContent::GetTextLength(PRInt32* aLengthResult)
{
if (!aLengthResult) {
return NS_ERROR_NULL_POINTER;
}
ValidateTextFragment();
*aLengthResult = mText.GetLength();
return NS_OK;
}
nsresult
nsAttributeContent::CopyText(nsString& aResult)
{
ValidateTextFragment();
if (mText.Is2b()) {
aResult.SetString(mText.Get2b(), mText.GetLength());
}
else {
aResult.SetString(mText.Get1b(), mText.GetLength());
}
return NS_OK;
}
// XXX shouldn't these update mContent's attribute?
nsresult
nsAttributeContent::SetText(const PRUnichar* aBuffer, PRInt32 aLength,
PRBool aNotify)
PRBool aNotify)
{
NS_PRECONDITION((aLength >= 0) && (nsnull != aBuffer), "bad args");
if (aLength < 0) {
@ -429,10 +471,11 @@ nsAttributeContent::SetText(const PRUnichar* aBuffer, PRInt32 aLength,
return NS_OK;
}
// XXX shouldn't these update mContent's attribute?
nsresult
nsAttributeContent::SetText(const char* aBuffer,
PRInt32 aLength,
PRBool aNotify)
PRInt32 aLength,
PRBool aNotify)
{
NS_PRECONDITION((aLength >= 0) && (nsnull != aBuffer), "bad args");
if (aLength < 0) {
@ -454,6 +497,8 @@ nsAttributeContent::SetText(const char* aBuffer,
nsresult
nsAttributeContent::IsOnlyWhitespace(PRBool* aResult)
{
ValidateTextFragment();
nsTextFragment& frag = mText;
if (frag.Is2b()) {
const PRUnichar* cp = frag.Get2b();

View File

@ -159,6 +159,8 @@ public:
// Implementation for nsITextContent
NS_IMETHOD GetText(const nsTextFragment*& aFragmentsResult,
PRInt32& aNumFragmentsResult);
NS_IMETHOD GetTextLength(PRInt32* aLengthResult);
NS_IMETHOD CopyText(nsString& aResult);
NS_IMETHOD SetText(const PRUnichar* aBuffer,
PRInt32 aLength,
PRBool aNotify);
@ -169,6 +171,8 @@ public:
//----------------------------------------
void ValidateTextFragment();
void ToCString(nsString& aBuf, PRInt32 aOffset, PRInt32 aLen) const;
// Up pointer to the real content object that we are
@ -390,9 +394,8 @@ nsAttributeContent::GetRangeList(nsVoidArray*& aResult) const
// Implementation of the nsITextContent interface
nsresult
nsAttributeContent::GetText(const nsTextFragment*& aFragmentsResult,
PRInt32& aNumFragmentsResult)
void
nsAttributeContent::ValidateTextFragment()
{
if (nsnull != mContent) {
nsAutoString result;
@ -401,17 +404,56 @@ nsAttributeContent::GetText(const nsTextFragment*& aFragmentsResult,
PRUnichar * text = result.ToNewUnicode();
mText.SetTo(text, result.Length());
delete text;
}
else {
mText.SetTo("", 0);
}
}
nsresult
nsAttributeContent::GetText(const nsTextFragment*& aFragmentsResult,
PRInt32& aNumFragmentsResult)
{
ValidateTextFragment();
if (nsnull != mContent) {
aFragmentsResult = &mText;
aNumFragmentsResult = 1;
return NS_OK;
}
// XXX is this a good idea, or should we just return an empty
// fragment with no data in it?
return NS_ERROR_FAILURE;
}
nsresult
nsAttributeContent::GetTextLength(PRInt32* aLengthResult)
{
if (!aLengthResult) {
return NS_ERROR_NULL_POINTER;
}
ValidateTextFragment();
*aLengthResult = mText.GetLength();
return NS_OK;
}
nsresult
nsAttributeContent::CopyText(nsString& aResult)
{
ValidateTextFragment();
if (mText.Is2b()) {
aResult.SetString(mText.Get2b(), mText.GetLength());
}
else {
aResult.SetString(mText.Get1b(), mText.GetLength());
}
return NS_OK;
}
// XXX shouldn't these update mContent's attribute?
nsresult
nsAttributeContent::SetText(const PRUnichar* aBuffer, PRInt32 aLength,
PRBool aNotify)
PRBool aNotify)
{
NS_PRECONDITION((aLength >= 0) && (nsnull != aBuffer), "bad args");
if (aLength < 0) {
@ -429,10 +471,11 @@ nsAttributeContent::SetText(const PRUnichar* aBuffer, PRInt32 aLength,
return NS_OK;
}
// XXX shouldn't these update mContent's attribute?
nsresult
nsAttributeContent::SetText(const char* aBuffer,
PRInt32 aLength,
PRBool aNotify)
PRInt32 aLength,
PRBool aNotify)
{
NS_PRECONDITION((aLength >= 0) && (nsnull != aBuffer), "bad args");
if (aLength < 0) {
@ -454,6 +497,8 @@ nsAttributeContent::SetText(const char* aBuffer,
nsresult
nsAttributeContent::IsOnlyWhitespace(PRBool* aResult)
{
ValidateTextFragment();
nsTextFragment& frag = mText;
if (frag.Is2b()) {
const PRUnichar* cp = frag.Get2b();