OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2011 Motorola Mobility, Inc. All rights reserved. | |
3 * | |
4 * Redistribution and use in source and binary forms, with or without modificati
on, | |
5 * are permitted provided that the following conditions are met: | |
6 * | |
7 * Redistributions of source code must retain the above copyright notice, | |
8 * this list of conditions and the following disclaimer. | |
9 * | |
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. | |
13 * | |
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. | |
17 * | |
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. | |
29 */ | |
30 | |
31 #include "config.h" | |
32 | |
33 #if ENABLE(MICRODATA) | |
34 | |
35 #include "HTMLPropertiesCollection.h" | |
36 | |
37 #include "DOMSettableTokenList.h" | |
38 #include "HTMLElement.h" | |
39 #include "HTMLNames.h" | |
40 #include "Node.h" | |
41 #include "NodeTraversal.h" | |
42 #include "PropertyNodeList.h" | |
43 | |
44 namespace WebCore { | |
45 | |
46 using namespace HTMLNames; | |
47 | |
48 PassRefPtr<HTMLPropertiesCollection> HTMLPropertiesCollection::create(Node* item
Node, CollectionType) | |
49 { | |
50 return adoptRef(new HTMLPropertiesCollection(itemNode)); | |
51 } | |
52 | |
53 HTMLPropertiesCollection::HTMLPropertiesCollection(Node* itemNode) | |
54 : HTMLCollection(itemNode, ItemProperties, OverridesItemAfter) | |
55 { | |
56 } | |
57 | |
58 HTMLPropertiesCollection::~HTMLPropertiesCollection() | |
59 { | |
60 } | |
61 | |
62 void HTMLPropertiesCollection::updateRefElements() const | |
63 { | |
64 if (isItemRefElementsCacheValid()) | |
65 return; | |
66 | |
67 m_itemRefElements.clear(); | |
68 setItemRefElementsCacheValid(); | |
69 toHTMLElement(ownerNode())->getItemRefElements(m_itemRefElements); | |
70 } | |
71 | |
72 static Node* nextNodeWithProperty(Node* rootNode, Node* previous, Node* ownerNod
e) | |
73 { | |
74 // An Microdata item may contain properties which in turn are items themselv
es. Properties can | |
75 // also themselves be groups of name-value pairs, by putting the itemscope a
ttribute on the element | |
76 // that declares the property. If the property has an itemscope attribute sp
ecified 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); | |
81 } | |
82 | |
83 Element* HTMLPropertiesCollection::virtualItemAfter(unsigned& offsetInArray, Ele
ment* previousItem) const | |
84 { | |
85 while (offsetInArray < m_itemRefElements.size()) { | |
86 if (Element* next = virtualItemAfter(m_itemRefElements[offsetInArray], p
reviousItem)) | |
87 return next; | |
88 offsetInArray++; | |
89 previousItem = 0; | |
90 } | |
91 return 0; | |
92 } | |
93 | |
94 HTMLElement* HTMLPropertiesCollection::virtualItemAfter(HTMLElement* rootNode, E
lement* previous) const | |
95 { | |
96 Node* current; | |
97 Node* ownerNode = this->ownerNode(); | |
98 current = previous ? nextNodeWithProperty(rootNode, previous, ownerNode) : r
ootNode; | |
99 | |
100 for (; current; current = nextNodeWithProperty(rootNode, current, ownerNode)
) { | |
101 if (current == ownerNode || !current->isHTMLElement()) | |
102 continue; | |
103 HTMLElement* element = toHTMLElement(current); | |
104 if (element->fastHasAttribute(itempropAttr) && element->itemProp()->leng
th()) { | |
105 return element; | |
106 } | |
107 } | |
108 | |
109 return 0; | |
110 } | |
111 | |
112 void HTMLPropertiesCollection::updateNameCache() const | |
113 { | |
114 if (hasNameCache()) | |
115 return; | |
116 | |
117 updateRefElements(); | |
118 | |
119 for (unsigned i = 0; i < m_itemRefElements.size(); ++i) { | |
120 HTMLElement* refElement = m_itemRefElements[i]; | |
121 for (HTMLElement* element = virtualItemAfter(refElement, 0); element; el
ement = virtualItemAfter(refElement, element)) { | |
122 DOMSettableTokenList* itemProperty = element->itemProp(); | |
123 for (unsigned propertyIndex = 0; propertyIndex < itemProperty->lengt
h(); ++propertyIndex) | |
124 updatePropertyCache(itemProperty->item(propertyIndex)); | |
125 } | |
126 } | |
127 | |
128 setHasNameCache(); | |
129 } | |
130 | |
131 PassRefPtr<DOMStringList> HTMLPropertiesCollection::names() const | |
132 { | |
133 updateNameCache(); | |
134 if (!m_propertyNames) | |
135 m_propertyNames = DOMStringList::create(); | |
136 return m_propertyNames; | |
137 } | |
138 | |
139 PassRefPtr<PropertyNodeList> HTMLPropertiesCollection::propertyNodeList(const St
ring& name) const | |
140 { | |
141 return ownerNode()->propertyNodeList(name); | |
142 } | |
143 | |
144 bool HTMLPropertiesCollection::hasNamedItem(const AtomicString& name) const | |
145 { | |
146 updateNameCache(); | |
147 if (m_propertyNames) | |
148 return m_propertyNames->contains(name); | |
149 return false; | |
150 } | |
151 | |
152 } // namespace WebCore | |
153 | |
154 #endif // ENABLE(MICRODATA) | |
OLD | NEW |