Bug 408096: & not escaped as & in href when creating plaintext url html r=benb, sr=cbiesinger, a1.9=beltzner

This commit is contained in:
mkmelin+mozilla@iki.fi 2008-04-12 06:16:26 -07:00
parent 86578ca070
commit 8466d74199
2 changed files with 38 additions and 9 deletions

View File

@ -64,7 +64,8 @@ static inline PRBool IsSpace(const PRUnichar aChar)
// Escape Char will take ch, escape it and append the result to
// aStringToAppendTo
void
mozTXTToHTMLConv::EscapeChar(const PRUnichar ch, nsString& aStringToAppendTo)
mozTXTToHTMLConv::EscapeChar(const PRUnichar ch, nsString& aStringToAppendTo,
PRBool inAttribute)
{
switch (ch)
{
@ -77,6 +78,13 @@ mozTXTToHTMLConv::EscapeChar(const PRUnichar ch, nsString& aStringToAppendTo)
case '&':
aStringToAppendTo.AppendLiteral("&");
break;
case '"':
if (inAttribute)
{
aStringToAppendTo.AppendLiteral(""");
break;
}
// else fall through
default:
aStringToAppendTo += ch;
}
@ -86,8 +94,8 @@ mozTXTToHTMLConv::EscapeChar(const PRUnichar ch, nsString& aStringToAppendTo)
// EscapeStr takes the passed in string and
// escapes it IN PLACE.
void
mozTXTToHTMLConv::EscapeStr(nsString& aInString)
void
mozTXTToHTMLConv::EscapeStr(nsString& aInString, PRBool inAttribute)
{
// the replace substring routines
// don't seem to work if you have a character
@ -115,6 +123,15 @@ mozTXTToHTMLConv::EscapeStr(nsString& aInString)
aInString.Insert(NS_LITERAL_STRING("&"), i);
i += 5; // skip past the integers we just added
break;
case '"':
if (inAttribute)
{
aInString.Cut(i, 1);
aInString.Insert(NS_LITERAL_STRING("""), i);
i += 6;
break;
}
// else fall through
default:
i++;
}
@ -146,6 +163,11 @@ mozTXTToHTMLConv::UnescapeStr(const PRUnichar * aInString, PRInt32 aStartPos, PR
aOutString.Append(PRUnichar('&'));
i += 5;
}
else if (!nsCRT::strncmp(subString, NS_LITERAL_STRING(""").get(), MinInt(6, aLength - remainingChars)))
{
aOutString.Append(PRUnichar('"'));
i += 6;
}
else
{
aOutString += aInString[i];
@ -383,7 +405,7 @@ mozTXTToHTMLConv::CalculateURLBoundaries(const PRUnichar * aInString, PRInt32 aI
default: break;
} //switch
EscapeStr(desc);
EscapeStr(desc, PR_FALSE);
txtURL.Append(&aInString[start], end - start + 1);
txtURL.StripWhitespace();
@ -465,8 +487,11 @@ mozTXTToHTMLConv::CheckURLAndCreateHTML(
break;
default: break;
}
nsAutoString escapedURL(txtURL);
EscapeStr(escapedURL, PR_TRUE);
outputHTML.AppendLiteral("\" href=\"");
outputHTML += txtURL;
outputHTML += escapedURL;
outputHTML.AppendLiteral("\">");
outputHTML += desc;
outputHTML.AppendLiteral("</a>");
@ -1196,7 +1221,7 @@ mozTXTToHTMLConv::ScanTXT(const PRUnichar * aInString, PRInt32 aInStringLength,
case '<':
case '>':
case '&':
EscapeChar(aInString[i], aOutString);
EscapeChar(aInString[i], aOutString, PR_FALSE);
i++;
break;
// Normal characters

View File

@ -141,13 +141,16 @@ private:
@param ch (in)
@param aStringToAppendto (out) - the string to append the escaped
string to.
@param inAttribute (in) - will escape quotes, too (which is
only needed for attribute values)
*/
void EscapeChar(const PRUnichar ch, nsString& aStringToAppendto);
void EscapeChar(const PRUnichar ch, nsString& aStringToAppendto,
PRBool inAttribute);
/**
See EscapeChar. Escapes the string in place.
*/
void EscapeStr(nsString& aInString);
void EscapeStr(nsString& aInString, PRBool inAttribute);
/**
Currently only reverts "<", ">" and "&". All others stay as they are.<p>
@ -156,7 +159,8 @@ private:
@param aLength (in) length of the buffer
@param aOutString (out) unescaped buffer
*/
void UnescapeStr(const PRUnichar * aInString, PRInt32 aStartPos, PRInt32 aLength, nsString& aOutString);
void UnescapeStr(const PRUnichar * aInString, PRInt32 aStartPos,
PRInt32 aLength, nsString& aOutString);
/**
<em>Note</em>: I use different strategies to pass context between the