DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (d8847129d134)

VCS Links

Line Code
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 524 525 526 527 528 529 530 531 532 533 534 535 536 537
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "nsHTMLTags.h"
#include "nsCRT.h"
#include "nsReadableUtils.h"
#include "nsString.h"
#include "nsStaticAtom.h"
#include "nsUnicharUtils.h"
#include "mozilla/HashFunctions.h"
#include <algorithm>

using namespace mozilla;

// C++ sucks! There's no way to do this with a macro, at least not
// that I know, if you know how to do this with a macro then please do
// so...
static const char16_t sHTMLTagUnicodeName_a[] =
  {'a', '\0'};
static const char16_t sHTMLTagUnicodeName_abbr[] =
  {'a', 'b', 'b', 'r', '\0'};
static const char16_t sHTMLTagUnicodeName_acronym[] =
  {'a', 'c', 'r', 'o', 'n', 'y', 'm', '\0'};
static const char16_t sHTMLTagUnicodeName_address[] =
  {'a', 'd', 'd', 'r', 'e', 's', 's', '\0'};
static const char16_t sHTMLTagUnicodeName_applet[] =
  {'a', 'p', 'p', 'l', 'e', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_area[] =
  {'a', 'r', 'e', 'a', '\0'};
static const char16_t sHTMLTagUnicodeName_article[] =
  {'a', 'r', 't', 'i', 'c', 'l', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_aside[] =
  {'a', 's', 'i', 'd', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_audio[] =
  {'a', 'u', 'd', 'i', 'o', '\0'};
static const char16_t sHTMLTagUnicodeName_b[] =
  {'b', '\0'};
static const char16_t sHTMLTagUnicodeName_base[] =
  {'b', 'a', 's', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_basefont[] =
  {'b', 'a', 's', 'e', 'f', 'o', 'n', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_bdo[] =
  {'b', 'd', 'o', '\0'};
static const char16_t sHTMLTagUnicodeName_bgsound[] =
  {'b', 'g', 's', 'o', 'u', 'n', 'd', '\0'};
static const char16_t sHTMLTagUnicodeName_big[] =
  {'b', 'i', 'g', '\0'};
static const char16_t sHTMLTagUnicodeName_blockquote[] =
  {'b', 'l', 'o', 'c', 'k', 'q', 'u', 'o', 't', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_body[] =
  {'b', 'o', 'd', 'y', '\0'};
static const char16_t sHTMLTagUnicodeName_br[] =
  {'b', 'r', '\0'};
static const char16_t sHTMLTagUnicodeName_button[] =
  {'b', 'u', 't', 't', 'o', 'n', '\0'};
static const char16_t sHTMLTagUnicodeName_canvas[] =
  {'c', 'a', 'n', 'v', 'a', 's', '\0'};
static const char16_t sHTMLTagUnicodeName_caption[] =
  {'c', 'a', 'p', 't', 'i', 'o', 'n', '\0'};
static const char16_t sHTMLTagUnicodeName_center[] =
  {'c', 'e', 'n', 't', 'e', 'r', '\0'};
static const char16_t sHTMLTagUnicodeName_cite[] =
  {'c', 'i', 't', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_code[] =
  {'c', 'o', 'd', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_col[] =
  {'c', 'o', 'l', '\0'};
static const char16_t sHTMLTagUnicodeName_colgroup[] =
  {'c', 'o', 'l', 'g', 'r', 'o', 'u', 'p', '\0'};
static const char16_t sHTMLTagUnicodeName_content[] =
  {'c', 'o', 'n', 't', 'e', 'n', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_data[] =
  {'d', 'a', 't', 'a', '\0'};
static const char16_t sHTMLTagUnicodeName_datalist[] =
  {'d', 'a', 't', 'a', 'l', 'i', 's', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_dd[] =
  {'d', 'd', '\0'};
static const char16_t sHTMLTagUnicodeName_del[] =
  {'d', 'e', 'l', '\0'};
static const char16_t sHTMLTagUnicodeName_dfn[] =
  {'d', 'f', 'n', '\0'};
static const char16_t sHTMLTagUnicodeName_dir[] =
  {'d', 'i', 'r', '\0'};
static const char16_t sHTMLTagUnicodeName_div[] =
  {'d', 'i', 'v', '\0'};
static const char16_t sHTMLTagUnicodeName_dl[] =
  {'d', 'l', '\0'};
static const char16_t sHTMLTagUnicodeName_dt[] =
  {'d', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_em[] =
  {'e', 'm', '\0'};
static const char16_t sHTMLTagUnicodeName_embed[] =
  {'e', 'm', 'b', 'e', 'd', '\0'};
static const char16_t sHTMLTagUnicodeName_extapp[] =
  {'e', 'x', 't', 'a', 'p', 'p', '\0'};
static const char16_t sHTMLTagUnicodeName_fieldset[] =
  {'f', 'i', 'e', 'l', 'd', 's', 'e', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_figcaption[] =
  {'f', 'i', 'g', 'c', 'a', 'p', 't', 'i', 'o', 'n', '\0'};
static const char16_t sHTMLTagUnicodeName_figure[] =
  {'f', 'i', 'g', 'u', 'r', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_font[] =
  {'f', 'o', 'n', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_footer[] =
  {'f', 'o', 'o', 't', 'e', 'r', '\0'};
static const char16_t sHTMLTagUnicodeName_form[] =
  {'f', 'o', 'r', 'm', '\0'};
static const char16_t sHTMLTagUnicodeName_frame[] =
  {'f', 'r', 'a', 'm', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_frameset[] =
  {'f', 'r', 'a', 'm', 'e', 's', 'e', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_h1[] =
  {'h', '1', '\0'};
static const char16_t sHTMLTagUnicodeName_h2[] =
  {'h', '2', '\0'};
static const char16_t sHTMLTagUnicodeName_h3[] =
  {'h', '3', '\0'};
static const char16_t sHTMLTagUnicodeName_h4[] =
  {'h', '4', '\0'};
static const char16_t sHTMLTagUnicodeName_h5[] =
  {'h', '5', '\0'};
static const char16_t sHTMLTagUnicodeName_h6[] =
  {'h', '6', '\0'};
static const char16_t sHTMLTagUnicodeName_head[] =
  {'h', 'e', 'a', 'd', '\0'};
static const char16_t sHTMLTagUnicodeName_header[] =
  {'h', 'e', 'a', 'd', 'e', 'r', '\0'};
static const char16_t sHTMLTagUnicodeName_hgroup[] =
  {'h', 'g', 'r', 'o', 'u', 'p', '\0'};
static const char16_t sHTMLTagUnicodeName_hr[] =
  {'h', 'r', '\0'};
static const char16_t sHTMLTagUnicodeName_html[] =
  {'h', 't', 'm', 'l', '\0'};
static const char16_t sHTMLTagUnicodeName_i[] =
  {'i', '\0'};
static const char16_t sHTMLTagUnicodeName_iframe[] =
  {'i', 'f', 'r', 'a', 'm', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_image[] =
  {'i', 'm', 'a', 'g', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_img[] =
  {'i', 'm', 'g', '\0'};
static const char16_t sHTMLTagUnicodeName_input[] =
  {'i', 'n', 'p', 'u', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_ins[] =
  {'i', 'n', 's', '\0'};
static const char16_t sHTMLTagUnicodeName_kbd[] =
  {'k', 'b', 'd', '\0'};
static const char16_t sHTMLTagUnicodeName_keygen[] =
  {'k', 'e', 'y', 'g', 'e', 'n', '\0'};
static const char16_t sHTMLTagUnicodeName_label[] =
  {'l', 'a', 'b', 'e', 'l', '\0'};
static const char16_t sHTMLTagUnicodeName_legend[] =
  {'l', 'e', 'g', 'e', 'n', 'd', '\0'};
static const char16_t sHTMLTagUnicodeName_li[] =
  {'l', 'i', '\0'};
static const char16_t sHTMLTagUnicodeName_link[] =
  {'l', 'i', 'n', 'k', '\0'};
static const char16_t sHTMLTagUnicodeName_listing[] =
  {'l', 'i', 's', 't', 'i', 'n', 'g', '\0'};
static const char16_t sHTMLTagUnicodeName_main[] =
  {'m', 'a', 'i', 'n', '\0'};
static const char16_t sHTMLTagUnicodeName_map[] =
  {'m', 'a', 'p', '\0'};
static const char16_t sHTMLTagUnicodeName_mark[] =
  {'m', 'a', 'r', 'k', '\0'};
static const char16_t sHTMLTagUnicodeName_marquee[] =
  {'m', 'a', 'r', 'q', 'u', 'e', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_menu[] =
  {'m', 'e', 'n', 'u', '\0'};
static const char16_t sHTMLTagUnicodeName_menuitem[] =
  {'m', 'e', 'n', 'u', 'i', 't', 'e', 'm', '\0'};
static const char16_t sHTMLTagUnicodeName_meta[] =
  {'m', 'e', 't', 'a', '\0'};
static const char16_t sHTMLTagUnicodeName_meter[] =
  {'m', 'e', 't', 'e', 'r', '\0'};
static const char16_t sHTMLTagUnicodeName_multicol[] =
  {'m', 'u', 'l', 't', 'i', 'c', 'o', 'l', '\0'};
static const char16_t sHTMLTagUnicodeName_nav[] =
  {'n', 'a', 'v', '\0'};
static const char16_t sHTMLTagUnicodeName_nobr[] =
  {'n', 'o', 'b', 'r', '\0'};
static const char16_t sHTMLTagUnicodeName_noembed[] =
  {'n', 'o', 'e', 'm', 'b', 'e', 'd', '\0'};
static const char16_t sHTMLTagUnicodeName_noframes[] =
  {'n', 'o', 'f', 'r', 'a', 'm', 'e', 's', '\0'};
static const char16_t sHTMLTagUnicodeName_noscript[] =
  {'n', 'o', 's', 'c', 'r', 'i', 'p', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_object[] =
  {'o', 'b', 'j', 'e', 'c', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_ol[] =
  {'o', 'l', '\0'};
static const char16_t sHTMLTagUnicodeName_optgroup[] =
  {'o', 'p', 't', 'g', 'r', 'o', 'u', 'p', '\0'};
static const char16_t sHTMLTagUnicodeName_option[] =
  {'o', 'p', 't', 'i', 'o', 'n', '\0'};
static const char16_t sHTMLTagUnicodeName_output[] =
  {'o', 'u', 't', 'p', 'u', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_p[] =
  {'p', '\0'};
static const char16_t sHTMLTagUnicodeName_param[] =
  {'p', 'a', 'r', 'a', 'm', '\0'};
static const char16_t sHTMLTagUnicodeName_picture[] =
  {'p', 'i', 'c', 't', 'u', 'r', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_plaintext[] =
  {'p', 'l', 'a', 'i', 'n', 't', 'e', 'x', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_pre[] =
  {'p', 'r', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_progress[] =
  {'p', 'r', 'o', 'g', 'r', 'e', 's', 's', '\0'};
static const char16_t sHTMLTagUnicodeName_q[] =
  {'q', '\0'};
static const char16_t sHTMLTagUnicodeName_rb[] =
  {'r', 'b', '\0'};
static const char16_t sHTMLTagUnicodeName_rp[] =
  {'r', 'p', '\0'};
static const char16_t sHTMLTagUnicodeName_rt[] =
  {'r', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_rtc[] =
  {'r', 't', 'c', '\0'};
static const char16_t sHTMLTagUnicodeName_ruby[] =
  {'r', 'u', 'b', 'y', '\0'};
static const char16_t sHTMLTagUnicodeName_s[] =
  {'s', '\0'};
static const char16_t sHTMLTagUnicodeName_samp[] =
  {'s', 'a', 'm', 'p', '\0'};
static const char16_t sHTMLTagUnicodeName_script[] =
  {'s', 'c', 'r', 'i', 'p', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_section[] =
  {'s', 'e', 'c', 't', 'i', 'o', 'n', '\0'};
static const char16_t sHTMLTagUnicodeName_select[] =
  {'s', 'e', 'l', 'e', 'c', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_shadow[] =
  {'s', 'h', 'a', 'd', 'o', 'w', '\0'};
static const char16_t sHTMLTagUnicodeName_small[] =
  {'s', 'm', 'a', 'l', 'l', '\0'};
static const char16_t sHTMLTagUnicodeName_source[] =
  {'s', 'o', 'u', 'r', 'c', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_span[] =
  {'s', 'p', 'a', 'n', '\0'};
static const char16_t sHTMLTagUnicodeName_strike[] =
  {'s', 't', 'r', 'i', 'k', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_strong[] =
  {'s', 't', 'r', 'o', 'n', 'g', '\0'};
static const char16_t sHTMLTagUnicodeName_style[] =
  {'s', 't', 'y', 'l', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_sub[] =
  {'s', 'u', 'b', '\0'};
static const char16_t sHTMLTagUnicodeName_sup[] =
  {'s', 'u', 'p', '\0'};
static const char16_t sHTMLTagUnicodeName_table[] =
  {'t', 'a', 'b', 'l', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_tbody[] =
  {'t', 'b', 'o', 'd', 'y', '\0'};
static const char16_t sHTMLTagUnicodeName_td[] =
  {'t', 'd', '\0'};
static const char16_t sHTMLTagUnicodeName_textarea[] =
  {'t', 'e', 'x', 't', 'a', 'r', 'e', 'a', '\0'};
static const char16_t sHTMLTagUnicodeName_tfoot[] =
  {'t', 'f', 'o', 'o', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_th[] =
  {'t', 'h', '\0'};
static const char16_t sHTMLTagUnicodeName_thead[] =
  {'t', 'h', 'e', 'a', 'd', '\0'};
static const char16_t sHTMLTagUnicodeName_template[] =
  {'t', 'e', 'm', 'p', 'l', 'a', 't', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_time[] =
  {'t', 'i', 'm', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_title[] =
  {'t', 'i', 't', 'l', 'e', '\0'};
static const char16_t sHTMLTagUnicodeName_tr[] =
  {'t', 'r', '\0'};
static const char16_t sHTMLTagUnicodeName_track[] =
  {'t', 'r', 'a', 'c', 'k', '\0'};
static const char16_t sHTMLTagUnicodeName_tt[] =
  {'t', 't', '\0'};
static const char16_t sHTMLTagUnicodeName_u[] =
  {'u', '\0'};
static const char16_t sHTMLTagUnicodeName_ul[] =
  {'u', 'l', '\0'};
static const char16_t sHTMLTagUnicodeName_var[] =
  {'v', 'a', 'r', '\0'};
static const char16_t sHTMLTagUnicodeName_video[] =
  {'v', 'i', 'd', 'e', 'o', '\0'};
static const char16_t sHTMLTagUnicodeName_wbr[] =
  {'w', 'b', 'r', '\0'};
static const char16_t sHTMLTagUnicodeName_xmp[] =
  {'x', 'm', 'p', '\0'};

// static array of unicode tag names
#define HTML_TAG(_tag, _classname) sHTMLTagUnicodeName_##_tag,
#define HTML_HTMLELEMENT_TAG(_tag) sHTMLTagUnicodeName_##_tag,
#define HTML_OTHER(_tag)
const char16_t* const nsHTMLTags::sTagUnicodeTable[] = {
#include "nsHTMLTagList.h"
};
#undef HTML_TAG
#undef HTML_HTMLELEMENT_TAG
#undef HTML_OTHER

// static array of tag atoms
nsIAtom* nsHTMLTags::sTagAtomTable[eHTMLTag_userdefined - 1];

int32_t nsHTMLTags::gTableRefCount;
PLHashTable* nsHTMLTags::gTagTable;
PLHashTable* nsHTMLTags::gTagAtomTable;


// char16_t* -> id hash
static PLHashNumber
HTMLTagsHashCodeUCPtr(const void *key)
{
  return HashString(static_cast<const char16_t*>(key));
}

static int
HTMLTagsKeyCompareUCPtr(const void *key1, const void *key2)
{
  const char16_t *str1 = (const char16_t *)key1;
  const char16_t *str2 = (const char16_t *)key2;

  return nsCRT::strcmp(str1, str2) == 0;
}

// nsIAtom* -> id hash
static PLHashNumber
HTMLTagsHashCodeAtom(const void *key)
{
  return NS_PTR_TO_INT32(key) >> 2;
}

#define NS_HTMLTAG_NAME_MAX_LENGTH 10

#define HTML_TAG(_tag, _classname) NS_STATIC_ATOM_BUFFER(Atombuffer_##_tag, #_tag)
#define HTML_HTMLELEMENT_TAG(_tag) NS_STATIC_ATOM_BUFFER(Atombuffer_##_tag, #_tag)
#define HTML_OTHER(_tag)
#include "nsHTMLTagList.h"
#undef HTML_TAG
#undef HTML_HTMLELEMENT_TAG
#undef HTML_OTHER


// static
nsresult
nsHTMLTags::AddRefTable(void)
{
  // static array of tag StaticAtom structs
#define HTML_TAG(_tag, _classname) NS_STATIC_ATOM(Atombuffer_##_tag, &nsHTMLTags::sTagAtomTable[eHTMLTag_##_tag - 1]),
#define HTML_HTMLELEMENT_TAG(_tag) NS_STATIC_ATOM(Atombuffer_##_tag, &nsHTMLTags::sTagAtomTable[eHTMLTag_##_tag - 1]),
#define HTML_OTHER(_tag)
  static const nsStaticAtom sTagAtoms_info[] = {
#include "nsHTMLTagList.h"
  };
#undef HTML_TAG
#undef HTML_HTMLELEMENT_TAG
#undef HTML_OTHER

  if (gTableRefCount++ == 0) {
    // Fill in our static atom pointers
    NS_RegisterStaticAtoms(sTagAtoms_info);


    NS_ASSERTION(!gTagTable && !gTagAtomTable, "pre existing hash!");

    gTagTable = PL_NewHashTable(64, HTMLTagsHashCodeUCPtr,
                                HTMLTagsKeyCompareUCPtr, PL_CompareValues,
                                nullptr, nullptr);
    NS_ENSURE_TRUE(gTagTable, NS_ERROR_OUT_OF_MEMORY);

    gTagAtomTable = PL_NewHashTable(64, HTMLTagsHashCodeAtom,
                                    PL_CompareValues, PL_CompareValues,
                                    nullptr, nullptr);
    NS_ENSURE_TRUE(gTagAtomTable, NS_ERROR_OUT_OF_MEMORY);

    // Fill in gTagTable with the above static char16_t strings as
    // keys and the value of the corresponding enum as the value in
    // the table.

    int32_t i;
    for (i = 0; i < NS_HTML_TAG_MAX; ++i) {
      PL_HashTableAdd(gTagTable, sTagUnicodeTable[i],
                      NS_INT32_TO_PTR(i + 1));

      PL_HashTableAdd(gTagAtomTable, sTagAtomTable[i],
                      NS_INT32_TO_PTR(i + 1));
    }



#if defined(DEBUG)
    {
      // let's verify that all names in the the table are lowercase...
      for (i = 0; i < NS_HTML_TAG_MAX; ++i) {
        nsAutoString temp1((char16_t*)sTagAtoms_info[i].mStringBuffer->Data());
        nsAutoString temp2((char16_t*)sTagAtoms_info[i].mStringBuffer->Data());
        ToLowerCase(temp1);
        NS_ASSERTION(temp1.Equals(temp2), "upper case char in table");
      }

      // let's verify that all names in the unicode strings above are
      // correct.
      for (i = 0; i < NS_HTML_TAG_MAX; ++i) {
        nsAutoString temp1(sTagUnicodeTable[i]);
        nsAutoString temp2((char16_t*)sTagAtoms_info[i].mStringBuffer->Data());
        NS_ASSERTION(temp1.Equals(temp2), "Bad unicode tag name!");
      }

      // let's verify that NS_HTMLTAG_NAME_MAX_LENGTH is correct
      uint32_t maxTagNameLength = 0;
      for (i = 0; i < NS_HTML_TAG_MAX; ++i) {
        uint32_t len = NS_strlen(sTagUnicodeTable[i]);
        maxTagNameLength = std::max(len, maxTagNameLength);        
      }
      NS_ASSERTION(maxTagNameLength == NS_HTMLTAG_NAME_MAX_LENGTH,
                   "NS_HTMLTAG_NAME_MAX_LENGTH not set correctly!");
    }
#endif
  }

  return NS_OK;
}

// static
void
nsHTMLTags::ReleaseTable(void)
{
  if (0 == --gTableRefCount) {
    if (gTagTable) {
      // Nothing to delete/free in this table, just destroy the table.

      PL_HashTableDestroy(gTagTable);
      PL_HashTableDestroy(gTagAtomTable);
      gTagTable = nullptr;
      gTagAtomTable = nullptr;
    }
  }
}

// static
nsHTMLTag
nsHTMLTags::LookupTag(const nsAString& aTagName)
{
  uint32_t length = aTagName.Length();

  if (length > NS_HTMLTAG_NAME_MAX_LENGTH) {
    return eHTMLTag_userdefined;
  }

  char16_t buf[NS_HTMLTAG_NAME_MAX_LENGTH + 1];

  nsAString::const_iterator iter;
  uint32_t i = 0;
  char16_t c;

  aTagName.BeginReading(iter);

  // Fast lowercasing-while-copying of ASCII characters into a
  // char16_t buffer

  while (i < length) {
    c = *iter;

    if (c <= 'Z' && c >= 'A') {
      c |= 0x20; // Lowercase the ASCII character.
    }

    buf[i] = c; // Copy ASCII character.

    ++i;
    ++iter;
  }

  buf[i] = 0;

  return CaseSensitiveLookupTag(buf);
}

#ifdef DEBUG
void
nsHTMLTags::TestTagTable()
{
     const char16_t *tag;
     nsHTMLTag id;
     nsCOMPtr<nsIAtom> atom;

     nsHTMLTags::AddRefTable();
     // Make sure we can find everything we are supposed to
     for (int i = 0; i < NS_HTML_TAG_MAX; ++i) {
       tag = sTagUnicodeTable[i];
       id = LookupTag(nsDependentString(tag));
       NS_ASSERTION(id != eHTMLTag_userdefined, "can't find tag id");
       const char16_t* check = GetStringValue(id);
       NS_ASSERTION(0 == nsCRT::strcmp(check, tag), "can't map id back to tag");

       nsAutoString uname(tag);
       ToUpperCase(uname);
       NS_ASSERTION(id == LookupTag(uname), "wrong id");

       NS_ASSERTION(id == CaseSensitiveLookupTag(tag), "wrong id");

       atom = do_GetAtom(tag);
       NS_ASSERTION(id == CaseSensitiveLookupTag(atom), "wrong id");
       NS_ASSERTION(atom == GetAtom(id), "can't map id back to atom");
     }

     // Make sure we don't find things that aren't there
     id = LookupTag(NS_LITERAL_STRING("@"));
     NS_ASSERTION(id == eHTMLTag_userdefined, "found @");
     id = LookupTag(NS_LITERAL_STRING("zzzzz"));
     NS_ASSERTION(id == eHTMLTag_userdefined, "found zzzzz");

     atom = do_GetAtom("@");
     id = CaseSensitiveLookupTag(atom);
     NS_ASSERTION(id == eHTMLTag_userdefined, "found @");
     atom = do_GetAtom("zzzzz");
     id = CaseSensitiveLookupTag(atom);
     NS_ASSERTION(id == eHTMLTag_userdefined, "found zzzzz");

     tag = GetStringValue((nsHTMLTag) 0);
     NS_ASSERTION(!tag, "found enum 0");
     tag = GetStringValue((nsHTMLTag) -1);
     NS_ASSERTION(!tag, "found enum -1");
     tag = GetStringValue((nsHTMLTag) (NS_HTML_TAG_MAX + 1));
     NS_ASSERTION(!tag, "found past max enum");

     atom = GetAtom((nsHTMLTag) 0);
     NS_ASSERTION(!atom, "found enum 0");
     atom = GetAtom((nsHTMLTag) -1);
     NS_ASSERTION(!atom, "found enum -1");
     atom = GetAtom((nsHTMLTag) (NS_HTML_TAG_MAX + 1));
     NS_ASSERTION(!atom, "found past max enum");

     ReleaseTable();
}

#endif // DEBUG