Home | History | Annotate | Download | only in parser
      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 #include "config.h"
     27 #include "HTMLFormattingElementList.h"
     28 
     29 #include "Element.h"
     30 #include "NotImplemented.h"
     31 
     32 namespace WebCore {
     33 
     34 HTMLFormattingElementList::HTMLFormattingElementList()
     35 {
     36 }
     37 
     38 HTMLFormattingElementList::~HTMLFormattingElementList()
     39 {
     40 }
     41 
     42 Element* HTMLFormattingElementList::closestElementInScopeWithName(const AtomicString& targetName)
     43 {
     44     for (unsigned i = 1; i <= m_entries.size(); ++i) {
     45         const Entry& entry = m_entries[m_entries.size() - i];
     46         if (entry.isMarker())
     47             return 0;
     48         if (entry.element()->hasLocalName(targetName))
     49             return entry.element();
     50     }
     51     return 0;
     52 }
     53 
     54 bool HTMLFormattingElementList::contains(Element* element)
     55 {
     56     return !!find(element);
     57 }
     58 
     59 HTMLFormattingElementList::Entry* HTMLFormattingElementList::find(Element* element)
     60 {
     61     size_t index = m_entries.reverseFind(element);
     62     if (index != notFound) {
     63         // This is somewhat of a hack, and is why this method can't be const.
     64         return &m_entries[index];
     65     }
     66     return 0;
     67 }
     68 
     69 HTMLFormattingElementList::Bookmark HTMLFormattingElementList::bookmarkFor(Element* element)
     70 {
     71     size_t index = m_entries.reverseFind(element);
     72     ASSERT(index != notFound);
     73     return Bookmark(&at(index));
     74 }
     75 
     76 void HTMLFormattingElementList::swapTo(Element* oldElement, Element* newElement, const Bookmark& bookmark)
     77 {
     78     ASSERT(contains(oldElement));
     79     ASSERT(!contains(newElement));
     80     if (!bookmark.hasBeenMoved()) {
     81         ASSERT(bookmark.mark()->element() == oldElement);
     82         bookmark.mark()->replaceElement(newElement);
     83         return;
     84     }
     85     size_t index = bookmark.mark() - first();
     86     ASSERT(index < size());
     87     m_entries.insert(index + 1, newElement);
     88     remove(oldElement);
     89 }
     90 
     91 void HTMLFormattingElementList::append(Element* element)
     92 {
     93     m_entries.append(element);
     94 }
     95 
     96 void HTMLFormattingElementList::remove(Element* element)
     97 {
     98     size_t index = m_entries.reverseFind(element);
     99     if (index != notFound)
    100         m_entries.remove(index);
    101 }
    102 
    103 void HTMLFormattingElementList::appendMarker()
    104 {
    105     m_entries.append(Entry::MarkerEntry);
    106 }
    107 
    108 void HTMLFormattingElementList::clearToLastMarker()
    109 {
    110     // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#clear-the-list-of-active-formatting-elements-up-to-the-last-marker
    111     while (m_entries.size()) {
    112         bool shouldStop = m_entries.last().isMarker();
    113         m_entries.removeLast();
    114         if (shouldStop)
    115             break;
    116     }
    117 }
    118 
    119 #ifndef NDEBUG
    120 
    121 void HTMLFormattingElementList::show()
    122 {
    123     for (unsigned i = 1; i <= m_entries.size(); ++i) {
    124         const Entry& entry = m_entries[m_entries.size() - i];
    125         if (entry.isMarker())
    126             fprintf(stderr, "marker\n");
    127         else
    128             entry.element()->showNode();
    129     }
    130 }
    131 
    132 #endif
    133 
    134 }
    135