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
|
/*
* Copyright (C) 2013 Google, Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef HTMLToken_h
#define HTMLToken_h
#include "Attribute.h"
#include "HTMLToken.h"
#include <wtf/OwnPtr.h>
#include <wtf/PassOwnPtr.h>
#include <wtf/RefCounted.h>
#include <wtf/RefPtr.h>
namespace WebCore {
class DoctypeData {
WTF_MAKE_NONCOPYABLE(DoctypeData);
public:
DoctypeData()
: m_hasPublicIdentifier(false)
, m_hasSystemIdentifier(false)
, m_forceQuirks(false)
{
}
// FIXME: This should use String instead of Vector<UChar>.
bool m_hasPublicIdentifier;
bool m_hasSystemIdentifier;
WTF::Vector<UChar> m_publicIdentifier;
WTF::Vector<UChar> m_systemIdentifier;
bool m_forceQuirks;
};
static inline Attribute* findAttributeInVector(Vector<Attribute>& attributes, const QualifiedName& name)
{
for (unsigned i = 0; i < attributes.size(); ++i) {
if (attributes.at(i).name().matches(name))
return &attributes.at(i);
}
return 0;
}
class HTMLToken {
WTF_MAKE_NONCOPYABLE(HTMLToken);
WTF_MAKE_FAST_ALLOCATED;
public:
enum Type {
Uninitialized,
DOCTYPE,
StartTag,
EndTag,
Comment,
Character,
EndOfFile,
};
class Attribute {
public:
class Range {
public:
int start;
int end;
};
Range nameRange;
Range valueRange;
Vector<UChar, 32> name;
Vector<UChar, 32> value;
};
typedef Vector<Attribute, 10> AttributeList;
typedef Vector<UChar, 256> DataVector;
HTMLToken() { clear(); }
void clear()
{
m_type = Uninitialized;
m_range.start = 0;
m_range.end = 0;
m_baseOffset = 0;
m_data.clear();
m_orAllData = 0;
}
bool isUninitialized() { return m_type == Uninitialized; }
Type type() const { return m_type; }
void makeEndOfFile()
{
ASSERT(m_type == Uninitialized);
m_type = EndOfFile;
}
/* Range and offset methods exposed for HTMLSourceTracker and HTMLViewSourceParser */
int startIndex() const { return m_range.start; }
int endIndex() const { return m_range.end; }
void setBaseOffset(int offset)
{
m_baseOffset = offset;
}
void end(int endOffset)
{
m_range.end = endOffset - m_baseOffset;
}
const DataVector& data() const
{
ASSERT(m_type == Character || m_type == Comment || m_type == StartTag || m_type == EndTag);
return m_data;
}
bool isAll8BitData() const
{
return (m_orAllData <= 0xff);
}
const DataVector& name() const
{
ASSERT(m_type == StartTag || m_type == EndTag || m_type == DOCTYPE);
return m_data;
}
void appendToName(UChar character)
{
ASSERT(m_type == StartTag || m_type == EndTag || m_type == DOCTYPE);
ASSERT(character);
m_data.append(character);
m_orAllData |= character;
}
/* DOCTYPE Tokens */
bool forceQuirks() const
{
ASSERT(m_type == DOCTYPE);
return m_doctypeData->m_forceQuirks;
}
void setForceQuirks()
{
ASSERT(m_type == DOCTYPE);
m_doctypeData->m_forceQuirks = true;
}
void beginDOCTYPE()
{
ASSERT(m_type == Uninitialized);
m_type = DOCTYPE;
m_doctypeData = adoptPtr(new DoctypeData);
}
void beginDOCTYPE(UChar character)
{
ASSERT(character);
beginDOCTYPE();
m_data.append(character);
m_orAllData |= character;
}
// FIXME: Distinguish between a missing public identifer and an empty one.
const WTF::Vector<UChar>& publicIdentifier() const
{
ASSERT(m_type == DOCTYPE);
return m_doctypeData->m_publicIdentifier;
}
// FIXME: Distinguish between a missing system identifer and an empty one.
const WTF::Vector<UChar>& systemIdentifier() const
{
ASSERT(m_type == DOCTYPE);
return m_doctypeData->m_systemIdentifier;
}
void setPublicIdentifierToEmptyString()
{
ASSERT(m_type == DOCTYPE);
m_doctypeData->m_hasPublicIdentifier = true;
m_doctypeData->m_publicIdentifier.clear();
}
void setSystemIdentifierToEmptyString()
{
ASSERT(m_type == DOCTYPE);
m_doctypeData->m_hasSystemIdentifier = true;
m_doctypeData->m_systemIdentifier.clear();
}
void appendToPublicIdentifier(UChar character)
{
ASSERT(character);
ASSERT(m_type == DOCTYPE);
ASSERT(m_doctypeData->m_hasPublicIdentifier);
m_doctypeData->m_publicIdentifier.append(character);
}
void appendToSystemIdentifier(UChar character)
{
ASSERT(character);
ASSERT(m_type == DOCTYPE);
ASSERT(m_doctypeData->m_hasSystemIdentifier);
m_doctypeData->m_systemIdentifier.append(character);
}
PassOwnPtr<DoctypeData> releaseDoctypeData()
{
return m_doctypeData.release();
}
/* Start/End Tag Tokens */
bool selfClosing() const
{
ASSERT(m_type == StartTag || m_type == EndTag);
return m_selfClosing;
}
void setSelfClosing()
{
ASSERT(m_type == StartTag || m_type == EndTag);
m_selfClosing = true;
}
void beginStartTag(UChar character)
{
ASSERT(character);
ASSERT(m_type == Uninitialized);
m_type = StartTag;
m_selfClosing = false;
m_currentAttribute = 0;
m_attributes.clear();
m_data.append(character);
m_orAllData |= character;
}
void beginEndTag(LChar character)
{
ASSERT(m_type == Uninitialized);
m_type = EndTag;
m_selfClosing = false;
m_currentAttribute = 0;
m_attributes.clear();
m_data.append(character);
}
void beginEndTag(const Vector<LChar, 32>& characters)
{
ASSERT(m_type == Uninitialized);
m_type = EndTag;
m_selfClosing = false;
m_currentAttribute = 0;
m_attributes.clear();
m_data.appendVector(characters);
}
void addNewAttribute()
{
ASSERT(m_type == StartTag || m_type == EndTag);
m_attributes.grow(m_attributes.size() + 1);
m_currentAttribute = &m_attributes.last();
#ifndef NDEBUG
m_currentAttribute->nameRange.start = 0;
m_currentAttribute->nameRange.end = 0;
m_currentAttribute->valueRange.start = 0;
m_currentAttribute->valueRange.end = 0;
#endif
}
void beginAttributeName(int offset)
{
m_currentAttribute->nameRange.start = offset - m_baseOffset;
}
void endAttributeName(int offset)
{
int index = offset - m_baseOffset;
m_currentAttribute->nameRange.end = index;
m_currentAttribute->valueRange.start = index;
m_currentAttribute->valueRange.end = index;
}
void beginAttributeValue(int offset)
{
m_currentAttribute->valueRange.start = offset - m_baseOffset;
#ifndef NDEBUG
m_currentAttribute->valueRange.end = 0;
#endif
}
void endAttributeValue(int offset)
{
m_currentAttribute->valueRange.end = offset - m_baseOffset;
}
void appendToAttributeName(UChar character)
{
ASSERT(character);
ASSERT(m_type == StartTag || m_type == EndTag);
// FIXME: We should be able to add the following ASSERT once we fix
// https://bugs.webkit.org/show_bug.cgi?id=62971
// ASSERT(m_currentAttribute->nameRange.start);
m_currentAttribute->name.append(character);
}
void appendToAttributeValue(UChar character)
{
ASSERT(character);
ASSERT(m_type == StartTag || m_type == EndTag);
ASSERT(m_currentAttribute->valueRange.start);
m_currentAttribute->value.append(character);
}
void appendToAttributeValue(size_t i, const String& value)
{
ASSERT(!value.isEmpty());
ASSERT(m_type == StartTag || m_type == EndTag);
append(m_attributes[i].value, value);
}
const AttributeList& attributes() const
{
ASSERT(m_type == StartTag || m_type == EndTag);
return m_attributes;
}
const Attribute* getAttributeItem(const QualifiedName& name) const
{
for (unsigned i = 0; i < m_attributes.size(); ++i) {
if (AtomicString(m_attributes.at(i).name) == name.localName())
return &m_attributes.at(i);
}
return 0;
}
// Used by the XSSAuditor to nuke XSS-laden attributes.
void eraseValueOfAttribute(size_t i)
{
ASSERT(m_type == StartTag || m_type == EndTag);
m_attributes[i].value.clear();
}
/* Character Tokens */
// Starting a character token works slightly differently than starting
// other types of tokens because we want to save a per-character branch.
void ensureIsCharacterToken()
{
ASSERT(m_type == Uninitialized || m_type == Character);
m_type = Character;
}
const DataVector& characters() const
{
ASSERT(m_type == Character);
return m_data;
}
void appendToCharacter(char character)
{
ASSERT(m_type == Character);
m_data.append(character);
}
void appendToCharacter(UChar character)
{
ASSERT(m_type == Character);
m_data.append(character);
m_orAllData |= character;
}
void appendToCharacter(const Vector<LChar, 32>& characters)
{
ASSERT(m_type == Character);
m_data.appendVector(characters);
}
/* Comment Tokens */
const DataVector& comment() const
{
ASSERT(m_type == Comment);
return m_data;
}
void beginComment()
{
ASSERT(m_type == Uninitialized);
m_type = Comment;
}
void appendToComment(UChar character)
{
ASSERT(character);
ASSERT(m_type == Comment);
m_data.append(character);
m_orAllData |= character;
}
void eraseCharacters()
{
ASSERT(m_type == Character);
m_data.clear();
m_orAllData = 0;
}
private:
Type m_type;
Attribute::Range m_range; // Always starts at zero.
int m_baseOffset;
DataVector m_data;
UChar m_orAllData;
// For StartTag and EndTag
bool m_selfClosing;
AttributeList m_attributes;
// A pointer into m_attributes used during lexing.
Attribute* m_currentAttribute;
// For DOCTYPE
OwnPtr<DoctypeData> m_doctypeData;
};
}
#endif
|