1/*
2 * Copyright (C) 2016 Apple 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
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "AttributeChangeInvalidation.h"
28
29#include "ElementIterator.h"
30#include "StyleInvalidationFunctions.h"
31#include "StyleInvalidator.h"
32
33namespace WebCore {
34namespace Style {
35
36static bool mayBeAffectedByAttributeChange(const RuleFeatureSet& features, bool isHTML, const QualifiedName& attributeName)
37{
38 auto& nameSet = isHTML ? features.attributeCanonicalLocalNamesInRules : features.attributeLocalNamesInRules;
39 return nameSet.contains(attributeName.localName());
40}
41
42void AttributeChangeInvalidation::invalidateStyle(const QualifiedName& attributeName, const AtomicString& oldValue, const AtomicString& newValue)
43{
44 if (newValue == oldValue)
45 return;
46
47 bool isHTML = m_element.isHTMLElement();
48
49 bool shouldInvalidateCurrent = false;
50 bool mayAffectStyleInShadowTree = false;
51
52 auto attributeNameForLookups = attributeName.localName().convertToASCIILowercase();
53
54 traverseRuleFeatures(m_element, [&] (const RuleFeatureSet& features, bool mayAffectShadowTree) {
55 if (mayAffectShadowTree && mayBeAffectedByAttributeChange(features, isHTML, attributeName))
56 mayAffectStyleInShadowTree = true;
57 if (features.attributesAffectingHost.contains(attributeNameForLookups))
58 shouldInvalidateCurrent = true;
59 else if (features.contentAttributeNamesInRules.contains(attributeNameForLookups))
60 shouldInvalidateCurrent = true;
61 });
62
63 if (mayAffectStyleInShadowTree) {
64 // FIXME: More fine-grained invalidation.
65 m_element.invalidateStyleForSubtree();
66 }
67
68 if (shouldInvalidateCurrent)
69 m_element.invalidateStyle();
70
71 auto& ruleSets = m_element.styleResolver().ruleSets();
72
73 auto* invalidationRuleSets = ruleSets.attributeInvalidationRuleSets(attributeNameForLookups);
74 if (!invalidationRuleSets)
75 return;
76
77 for (auto& invalidationRuleSet : *invalidationRuleSets) {
78 for (auto* selector : invalidationRuleSet.invalidationSelectors) {
79 bool oldMatches = !oldValue.isNull() && SelectorChecker::attributeSelectorMatches(m_element, attributeName, oldValue, *selector);
80 bool newMatches = !newValue.isNull() && SelectorChecker::attributeSelectorMatches(m_element, attributeName, newValue, *selector);
81 if (oldMatches != newMatches) {
82 m_invalidationRuleSets.append(&invalidationRuleSet);
83 break;
84 }
85 }
86 }
87}
88
89void AttributeChangeInvalidation::invalidateStyleWithRuleSets()
90{
91 for (auto* invalidationRuleSet : m_invalidationRuleSets) {
92 Invalidator invalidator(*invalidationRuleSet->ruleSet);
93 invalidator.invalidateStyleWithMatchElement(m_element, invalidationRuleSet->matchElement);
94 }
95}
96
97
98}
99}
100