Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: Source/core/html/HTMLAllCollection.cpp

Issue 158563004: Make HTMLAllCollection named property getter behave more according to spec (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Fix linking error Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/html/HTMLAllCollection.h ('k') | Source/core/html/HTMLAllCollection.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2009, 2011, 2012 Apple Inc. All rights reserved. 2 * Copyright (C) 2009, 2011, 2012 Apple Inc. All rights reserved.
3 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
3 * 4 *
4 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
6 * are met: 7 * are met:
7 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 11 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution. 12 * documentation and/or other materials provided with the distribution.
12 * 13 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY 14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR 17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */ 25 */
25 26
26 #include "config.h" 27 #include "config.h"
27 #include "core/html/HTMLAllCollection.h" 28 #include "core/html/HTMLAllCollection.h"
28 29
30 #include "HTMLNames.h"
29 #include "core/dom/Element.h" 31 #include "core/dom/Element.h"
30 #include "core/dom/NamedNodesCollection.h" 32 #include "core/dom/NamedNodesCollection.h"
33 #include "core/html/HTMLElement.h"
34 #include "wtf/HashSet.h"
31 35
32 namespace WebCore { 36 namespace WebCore {
33 37
38 using namespace HTMLNames;
39
34 PassRefPtr<HTMLAllCollection> HTMLAllCollection::create(ContainerNode* node, Col lectionType type) 40 PassRefPtr<HTMLAllCollection> HTMLAllCollection::create(ContainerNode* node, Col lectionType type)
35 { 41 {
36 return adoptRef(new HTMLAllCollection(node, type)); 42 return adoptRef(new HTMLAllCollection(node, type));
37 } 43 }
38 44
39 HTMLAllCollection::HTMLAllCollection(ContainerNode* node, CollectionType type) 45 HTMLAllCollection::HTMLAllCollection(ContainerNode* node, CollectionType type)
40 : HTMLCollection(node, type, DoesNotOverrideItemAfter) 46 : HTMLCollection(node, type, DoesNotOverrideItemAfter)
41 { 47 {
42 ScriptWrappable::init(this); 48 ScriptWrappable::init(this);
43 } 49 }
44 50
51 bool HTMLAllCollection::elementNameShouldBeVisible(const HTMLElement& element)
52 {
53 // The document.all collection returns only certain types of elements by nam e,
54 // although it returns any type of element by id.
55 return element.hasLocalName(aTag)
56 || element.hasLocalName(appletTag)
57 || element.hasLocalName(areaTag)
58 || element.hasLocalName(embedTag)
59 || element.hasLocalName(formTag)
60 || element.hasLocalName(frameTag)
61 || element.hasLocalName(framesetTag)
62 || element.hasLocalName(iframeTag)
63 || element.hasLocalName(imgTag)
64 || element.hasLocalName(objectTag)
65 || element.hasLocalName(inputTag) // FIXME: Not according to spec.
arv (Not doing code reviews) 2014/02/11 20:48:42 If IE11 behaves this way we should get the spec ch
Inactive 2014/02/11 20:50:37 Yes, based on my testing IE11 return the names of
Inactive 2014/02/11 20:55:10 http://jsfiddle.net/jAx2t/3/
Inactive 2014/02/11 21:02:39 I filed a bug against the spec: https://www.w3.org
Inactive 2014/02/12 01:32:37 Would you like me to remove those 2 FIXME comments
66 || element.hasLocalName(selectTag); // FIXME: Not according to spec.
67 }
68
69 void HTMLAllCollection::supportedPropertyNames(Vector<String>& names)
70 {
71 // As per the specification (http://www.whatwg.org/specs/web-apps/current-wo rk/multipage/common-dom-interfaces.html#htmlallcollection-0):
72 // The supported property names consist of the non-empty values of all the i d attributes of all the elements
73 // represented by the collection, and the non-empty values of all the name a ttributes of all the a, applet,
74 // area, embed, form, frame, frameset, iframe, img, and object elements repr esented by the collection, in
75 // tree order, ignoring later duplicates, with the id of an element precedin g its name if it contributes
76 // both, they differ from each other, and neither is the duplicate of an ear lier entry.
77 HashSet<AtomicString> existingNames;
78 ContainerNode& root = rootNode();
79 for (Element* element = traverseToFirstElement(root); element; element = tra verseNextElement(*element, root)) {
haraken 2014/02/12 01:23:01 We're duplicating this code in several files. It s
Inactive 2014/02/12 01:31:05 If it was just about the condition, I would have s
Inactive 2014/02/12 16:28:41 I am updating the id/name order in HTMLCollection
80 const AtomicString& idAttribute = element->getIdAttribute();
81 if (!idAttribute.isEmpty()) {
82 HashSet<AtomicString>::AddResult addResult = existingNames.add(idAtt ribute);
83 if (addResult.isNewEntry)
84 names.append(idAttribute);
85 }
86 if (!element->isHTMLElement())
87 continue;
88 const AtomicString& nameAttribute = element->getNameAttribute();
89 if (!nameAttribute.isEmpty() && elementNameShouldBeVisible(toHTMLElement (*element))) {
90 HashSet<AtomicString>::AddResult addResult = existingNames.add(nameA ttribute);
91 if (addResult.isNewEntry)
92 names.append(nameAttribute);
93 }
94 }
95 }
96
45 HTMLAllCollection::~HTMLAllCollection() 97 HTMLAllCollection::~HTMLAllCollection()
46 { 98 {
47 } 99 }
48 100
49 Element* HTMLAllCollection::namedItemWithIndex(const AtomicString& name, unsigne d index) const 101 Element* HTMLAllCollection::namedItemWithIndex(const AtomicString& name, unsigne d index) const
50 { 102 {
51 updateNameCache(); 103 updateNameCache();
52 104
53 if (Vector<Element*>* cache = idCache(name)) { 105 if (Vector<Element*>* cache = idCache(name)) {
54 if (index < cache->size()) 106 if (index < cache->size())
55 return cache->at(index); 107 return cache->at(index);
56 index -= cache->size(); 108 index -= cache->size();
57 } 109 }
58 110
59 if (Vector<Element*>* cache = nameCache(name)) { 111 if (Vector<Element*>* cache = nameCache(name)) {
60 if (index < cache->size()) 112 if (index < cache->size())
61 return cache->at(index); 113 return cache->at(index);
62 } 114 }
63 115
64 return 0; 116 return 0;
65 } 117 }
66 118
67 void HTMLAllCollection::anonymousNamedGetter(const AtomicString& name, bool& ret urnValue0Enabled, RefPtr<NodeList>& returnValue0, bool& returnValue1Enabled, Ref Ptr<Element>& returnValue1) 119 void HTMLAllCollection::namedGetter(const AtomicString& name, bool& returnValue0 Enabled, RefPtr<NodeList>& returnValue0, bool& returnValue1Enabled, RefPtr<Eleme nt>& returnValue1)
68 { 120 {
69 Vector<RefPtr<Element> > namedItems; 121 Vector<RefPtr<Element> > namedItems;
70 this->namedItems(name, namedItems); 122 this->namedItems(name, namedItems);
71 123
72 if (!namedItems.size()) 124 if (!namedItems.size())
73 return; 125 return;
74 126
75 if (namedItems.size() == 1) { 127 if (namedItems.size() == 1) {
76 returnValue1Enabled = true; 128 returnValue1Enabled = true;
77 returnValue1 = namedItems.at(0); 129 returnValue1 = namedItems.at(0);
78 return; 130 return;
79 } 131 }
80 132
81 // FIXME: HTML5 specification says this should be a HTMLCollection. 133 // FIXME: HTML5 specification says this should be a HTMLCollection.
82 // http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-in terfaces.html#htmlallcollection 134 // http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-in terfaces.html#htmlallcollection
83 returnValue0Enabled = true; 135 returnValue0Enabled = true;
84 returnValue0 = NamedNodesCollection::create(namedItems); 136 returnValue0 = NamedNodesCollection::create(namedItems);
85 } 137 }
86 138
87 } // namespace WebCore 139 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/html/HTMLAllCollection.h ('k') | Source/core/html/HTMLAllCollection.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698