2 * Copyright (c) 2011 Motorola Mobility, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without modification,
5 * are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation and/or
12 * other materials provided with the distribution.
14 * Neither the name of Motorola Mobility, Inc. nor the names of its contributors may
15 * be used to endorse or promote products derived from this software without
16 * specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "HTMLPropertiesCollection.h"
37 #include "DOMSettableTokenList.h"
38 #include "HTMLElement.h"
39 #include "HTMLNames.h"
41 #include "NodeTraversal.h"
42 #include "PropertyNodeList.h"
46 using namespace HTMLNames;
48 PassRefPtr<HTMLPropertiesCollection> HTMLPropertiesCollection::create(Node* itemNode, CollectionType)
50 return adoptRef(new HTMLPropertiesCollection(itemNode));
53 HTMLPropertiesCollection::HTMLPropertiesCollection(Node* itemNode)
54 : HTMLCollection(itemNode, ItemProperties, OverridesItemAfter)
58 HTMLPropertiesCollection::~HTMLPropertiesCollection()
62 void HTMLPropertiesCollection::updateRefElements() const
64 if (isItemRefElementsCacheValid())
67 m_itemRefElements.clear();
68 setItemRefElementsCacheValid();
69 toHTMLElement(ownerNode())->getItemRefElements(m_itemRefElements);
72 static Node* nextNodeWithProperty(Node* rootNode, Node* previous, Node* ownerNode)
74 // An Microdata item may contain properties which in turn are items themselves. Properties can
75 // also themselves be groups of name-value pairs, by putting the itemscope attribute on the element
76 // that declares the property. If the property has an itemscope attribute specified then we need
77 // to traverse the next sibling.
78 return previous == ownerNode || (previous->isHTMLElement() && !toHTMLElement(previous)->fastHasAttribute(itemscopeAttr))
79 ? NodeTraversal::next(previous, rootNode)
80 : NodeTraversal::nextSkippingChildren(previous, rootNode);
83 Element* HTMLPropertiesCollection::virtualItemAfter(unsigned& offsetInArray, Element* previousItem) const
85 while (offsetInArray < m_itemRefElements.size()) {
86 if (Element* next = virtualItemAfter(m_itemRefElements[offsetInArray], previousItem))
94 HTMLElement* HTMLPropertiesCollection::virtualItemAfter(HTMLElement* rootNode, Element* previous) const
97 Node* ownerNode = this->ownerNode();
98 current = previous ? nextNodeWithProperty(rootNode, previous, ownerNode) : rootNode;
100 for (; current; current = nextNodeWithProperty(rootNode, current, ownerNode)) {
101 if (current == ownerNode || !current->isHTMLElement())
103 HTMLElement* element = toHTMLElement(current);
104 if (element->fastHasAttribute(itempropAttr) && element->itemProp()->length()) {
112 void HTMLPropertiesCollection::updateNameCache() const
119 for (unsigned i = 0; i < m_itemRefElements.size(); ++i) {
120 HTMLElement* refElement = m_itemRefElements[i];
121 for (HTMLElement* element = virtualItemAfter(refElement, 0); element; element = virtualItemAfter(refElement, element)) {
122 DOMSettableTokenList* itemProperty = element->itemProp();
123 for (unsigned propertyIndex = 0; propertyIndex < itemProperty->length(); ++propertyIndex)
124 updatePropertyCache(itemProperty->item(propertyIndex));
131 PassRefPtr<DOMStringList> HTMLPropertiesCollection::names() const
134 if (!m_propertyNames)
135 m_propertyNames = DOMStringList::create();
136 return m_propertyNames;
139 PassRefPtr<PropertyNodeList> HTMLPropertiesCollection::propertyNodeList(const String& name) const
141 return ownerNode()->propertyNodeList(name);
144 bool HTMLPropertiesCollection::hasNamedItem(const AtomicString& name) const
148 return m_propertyNames->contains(name);
152 } // namespace WebCore
154 #endif // ENABLE(MICRODATA)