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

Side by Side Diff: Source/core/dom/CSSSelectorWatch.cpp

Issue 18371008: Add a WebDocument::watchCssSelectors(selectors) (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@pinned
Patch Set: Fix most comments; TODO benchmark and use Element::recalcStyle Created 7 years, 5 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
OLDNEW
(Empty)
1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without 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
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "config.h"
32 #include "core/dom/CSSSelectorWatch.h"
33
34 #include "core/css/CSSParser.h"
35 #include "core/css/CSSSelectorList.h"
36 #include "core/css/StylePropertySet.h"
37 #include "core/css/StyleRule.h"
38 #include "core/dom/Document.h"
39 #include "core/dom/ScriptExecutionContext.h"
40 #include "core/loader/FrameLoaderClient.h"
41 #include "core/page/Frame.h"
42 #include "core/rendering/style/StyleRareNonInheritedData.h"
43
44 namespace WebCore {
45
46 // The address of this string is important; its value is just documentation.
47 static const char kSupplementName[] = "CSSSelectorWatch";
48
49 CSSSelectorWatch::CSSSelectorWatch(Document* document)
50 : m_document(document)
51 , m_callbackSelectorChangeTimer(this, &CSSSelectorWatch::callbackSelectorCha ngeTimerFired)
52 , m_weakThisFactory(this)
53 {
54 }
55
56 CSSSelectorWatch::~CSSSelectorWatch()
57 {
58 }
esprehn 2013/07/17 07:14:32 We often just leave this in the header like { }
Jeffrey Yasskin 2013/08/02 01:34:07 'k.
59
60 CSSSelectorWatch* CSSSelectorWatch::from(Document* document)
61 {
62 CSSSelectorWatch* watch = static_cast<CSSSelectorWatch*>(Supplement<ScriptEx ecutionContext>::from(document, kSupplementName));
63 if (!watch) {
64 watch = new CSSSelectorWatch(document);
65 Supplement<ScriptExecutionContext>::provideTo(document, kSupplementName, adoptPtr(watch));
66 }
67 return watch;
68 }
69
70 void CSSSelectorWatch::callbackSelectorChangeTimerFired(Timer<CSSSelectorWatch>* )
71 {
72 if (m_addedSelectors.isEmpty() && m_removedSelectors.isEmpty())
73 return;
74 if (m_document->frame()) {
75 Vector<String> addedSelectors, removedSelectors;
76 copyToVector(m_addedSelectors, addedSelectors);
77 copyToVector(m_removedSelectors, removedSelectors);
esprehn 2013/07/17 07:14:32 Use swap() instead.
Jeffrey Yasskin 2013/08/02 01:34:07 m_removedSelectors is a HashSet, while removedSele
78 m_document->frame()->loader()->client()->selectorMatchChanged(addedSelec tors, removedSelectors);
79 }
80 m_addedSelectors.clear();
81 m_removedSelectors.clear();
esprehn 2013/07/17 07:14:32 remove once you swap()
82 }
83
84 void CSSSelectorWatch::addSelectorMatch(const String& selector, StyleRareNonInhe ritedData* data)
85 {
86 // Set m_selectorWatch here to avoid exposing the weakFactory outside of CSS SelectorWatch.
87 data->m_selectorWatch = m_weakThisFactory.createWeakPtr();
88
89 HashMap<String, int>::iterator count = m_matchingCallbackSelectors.find(sele ctor);
90 if (count != m_matchingCallbackSelectors.end()) {
91 ++count->value;
92 return;
93 }
94 if (!m_callbackSelectorChangeTimer.isActive())
95 m_callbackSelectorChangeTimer.startOneShot(0);
96
97 m_matchingCallbackSelectors.set(selector, 1);
98 if (m_removedSelectors.contains(selector))
99 m_removedSelectors.remove(selector);
100 else
101 m_addedSelectors.add(selector);
102 }
103
104 void CSSSelectorWatch::removeSelectorMatch(const String& selector)
105 {
106 HashMap<String, int>::iterator count = m_matchingCallbackSelectors.find(sele ctor);
107 if (count == m_matchingCallbackSelectors.end())
108 return;
109 --count->value;
110 if (!count->value) {
111 if (!m_callbackSelectorChangeTimer.isActive())
112 m_callbackSelectorChangeTimer.startOneShot(0);
113
114 m_matchingCallbackSelectors.remove(count);
115 if (m_addedSelectors.contains(selector))
116 m_addedSelectors.remove(selector);
117 else
118 m_removedSelectors.add(selector);
119 }
120 }
121
122 void CSSSelectorWatch::watchCSSSelectors(const Vector<String>& selectors)
123 {
124 m_watchedCallbackSelectors.clear();
125 CSSParserContext context(UASheetMode);
126 CSSParser parser(context);
127
128 const CSSProperty callbackProperty(CSSPropertyInternalCallback, CSSPrimitive Value::createIdentifier(CSSValueInternalPresence));
129 const RefPtr<StylePropertySet> callbackPropertySet = ImmutableStylePropertyS et::create(&callbackProperty, 1, UASheetMode);
130
131 CSSSelectorList selectorList;
132 for (unsigned i = 0; i < selectors.size(); ++i) {
133 RefPtr<StyleRule> rule = StyleRule::create(i);
134
135 parser.parseSelector(selectors[i], selectorList);
esprehn 2013/07/17 07:14:32 What about invalid selectors?
Jeffrey Yasskin 2013/08/02 01:34:07 Fixed this by omitting invalid selectors, and test
136 rule->wrapperAdoptSelectorList(selectorList);
137
138 rule->setProperties(callbackPropertySet);
139
140 m_watchedCallbackSelectors.append(rule.release());
141 }
142 m_document->styleResolverChanged(DeferRecalcStyle);
143 }
144
145 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698