summaryrefslogtreecommitdiffstats
path: root/parser/html/nsHtml5Portability.cpp
blob: 613f4e5305b20642dc86dd011f44eafb8839308a (plain)
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
/* 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 "nsIAtom.h"
#include "nsString.h"
#include "jArray.h"
#include "nsHtml5Portability.h"
#include "nsHtml5TreeBuilder.h"
#include "mozilla/CheckedInt.h"

int32_t nsHtml5Portability::checkedAdd(int32_t a, int32_t b) {
  mozilla::CheckedInt<int32_t> sum(a);
  sum += b;
  MOZ_RELEASE_ASSERT(sum.isValid(),
                     "HTML input too large for signed 32-bit integer.");
  return sum.value();
}

nsIAtom*
nsHtml5Portability::newLocalNameFromBuffer(char16_t* buf, int32_t offset, int32_t length, nsHtml5AtomTable* interner)
{
  NS_ASSERTION(!offset, "The offset should always be zero here.");
  NS_ASSERTION(interner, "Didn't get an atom service.");
  return interner->GetAtom(nsDependentSubstring(buf, buf + length));
}

static bool
ContainsWhiteSpace(mozilla::Span<char16_t> aSpan)
{
  for (char16_t c : aSpan) {
    if (nsContentUtils::IsHTMLWhitespace(c)) {
      return true;
    }
  }
  return false;
}

nsHtml5String
nsHtml5Portability::newStringFromBuffer(char16_t* buf,
                                        int32_t offset,
                                        int32_t length,
                                        nsHtml5TreeBuilder* treeBuilder,
                                        bool maybeAtomize)
{
  if (!length) {
    return nsHtml5String::EmptyString();
  }
  if (maybeAtomize && !ContainsWhiteSpace(mozilla::MakeSpan(buf + offset, length))) {
    return nsHtml5String::FromAtom(NS_AtomizeMainThread(nsDependentSubstring(buf + offset, length)));
  }
  return nsHtml5String::FromBuffer(buf + offset, length, treeBuilder);
}

nsHtml5String
nsHtml5Portability::newEmptyString()
{
  return nsHtml5String::EmptyString();
}

nsHtml5String
nsHtml5Portability::newStringFromLiteral(const char* literal)
{
  return nsHtml5String::FromLiteral(literal);
}

nsHtml5String
nsHtml5Portability::newStringFromString(nsHtml5String string)
{
  return string.Clone();
}

jArray<char16_t,int32_t>
nsHtml5Portability::newCharArrayFromLocal(nsIAtom* local)
{
  nsAutoString temp;
  local->ToString(temp);
  int32_t len = temp.Length();
  jArray<char16_t,int32_t> arr = jArray<char16_t,int32_t>::newJArray(len);
  memcpy(arr, temp.BeginReading(), len * sizeof(char16_t));
  return arr;
}

jArray<char16_t, int32_t>
nsHtml5Portability::newCharArrayFromString(nsHtml5String string)
{
  MOZ_RELEASE_ASSERT(string);
  uint32_t len = string.Length();
  MOZ_RELEASE_ASSERT(len < INT32_MAX);
  jArray<char16_t,int32_t> arr = jArray<char16_t,int32_t>::newJArray(len);
  string.CopyToBuffer(arr);
  return arr;
}

nsIAtom*
nsHtml5Portability::newLocalFromLocal(nsIAtom* local, nsHtml5AtomTable* interner)
{
  NS_PRECONDITION(local, "Atom was null.");
  NS_PRECONDITION(interner, "Atom table was null");
  if (!local->IsStaticAtom()) {
    nsAutoString str;
    local->ToString(str);
    local = interner->GetAtom(str);
  }
  return local;
}

bool
nsHtml5Portability::localEqualsBuffer(nsIAtom* local, char16_t* buf, int32_t offset, int32_t length)
{
  return local->Equals(buf + offset, length);
}

bool
nsHtml5Portability::lowerCaseLiteralIsPrefixOfIgnoreAsciiCaseString(
  const char* lowerCaseLiteral,
  nsHtml5String string)
{
  return string.LowerCaseStartsWithASCII(lowerCaseLiteral);
}

bool
nsHtml5Portability::lowerCaseLiteralEqualsIgnoreAsciiCaseString(
  const char* lowerCaseLiteral,
  nsHtml5String string)
{
  return string.LowerCaseEqualsASCII(lowerCaseLiteral);
}

bool
nsHtml5Portability::literalEqualsString(const char* literal,
                                        nsHtml5String string)
{
  return string.EqualsASCII(literal);
}

bool
nsHtml5Portability::stringEqualsString(nsHtml5String one, nsHtml5String other)
{
  return one.Equals(other);
}

void
nsHtml5Portability::initializeStatics()
{
}

void
nsHtml5Portability::releaseStatics()
{
}