Title: [123537] trunk/Source/WebCore
Revision
123537
Author
k...@webkit.org
Date
2012-07-24 15:19:44 -0700 (Tue, 24 Jul 2012)

Log Message

Ensure Noah's ark without reading the DOM tree.
https://bugs.webkit.org/show_bug.cgi?id=92065

Reviewed by Adam Barth.

Technically we shouldn't read attributes back from the DOM. If _javascript_ changes
the attributes values, we could get a slightly wrong output here.

Read attributes from tokens saved in the active formatting element list.

No new tests, covered by existing tests.

* html/parser/HTMLFormattingElementList.cpp:
(WebCore::attributeCount):
(WebCore::HTMLFormattingElementList::append):
(WebCore::HTMLFormattingElementList::tryToEnsureNoahsArkConditionQuickly):
(WebCore::HTMLFormattingElementList::ensureNoahsArkCondition):
* html/parser/HTMLFormattingElementList.h:
(HTMLFormattingElementList):
* html/parser/HTMLStackItem.h:
(WebCore::HTMLStackItem::localName):
(HTMLStackItem):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (123536 => 123537)


--- trunk/Source/WebCore/ChangeLog	2012-07-24 22:12:54 UTC (rev 123536)
+++ trunk/Source/WebCore/ChangeLog	2012-07-24 22:19:44 UTC (rev 123537)
@@ -1,3 +1,28 @@
+2012-07-24  Kwang Yul Seo  <sk...@company100.net>
+
+        Ensure Noah's ark without reading the DOM tree.
+        https://bugs.webkit.org/show_bug.cgi?id=92065
+
+        Reviewed by Adam Barth.
+
+        Technically we shouldn't read attributes back from the DOM. If _javascript_ changes
+        the attributes values, we could get a slightly wrong output here.
+
+        Read attributes from tokens saved in the active formatting element list.
+
+        No new tests, covered by existing tests.
+
+        * html/parser/HTMLFormattingElementList.cpp:
+        (WebCore::attributeCount):
+        (WebCore::HTMLFormattingElementList::append):
+        (WebCore::HTMLFormattingElementList::tryToEnsureNoahsArkConditionQuickly):
+        (WebCore::HTMLFormattingElementList::ensureNoahsArkCondition):
+        * html/parser/HTMLFormattingElementList.h:
+        (HTMLFormattingElementList):
+        * html/parser/HTMLStackItem.h:
+        (WebCore::HTMLStackItem::localName):
+        (HTMLStackItem):
+
 2012-07-23  Kwang Yul Seo  <sk...@company100.net>
 
         Clear the external characters pointer of an AtomicHTMLToken before the raw token is cleared.

Modified: trunk/Source/WebCore/html/parser/HTMLFormattingElementList.cpp (123536 => 123537)


--- trunk/Source/WebCore/html/parser/HTMLFormattingElementList.cpp	2012-07-24 22:12:54 UTC (rev 123536)
+++ trunk/Source/WebCore/html/parser/HTMLFormattingElementList.cpp	2012-07-24 22:19:44 UTC (rev 123537)
@@ -40,9 +40,9 @@
 // Noah's Ark of Formatting Elements can fit three of each element.
 static const size_t kNoahsArkCapacity = 3;
 
-static inline size_t attributeCountWithoutUpdate(Element* element)
+static inline size_t attributeCount(AtomicHTMLToken* token)
 {
-    return element->hasAttributesWithoutUpdate() ? element->attributeCount() : 0;
+    return token->attributes().size();
 }
 
 HTMLFormattingElementList::HTMLFormattingElementList()
@@ -104,7 +104,7 @@
 
 void HTMLFormattingElementList::append(PassRefPtr<HTMLStackItem> item)
 {
-    ensureNoahsArkCondition(item->element());
+    ensureNoahsArkCondition(item.get());
     m_entries.append(item);
 }
 
@@ -131,7 +131,7 @@
     }
 }
 
