Skip to content

Commit 449c4ed

Browse files
committed
escaping url(
1 parent 8398074 commit 449c4ed

File tree

3 files changed

+235
-120
lines changed

3 files changed

+235
-120
lines changed

org/w3c/css/parser/analyzer/CssParser.jj

Lines changed: 29 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ import org.w3c.css.util.ApplContext;
5656
import org.w3c.css.util.CssProfile;
5757
import org.w3c.css.util.CssVersion;
5858
import org.w3c.css.util.InvalidParamException;
59+
import org.w3c.css.util.StringUtils;
5960
import org.w3c.css.util.Util;
6061
import org.w3c.css.util.WarningParamException;
61-
import org.w3c.css.values.CssAngle;
6262
import org.w3c.css.values.CssANPlusB;
63+
import org.w3c.css.values.CssAngle;
6364
import org.w3c.css.values.CssAttr;
6465
import org.w3c.css.values.CssBracket;
6566
import org.w3c.css.values.CssCalc;
@@ -93,6 +94,8 @@ import org.w3c.css.values.CssValue;
9394
import org.w3c.css.values.CssVariable;
9495
import org.w3c.css.values.CssVolume;
9596

97+
import static org.w3c.css.util.StringUtils.convertIdent;
98+
9699
import java.io.InputStream;
97100
import java.io.InputStreamReader;
98101
import java.io.UnsupportedEncodingException;
@@ -528,6 +531,9 @@ SPECIAL_TOKEN :
528531
{
529532
< #H : ["0"-"9", "a"-"f"] >
530533
| < #NONASCII : ["\u0080"-"\uFFFF"] >
534+
| < #__U : "u" | ( "\\" ( "0" )? ( "0" )? ( "0" )? ( "0" )? ( "55" | "75" ) ( "\r\n" | [ " ", "\t" , "\n" , "\r", "\f" ] )? ) >
535+
| < #__R : "r" | ( "\\" ( "0" )? ( "0" )? ( "0" )? ( "0" )? ( "52" | "72" ) ( "\r\n" | [ " ", "\t" , "\n" , "\r", "\f" ] )? ) >
536+
| < #__L : "l" | ( "\\" ( "0" )? ( "0" )? ( "0" )? ( "0" )? ( "4c" | "6c" ) ( "\r\n" | [ " ", "\t" , "\n" , "\r", "\f" ] )? ) >
531537
| < #UNICODE : "\\" <H> ( <H> )? ( <H> )? ( <H> )? ( <H> )? ( <H> )?
532538
( "\r\n" | [ " ", "\t" , "\n" , "\r", "\f" ] )? >
533539
| < #ESCAPE : <UNICODE> | ( "\\" ~[ "\r", "\n", "\f", "0"-"9", "a"-"f" ] ) >
@@ -546,6 +552,7 @@ SPECIAL_TOKEN :
546552
| < #_S : ( [ " ", "\t" , "\n" , "\r", "\f" ] ) ( <COMMENT> | [ " ", "\t" , "\n" , "\r", "\f" ] )* >
547553
| < #_W : ( <_S> )? >
548554
| < #NL : ( "\n" | "\r\n" | "\r" | "\f" ) >
555+
| < URLPREFIX : <__U> <__R> <__L> "(" >
549556
}
550557
/*
551558
* The _S definition is not ( [ " ", "\t" , "\n" , "\r", "\f" ] ) + as we need to add support
@@ -638,7 +645,7 @@ TOKEN :
638645
<DEFAULT>
639646
TOKEN [IGNORE_CASE] :
640647
{
641-
< URL : "url(" ( <S> )* ( <STRING> | <_URL> ) ( <S> )* <RPAREN> >
648+
< URL : <URLPREFIX> ( <S> )* ( <STRING> | <_URL> ) ( <S> )* <RPAREN> >
642649
}
643650

644651
<DEFAULT>
@@ -3949,134 +3956,39 @@ void skipAfterExpression(Exception e) {
39493956
}
39503957

39513958
JAVACODE
3952-
String convertStringIndex(String s, int start, int len, boolean escapeFirst) {
3953-
int index = start;
3954-
int t;
3955-
int maxCount = 0;
3956-
boolean gotWhiteSpace = false;
3957-
int count = 0;
3958-
if ((start == 0) && (len == s.length()) && (s.indexOf('\\') == -1)) {
3959-
return s;
3960-
}
3961-
StringBuilder buf = new StringBuilder(len);
3962-
maxCount = ((ac.getCssVersion() == CssVersion.CSS1) ? 4 : 6);
3963-
3964-
while (index < len) {
3965-
char c = s.charAt(index);
3966-
if (c == '\\') {
3967-
if (++index < len) {
3968-
c = s.charAt(index);
3969-
switch (c) {
3970-
case '0': case '1': case '2': case '3': case '4':
3971-
case '5': case '6': case '7': case '8': case '9':
3972-
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
3973-
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
3974-
int numValue = Character.digit(c, 16);
3975-
count = 1;
3976-
while (index + 1 < len) {
3977-
c = s.charAt(index+1);
3978-
t = Character.digit(c, 16);
3979-
if (t != -1 && count++ < maxCount) {
3980-
numValue = (numValue<<4) | t;
3981-
index++;
3982-
} else {
3983-
if (c == ' ' || c == '\t' ||
3984-
c == '\n' || c == '\f' ) {
3985-
// skip the latest white space
3986-
index++;
3987-
gotWhiteSpace = true;
3988-
} else if ( c == '\r' ) {
3989-
index++;
3990-
gotWhiteSpace = true;
3991-
// special case for \r\n
3992-
if (index+1 < len) {
3993-
if (s.charAt(index + 1) == '\n') {
3994-
index++;
3995-
}
3996-
}
3997-
}
3998-
break;
3999-
}
4000-
}
4001-
if (!escapeFirst && numValue < 255 && numValue>31) {
4002-
if (! ( (numValue>96 && numValue<123) // [a-z]
4003-
|| (numValue>64 && numValue<91) // [A-Z]
4004-
|| (numValue>47 && numValue<58) // [0-9]
4005-
|| (numValue == 95) // _
4006-
|| (numValue == 45) // -
4007-
)
4008-
) {
4009-
buf.append('\\');
4010-
}
4011-
buf.append((char) numValue);
4012-
break;
4013-
}
4014-
char b[];
4015-
// we fully escape when we have a space
4016-
if (gotWhiteSpace) {
4017-
b = new char[maxCount];
4018-
t = maxCount;
4019-
} else {
4020-
b = new char[count];
4021-
t = count;
4022-
}
4023-
while (t > 0) {
4024-
b[--t] = hexdigits[numValue & 0xF];
4025-
numValue >>>= 4;
4026-
}
4027-
buf.append('\\').append(b);
4028-
break;
4029-
case '\n':
4030-
case '\f':
4031-
break;
4032-
case '\r':
4033-
if (index + 1 < len) {
4034-
if (s.charAt(index + 1) == '\n') {
4035-
index ++;
4036-
}
4037-
}
4038-
break;
4039-
case '-' : case '_' : case 'g' : case 'G' :
4040-
case 'h' : case 'H' : case 'i' : case 'I' :
4041-
case 'j' : case 'J' : case 'k' : case 'K' :
4042-
case 'l' : case 'L' : case 'm' : case 'M' :
4043-
case 'n' : case 'N' : case 'o' : case 'O' :
4044-
case 'p' : case 'P' : case 'q' : case 'Q' :
4045-
case 'r' : case 'R' : case 's' : case 'S' :
4046-
case 't' : case 'T' : case 'u' : case 'U' :
4047-
case 'v' : case 'V' : case 'w' : case 'W' :
4048-
case 'x' : case 'X' : case 'y' : case 'Y' :
4049-
case 'z' : case 'Z' :
4050-
buf.append(c);
4051-
break;
4052-
default:
4053-
buf.append('\\').append(c);
4054-
}
4055-
} else {
3959+
public String convertStringIndex(String s, int start, int len, boolean escapeFirst) {
3960+
try {
3961+
return StringUtils.convertStringIndex(s, start, len, escapeFirst, ac);
3962+
} catch (InvalidParamException nex) {
40563963
throw new ParseException("invalid string");
4057-
}
4058-
} else {
4059-
buf.append(c);
40603964
}
4061-
escapeFirst = false;
4062-
index++;
4063-
}
4064-
return buf.toString();
40653965
}
40663966

40673967
JAVACODE
4068-
String convertIdent(String s) {
4069-
return convertStringIndex(s, 0, s.length(), false);
3968+
String convertIdent(String s) throws ParseException {
3969+
try {
3970+
return StringUtils.convertIdent(s, ac);
3971+
} catch (InvalidParamException nex) {
3972+
throw new ParseException("invalid string");
3973+
}
40703974
}
40713975

40723976
JAVACODE
40733977
String convertClassIdent(String s) {
4074-
return convertStringIndex(s, 0, s.length(), true);
3978+
try {
3979+
return StringUtils.convertClassIdent(s, ac);
3980+
} catch (InvalidParamException nex) {
3981+
throw new ParseException("invalid string");
3982+
}
40753983
}
40763984

40773985
JAVACODE
40783986
String convertString(String s) {
4079-
return convertStringIndex(s, 0, s.length(), false);
3987+
try {
3988+
return StringUtils.convertString(s, ac);
3989+
} catch (InvalidParamException nex) {
3990+
throw new ParseException("invalid string");
3991+
}
40803992
}
40813993

40823994
JAVACODE

0 commit comments

Comments
 (0)