1/*
2 * Copyright (C) 2013 Google, Inc. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL GOOGLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27
28#if ENABLE(THREADED_HTML_PARSER)
29
30#include "CompactHTMLToken.h"
31
32#include "HTMLParserIdioms.h"
33#include "HTMLToken.h"
34#include "QualifiedName.h"
35#include "XSSAuditorDelegate.h"
36
37namespace WebCore {
38
39struct SameSizeAsCompactHTMLToken  {
40    unsigned bitfields;
41    HTMLIdentifier data;
42    Vector<Attribute> vector;
43    TextPosition textPosition;
44};
45
46COMPILE_ASSERT(sizeof(CompactHTMLToken) == sizeof(SameSizeAsCompactHTMLToken), CompactHTMLToken_should_stay_small);
47
48CompactHTMLToken::CompactHTMLToken(const HTMLToken* token, const TextPosition& textPosition)
49    : m_type(token->type())
50    , m_isAll8BitData(false)
51    , m_doctypeForcesQuirks(false)
52    , m_textPosition(textPosition)
53{
54    switch (m_type) {
55    case HTMLToken::Uninitialized:
56        ASSERT_NOT_REACHED();
57        break;
58    case HTMLToken::DOCTYPE: {
59        m_data = HTMLIdentifier(token->name(), Likely8Bit);
60        // There is only 1 DOCTYPE token per document, so to avoid increasing the
61        // size of CompactHTMLToken, we just use the m_attributes vector.
62        m_attributes.append(Attribute(HTMLIdentifier(token->publicIdentifier(), Likely8Bit), String(token->systemIdentifier())));
63        m_doctypeForcesQuirks = token->forceQuirks();
64        break;
65    }
66    case HTMLToken::EndOfFile:
67        break;
68    case HTMLToken::StartTag:
69        m_attributes.reserveInitialCapacity(token->attributes().size());
70        for (Vector<HTMLToken::Attribute>::const_iterator it = token->attributes().begin(); it != token->attributes().end(); ++it)
71            m_attributes.append(Attribute(HTMLIdentifier(it->name, Likely8Bit), StringImpl::create8BitIfPossible(it->value)));
72        // Fall through!
73    case HTMLToken::EndTag:
74        m_selfClosing = token->selfClosing();
75        // Fall through!
76    case HTMLToken::Comment:
77    case HTMLToken::Character: {
78        m_isAll8BitData = token->isAll8BitData();
79        m_data = HTMLIdentifier(token->data(), token->isAll8BitData() ? Force8Bit : Force16Bit);
80        break;
81    }
82    default:
83        ASSERT_NOT_REACHED();
84        break;
85    }
86}
87
88const CompactHTMLToken::Attribute* CompactHTMLToken::getAttributeItem(const QualifiedName& name) const
89{
90    for (unsigned i = 0; i < m_attributes.size(); ++i) {
91        if (threadSafeMatch(m_attributes.at(i).name, name))
92            return &m_attributes.at(i);
93    }
94    return 0;
95}
96
97bool CompactHTMLToken::isSafeToSendToAnotherThread() const
98{
99    for (Vector<Attribute>::const_iterator it = m_attributes.begin(); it != m_attributes.end(); ++it) {
100        if (!it->name.isSafeToSendToAnotherThread())
101            return false;
102        if (!it->value.isSafeToSendToAnotherThread())
103            return false;
104    }
105    return m_data.isSafeToSendToAnotherThread();
106}
107
108}
109
110#endif // ENABLE(THREADED_HTML_PARSER)
111