| OLD | NEW |
| (Empty) |
| 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 APPLE 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 APPLE 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 #ifndef SKY_ENGINE_CORE_HTML_PARSER_ATOMICHTMLTOKEN_H_ | |
| 27 #define SKY_ENGINE_CORE_HTML_PARSER_ATOMICHTMLTOKEN_H_ | |
| 28 | |
| 29 #include "gen/sky/core/HTMLElementLookupTrie.h" | |
| 30 #include "sky/engine/core/dom/Attribute.h" | |
| 31 #include "sky/engine/core/html/parser/CompactHTMLToken.h" | |
| 32 #include "sky/engine/core/html/parser/HTMLToken.h" | |
| 33 #include "sky/engine/wtf/RefCounted.h" | |
| 34 #include "sky/engine/wtf/RefPtr.h" | |
| 35 | |
| 36 namespace blink { | |
| 37 | |
| 38 class AtomicHTMLToken { | |
| 39 WTF_MAKE_NONCOPYABLE(AtomicHTMLToken); | |
| 40 public: | |
| 41 | |
| 42 HTMLToken::Type type() const { return m_type; } | |
| 43 | |
| 44 const AtomicString& name() const | |
| 45 { | |
| 46 ASSERT(usesName()); | |
| 47 return m_name; | |
| 48 } | |
| 49 | |
| 50 void setName(const AtomicString& name) | |
| 51 { | |
| 52 ASSERT(usesName()); | |
| 53 m_name = name; | |
| 54 } | |
| 55 | |
| 56 bool selfClosing() const | |
| 57 { | |
| 58 ASSERT(m_type == HTMLToken::StartTag || m_type == HTMLToken::EndTag); | |
| 59 return m_selfClosing; | |
| 60 } | |
| 61 | |
| 62 Attribute* getAttributeItem(const QualifiedName& attributeName) | |
| 63 { | |
| 64 ASSERT(usesAttributes()); | |
| 65 return findAttributeInVector(m_attributes, attributeName); | |
| 66 } | |
| 67 | |
| 68 Vector<Attribute>& attributes() | |
| 69 { | |
| 70 ASSERT(usesAttributes()); | |
| 71 return m_attributes; | |
| 72 } | |
| 73 | |
| 74 const Vector<Attribute>& attributes() const | |
| 75 { | |
| 76 ASSERT(usesAttributes()); | |
| 77 return m_attributes; | |
| 78 } | |
| 79 | |
| 80 const String& characters() const | |
| 81 { | |
| 82 ASSERT(m_type == HTMLToken::Character); | |
| 83 return m_data; | |
| 84 } | |
| 85 | |
| 86 explicit AtomicHTMLToken(HTMLToken& token) | |
| 87 : m_type(token.type()) | |
| 88 { | |
| 89 switch (m_type) { | |
| 90 case HTMLToken::Uninitialized: | |
| 91 ASSERT_NOT_REACHED(); | |
| 92 break; | |
| 93 case HTMLToken::EndOfFile: | |
| 94 break; | |
| 95 case HTMLToken::StartTag: | |
| 96 case HTMLToken::EndTag: { | |
| 97 m_selfClosing = token.selfClosing(); | |
| 98 if (StringImpl* tagName = lookupHTMLTag(token.name().data(), token.n
ame().size())) | |
| 99 m_name = AtomicString(tagName); | |
| 100 else | |
| 101 m_name = AtomicString(token.name()); | |
| 102 initializeAttributes(token.attributes()); | |
| 103 break; | |
| 104 } | |
| 105 case HTMLToken::Character: | |
| 106 if (token.isAll8BitData()) | |
| 107 m_data = String::make8BitFrom16BitSource(token.data()); | |
| 108 else | |
| 109 m_data = String(token.data()); | |
| 110 break; | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 explicit AtomicHTMLToken(const CompactHTMLToken& token) | |
| 115 : m_type(token.type()) | |
| 116 { | |
| 117 switch (m_type) { | |
| 118 case HTMLToken::Uninitialized: | |
| 119 ASSERT_NOT_REACHED(); | |
| 120 break; | |
| 121 case HTMLToken::EndOfFile: | |
| 122 break; | |
| 123 case HTMLToken::StartTag: | |
| 124 m_attributes.reserveInitialCapacity(token.attributes().size()); | |
| 125 for (Vector<CompactHTMLToken::Attribute>::const_iterator it = token.
attributes().begin(); it != token.attributes().end(); ++it) { | |
| 126 QualifiedName name(AtomicString(it->name)); | |
| 127 // FIXME: This is N^2 for the number of attributes. | |
| 128 if (!findAttributeInVector(m_attributes, name)) | |
| 129 m_attributes.append(Attribute(name, AtomicString(it->value))
); | |
| 130 } | |
| 131 // Fall through! | |
| 132 case HTMLToken::EndTag: | |
| 133 m_selfClosing = token.selfClosing(); | |
| 134 m_name = AtomicString(token.data()); | |
| 135 break; | |
| 136 case HTMLToken::Character: | |
| 137 m_data = token.data(); | |
| 138 break; | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 explicit AtomicHTMLToken(HTMLToken::Type type) | |
| 143 : m_type(type) | |
| 144 , m_selfClosing(false) | |
| 145 { | |
| 146 } | |
| 147 | |
| 148 AtomicHTMLToken(HTMLToken::Type type, const AtomicString& name, const Vector
<Attribute>& attributes = Vector<Attribute>()) | |
| 149 : m_type(type) | |
| 150 , m_name(name) | |
| 151 , m_selfClosing(false) | |
| 152 , m_attributes(attributes) | |
| 153 { | |
| 154 ASSERT(usesName()); | |
| 155 } | |
| 156 | |
| 157 private: | |
| 158 HTMLToken::Type m_type; | |
| 159 | |
| 160 void initializeAttributes(const HTMLToken::AttributeList& attributes); | |
| 161 QualifiedName nameForAttribute(const HTMLToken::Attribute&) const; | |
| 162 | |
| 163 bool usesName() const; | |
| 164 | |
| 165 bool usesAttributes() const; | |
| 166 | |
| 167 // "name" for StartTag and EndTag | |
| 168 AtomicString m_name; | |
| 169 | |
| 170 // "characters" for Character | |
| 171 String m_data; | |
| 172 | |
| 173 // For StartTag and EndTag | |
| 174 bool m_selfClosing; | |
| 175 | |
| 176 Vector<Attribute> m_attributes; | |
| 177 }; | |
| 178 | |
| 179 inline void AtomicHTMLToken::initializeAttributes(const HTMLToken::AttributeList
& attributes) | |
| 180 { | |
| 181 size_t size = attributes.size(); | |
| 182 if (!size) | |
| 183 return; | |
| 184 | |
| 185 m_attributes.clear(); | |
| 186 m_attributes.reserveInitialCapacity(size); | |
| 187 for (size_t i = 0; i < size; ++i) { | |
| 188 const HTMLToken::Attribute& attribute = attributes[i]; | |
| 189 if (attribute.name.isEmpty()) | |
| 190 continue; | |
| 191 | |
| 192 ASSERT(attribute.nameRange.start); | |
| 193 ASSERT(attribute.nameRange.end); | |
| 194 ASSERT(attribute.valueRange.start); | |
| 195 ASSERT(attribute.valueRange.end); | |
| 196 | |
| 197 AtomicString value(attribute.value); | |
| 198 const QualifiedName& name = nameForAttribute(attribute); | |
| 199 // FIXME: This is N^2 for the number of attributes. | |
| 200 if (!findAttributeInVector(m_attributes, name)) | |
| 201 m_attributes.append(Attribute(name, value)); | |
| 202 } | |
| 203 } | |
| 204 | |
| 205 } | |
| 206 | |
| 207 #endif // SKY_ENGINE_CORE_HTML_PARSER_ATOMICHTMLTOKEN_H_ | |
| OLD | NEW |