| 1 | /* |
| 2 | * Copyright (C) 2010 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 | #pragma once |
| 27 | |
| 28 | #include "HTMLStackItem.h" |
| 29 | #include <wtf/Forward.h> |
| 30 | #include <wtf/RefPtr.h> |
| 31 | #include <wtf/Vector.h> |
| 32 | |
| 33 | namespace WebCore { |
| 34 | |
| 35 | class Element; |
| 36 | |
| 37 | // This may end up merged into HTMLElementStack. |
| 38 | class HTMLFormattingElementList { |
| 39 | WTF_MAKE_NONCOPYABLE(HTMLFormattingElementList); |
| 40 | public: |
| 41 | HTMLFormattingElementList(); |
| 42 | ~HTMLFormattingElementList(); |
| 43 | |
| 44 | // Ideally Entry would be private, but HTMLTreeBuilder has to coordinate |
| 45 | // between the HTMLFormattingElementList and HTMLElementStack and needs |
| 46 | // access to Entry::isMarker() and Entry::replaceElement() to do so. |
| 47 | class Entry { |
| 48 | public: |
| 49 | // Inline because they're hot and Vector<T> uses them. |
| 50 | explicit Entry(Ref<HTMLStackItem>&& item) |
| 51 | : m_item(WTFMove(item)) |
| 52 | { |
| 53 | } |
| 54 | enum MarkerEntryType { MarkerEntry }; |
| 55 | Entry(MarkerEntryType) |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | bool isMarker() const { return !m_item; } |
| 60 | |
| 61 | HTMLStackItem* stackItem() const { return m_item.get(); } |
| 62 | Element& element() const |
| 63 | { |
| 64 | // The fact that !m_item == isMarker() is an implementation detail |
| 65 | // callers should check isMarker() before calling element(). |
| 66 | ASSERT(m_item); |
| 67 | return m_item->element(); |
| 68 | } |
| 69 | void replaceElement(Ref<HTMLStackItem>&& item) { m_item = WTFMove(item); } |
| 70 | |
| 71 | // Needed for use with Vector. These are super-hot and must be inline. |
| 72 | bool operator==(Element* element) const { return !m_item ? !element : &m_item->element() == element; } |
| 73 | bool operator!=(Element* element) const { return !m_item ? !!element : &m_item->element() != element; } |
| 74 | |
| 75 | private: |
| 76 | RefPtr<HTMLStackItem> m_item; |
| 77 | }; |
| 78 | |
| 79 | class Bookmark { |
| 80 | public: |
| 81 | explicit Bookmark(Entry& entry) |
| 82 | : m_hasBeenMoved(false) |
| 83 | , m_mark(&entry) |
| 84 | { |
| 85 | } |
| 86 | |
| 87 | void moveToAfter(Entry& before) |
| 88 | { |
| 89 | m_hasBeenMoved = true; |
| 90 | m_mark = &before; |
| 91 | } |
| 92 | |
| 93 | bool hasBeenMoved() const { return m_hasBeenMoved; } |
| 94 | Entry& mark() const { ASSERT(m_mark); return *m_mark; } |
| 95 | |
| 96 | private: |
| 97 | bool m_hasBeenMoved; |
| 98 | Entry* m_mark; |
| 99 | }; |
| 100 | |
| 101 | bool isEmpty() const { return !size(); } |
| 102 | size_t size() const { return m_entries.size(); } |
| 103 | |
| 104 | Element* closestElementInScopeWithName(const AtomicString&); |
| 105 | |
| 106 | Entry* find(Element&); |
| 107 | bool contains(Element&); |
| 108 | void append(Ref<HTMLStackItem>&&); |
| 109 | void remove(Element&); |
| 110 | |
| 111 | Bookmark bookmarkFor(Element&); |
| 112 | void swapTo(Element& oldElement, Ref<HTMLStackItem>&& newItem, const Bookmark&); |
| 113 | |
| 114 | void appendMarker(); |
| 115 | // clearToLastMarker also clears the marker (per the HTML5 spec). |
| 116 | void clearToLastMarker(); |
| 117 | |
| 118 | const Entry& at(size_t i) const { return m_entries[i]; } |
| 119 | Entry& at(size_t i) { return m_entries[i]; } |
| 120 | |
| 121 | #if ENABLE(TREE_DEBUGGING) |
| 122 | void show(); |
| 123 | #endif |
| 124 | |
| 125 | private: |
| 126 | Entry& first() { return at(0); } |
| 127 | |
| 128 | // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#list-of-active-formatting-elements |
| 129 | // These functions enforce the "Noah's Ark" condition, which removes redundant mis-nested elements. |
| 130 | void tryToEnsureNoahsArkConditionQuickly(HTMLStackItem&, Vector<HTMLStackItem*>& remainingCandidates); |
| 131 | void ensureNoahsArkCondition(HTMLStackItem&); |
| 132 | |
| 133 | Vector<Entry> m_entries; |
| 134 | }; |
| 135 | |
| 136 | } // namespace WebCore |
| 137 | |