1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 | package Torello.CSS; import Torello.Java.Additional.ByRef; import Torello.Java.Additional.EffectivelyFinal; import Torello.Java.UnreachableError; import java.util.Vector; import java.util.stream.IntStream; import java.util.function.Consumer; /** Any {@code URL} */ @Torello.JavaDoc.JDHeaderBackgroundImg(EmbedTagFileID="CSS_TOK") public class URLToken extends CSSToken implements CharSequence, java.io.Serializable, Comparable<CharSequence> { /** <EMBED CLASS='external-html' DATA-FILE-ID=SVUID> */ protected static final long serialVersionUID = 1; /** The unescaped text that constitutes this {@code URL}. */ public final String unescapedURL; // ******************************************************************************************** // ******************************************************************************************** // Private Constructor, API "is" and "if" Methods // ******************************************************************************************** // ******************************************************************************************** private URLToken( final int[] css, final int sPos, final int ePos, final IntStream.Builder urlStrBuilder ) { super(css, sPos, ePos); int[] urlArr = urlStrBuilder.build().toArray(); this.unescapedURL = new String(urlArr, 0, urlArr.length); } @Override public final boolean isURL() { return true; } @Override public final URLToken ifURL() { return this; } // ******************************************************************************************** // ******************************************************************************************** // User's Constructor: a static "build" method // ******************************************************************************************** // ******************************************************************************************** /** * <EMBED CLASS=defs DATA-TOK=Str DATA-P=urlStr> * <EMBED CLASS='external-html' DATA-FILE-ID=BUILD_DESC> * @param urlStr <EMBED CLASS='external-html' DATA-FILE-ID=BUILD_PARAM> * @return <EMBED CLASS='external-html' DATA-FILE-ID=BUILD_RET> * @throws TokenizeException <EMBED CLASS='external-html' DATA-FILE-ID=BUILD_TOK_EX> */ @SuppressWarnings("unchecked") public static URLToken build(final String urlStr) { if (urlStr.length() == 0) throw new TokenizeException(); final int[] css = urlStr.codePoints().toArray(); if (css.length < 1) throw new TokenizeException(URLToken.class); if (Whitespace.is(css[0])) throw new TokenizeException ("A URL cannot begin with Whitespace."); final EffectivelyFinal<CSSToken> saveIt = new EffectivelyFinal<>(null); final Consumer<CSSToken> acceptor = (CSSToken t) -> { if (t instanceof Whitespace) throw new TokenizeException ("The URL provided contained unescaped Whitespace"); else if (t instanceof Comment) throw new TokenizeException ("The URL provided contained a CSS Comment"); else if (t instanceof BadURL) throw new TokenizeException ("The URL provided was parsed into an instanceof BadURL: [" + t.str + "]"); else if (t instanceof URLToken) saveIt.f = t; // These are the only types that may be returned by Class CSSToken else throw new UnreachableError(); }; URLToken.consume( css, new ByRef<>(0), acceptor, (TokenizeError te) -> te.throwException(), true ); // Need to guarantee that the entire String was consumed in the process of tokenizing the // input String. 'TokenzeException' has a nicely worded Esception-Message to explain what // has occured here. if (urlStr.length() != saveIt.f.str.length()) throw new TokenizeException(urlStr, saveIt.f.str); return (URLToken) saveIt.f; } // ******************************************************************************************** // ******************************************************************************************** // CONSUME // ******************************************************************************************** // ******************************************************************************************** // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // Copied from: // https://drafts.csswg.org/css-syntax-3/#consume-url-token // April 2024 // *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** // // 4.3.6. Consume a url token // // This section describes how to consume a url token from a stream of code points. It returns // either a <url-token> or a <bad-url-token>. // // NOTE: This algorithm assumes that the initial "url(" has already been consumed. This // algorithm also assumes that it’s being called to consume an "unquoted" value, like url(foo). // A quoted value, like url("foo"), is parsed as a <function-token>. Consume an ident-like // token automatically handles this distinction; this algorithm shouldn’t be called directly // otherwise. // // 1. Initially create a <url-token> with its value set to the empty string. // // 2. Consume as much whitespace as possible. // // 3. Repeatedly consume the next input code point from the stream: // // ** U+0029 RIGHT PARENTHESIS ()) // Return the <url-token>. // // ** EOF // This is a parse error. Return the <url-token>. // // ** whitespace // Consume as much whitespace as possible. If the next input code point is // U+0029 RIGHT PARENTHESIS ()) or EOF, consume it and return the <url-token> (if EOF was // encountered, this is a parse error); otherwise, consume the remnants of a bad url, // create a <bad-url-token>, and return it. // // ** U+0022 QUOTATION MARK (") // ** U+0027 APOSTROPHE (') // ** U+0028 LEFT PARENTHESIS (() // ** non-printable code point // This is a parse error. Consume the remnants of a bad url, create a <bad-url-token>, and // return it. // // ** U+005C REVERSE SOLIDUS (\) // If the stream starts with a valid escape, consume an escaped code point and append the // returned code point to the <url-token>’s value. // // Otherwise, this is a parse error. Consume the remnants of a bad url, create a // <bad-url-token>, and return it. // // ** anything else // Append the current input code point to the <url-token>’s value. /** * This is a tokenizer method which <B>"consumes"</B> the next {@code URLToken} from the input * Code-Point Array. * * <EMBED CLASS=defs DATA-TOK=URLToken DATA-URL=consume-url-token DATA-OP=Consume> * <EMBED CLASS=external-html DATA-FILE-ID=COPIED_CSS_WG> * <EMBED CLASS=external-html DATA-FILE-ID=URL_TOKEN> * <EMBED CLASS=external-html DATA-FILE-ID=URL_TOK_SVG> */ protected static void consume( // When invoked from 'CSSTokenizer' final int[] css, // C, int[] css final ByRef<Integer> POS, // P, array-pos loop-variable final Consumer<CSSToken> returnParsedToken, // T, Vector<CSSToken>.add final Consumer<TokenizeError> errorEncountered, // E, Vector<TokenizeError>.add final boolean fromBuildMethod // Minor-Hack to solve a problems // SOLVING-PROBLEMS, THAT'S WHAT WE DO ) { final IntStream.Builder urlStrBuilder = IntStream.builder(); final int sPos = POS.f; int c; while (POS.f < css.length) switch (c = css[POS.f]) { // ** U+0029 RIGHT PARENTHESIS ()) // Return the <url-token>. case ')': returnParsedToken.accept(new URLToken(css, sPos, POS.f, urlStrBuilder)); return; // ** whitespace // Consume as much whitespace as possible. If the next input code point is // U+0029 RIGHT PARENTHESIS ()) or EOF, consume it and return the <url-token> (if EOF was // encountered, this is a parse error); otherwise, consume the remnants of a bad url, // create a <bad-url-token>, and return it. case '\u000B': case ' ': case '\t': case '\f': case '\n': case '\r': final int ePos = POS.f; Vector<CSSToken> v = new Vector<>(); Consumer<CSSToken> acceptor = v::add; while (POS.f < css.length) if (Whitespace.is(css[POS.f])) Whitespace.consume(css, POS, acceptor); else if (Comment.is(css, POS.f)) Comment.consume(css, POS, acceptor, errorEncountered); else break; // ==> EOF, consume it and return the <url-token> (if EOF was encountered, this is // a parse error) if (POS.f >= css.length) { if (! fromBuildMethod) errorEncountered.accept( new TokenizeError( css, sPos, POS.f, URLToken.class, "CSS-Input EOF was encountered before reaching the URL's closing ')'" )); returnParsedToken.accept(new URLToken(css, sPos, ePos, urlStrBuilder)); if (v.size() > 0) for (CSSToken t : v) returnParsedToken.accept(t); } else if (css[POS.f] == ')') { returnParsedToken.accept(new URLToken(css, sPos, ePos, urlStrBuilder)); if (v.size() > 0) for (CSSToken t : v) returnParsedToken.accept(t); } else { errorEncountered.accept( new TokenizeError( css, sPos, POS.f, URLToken.class, "Whitespace and/or comments before the end of a URL" )); BadURL.consume(css, POS, returnParsedToken, sPos); } return; // ** U+0022 QUOTATION MARK (") // ** U+0027 APOSTROPHE (') // ** U+0028 LEFT PARENTHESIS (() // ** non-printable code point // This is a parse error. Consume the remnants of a bad url, create a // <bad-url-token>, and return it. case '"': case '\'': case '(': errorEncountered.accept( new TokenizeError( css, sPos, POS.f, URLToken.class, "Unescaped Character within URL Found: ['" + c + "'']" )); // NOTE: The "non-printable code-point" will be handled by the default-case BadURL.consume(css, POS, returnParsedToken, sPos); return; // ** U+005C REVERSE SOLIDUS (\) // If the stream starts with a valid escape, consume an escaped code point and // append the returned code point to the <url-token>’s value. // // Otherwise, this is a parse error. Consume the remnants of a bad url, create a // <bad-url-token>, and return it. case '\\': if (CSSUtil.isValidEscape(css, POS.f)) { POS.f = CSSUtil.consumeEscapedUnicode(css, POS.f+1, urlStrBuilder); break; } else { errorEncountered.accept( new TokenizeError( css, sPos, POS.f, URLToken.class, "A Reverse-Solidu (Backslash) Character was encountered, but " + "unfortunately it was not a valid CSS Character-Escape Sequence" )); BadURL.consume(css, POS, returnParsedToken, sPos); return; } // ** non-printable code point // This is a parse error. Consume the remnants of a bad url, create a // <bad-url-token>, and return it. // // ** anything else // Append the current input code point to the <url-token>’s value. default: if (CSSUtil.nonPrintableCodePoint(c)) { errorEncountered.accept( new TokenizeError( css, sPos, POS.f, URLToken.class, "A non-printable Code-Point was Encountered (CodePonit #" + c + ")" )); BadURL.consume(css, POS, returnParsedToken, sPos); return; } POS.f++; urlStrBuilder.accept(c); } // If this line is reached, it means that the loop "broke" because the end of the CSS was // reached. If there had been a proper ending to the URL, it would already have been // returned inside the Loop's Main Switch-Statement // // MINOR-SPAGHETTI: If this "consume" method is called from "build", then there will not // be a closing ')'. If this line is reached, and it was called from the // build-method, this is success, rather than failure // // The value of boolean "fromBuildMethod" is retrieved as a parameter from this method's // input-parameters. URLToken.consume(...) is called from one two places: // 1) Identifer.consumeIdentLikeSequence // 2) Method "build" (at the top of this class) // // When called from // 2) Identifier: fromBuildMethod ==> false // 3) Build (above): fromBuildMethod ==> true if (! fromBuildMethod) errorEncountered.accept( new TokenizeError( css, sPos, POS.f, URLToken.class, "EOF Encountered prior to reaching the end of a URL" )); returnParsedToken.accept(new URLToken(css, sPos, POS.f, urlStrBuilder)); } } |