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 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
|
/*
* Copyright (C) 2011 Nokia Inc. All rights reserved.
* Copyright (C) 2012 Google Inc. All rights reserved.
* Copyright (C) 2013, 2017 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "RenderQuote.h"
#include "QuotesData.h"
#include "RenderBoxModelObjectInlines.h"
#include "RenderTextFragment.h"
#include "RenderTreeBuilder.h"
#include "RenderView.h"
#include <array>
#include <wtf/StdLibExtras.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/unicode/CharacterNames.h>
namespace WebCore {
using namespace WTF::Unicode;
WTF_MAKE_TZONE_OR_ISO_ALLOCATED_IMPL(RenderQuote);
// These strings need to be compared according to "Extended Filtering", as in Section 3.3.2 in RFC4647.
// https://tools.ietf.org/html/rfc4647#page-10
//
// The "checkFurther" field is needed in one specific situation.
// In the quoteTable below, there are lines like:
// { "de"_span , 0x201e, 0x201c, 0x201a, 0x2018 },
// { "de-ch"_span, 0x00ab, 0x00bb, 0x2039, 0x203a },
// Let's say the binary search arbitrarily decided to test our key against the upper line "de" first.
// If the key we're testing against is "de-ch", then we should report "greater than",
// so the binary search will keep searching and eventually find the "de-ch" line.
// However, if the key we're testing against is "de-de", then we should report "equal to",
// because these are the quotes we should use for all "de" except for "de-ch".
struct QuotesForLanguage {
std::span<const char> language;
uint8_t checkFurther { 0 };
UChar open1 { 0 };
UChar close1 { 0 };
UChar open2 { 0 };
UChar close2 { 0 };
};
// Table of quotes from http://www.whatwg.org/specs/web-apps/current-work/multipage/rendering.html#quotes
// FIXME: This table is out-of-date.
static constexpr std::array quoteTable {
QuotesForLanguage { "af"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "agq"_span, 0, 0x201e, 0x201d, 0x201a, 0x2019 },
QuotesForLanguage { "ak"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "am"_span, 0, 0x00ab, 0x00bb, 0x2039, 0x203a },
QuotesForLanguage { "ar"_span, 0, 0x201d, 0x201c, 0x2019, 0x2018 },
QuotesForLanguage { "asa"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "az-cyrl"_span, 0, 0x00ab, 0x00bb, 0x2039, 0x203a },
QuotesForLanguage { "bas"_span, 0, 0x00ab, 0x00bb, 0x201e, 0x201c },
QuotesForLanguage { "bem"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "bez"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "bg"_span, 0, 0x201e, 0x201c, 0x201a, 0x2018 },
QuotesForLanguage { "bm"_span, 0, 0x00ab, 0x00bb, 0x201c, 0x201d },
QuotesForLanguage { "bn"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "br"_span, 0, 0x00ab, 0x00bb, 0x2039, 0x203a },
QuotesForLanguage { "brx"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "bs-cyrl"_span, 0, 0x201e, 0x201c, 0x201a, 0x2018 },
QuotesForLanguage { "ca"_span, 0, 0x201c, 0x201d, 0x00ab, 0x00bb },
QuotesForLanguage { "cgg"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "chr"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "cs"_span, 0, 0x201e, 0x201c, 0x201a, 0x2018 },
QuotesForLanguage { "da"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "dav"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "de"_span, 1, 0x201e, 0x201c, 0x201a, 0x2018 },
QuotesForLanguage { "de-ch"_span, 0, 0x00ab, 0x00bb, 0x2039, 0x203a },
QuotesForLanguage { "dje"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "dua"_span, 0, 0x00ab, 0x00bb, 0x2018, 0x2019 },
QuotesForLanguage { "dyo"_span, 0, 0x00ab, 0x00bb, 0x201c, 0x201d },
QuotesForLanguage { "dz"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "ebu"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "ee"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "el"_span, 0, 0x00ab, 0x00bb, 0x201c, 0x201d },
QuotesForLanguage { "en"_span, 1, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "en-gb"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "es"_span, 0, 0x201c, 0x201d, 0x00ab, 0x00bb },
QuotesForLanguage { "et"_span, 0, 0x201e, 0x201c, 0x201a, 0x2018 },
QuotesForLanguage { "eu"_span, 0, 0x201c, 0x201d, 0x00ab, 0x00bb },
QuotesForLanguage { "ewo"_span, 0, 0x00ab, 0x00bb, 0x201c, 0x201d },
QuotesForLanguage { "fa"_span, 0, 0x00ab, 0x00bb, 0x2039, 0x203a },
QuotesForLanguage { "ff"_span, 0, 0x201e, 0x201d, 0x201a, 0x2019 },
QuotesForLanguage { "fi"_span, 0, 0x201d, 0x201d, 0x2019, 0x2019 },
QuotesForLanguage { "fr"_span, 2, 0x00ab, 0x00bb, 0x00ab, 0x00bb },
QuotesForLanguage { "fr-ca"_span, 0, 0x00ab, 0x00bb, 0x2039, 0x203a },
QuotesForLanguage { "fr-ch"_span, 0, 0x00ab, 0x00bb, 0x2039, 0x203a },
QuotesForLanguage { "gsw"_span, 0, 0x00ab, 0x00bb, 0x2039, 0x203a },
QuotesForLanguage { "gu"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "guz"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "ha"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "he"_span, 0, 0x0022, 0x0022, 0x0027, 0x0027 },
QuotesForLanguage { "hi"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "hr"_span, 0, 0x201e, 0x201c, 0x201a, 0x2018 },
QuotesForLanguage { "hu"_span, 0, 0x201e, 0x201d, 0x00bb, 0x00ab },
QuotesForLanguage { "id"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "ig"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "it"_span, 0, 0x00ab, 0x00bb, 0x201c, 0x201d },
QuotesForLanguage { "ja"_span, 0, 0x300c, 0x300d, 0x300e, 0x300f },
QuotesForLanguage { "jgo"_span, 0, 0x00ab, 0x00bb, 0x2039, 0x203a },
QuotesForLanguage { "jmc"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "kab"_span, 0, 0x00ab, 0x00bb, 0x201c, 0x201d },
QuotesForLanguage { "kam"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "kde"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "kea"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "khq"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "ki"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "kkj"_span, 0, 0x00ab, 0x00bb, 0x2039, 0x203a },
QuotesForLanguage { "kln"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "km"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "kn"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "ko"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "ksb"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "ksf"_span, 0, 0x00ab, 0x00bb, 0x2018, 0x2019 },
QuotesForLanguage { "lag"_span, 0, 0x201d, 0x201d, 0x2019, 0x2019 },
QuotesForLanguage { "lg"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "ln"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "lo"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "lt"_span, 0, 0x201e, 0x201c, 0x201e, 0x201c },
QuotesForLanguage { "lu"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "luo"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "luy"_span, 0, 0x201e, 0x201c, 0x201a, 0x2018 },
QuotesForLanguage { "lv"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "mas"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "mer"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "mfe"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "mg"_span, 0, 0x00ab, 0x00bb, 0x201c, 0x201d },
QuotesForLanguage { "mgo"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "mk"_span, 0, 0x201e, 0x201c, 0x201a, 0x2018 },
QuotesForLanguage { "ml"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "mr"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "ms"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "mua"_span, 0, 0x00ab, 0x00bb, 0x201c, 0x201d },
QuotesForLanguage { "my"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "naq"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "nb"_span, 0, 0x00ab, 0x00bb, 0x2018, 0x2019 },
QuotesForLanguage { "nd"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "nl"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "nmg"_span, 0, 0x201e, 0x201d, 0x00ab, 0x00bb },
QuotesForLanguage { "nn"_span, 0, 0x00ab, 0x00bb, 0x2018, 0x2019 },
QuotesForLanguage { "nnh"_span, 0, 0x00ab, 0x00bb, 0x201c, 0x201d },
QuotesForLanguage { "nus"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "nyn"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "pl"_span, 0, 0x201e, 0x201d, 0x00ab, 0x00bb },
QuotesForLanguage { "pt"_span, 1, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "pt-pt"_span, 0, 0x00ab, 0x00bb, 0x201c, 0x201d },
QuotesForLanguage { "rn"_span, 0, 0x201d, 0x201d, 0x2019, 0x2019 },
QuotesForLanguage { "ro"_span, 0, 0x201e, 0x201d, 0x00ab, 0x00bb },
QuotesForLanguage { "rof"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "ru"_span, 0, 0x00ab, 0x00bb, 0x201e, 0x201c },
QuotesForLanguage { "rw"_span, 0, 0x00ab, 0x00bb, 0x2018, 0x2019 },
QuotesForLanguage { "rwk"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "saq"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "sbp"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "seh"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "ses"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "sg"_span, 0, 0x00ab, 0x00bb, 0x201c, 0x201d },
QuotesForLanguage { "shi"_span, 1, 0x00ab, 0x00bb, 0x201e, 0x201d },
QuotesForLanguage { "shi-tfng"_span, 0, 0x00ab, 0x00bb, 0x201e, 0x201d },
QuotesForLanguage { "si"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "sk"_span, 0, 0x201e, 0x201c, 0x201a, 0x2018 },
QuotesForLanguage { "sl"_span, 0, 0x201e, 0x201c, 0x201a, 0x2018 },
QuotesForLanguage { "sn"_span, 0, 0x201d, 0x201d, 0x2019, 0x2019 },
QuotesForLanguage { "so"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "sq"_span, 0, 0x201e, 0x201c, 0x201a, 0x2018 },
QuotesForLanguage { "sr"_span, 1, 0x201e, 0x201c, 0x201a, 0x2018 },
QuotesForLanguage { "sr-latn"_span, 0, 0x201e, 0x201c, 0x201a, 0x2018 },
QuotesForLanguage { "sv"_span, 0, 0x201d, 0x201d, 0x2019, 0x2019 },
QuotesForLanguage { "sw"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "swc"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "ta"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "te"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "teo"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "th"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "ti-er"_span, 0, 0x2018, 0x2019, 0x201c, 0x201d },
QuotesForLanguage { "to"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "tr"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "twq"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "tzm"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "uk"_span, 0, 0x00ab, 0x00bb, 0x201e, 0x201c },
QuotesForLanguage { "ur"_span, 0, 0x201d, 0x201c, 0x2019, 0x2018 },
QuotesForLanguage { "vai"_span, 1, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "vai-latn"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "vi"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "vun"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "xh"_span, 0, 0x2018, 0x2019, 0x201c, 0x201d },
QuotesForLanguage { "xog"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "yav"_span, 0, 0x00ab, 0x00bb, 0x00ab, 0x00bb },
QuotesForLanguage { "yo"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "zh"_span, 1, 0x201c, 0x201d, 0x2018, 0x2019 },
QuotesForLanguage { "zh-hant"_span, 0, 0x300c, 0x300d, 0x300e, 0x300f },
QuotesForLanguage { "zu"_span, 0, 0x201c, 0x201d, 0x2018, 0x2019 },
};
RenderQuote::RenderQuote(Document& document, RenderStyle&& style, QuoteType quote)
: RenderInline(Type::Quote, document, WTFMove(style))
, m_type(quote)
, m_text(emptyString())
{
ASSERT(isRenderQuote());
}
// Do not add any code in below destructor. Add it to willBeDestroyed() instead.
RenderQuote::~RenderQuote() = default;
void RenderQuote::insertedIntoTree()
{
RenderInline::insertedIntoTree();
view().setHasQuotesNeedingUpdate(true);
}
void RenderQuote::willBeRemovedFromTree()
{
view().setHasQuotesNeedingUpdate(true);
RenderInline::willBeRemovedFromTree();
}
void RenderQuote::styleDidChange(StyleDifference diff, const RenderStyle* oldStyle)
{
RenderInline::styleDidChange(diff, oldStyle);
if (diff >= StyleDifference::Layout) {
m_needsTextUpdate = true;
view().setHasQuotesNeedingUpdate(true);
}
}
constexpr unsigned maxDistinctQuoteCharacters = 16;
#if ASSERT_ENABLED
static void checkNumberOfDistinctQuoteCharacters(UChar character)
{
ASSERT(character);
static std::array<UChar, maxDistinctQuoteCharacters> distinctQuoteCharacters;
for (unsigned i = 0; i < maxDistinctQuoteCharacters; ++i) {
if (distinctQuoteCharacters[i] == character)
return;
if (!distinctQuoteCharacters[i]) {
distinctQuoteCharacters[i] = character;
return;
}
}
ASSERT_NOT_REACHED();
}
#endif // ASSERT_ENABLED
struct SubtagComparison {
size_t keyLength;
size_t keyContinue;
size_t rangeLength;
size_t rangeContinue;
int comparison;
};
static SubtagComparison subtagCompare(std::span<const char> key, std::span<const char> range)
{
SubtagComparison result;
result.keyLength = key.size();
result.keyContinue = result.keyLength;
if (size_t hyphenIndex = find(key, '-'); hyphenIndex != notFound) {
result.keyLength = hyphenIndex;
result.keyContinue = result.keyLength + 1;
}
result.rangeLength = range.size();
result.rangeContinue = result.rangeLength;
if (size_t hyphenIndex = find(range, '-'); hyphenIndex != notFound) {
result.rangeLength = hyphenIndex;
result.rangeContinue = result.rangeLength + 1;
}
if (result.keyLength == result.rangeLength)
result.comparison = compareSpans(key.first(result.keyLength), range.first(result.keyLength));
else
result.comparison = compareSpans(key, range);
return result;
}
static int quoteTableLanguageComparisonFunction(const QuotesForLanguage& key, std::span<const QuotesForLanguage> range)
{
// These strings need to be compared according to "Extended Filtering", as in Section 3.3.2 in RFC4647.
// https://tools.ietf.org/html/rfc4647#page-10
//
// We can exploit a few things here to improve perf:
// 1. The first subtag must be matched exactly
// 2. All the ranges have either 1 or 2 subtags
// 3. None of the subtags in any of the ranges are wildcards
//
// Also, see the comment just above the QuotesForLanguage struct.
auto firstSubtagComparison = subtagCompare(key.language, range[0].language);
if (firstSubtagComparison.keyLength != firstSubtagComparison.rangeLength)
return firstSubtagComparison.comparison;
if (firstSubtagComparison.comparison)
return firstSubtagComparison.comparison;
for (auto& checkFurtherRange : range.subspan(1)) {
if (!quoteTableLanguageComparisonFunction(key, singleElementSpan(checkFurtherRange))) {
// Tell the binary search to check later in the array of ranges, to eventually find the match we just found here.
return 1;
}
}
for (size_t keyOffset = firstSubtagComparison.keyContinue; ;) {
auto nextSubtagComparison = subtagCompare(key.language.subspan(keyOffset), range[0].language.subspan(firstSubtagComparison.rangeContinue));
if (!nextSubtagComparison.rangeLength) {
// E.g. The key is "zh-Hans" and the range is "zh".
return 0;
}
if (!nextSubtagComparison.keyLength) {
// E.g. the key is "zh" and the range is "zh-Hant".
return nextSubtagComparison.comparison;
}
if (nextSubtagComparison.keyLength == 1) {
// E.g. the key is "zh-x-Hant" and the range is "zh-Hant".
// We want to try to find the range "zh", so tell the binary search to check earlier in the array of ranges.
return -1;
}
if (nextSubtagComparison.keyLength == nextSubtagComparison.rangeLength && !nextSubtagComparison.comparison) {
// E.g. the key is "de-Latn-ch" and the range is "de-ch".
return 0;
}
keyOffset += nextSubtagComparison.keyContinue;
}
}
static const QuotesForLanguage* binaryFindQuotes(const QuotesForLanguage& key, std::span<const QuotesForLanguage> subrange = quoteTable)
{
if (subrange.empty())
return nullptr;
auto& middle = subrange[subrange.size() / 2];
int comparison = quoteTableLanguageComparisonFunction(key, std::span { quoteTable }.subspan(&middle - quoteTable.data(), 1 + middle.checkFurther));
if (!comparison)
return &middle;
if (comparison < 0)
return binaryFindQuotes(key, subrange.first(subrange.size() / 2));
return binaryFindQuotes(key, subrange.subspan(subrange.size() / 2 + 1));
}
static const QuotesForLanguage* quotesForLanguage(const String& language)
{
#if ASSERT_ENABLED
// One time check that the table meets the constraints that the code below relies on.
static bool didOneTimeCheck = false;
if (!didOneTimeCheck) {
didOneTimeCheck = true;
checkNumberOfDistinctQuoteCharacters(quotationMark);
checkNumberOfDistinctQuoteCharacters(apostrophe);
for (unsigned i = 0; i < std::size(quoteTable); ++i) {
if (i)
ASSERT(compareSpans(quoteTable[i - 1].language, quoteTable[i].language) < 0);
for (auto character : quoteTable[i].language)
ASSERT(isASCIILower(character) || character == '-');
checkNumberOfDistinctQuoteCharacters(quoteTable[i].open1);
checkNumberOfDistinctQuoteCharacters(quoteTable[i].close1);
checkNumberOfDistinctQuoteCharacters(quoteTable[i].open2);
checkNumberOfDistinctQuoteCharacters(quoteTable[i].close2);
}
}
#endif // ASSERT_ENABLED
unsigned length = language.length();
if (!length)
return nullptr;
Vector<char> languageKeyBuffer(length);
for (unsigned i = 0; i < length; ++i) {
UChar character = toASCIILower(language[i]);
if (!(isASCIILower(character) || character == '-'))
return nullptr;
languageKeyBuffer[i] = static_cast<char>(character);
}
return binaryFindQuotes({ languageKeyBuffer.span() });
}
static StringImpl* stringForQuoteCharacter(UChar character)
{
// Use linear search because there is a small number of distinct characters, thus binary search is unneeded.
ASSERT(character);
struct StringForCharacter {
UChar character;
StringImpl* string;
};
static std::array<StringForCharacter, maxDistinctQuoteCharacters> strings;
for (auto& string : strings) {
if (string.character == character)
return string.string;
if (!string.character) {
string.character = character;
string.string = &StringImpl::create8BitIfPossible(span(character)).leakRef();
return string.string;
}
}
ASSERT_NOT_REACHED();
return StringImpl::empty();
}
static inline StringImpl* quotationMarkString()
{
static StringImpl* quotationMarkString = stringForQuoteCharacter(quotationMark);
return quotationMarkString;
}
static inline StringImpl* apostropheString()
{
static StringImpl* apostropheString = stringForQuoteCharacter(apostrophe);
return apostropheString;
}
void RenderQuote::updateTextRenderer(RenderTreeBuilder& builder)
{
ASSERT_WITH_SECURITY_IMPLICATION(document().inRenderTreeUpdate());
String text = computeText();
if (m_text == text)
return;
m_text = text;
if (auto* renderText = dynamicDowncast<RenderTextFragment>(lastChild())) {
renderText->setContentString(m_text);
return;
}
builder.attach(*this, createRenderer<RenderTextFragment>(document(), m_text));
}
String RenderQuote::computeText() const
{
if (m_depth < 0)
return emptyString();
bool isOpenQuote = false;
switch (m_type) {
case QuoteType::NoOpenQuote:
case QuoteType::NoCloseQuote:
return emptyString();
case QuoteType::OpenQuote:
isOpenQuote = true;
FALLTHROUGH;
case QuoteType::CloseQuote:
if (const auto* quotes = style().quotes())
return isOpenQuote ? quotes->openQuote(m_depth).impl() : quotes->closeQuote(m_depth).impl();
if (const auto* quotes = quotesForLanguage(style().computedLocale()))
return stringForQuoteCharacter(isOpenQuote ? (m_depth ? quotes->open2 : quotes->open1) : (m_depth ? quotes->close2 : quotes->close1));
// FIXME: Should the default be the quotes for "en" rather than straight quotes?
// (According to https://html.spec.whatwg.org/multipage/rendering.html#quotes, the answer is "yes".)
return m_depth ? apostropheString() : quotationMarkString();
}
ASSERT_NOT_REACHED();
return emptyString();
}
bool RenderQuote::isOpen() const
{
switch (m_type) {
case QuoteType::OpenQuote:
case QuoteType::NoOpenQuote:
return true;
case QuoteType::CloseQuote:
case QuoteType::NoCloseQuote:
return false;
}
ASSERT_NOT_REACHED();
return false;
}
void RenderQuote::updateRenderer(RenderTreeBuilder& builder, RenderQuote* previousQuote)
{
ASSERT_WITH_SECURITY_IMPLICATION(document().inRenderTreeUpdate());
int depth = -1;
if (previousQuote) {
depth = previousQuote->m_depth;
if (previousQuote->isOpen())
++depth;
}
if (!isOpen())
--depth;
else if (depth < 0)
depth = 0;
if (m_depth == depth && !m_needsTextUpdate)
return;
m_depth = depth;
m_needsTextUpdate = false;
updateTextRenderer(builder);
}
} // namespace WebCore
|