Fix CSS id for string prefixed with a digit

When encoding a string as a CSS identifier, we must add an underscore if
the strings starts with a digit.
This commit is contained in:
Francis Lachapelle
2014-03-06 21:32:36 -05:00
parent 28b1938bd9
commit 6cecca6c4f
4 changed files with 37 additions and 14 deletions
+25 -10
View File
@@ -332,8 +332,7 @@ static NSCharacterSet *controlCharSet = nil;
(cssEscapingCount + 1)
* sizeof (unichar));
for (count = 0; count < cssEscapingCount; count++)
*(cssEscapingCharacters + count)
= [[characters objectAtIndex: count] characterAtIndex: 0];
*(cssEscapingCharacters + count) = [[characters objectAtIndex: count] characterAtIndex: 0];
*(cssEscapingCharacters + cssEscapingCount) = 0;
}
@@ -360,14 +359,20 @@ static NSCharacterSet *controlCharSet = nil;
cssIdentifier = [NSMutableString string];
max = [self length];
for (count = 0; count < max; count++)
if (max > 0)
{
currentChar = [self characterAtIndex: count];
idx = [self _cssCharacterIndex: currentChar];
if (idx > -1)
[cssIdentifier appendString: cssEscapingStrings[idx]];
else
[cssIdentifier appendFormat: @"%C", currentChar];
if (isdigit([self characterAtIndex: 0]))
// A CSS identifier can't start with a digit; we add an underscore
[cssIdentifier appendString: @"_"];
for (count = 0; count < max; count++)
{
currentChar = [self characterAtIndex: count];
idx = [self _cssCharacterIndex: currentChar];
if (idx > -1)
[cssIdentifier appendString: cssEscapingStrings[idx]];
else
[cssIdentifier appendFormat: @"%C", currentChar];
}
}
return cssIdentifier;
@@ -397,7 +402,17 @@ static NSCharacterSet *controlCharSet = nil;
newString = [NSMutableString string];
max = [self length];
for (count = 0; count < max - 2; count++)
count = 0;
if (max > 0
&& [self characterAtIndex: 0] == '_'
&& isdigit([self characterAtIndex: 1]))
{
/* If the identifier starts with an underscore followed by a digit,
we remove the underscore */
count = 1;
}
for (; count < max - 2; count++)
{
currentChar = [self characterAtIndex: count];
if (currentChar == '_')