-void HTMLFormattingElementList::tryToEnsureNoahsArkConditionQuickly(Element* newElement, Vector<Element*>& remainingCandidates)
+void HTMLFormattingElementList::tryToEnsureNoahsArkConditionQuickly(HTMLStackItem* newItem, Vector<HTMLStackItem*>& remainingCandidates)
 {
     ASSERT(remainingCandidates.isEmpty());
 
@@ -140,9 +140,9 @@
 
     // Use a vector with inline capacity to avoid a malloc in the common case
     // of a quickly ensuring the condition.
-    Vector<Element*, 10> candidates;
+    Vector<HTMLStackItem*, 10> candidates;
 
-    size_t newElementAttributeCount = attributeCountWithoutUpdate(newElement);
+    size_t newItemAttributeCount = attributeCount(newItem->token());
 
     for (size_t i = m_entries.size(); i; ) {
         --i;
@@ -151,10 +151,10 @@
             break;
 
         // Quickly reject obviously non-matching candidates.
-        Element* candidate = entry.element();
-        if (newElement->tagQName() != candidate->tagQName())
+        HTMLStackItem* candidate = entry.stackItem().get();
+        if (newItem->localName() != candidate->localName() || newItem->namespaceURI() != candidate->namespaceURI())
             continue;
-        if (attributeCountWithoutUpdate(candidate) != newElementAttributeCount)
+        if (attributeCount(candidate->token()) != newItemAttributeCount)
             continue;
 
         candidates.append(candidate);
@@ -166,38 +166,31 @@
     remainingCandidates.append(candidates);
 }
 
-void HTMLFormattingElementList::ensureNoahsArkCondition(Element* newElement)
+void HTMLFormattingElementList::ensureNoahsArkCondition(HTMLStackItem* newItem)
 {
-    Vector<Element*> candidates;
-    tryToEnsureNoahsArkConditionQuickly(newElement, candidates);
+    Vector<HTMLStackItem*> candidates;
+    tryToEnsureNoahsArkConditionQuickly(newItem, candidates);
     if (candidates.isEmpty())
         return;
 
     // We pre-allocate and re-use this second vector to save one malloc per
     // attribute that we verify.
-    Vector<Element*> remainingCandidates;
+    Vector<HTMLStackItem*> remainingCandidates;
     remainingCandidates.reserveInitialCapacity(candidates.size());
 
-    size_t newElementAttributeCount = attributeCountWithoutUpdate(newElement);
+    const Vector<Attribute>& attributes = newItem->token()->attributes();
+    for (size_t i = 0; i < attributes.size(); ++i) {
+        const Attribute& attribute = attributes[i];
 
-    for (size_t i = 0; i < newElementAttributeCount; ++i) {
-        Attribute* attribute = newElement->attributeItem(i);
-
         for (size_t j = 0; j < candidates.size(); ++j) {
-            Element* candidate = candidates[j];
+            HTMLStackItem* candidate = candidates[j];
 
             // These properties should already have been checked by tryToEnsureNoahsArkConditionQuickly.
-            ASSERT(newElement->attributeCount() == candidate->attributeCount());
-            ASSERT(newElement->tagQName() == candidate->tagQName());
+            ASSERT(attributeCount(newItem->token()) == attributeCount(candidate->token()));
+            ASSERT(newItem->localName() == candidate->localName() && newItem->namespaceURI() == candidate->namespaceURI());
 
-            // FIXME: Technically we shouldn't read this information back from
-            // the DOM. Instead, the parser should keep a copy of the information.
-            // This isn't really much of a problem for our implementation because
-            // we run the parser on the main thread, but the spec is written so
-            // that implementations can run off the main thread. If _javascript_
-            // changes the attributes values, we could get a slightly wrong
-            // output here.
-            if (candidate->fastGetAttribute(attribute->name()) == attribute->value())
+            Attribute* candidateAttribute = candidate->token()->getAttributeItem(attribute.name());
+            if (candidateAttribute && candidateAttribute->value() == attribute.value())
                 remainingCandidates.append(candidate);
         }
 
@@ -212,7 +205,7 @@
     // however, that we wil spin the loop more than once because of how the
     // formatting element list gets permuted.
     for (size_t i = kNoahsArkCapacity - 1; i < candidates.size(); ++i)
-        remove(candidates[i]);
+        remove(candidates[i]->element());
 }
 
 #ifndef NDEBUG

Modified: trunk/Source/WebCore/html/parser/HTMLFormattingElementList.h (123536 => 123537)


--- trunk/Source/WebCore/html/parser/HTMLFormattingElementList.h	2012-07-24 22:12:54 UTC (rev 123536)
+++ trunk/Source/WebCore/html/parser/HTMLFormattingElementList.h	2012-07-24 22:19:44 UTC (rev 123537)
@@ -130,8 +130,8 @@
 
     // http://www.whatwg.org/specs/web-apps/current-work/multipage/parsing.html#list-of-active-formatting-elements
     // These functions enforce the "Noah's Ark" condition, which removes redundant mis-nested elements.
-    void tryToEnsureNoahsArkConditionQuickly(Element*, Vector<Element*>& remainingCandiates);
-    void ensureNoahsArkCondition(Element*);
+    void tryToEnsureNoahsArkConditionQuickly(HTMLStackItem*, Vector<HTMLStackItem*>& remainingCandiates);
+    void ensureNoahsArkCondition(HTMLStackItem*);
 
     Vector<Entry> m_entries;
 };

Modified: trunk/Source/WebCore/html/parser/HTMLStackItem.h (123536 => 123537)


--- trunk/Source/WebCore/html/parser/HTMLStackItem.h	2012-07-24 22:12:54 UTC (rev 123536)
+++ trunk/Source/WebCore/html/parser/HTMLStackItem.h	2012-07-24 22:19:44 UTC (rev 123537)
@@ -57,6 +57,7 @@
 
     AtomicHTMLToken* token() { return m_token.get(); }
     const AtomicString& namespaceURI() const { return m_namespaceURI; }
+    const AtomicString& localName() const { return m_token->name(); }
 
 private:
     HTMLStackItem(PassRefPtr<ContainerNode> node)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to