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#include "core/html/parser/CompactHTMLToken.h"
28
29#include "core/dom/QualifiedName.h"
30#include "core/html/parser/HTMLParserIdioms.h"
31
32namespace blink {
33
34struct SameSizeAsCompactHTMLToken  {
35    unsigned bitfields;
36    String data;
37    Vector<Attribute> vector;
38    TextPosition textPosition;
39};
40
41COMPILE_ASSERT(sizeof(CompactHTMLToken) == sizeof(SameSizeAsCompactHTMLToken), CompactHTMLToken_should_stay_small);
42
43CompactHTMLToken::CompactHTMLToken(const HTMLToken* token, const TextPosition& textPosition)
44    : m_type(token->type())
45    , m_isAll8BitData(false)
46    , m_doctypeForcesQuirks(false)
47    , m_textPosition(textPosition)
48{
49    switch (m_type) {
50    case HTMLToken::Uninitialized:
51        ASSERT_NOT_REACHED();
52        break;
53    case HTMLToken::DOCTYPE: {
54        m_data = attemptStaticStringCreation(token->name(), Likely8Bit);
55
56        // There is only 1 DOCTYPE token per document, so to avoid increasing the
57        // size of CompactHTMLToken, we just use the m_attributes vector.
58        m_attributes.append(Attribute(attemptStaticStringCreation(token->publicIdentifier(), Likely8Bit), String(token->systemIdentifier())));
59        m_doctypeForcesQuirks = token->forceQuirks();
60        break;
61    }
62    case HTMLToken::EndOfFile:
63        break;
64    case HTMLToken::StartTag:
65        m_attributes.reserveInitialCapacity(token->attributes().size());
66        for (Vector<HTMLToken::Attribute>::const_iterator it = token->attributes().begin(); it != token->attributes().end(); ++it)
67            m_attributes.append(Attribute(attemptStaticStringCreation(it->name, Likely8Bit), StringImpl::create8BitIfPossible(it->value)));
68        // Fall through!
69    case HTMLToken::EndTag:
70        m_selfClosing = token->selfClosing();
71        // Fall through!
72    case HTMLToken::Comment:
73    case HTMLToken::Character: {
74        m_isAll8BitData = token->isAll8BitData();
75        m_data = attemptStaticStringCreation(token->data(), token->isAll8BitData() ? Force8Bit : Force16Bit);
76        break;
77    }
78    default:
79        ASSERT_NOT_REACHED();
80        break;
81    }
82}
83
84const CompactHTMLToken::Attribute* CompactHTMLToken::getAttributeItem(const QualifiedName& name) const
85{
86    for (unsigned i = 0; i < m_attributes.size(); ++i) {
87        if (threadSafeMatch(m_attributes.at(i).name, name))
88            return &m_attributes.at(i);
89    }
90    return 0;
91}
92
93bool CompactHTMLToken::isSafeToSendToAnotherThread() const
94{
95    for (Vector<Attribute>::const_iterator it = m_attributes.begin(); it != m_attributes.end(); ++it) {
96        if (!it->name.isSafeToSendToAnotherThread())
97            return false;
98        if (!it->value.isSafeToSendToAnotherThread())
99            return false;
100    }
101    return m_data.isSafeToSendToAnotherThread();
102}
103
104}
105