| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2010 Google, Inc. All Rights Reserved. | |
| 3 * Copyright (C) 2011 Apple Inc. All rights reserved. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions | |
| 7 * are met: | |
| 8 * 1. Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 * notice, this list of conditions and the following disclaimer in the | |
| 12 * documentation and/or other materials provided with the distribution. | |
| 13 * | |
| 14 * THIS SOFTWARE IS PROVIDED BY GOOGLE INC. ``AS IS'' AND ANY | |
| 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GOOGLE INC. OR | |
| 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
| 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 25 */ | |
| 26 | |
| 27 #ifndef SKY_ENGINE_CORE_HTML_PARSER_HTMLCONSTRUCTIONSITE_H_ | |
| 28 #define SKY_ENGINE_CORE_HTML_PARSER_HTMLCONSTRUCTIONSITE_H_ | |
| 29 | |
| 30 #include "sky/engine/core/dom/Document.h" | |
| 31 #include "sky/engine/core/html/parser/HTMLElementStack.h" | |
| 32 #include "sky/engine/wtf/Noncopyable.h" | |
| 33 #include "sky/engine/wtf/PassRefPtr.h" | |
| 34 #include "sky/engine/wtf/RefPtr.h" | |
| 35 #include "sky/engine/wtf/Vector.h" | |
| 36 #include "sky/engine/wtf/text/StringBuilder.h" | |
| 37 | |
| 38 namespace blink { | |
| 39 | |
| 40 struct HTMLConstructionSiteTask { | |
| 41 ALLOW_ONLY_INLINE_ALLOCATION(); | |
| 42 public: | |
| 43 enum Operation { | |
| 44 Insert, | |
| 45 InsertText, // Handles possible merging of text nodes. | |
| 46 }; | |
| 47 | |
| 48 explicit HTMLConstructionSiteTask(Operation op) | |
| 49 : operation(op) | |
| 50 , selfClosing(false) | |
| 51 { | |
| 52 } | |
| 53 | |
| 54 Operation operation; | |
| 55 RefPtr<ContainerNode> parent; | |
| 56 RefPtr<Node> child; | |
| 57 bool selfClosing; | |
| 58 }; | |
| 59 | |
| 60 } // namespace blink | |
| 61 | |
| 62 WTF_ALLOW_MOVE_INIT_AND_COMPARE_WITH_MEM_FUNCTIONS(blink::HTMLConstructionSiteTa
sk); | |
| 63 | |
| 64 namespace blink { | |
| 65 | |
| 66 class AtomicHTMLToken; | |
| 67 class Document; | |
| 68 class Element; | |
| 69 | |
| 70 class HTMLConstructionSite final { | |
| 71 WTF_MAKE_NONCOPYABLE(HTMLConstructionSite); | |
| 72 DISALLOW_ALLOCATION(); | |
| 73 public: | |
| 74 explicit HTMLConstructionSite(Document*); | |
| 75 explicit HTMLConstructionSite(DocumentFragment*); | |
| 76 ~HTMLConstructionSite(); | |
| 77 | |
| 78 void detach(); | |
| 79 | |
| 80 // executeQueuedTasks empties the queue but does not flush pending text. | |
| 81 // NOTE: Possible reentrancy via JavaScript execution. | |
| 82 void executeQueuedTasks(); | |
| 83 | |
| 84 // flushPendingText turns pending text into queued Text insertions, but does
not execute them. | |
| 85 void flushPendingText(); | |
| 86 | |
| 87 // Called before every token in HTMLTreeBuilder::processToken, thus inlined: | |
| 88 void flush() | |
| 89 { | |
| 90 if (!hasPendingTasks()) | |
| 91 return; | |
| 92 flushPendingText(); | |
| 93 executeQueuedTasks(); // NOTE: Possible reentrancy via JavaScript execut
ion. | |
| 94 ASSERT(!hasPendingTasks()); | |
| 95 } | |
| 96 | |
| 97 bool hasPendingTasks() | |
| 98 { | |
| 99 return !m_pendingText.isEmpty() || !m_taskQueue.isEmpty(); | |
| 100 } | |
| 101 | |
| 102 void processEndOfFile(); | |
| 103 void finishedParsing(); | |
| 104 | |
| 105 void insertHTMLElement(AtomicHTMLToken*); | |
| 106 void insertSelfClosingHTMLElement(AtomicHTMLToken*); | |
| 107 void insertScriptElement(AtomicHTMLToken*); | |
| 108 void insertTextNode(const String&); | |
| 109 | |
| 110 bool isEmpty() const { return !m_openElements.stackDepth(); } | |
| 111 Element* currentElement() const { return m_openElements.top(); } | |
| 112 ContainerNode* currentNode() const { return m_openElements.topNode(); } | |
| 113 Document& ownerDocumentForCurrentNode(); | |
| 114 HTMLElementStack* openElements() const { return &m_openElements; } | |
| 115 | |
| 116 private: | |
| 117 // In the common case, this queue will have only one task because most | |
| 118 // tokens produce only one DOM mutation. | |
| 119 typedef Vector<HTMLConstructionSiteTask, 1> TaskQueue; | |
| 120 | |
| 121 void attachLater(ContainerNode* parent, PassRefPtr<Node> child, bool selfClo
sing = false); | |
| 122 | |
| 123 PassRefPtr<Element> createElement(AtomicHTMLToken*); | |
| 124 | |
| 125 void queueTask(const HTMLConstructionSiteTask&); | |
| 126 | |
| 127 RawPtr<Document> m_document; | |
| 128 | |
| 129 // This is the root ContainerNode to which the parser attaches all newly | |
| 130 // constructed nodes. It points to a DocumentFragment when parsing fragments | |
| 131 // and a Document in all other cases. | |
| 132 RawPtr<ContainerNode> m_attachmentRoot; | |
| 133 | |
| 134 mutable HTMLElementStack m_openElements; | |
| 135 | |
| 136 TaskQueue m_taskQueue; | |
| 137 | |
| 138 class PendingText { | |
| 139 DISALLOW_ALLOCATION(); | |
| 140 public: | |
| 141 PendingText() | |
| 142 { | |
| 143 } | |
| 144 | |
| 145 void append(PassRefPtr<ContainerNode> newParent, const String& newString
) | |
| 146 { | |
| 147 ASSERT(!parent || parent == newParent); | |
| 148 parent = newParent; | |
| 149 stringBuilder.append(newString); | |
| 150 } | |
| 151 | |
| 152 void swap(PendingText& other) | |
| 153 { | |
| 154 parent.swap(other.parent); | |
| 155 stringBuilder.swap(other.stringBuilder); | |
| 156 } | |
| 157 | |
| 158 void discard() | |
| 159 { | |
| 160 PendingText discardedText; | |
| 161 swap(discardedText); | |
| 162 } | |
| 163 | |
| 164 bool isEmpty() | |
| 165 { | |
| 166 // When the stringbuilder is empty, the parent should also be "empty
". | |
| 167 ASSERT(stringBuilder.isEmpty() == !parent); | |
| 168 ASSERT(!stringBuilder.isEmpty() || !nextChild); | |
| 169 return stringBuilder.isEmpty(); | |
| 170 } | |
| 171 | |
| 172 RefPtr<ContainerNode> parent; | |
| 173 RefPtr<Node> nextChild; | |
| 174 StringBuilder stringBuilder; | |
| 175 }; | |
| 176 | |
| 177 PendingText m_pendingText; | |
| 178 }; | |
| 179 | |
| 180 } // namespace blink | |
| 181 | |
| 182 #endif // SKY_ENGINE_CORE_HTML_PARSER_HTMLCONSTRUCTIONSITE_H_ | |
| OLD | NEW |