Skip to content

Commit d23f09b

Browse files
committed
CSSUrl, remove quotes from the URL kept in .value
1 parent f0c136b commit d23f09b

File tree

2 files changed

+193
-0
lines changed

2 files changed

+193
-0
lines changed

org/w3c/css/values/CssURL.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,18 @@ public void set(String s, ApplContext ac, URL base)
8888
String urlname = s.substring(4, s.length() - 1).trim();
8989
this.base = base;
9090

91+
urlname = urlname.trim();
92+
if (urlname.isEmpty()){
93+
// okay, no further modifications needed
94+
}else if (urlname.charAt(0)=='"' || urlname.charAt(0)=='\''){
95+
final int l = urlname.length()-1;
96+
if (urlname.charAt(0)==urlname.charAt(l)){
97+
urlname = urlname.substring(1, l);
98+
}else{
99+
throw new InvalidParamException("url", s, ac);
100+
}
101+
}
102+
91103
value = filterURLData(urlname);
92104
full = null;
93105
if (!urlHeading.startsWith("url"))

org/w3c/css/values/CssURL.java.orig

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
//
2+
// $Id$
3+
// From Philippe Le Hegaret (Philippe.Le_Hegaret@sophia.inria.fr)
4+
//
5+
// (c) COPYRIGHT MIT and INRIA, 1997.
6+
// Please first read the full copyright statement in file COPYRIGHT.html
7+
package org.w3c.css.values;
8+
9+
import org.w3c.css.util.ApplContext;
10+
import org.w3c.css.util.HTTPURL;
11+
import org.w3c.css.util.InvalidParamException;
12+
13+
import java.net.MalformedURLException;
14+
import java.net.URL;
15+
16+
/**
17+
* <H3>
18+
* &nbsp;&nbsp; URL
19+
* </H3>
20+
* <p/>
21+
* A Uniform Resource Locator (URL) is identified with a functional notation:
22+
* <PRE>
23+
* BODY { background: url(http://www.bg.com/pinkish.gif) }
24+
* </PRE>
25+
* <p/>
26+
* The format of a URL value is 'url(' followed by optional white space followed
27+
* by an optional single quote (') or double quote (") character followed by
28+
* the URL itself (as defined in <A HREF="#ref11">[11]</A>) followed by an optional
29+
* single quote (') or double quote (") character followed by optional whitespace
30+
* followed by ')'. Quote characters that are not part of the URL itself must
31+
* be balanced.
32+
* <p/>
33+
* Parentheses, commas, whitespace characters, single quotes (') and double
34+
* quotes (") appearing in a URL must be escaped with a backslash: '\(', '\)',
35+
* '\,'.
36+
* <p/>
37+
* Partial URLs are interpreted relative to the source of the style sheet, not
38+
* relative to the document:
39+
* <PRE>
40+
* BODY { background: url(yellow) }
41+
* </PRE>
42+
* See also
43+
* <p/>
44+
* <A NAME="ref11">[11]</A> T Berners-Lee, L Masinter, M McCahill: "Uniform
45+
* Resource Locators (URL)", <A href="ftp://ds.internic.net/rfc/rfc1738.txt">RFC
46+
* 1738</A>, CERN, Xerox Corporation, University of Minnesota, December 1994
47+
*
48+
* @version $Revision$
49+
*/
50+
public class CssURL extends CssValue {
51+
52+
public static final int type = CssTypes.CSS_URL;
53+
54+
public final int getType() {
55+
return type;
56+
}
57+
58+
String value;
59+
String full = null;
60+
61+
URL base;
62+
URL urlValue = null;
63+
64+
/**
65+
* Set the value of this URL.
66+
*
67+
* @param s the string representation of the URL.
68+
* @param ac For errors and warnings reports.
69+
* @throws InvalidParamException The unit is incorrect
70+
* @deprecated
71+
*/
72+
public void set(String s, ApplContext ac)
73+
throws InvalidParamException {
74+
throw new InvalidParamException("Deprecated method invocation", ac);
75+
}
76+
77+
/**
78+
* Set the value of this URL.
79+
*
80+
* @param s the string representation of the URL.
81+
* @param ac For errors and warnings reports.
82+
* @param base the base location of the style sheet
83+
* @throws InvalidParamException The unit is incorrect
84+
*/
85+
public void set(String s, ApplContext ac, URL base)
86+
throws InvalidParamException {
87+
String urlHeading = s.substring(0, 3).toLowerCase();
88+
String urlname = s.substring(4, s.length() - 1).trim();
89+
this.base = base;
90+
91+
urlname = urlname.trim();
92+
if (urlname.isEmpty()){
93+
// okay, no further modifications needed
94+
}else if (urlname.charAt(0)=='"' || urlname.charAt(0)=='\''){
95+
final int l = urlname.length()-1;
96+
if (urlname.charAt(0)==urlname.charAt(l)){
97+
urlname = urlname.substring(1, l);
98+
}else{
99+
throw new InvalidParamException("url", s, ac);
100+
}
101+
}
102+
103+
value = filterURLData(urlname);
104+
full = null;
105+
if (!urlHeading.startsWith("url"))
106+
throw new InvalidParamException("url", s, ac);
107+
// special case for data url...
108+
if (urlname.contains("data:")) {
109+
// no more processing.
110+
return;
111+
}
112+
// now add the URL to the list of seen URLs in the context
113+
try {
114+
ac.addLinkedURI(getURL());
115+
} catch (MalformedURLException mex) {
116+
// error? throw an exception
117+
throw new InvalidParamException("url", s, ac);
118+
}
119+
}
120+
121+
private String filterURLData(String source) {
122+
StringBuilder sb = new StringBuilder();
123+
// here we just escape < and >, we might do more validation
124+
// like base64 encoding checks, when necessary
125+
for (char c : source.toCharArray()) {
126+
switch (c) {
127+
case '<':
128+
sb.append("%3c");
129+
break;
130+
case '>':
131+
sb.append("%3e");
132+
break;
133+
default:
134+
sb.append(c);
135+
}
136+
}
137+
return sb.toString();
138+
}
139+
140+
/**
141+
* Get the internal value.
142+
*/
143+
public Object get() {
144+
return value;
145+
}
146+
147+
/**
148+
* Returns the URL
149+
*
150+
* @return the URL
151+
* @throws java.net.MalformedURLException (self explanatory)
152+
*/
153+
public URL getURL() throws MalformedURLException {
154+
if (urlValue == null) {
155+
urlValue = HTTPURL.getURL(base, value);
156+
}
157+
return urlValue;
158+
}
159+
160+
/**
161+
* Returns a string representation of the object.
162+
*/
163+
public String toString() {
164+
if (full != null) {
165+
return full;
166+
}
167+
StringBuilder sb = new StringBuilder("url(");
168+
sb.append(value).append(')');
169+
return full = sb.toString();
170+
}
171+
172+
/**
173+
* Compares two values for equality.
174+
*
175+
* @param url The other value.
176+
*/
177+
public boolean equals(Object url) {
178+
return (url instanceof CssURL && value.equals(((CssURL) url).value));
179+
}
180+
181+
}

0 commit comments

Comments
 (0)