Bug 480647 part 2 - Move ParseLegacyFontSize to nsContentUtils so editor/ can use it; r=bz

This commit is contained in:
Aryeh Gregor 2012-04-24 13:57:23 +03:00
parent dcdf030a23
commit 6bc478db8b
3 changed files with 63 additions and 54 deletions

View File

@ -444,6 +444,15 @@ public:
*/ */
static bool ParseIntMarginValue(const nsAString& aString, nsIntMargin& aResult); static bool ParseIntMarginValue(const nsAString& aString, nsIntMargin& aResult);
/**
* Parse the value of the <font size=""> attribute according to the HTML5
* spec as of April 16, 2012.
*
* @param aValue the value to parse
* @return 1 to 7, or 0 if the value couldn't be parsed
*/
static PRInt32 ParseLegacyFontSize(const nsAString& aValue);
static void Shutdown(); static void Shutdown();
/** /**

View File

@ -1307,6 +1307,59 @@ nsContentUtils::ParseIntMarginValue(const nsAString& aString, nsIntMargin& resul
return true; return true;
} }
// static
PRInt32
nsContentUtils::ParseLegacyFontSize(const nsAString& aValue)
{
nsAString::const_iterator iter, end;
aValue.BeginReading(iter);
aValue.EndReading(end);
while (iter != end && nsContentUtils::IsHTMLWhitespace(*iter)) {
++iter;
}
if (iter == end) {
return 0;
}
bool relative = false;
bool negate = false;
if (*iter == PRUnichar('-')) {
relative = true;
negate = true;
++iter;
} else if (*iter == PRUnichar('+')) {
relative = true;
++iter;
}
if (*iter < PRUnichar('0') || *iter > PRUnichar('9')) {
return 0;
}
// We don't have to worry about overflow, since we can bail out as soon as
// we're bigger than 7.
PRInt32 value = 0;
while (iter != end && *iter >= PRUnichar('0') && *iter <= PRUnichar('9')) {
value = 10*value + (*iter - PRUnichar('0'));
if (value >= 7) {
break;
}
++iter;
}
if (relative) {
if (negate) {
value = 3 - value;
} else {
value = 3 + value;
}
}
return clamped(value, 1, 7);
}
/* static */ /* static */
void void
nsContentUtils::GetOfflineAppManifest(nsIDocument *aDocument, nsIURI **aURI) nsContentUtils::GetOfflineAppManifest(nsIDocument *aDocument, nsIURI **aURI)

View File

@ -118,59 +118,6 @@ NS_IMPL_STRING_ATTR(nsHTMLFontElement, Face, face)
NS_IMPL_STRING_ATTR(nsHTMLFontElement, Size, size) NS_IMPL_STRING_ATTR(nsHTMLFontElement, Size, size)
// Returns 1 to 7, or 0 on error. Follows HTML spec as of April 16, 2012.
static PRInt32
ParseLegacyFontSize(const nsAString& aValue)
{
nsAString::const_iterator iter, end;
aValue.BeginReading(iter);
aValue.EndReading(end);
while (iter != end && nsContentUtils::IsHTMLWhitespace(*iter)) {
++iter;
}
if (iter == end) {
return 0;
}
bool relative = false;
bool negate = false;
if (*iter == PRUnichar('-')) {
relative = true;
negate = true;
++iter;
} else if (*iter == PRUnichar('+')) {
relative = true;
++iter;
}
if (*iter < PRUnichar('0') || *iter > PRUnichar('9')) {
return 0;
}
// We don't have to worry about overflow, since we can bail out as soon as
// we're bigger than 7.
PRInt32 value = 0;
while (iter != end && *iter >= PRUnichar('0') && *iter <= PRUnichar('9')) {
value = 10*value + (*iter - PRUnichar('0'));
if (value >= 7) {
break;
}
++iter;
}
if (relative) {
if (negate) {
value = 3 - value;
} else {
value = 3 + value;
}
}
return clamped(value, 1, 7);
}
bool bool
nsHTMLFontElement::ParseAttribute(PRInt32 aNamespaceID, nsHTMLFontElement::ParseAttribute(PRInt32 aNamespaceID,
nsIAtom* aAttribute, nsIAtom* aAttribute,
@ -179,7 +126,7 @@ nsHTMLFontElement::ParseAttribute(PRInt32 aNamespaceID,
{ {
if (aNamespaceID == kNameSpaceID_None) { if (aNamespaceID == kNameSpaceID_None) {
if (aAttribute == nsGkAtoms::size) { if (aAttribute == nsGkAtoms::size) {
PRInt32 size = ParseLegacyFontSize(aValue); PRInt32 size = nsContentUtils::ParseLegacyFontSize(aValue);
if (size) { if (size) {
aResult.SetTo(size, &aValue); aResult.SetTo(size, &aValue);
return true; return true;