This source file includes following definitions.
- closestElementInScopeWithName
- contains
- find
- bookmarkFor
- swapTo
- append
- remove
- appendMarker
- clearToLastMarker
- tryToEnsureNoahsArkConditionQuickly
- ensureNoahsArkCondition
- show
#include "config.h"
#include "core/html/parser/HTMLFormattingElementList.h"
#ifndef NDEBUG
#include <stdio.h>
#endif
namespace WebCore {
static const size_t kNoahsArkCapacity = 3;
HTMLFormattingElementList::HTMLFormattingElementList()
{
}
HTMLFormattingElementList::~HTMLFormattingElementList()
{
}
Element* HTMLFormattingElementList::closestElementInScopeWithName(const AtomicString& targetName)
{
for (unsigned i = 1; i <= m_entries.size(); ++i) {
const Entry& entry = m_entries[m_entries.size() - i];
if (entry.isMarker())
return 0;
if (entry.stackItem()->matchesHTMLTag(targetName))
return entry.element();
}
return 0;
}
bool HTMLFormattingElementList::contains(Element* element)
{
return !!find(element);
}
HTMLFormattingElementList::Entry* HTMLFormattingElementList::find(Element* element)
{
size_t index = m_entries.reverseFind(element);
if (index != kNotFound) {
return &m_entries[index];
}
return 0;
}
HTMLFormattingElementList::Bookmark HTMLFormattingElementList::bookmarkFor(Element* element)
{
size_t index = m_entries.reverseFind(element);
ASSERT(index != kNotFound);
return Bookmark(&at(index));
}
void HTMLFormattingElementList::swapTo(Element* oldElement, PassRefPtr<HTMLStackItem> newItem, const Bookmark& bookmark)
{
ASSERT(contains(oldElement));
ASSERT(!contains(newItem->element()));
if (!bookmark.hasBeenMoved()) {
ASSERT(bookmark.mark()->element() == oldElement);
bookmark.mark()->replaceElement(newItem);
return;
}
size_t index = bookmark.mark() - first();
ASSERT_WITH_SECURITY_IMPLICATION(index < size());
m_entries.insert(index + 1, newItem);
remove(oldElement);
}
void HTMLFormattingElementList::append(PassRefPtr<HTMLStackItem> item)
{
ensureNoahsArkCondition(item.get());
m_entries.append(item);
}
void HTMLFormattingElementList::remove(Element* element)
{
size_t index = m_entries.reverseFind(element);
if (index != kNotFound)
m_entries.remove(index);
}
void HTMLFormattingElementList::appendMarker()
{
m_entries.append(Entry::MarkerEntry);
}
void HTMLFormattingElementList::clearToLastMarker()
{
while (m_entries.size()) {
bool shouldStop = m_entries.last().isMarker();
m_entries.removeLast();
if (shouldStop)
break;
}
}
void HTMLFormattingElementList::tryToEnsureNoahsArkConditionQuickly(HTMLStackItem* newItem, Vector<HTMLStackItem*>& remainingCandidates)
{
ASSERT(remainingCandidates.isEmpty());
if (m_entries.size() < kNoahsArkCapacity)
return;
Vector<HTMLStackItem*, 10> candidates;
size_t newItemAttributeCount = newItem->attributes().size();
for (size_t i = m_entries.size(); i; ) {
--i;
Entry& entry = m_entries[i];
if (entry.isMarker())
break;
HTMLStackItem* candidate = entry.stackItem().get();
if (newItem->localName() != candidate->localName() || newItem->namespaceURI() != candidate->namespaceURI())
continue;
if (candidate->attributes().size() != newItemAttributeCount)
continue;
candidates.append(candidate);
}
if (candidates.size() < kNoahsArkCapacity)
return;
remainingCandidates.appendVector(candidates);
}
void HTMLFormattingElementList::ensureNoahsArkCondition(HTMLStackItem* newItem)
{
Vector<HTMLStackItem*> candidates;
tryToEnsureNoahsArkConditionQuickly(newItem, candidates);
if (candidates.isEmpty())
return;
Vector<HTMLStackItem*> remainingCandidates;
remainingCandidates.reserveInitialCapacity(candidates.size());
const Vector<Attribute>& attributes = newItem->attributes();
for (size_t i = 0; i < attributes.size(); ++i) {
const Attribute& attribute = attributes[i];
for (size_t j = 0; j < candidates.size(); ++j) {
HTMLStackItem* candidate = candidates[j];
ASSERT(newItem->attributes().size() == candidate->attributes().size());
ASSERT(newItem->localName() == candidate->localName() && newItem->namespaceURI() == candidate->namespaceURI());
Attribute* candidateAttribute = candidate->getAttributeItem(attribute.name());
if (candidateAttribute && candidateAttribute->value() == attribute.value())
remainingCandidates.append(candidate);
}
if (remainingCandidates.size() < kNoahsArkCapacity)
return;
candidates.swap(remainingCandidates);
remainingCandidates.shrink(0);
}
for (size_t i = kNoahsArkCapacity - 1; i < candidates.size(); ++i)
remove(candidates[i]->element());
}
#ifndef NDEBUG
void HTMLFormattingElementList::show()
{
for (unsigned i = 1; i <= m_entries.size(); ++i) {
const Entry& entry = m_entries[m_entries.size() - i];
if (entry.isMarker())
fprintf(stderr, "marker\n");
else
entry.element()->showNode();
}
}
#endif
}