Changeset 35143 in webkit


Ignore:
Timestamp:
Jul 11, 2008 7:13:27 PM (16 years ago)
Author:
hyatt@apple.com
Message:

2008-07-11 David Hyatt <hyatt@apple.com>

Implement the DOM level 3 compareDocumentPosition method on Node.

Reviewed by Darin

  • dom/Node.cpp: (WebCore::Node::compareDocumentPosition):
  • dom/Node.h:
  • dom/Node.idl:
Location:
trunk
Files:
35 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition01-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition01
    2 Status  error
    3 Message Line 103: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition02-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition02
    2 Status  error
    3 Message Line 113: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition03-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition03
    22Status  error
    3 Message Line 112: TypeError
     3Message Line 115: ReferenceError
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition04-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition04
    2 Status  error
    3 Message Line 96: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition05-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition05
    22Status  error
    3 Message Line 113: TypeError
     3Message Line 117: ReferenceError
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition06-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition06
    2 Status  error
    3 Message Line 103: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition07-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition07
    2 Status  error
    3 Message Line 104: TypeError
     2Status  Success
    43
    54
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition08-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition08
    2 Status  error
    3 Message Line 101: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition09-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition09
    2 Status  error
    3 Message Line 105: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition10-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition10
    2 Status  error
    3 Message Line 101: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition11-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition11
    2 Status  error
    3 Message Line 104: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition12-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition12
    2 Status  error
    3 Message Line 101: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition13-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition13
    2 Status  error
    3 Message Line 105: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition16-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition16
    22Status  error
    3 Message Line 106: TypeError
     3Message Line 109: ReferenceError
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition17-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition17
    2 Status  error
    3 Message Line 104: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition18-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition18
    2 Status  error
    3 Message Line 107: TypeError
     2Status  Success
    43T1T2
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition19-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition19
    2 Status  error
    3 Message Line 116: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition20-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition20
    2 Status  error
    3 Message Line 109: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition21-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition21
    2 Status  error
    3 Message Line 112: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition25-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition25
    2 Status  error
    3 Message Line 105: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition30-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition30
    2 Status  error
    3 Message Line 104: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition31-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition31
    2 Status  error
    3 Message Line 109: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition32-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition32
    2 Status  error
    3 Message Line 106: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition33-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition33
    22Status  error
    3 Message Line 109: TypeError
     3Message Line 116: ReferenceError
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition34-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition34
    2 Status  error
    3 Message Line 110: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition35-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition35
    2 Status  error
    3 Message Line 103: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition36-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition36
    2 Status  error
    3 Message Line 108: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition37-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition37
    2 Status  error
    3 Message Line 107: TypeError
     2Status  Success
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition38-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition38
    2 Status  error
    3 Message Line 106: TypeError
     2Status  failure
     3Message nodecomparedocumentpositionIsContainsFollowing38: assertEquals failed, actual 33, expected 20.
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition39-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition39
    2 Status  error
    3 Message Line 105: TypeError
     2Status  failure
     3Message isImplementationSpecific: assertEquals failed, actual 36, expected 32.
  • trunk/LayoutTests/dom/xhtml/level3/core/nodecomparedocumentposition40-expected.txt

    r33979 r35143  
    11Test    http://www.w3.org/2001/DOM-Test-Suite/level3/core/nodecomparedocumentposition40
    2 Status  error
    3 Message Line 106: TypeError
     2Status  failure
     3Message isImplementationSpecific: assertEquals failed, actual 36, expected 32.
  • trunk/WebCore/ChangeLog

    r35141 r35143  
     12008-07-11  David Hyatt  <hyatt@apple.com>
     2
     3        Implement the DOM level 3 compareDocumentPosition method on Node.
     4
     5        Reviewed by Darin
     6
     7        * dom/Node.cpp:
     8        (WebCore::Node::compareDocumentPosition):
     9        * dom/Node.h:
     10        * dom/Node.idl:
     11
    1122008-07-11  Brady Eidson  <beidson@apple.com>
    213
  • trunk/WebCore/dom/Node.cpp

    r35122 r35143  
    17711771}
    17721772
     1773unsigned short Node::compareDocumentPosition(Node* otherNode, ExceptionCode& ec)
     1774{
     1775    // It is not clear what should be done if |otherNode| is 0.
     1776    if (!otherNode)
     1777        return DOCUMENT_POSITION_DISCONNECTED;
     1778
     1779    if (otherNode == this)
     1780        return DOCUMENT_POSITION_EQUIVALENT;
     1781   
     1782    Attr* attr1 = nodeType() == ATTRIBUTE_NODE ? static_cast<Attr*>(this) : 0;
     1783    Attr* attr2 = otherNode->nodeType() == ATTRIBUTE_NODE ? static_cast<Attr*>(otherNode) : 0;
     1784   
     1785    Node* start1 = attr1 ? attr1->ownerElement() : this;
     1786    Node* start2 = attr2 ? attr2->ownerElement() : otherNode;
     1787   
     1788    // If either of start1 or start2 is null, then we are disconnected, since one of the nodes is
     1789    // an orphaned attribute node.
     1790    if (!start1 || !start2)
     1791        return DOCUMENT_POSITION_DISCONNECTED | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC;
     1792
     1793    Vector<Node*, 16> chain1;
     1794    Vector<Node*, 16> chain2;
     1795    if (attr1)
     1796        chain1.append(attr1);
     1797    if (attr2)
     1798        chain2.append(attr2);
     1799   
     1800    if (attr1 && attr2 && start1 == start2 && start1) {
     1801        // We are comparing two attributes on the same node.  Crawl our attribute map
     1802        // and see which one we hit first.
     1803        NamedAttrMap* map = attr1->ownerElement()->attributes(true);
     1804        unsigned length = map->length();
     1805        for (unsigned i = 0; i < length; ++i) {
     1806            // If neither of the two determining nodes is a child node and nodeType is the same for both determining nodes, then an
     1807            // implementation-dependent order between the determining nodes is returned. This order is stable as long as no nodes of
     1808            // the same nodeType are inserted into or removed from the direct container. This would be the case, for example,
     1809            // when comparing two attributes of the same element, and inserting or removing additional attributes might change
     1810            // the order between existing attributes.
     1811            Attribute* attr = map->attributeItem(i);
     1812            if (attr1->attr() == attr)
     1813                return DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | DOCUMENT_POSITION_FOLLOWING;
     1814            if (attr2->attr() == attr)
     1815                return DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | DOCUMENT_POSITION_PRECEDING;
     1816        }
     1817       
     1818        ASSERT_NOT_REACHED();
     1819        return DOCUMENT_POSITION_DISCONNECTED;
     1820    }
     1821
     1822    // If one node is in the document and the other is not, we must be disconnected.
     1823    // If the nodes have different owning documents, they must be disconnected.  Note that we avoid
     1824    // comparing Attr nodes here, since they return false from inDocument() all the time (which seems like a bug).
     1825    if (start1->inDocument() != start2->inDocument() ||
     1826        start1->document() != start2->document())
     1827        return DOCUMENT_POSITION_DISCONNECTED | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC;
     1828
     1829    // We need to find a common ancestor container, and then compare the indices of the two immediate children.
     1830    Node* current;
     1831    for (current = start1; current; current = current->parentNode())
     1832        chain1.append(current);
     1833    for (current = start2; current; current = current->parentNode())
     1834        chain2.append(current);
     1835   
     1836    // Walk the two chains backwards and look for the first difference.
     1837    unsigned index1 = chain1.size();
     1838    unsigned index2 = chain2.size();
     1839    for (unsigned i = std::min(index1, index2); i; --i) {
     1840        Node* child1 = chain1[--index1];
     1841        Node* child2 = chain2[--index2];
     1842        if (child1 != child2) {
     1843            // If one of the children is an attribute, it wins.
     1844            if (child1->nodeType() == ATTRIBUTE_NODE)
     1845                return DOCUMENT_POSITION_FOLLOWING;
     1846            if (child2->nodeType() == ATTRIBUTE_NODE)
     1847                return DOCUMENT_POSITION_PRECEDING;
     1848           
     1849            // Otherwise we need to see which node occurs first.  Crawl backwards from child2 looking for child1.
     1850            for (Node* child = child2->previousSibling(); child; child = child->previousSibling()) {
     1851                if (child == child1)
     1852                    return DOCUMENT_POSITION_FOLLOWING;
     1853            }
     1854            return DOCUMENT_POSITION_PRECEDING;
     1855        }
     1856    }
     1857   
     1858    // There was no difference between the two parent chains, i.e., one was a subset of the other.  The shorter
     1859    // chain is the ancestor.
     1860    return index1 < index2 ?
     1861               DOCUMENT_POSITION_FOLLOWING | DOCUMENT_POSITION_CONTAINED_BY :
     1862               DOCUMENT_POSITION_PRECEDING | DOCUMENT_POSITION_CONTAINS;
     1863}
     1864
    17731865#ifndef NDEBUG
    17741866
  • trunk/WebCore/dom/Node.h

    r35122 r35143  
    6464enum StyleChangeType { NoStyleChange, InlineStyleChange, FullStyleChange };
    6565
     66const unsigned short      DOCUMENT_POSITION_EQUIVALENT   = 0x00;
     67const unsigned short      DOCUMENT_POSITION_DISCONNECTED = 0x01;
     68const unsigned short      DOCUMENT_POSITION_PRECEDING    = 0x02;
     69const unsigned short      DOCUMENT_POSITION_FOLLOWING    = 0x04;
     70const unsigned short      DOCUMENT_POSITION_CONTAINS     = 0x08;
     71const unsigned short      DOCUMENT_POSITION_CONTAINED_BY = 0x10;
     72const unsigned short      DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC = 0x20;
     73
    6674// this class implements nodes, which can have a parent but no children:
    6775class Node : public TreeShared<Node> {
     
    8391        XPATH_NAMESPACE_NODE = 13
    8492    };
    85 
     93   
    8694    static bool isSupported(const String& feature, const String& version);
    8795
     
    475483    PassRefPtr<NodeList> querySelectorAll(const String& selectors, ExceptionCode&);
    476484
     485    unsigned short compareDocumentPosition(Node*, ExceptionCode&);
     486
    477487private: // members
    478488    DocPtr<Document> m_document;
  • trunk/WebCore/dom/Node.idl

    r34139 r35143  
    102102        [ConvertNullStringTo=Null] DOMString          lookupNamespaceURI(in [ConvertNullToNullString] DOMString prefix);
    103103
    104 #if 0
    105104        // DocumentPosition
    106105        const unsigned short      DOCUMENT_POSITION_DISCONNECTED = 0x01;
     
    114113            raises(DOMException);
    115114
     115#if 0
    116116        DOMObject          getFeature(in DOMString feature,
    117117                                      in DOMString version);
Note: See TracChangeset for help on using the changeset viewer.