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

Side by Side Diff: third_party/WebKit/Source/core/dom/Element.cpp

Issue 2473743003: Call Element::rebuildLayoutTree from Document::updateStyle directly (Closed)
Patch Set: Mirror setChildNeedsStyleRecalc and markAncestorsWithNeedsReattachLayoutTree with setChildNeedsReat… Created 3 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
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Peter Kelly (pmk@post.com) 4 * (C) 2001 Peter Kelly (pmk@post.com)
5 * (C) 2001 Dirk Mueller (mueller@kde.org) 5 * (C) 2001 Dirk Mueller (mueller@kde.org)
6 * (C) 2007 David Smith (catfish.man@gmail.com) 6 * (C) 2007 David Smith (catfish.man@gmail.com)
7 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012, 2013 Apple Inc. 7 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012, 2013 Apple Inc.
8 * All rights reserved. 8 * All rights reserved.
9 * (C) 2007 Eric Seidel (eric@webkit.org) 9 * (C) 2007 Eric Seidel (eric@webkit.org)
10 * 10 *
(...skipping 1866 matching lines...) Expand 10 before | Expand all | Expand 10 after
1877 } 1877 }
1878 1878
1879 PassRefPtr<ComputedStyle> Element::originalStyleForLayoutObject() { 1879 PassRefPtr<ComputedStyle> Element::originalStyleForLayoutObject() {
1880 DCHECK(document().inStyleRecalc()); 1880 DCHECK(document().inStyleRecalc());
1881 return document().ensureStyleResolver().styleForElement(this); 1881 return document().ensureStyleResolver().styleForElement(this);
1882 } 1882 }
1883 1883
1884 void Element::recalcStyle(StyleRecalcChange change, Text* nextTextSibling) { 1884 void Element::recalcStyle(StyleRecalcChange change, Text* nextTextSibling) {
1885 DCHECK(document().inStyleRecalc()); 1885 DCHECK(document().inStyleRecalc());
1886 DCHECK(!document().lifecycle().inDetach()); 1886 DCHECK(!document().lifecycle().inDetach());
1887 DCHECK(!parentOrShadowHostNode()->needsStyleRecalc());
esprehn 2017/02/16 05:49:25 ditto, entering into here with a dirty style is a
nainar 2017/02/16 06:47:54 This isn't true anymore in case we need this infor
esprehn 2017/02/16 06:54:12 Hmm? We should have cleared all the bits by callin
nainar 2017/02/16 07:13:20 Test case: <div style="display:none"> <input ty
1888 DCHECK(inActiveDocument()); 1887 DCHECK(inActiveDocument());
1889 1888
1890 if (hasCustomStyleCallbacks()) 1889 if (hasCustomStyleCallbacks())
1891 willRecalcStyle(change); 1890 willRecalcStyle(change);
1892 1891
1893 if (change >= IndependentInherit || needsStyleRecalc()) { 1892 if (change >= IndependentInherit || needsStyleRecalc()) {
1894 if (hasRareData()) { 1893 if (hasRareData()) {
1895 ElementRareData* data = elementRareData(); 1894 ElementRareData* data = elementRareData();
1896 if (change != IndependentInherit) 1895 if (change != IndependentInherit)
1897 data->clearComputedStyle(); 1896 data->clearComputedStyle();
1898 1897
1899 if (change >= IndependentInherit) { 1898 if (change >= IndependentInherit) {
1900 if (ElementAnimations* elementAnimations = data->elementAnimations()) 1899 if (ElementAnimations* elementAnimations = data->elementAnimations())
1901 elementAnimations->setAnimationStyleChange(false); 1900 elementAnimations->setAnimationStyleChange(false);
1902 } 1901 }
1903 } 1902 }
1904 if (parentComputedStyle()) 1903 if (parentComputedStyle()) {
1905 change = recalcOwnStyle(change, nextTextSibling); 1904 change = recalcOwnStyle(change, nextTextSibling);
1906 clearNeedsStyleRecalc(); 1905 } else {
1907 clearNeedsReattachLayoutTree(); 1906 // In case we don't perform recalcOwnStyle we will never clear the
1907 // NeedsReattachLayoutTree flag which is set on the creation of each
1908 // Node. Clear that here.
1909 clearNeedsReattachLayoutTree();
esprehn 2017/02/16 05:49:25 why would this be true? We should still follow the
nainar 2017/02/16 06:47:54 Done.
1910 }
1908 } 1911 }
1909 1912
1910 // If we reattached we don't need to recalc the style of our descendants 1913 // If we are going to reattach we don't need to recalc the style of
1911 // anymore. 1914 // our descendants anymore.
1912 if ((change >= UpdatePseudoElements && change < Reattach) || 1915 if (change < Reattach &&
1913 childNeedsStyleRecalc()) { 1916 (change >= UpdatePseudoElements || childNeedsStyleRecalc())) {
1914 SelectorFilterParentScope filterScope(*this); 1917 SelectorFilterParentScope filterScope(*this);
1915 StyleSharingDepthScope sharingScope(*this); 1918 StyleSharingDepthScope sharingScope(*this);
1916 1919
1917 updatePseudoElement(PseudoIdBefore, change); 1920 updatePseudoElement(PseudoIdBefore, change);
1918 1921
1919 if (change > UpdatePseudoElements || childNeedsStyleRecalc()) { 1922 if (change > UpdatePseudoElements || childNeedsStyleRecalc()) {
1920 for (ShadowRoot* root = youngestShadowRoot(); root; 1923 for (ShadowRoot* root = youngestShadowRoot(); root;
1921 root = root->olderShadowRoot()) { 1924 root = root->olderShadowRoot()) {
1922 if (root->shouldCallRecalcStyle(change)) 1925 if (root->shouldCallRecalcStyle(change))
1923 root->recalcStyle(change); 1926 root->recalcStyle(change);
1924 } 1927 }
1925 recalcDescendantStyles(change); 1928 recalcDescendantStyles(change);
1926 } 1929 }
1927 1930
1928 updatePseudoElement(PseudoIdAfter, change); 1931 updatePseudoElement(PseudoIdAfter, change);
1929 updatePseudoElement(PseudoIdBackdrop, change); 1932 updatePseudoElement(PseudoIdBackdrop, change);
1930 1933
1931 // If our children have changed then we need to force the first-letter 1934 // If our children have changed then we need to force the first-letter
1932 // checks as we don't know if they effected the first letter or not. 1935 // checks as we don't know if they effected the first letter or not.
1933 // This can be seen when a child transitions from floating to 1936 // This can be seen when a child transitions from floating to
1934 // non-floating we have to take it into account for the first letter. 1937 // non-floating we have to take it into account for the first letter.
1935 updatePseudoElement(PseudoIdFirstLetter, 1938 updatePseudoElement(PseudoIdFirstLetter,
1936 childNeedsStyleRecalc() ? Force : change); 1939 childNeedsStyleRecalc() ? Force : change);
1937 1940
1938 clearChildNeedsStyleRecalc(); 1941 clearChildNeedsStyleRecalc();
1939 clearChildNeedsReattachLayoutTree();
1940 } 1942 }
1941 1943
1944 if (!needsReattachLayoutTree() && !childNeedsReattachLayoutTree())
1945 clearNeedsStyleRecalc();
1946
1942 if (hasCustomStyleCallbacks()) 1947 if (hasCustomStyleCallbacks())
1943 didRecalcStyle(); 1948 didRecalcStyle();
1944 } 1949 }
1945 1950
1946 PassRefPtr<ComputedStyle> Element::propagateInheritedProperties( 1951 PassRefPtr<ComputedStyle> Element::propagateInheritedProperties(
1947 StyleRecalcChange change) { 1952 StyleRecalcChange change) {
1948 if (change != IndependentInherit) 1953 if (change != IndependentInherit)
1949 return nullptr; 1954 return nullptr;
1950 if (isPseudoElement()) 1955 if (isPseudoElement())
1951 return nullptr; 1956 return nullptr;
1952 if (needsStyleRecalc()) 1957 if (needsStyleRecalc())
1953 return nullptr; 1958 return nullptr;
1954 if (hasAnimations()) 1959 if (hasAnimations())
1955 return nullptr; 1960 return nullptr;
1956 const ComputedStyle* parentStyle = parentComputedStyle(); 1961 const ComputedStyle* parentStyle = parentComputedStyle();
1957 DCHECK(parentStyle); 1962 DCHECK(parentStyle);
1958 const ComputedStyle* style = computedStyle(); 1963 const ComputedStyle* style = computedStyle();
1959 if (!style || style->animations() || style->transitions()) 1964 if (!style || style->animations() || style->transitions())
1960 return nullptr; 1965 return nullptr;
1961 RefPtr<ComputedStyle> newStyle = ComputedStyle::clone(*style); 1966 RefPtr<ComputedStyle> newStyle = ComputedStyle::clone(*style);
1962 newStyle->propagateIndependentInheritedProperties(*parentStyle); 1967 newStyle->propagateIndependentInheritedProperties(*parentStyle);
1963 INCREMENT_STYLE_STATS_COUNTER(document().styleEngine(), 1968 INCREMENT_STYLE_STATS_COUNTER(document().styleEngine(),
1964 independentInheritedStylesPropagated, 1); 1969 independentInheritedStylesPropagated, 1);
1965 return newStyle; 1970 return newStyle;
1966 } 1971 }
1967 1972
1968 StyleRecalcChange Element::recalcOwnStyle(StyleRecalcChange change, 1973 StyleRecalcChange Element::recalcOwnStyle(StyleRecalcChange change,
1969 Text* nextTextSibling) { 1974 Text* nextTextSibling) {
1970 DCHECK(document().inStyleRecalc()); 1975 DCHECK(document().inStyleRecalc());
1971 DCHECK(!parentOrShadowHostNode()->needsStyleRecalc());
esprehn 2017/02/16 05:49:25 Add this back, entering into recalcOwnStyle with a
nainar 2017/02/16 06:47:54 This isn't true anymore in case we need this infor
esprehn 2017/02/16 06:54:12 This should be true, we're only leaving the style
nainar 2017/02/16 07:13:20 Same test case as above. I see what you mean here
1972 DCHECK(change >= IndependentInherit || needsStyleRecalc()); 1976 DCHECK(change >= IndependentInherit || needsStyleRecalc());
1973 DCHECK(parentComputedStyle()); 1977 DCHECK(parentComputedStyle());
1974 1978
1975 RefPtr<ComputedStyle> oldStyle = mutableComputedStyle(); 1979 RefPtr<ComputedStyle> oldStyle = mutableComputedStyle();
1976 1980
1977 // When propagating inherited changes, we don't need to do a full style recalc 1981 // When propagating inherited changes, we don't need to do a full style recalc
1978 // if the only changed properties are independent. In this case, we can simply 1982 // if the only changed properties are independent. In this case, we can simply
1979 // set these directly on the ComputedStyle object. 1983 // set these directly on the ComputedStyle object.
1980 RefPtr<ComputedStyle> newStyle = propagateInheritedProperties(change); 1984 RefPtr<ComputedStyle> newStyle = propagateInheritedProperties(change);
1981 if (!newStyle) 1985 if (!newStyle)
1982 newStyle = styleForLayoutObject(); 1986 newStyle = styleForLayoutObject();
1983 DCHECK(newStyle); 1987 DCHECK(newStyle);
1984 1988
1985 StyleRecalcChange localChange = 1989 StyleRecalcChange localChange =
1986 ComputedStyle::stylePropagationDiff(oldStyle.get(), newStyle.get()); 1990 ComputedStyle::stylePropagationDiff(oldStyle.get(), newStyle.get());
1987 if (localChange == NoChange) { 1991 if (localChange == NoChange) {
1988 INCREMENT_STYLE_STATS_COUNTER(document().styleEngine(), stylesUnchanged, 1); 1992 INCREMENT_STYLE_STATS_COUNTER(document().styleEngine(), stylesUnchanged, 1);
1989 } else { 1993 } else {
1990 INCREMENT_STYLE_STATS_COUNTER(document().styleEngine(), stylesChanged, 1); 1994 INCREMENT_STYLE_STATS_COUNTER(document().styleEngine(), stylesChanged, 1);
1991 } 1995 }
1992 1996
1993 if (localChange == Reattach) { 1997 if (localChange == Reattach) {
1994 StyleReattachData styleReattachData; 1998 StyleReattachData styleReattachData;
1995 styleReattachData.computedStyle = std::move(newStyle); 1999 styleReattachData.computedStyle = std::move(newStyle);
1996 styleReattachData.nextTextSibling = nextTextSibling; 2000 styleReattachData.nextTextSibling = nextTextSibling;
1997 document().addStyleReattachData(*this, styleReattachData); 2001 document().addStyleReattachData(*this, styleReattachData);
1998 setNeedsReattachLayoutTree(); 2002 setNeedsReattachLayoutTree();
1999 return rebuildLayoutTree(); 2003 return Reattach;
2000 } 2004 }
2001 2005
2002 DCHECK(oldStyle); 2006 DCHECK(oldStyle);
2003 2007
2004 if (localChange != NoChange) 2008 if (localChange != NoChange)
2005 updateCallbackSelectors(oldStyle.get(), newStyle.get()); 2009 updateCallbackSelectors(oldStyle.get(), newStyle.get());
2006 2010
2007 if (LayoutObject* layoutObject = this->layoutObject()) { 2011 if (LayoutObject* layoutObject = this->layoutObject()) {
2008 if (localChange != NoChange || 2012 if (localChange != NoChange ||
2009 pseudoStyleCacheIsInvalid(oldStyle.get(), newStyle.get())) { 2013 pseudoStyleCacheIsInvalid(oldStyle.get(), newStyle.get())) {
(...skipping 20 matching lines...) Expand all
2030 return Inherit; 2034 return Inherit;
2031 newStyle->copyChildDependentFlagsFrom(*oldStyle); 2035 newStyle->copyChildDependentFlagsFrom(*oldStyle);
2032 } 2036 }
2033 if (oldStyle->hasPseudoElementStyle() || newStyle->hasPseudoElementStyle()) 2037 if (oldStyle->hasPseudoElementStyle() || newStyle->hasPseudoElementStyle())
2034 return UpdatePseudoElements; 2038 return UpdatePseudoElements;
2035 } 2039 }
2036 2040
2037 return localChange; 2041 return localChange;
2038 } 2042 }
2039 2043
2040 StyleRecalcChange Element::rebuildLayoutTree() { 2044 void Element::rebuildLayoutTree() {
2041 DCHECK(inActiveDocument()); 2045 DCHECK(inActiveDocument());
2046 DCHECK(parentNode());
2047
2042 StyleReattachData styleReattachData = document().getStyleReattachData(*this); 2048 StyleReattachData styleReattachData = document().getStyleReattachData(*this);
2043 AttachContext reattachContext; 2049 AttachContext reattachContext;
2044 reattachContext.resolvedStyle = styleReattachData.computedStyle.get(); 2050 reattachContext.resolvedStyle = styleReattachData.computedStyle.get();
2045 bool layoutObjectWillChange = needsAttach() || layoutObject(); 2051 bool layoutObjectWillChange = needsAttach() || layoutObject();
2046 2052
2047 // We are calling Element::rebuildLayoutTree() from inside 2053 if (needsReattachLayoutTree()) {
2048 // Element::recalcOwnStyle where we set the NeedsReattachLayoutTree 2054 reattachLayoutTree(reattachContext);
2049 // flag - so needsReattachLayoutTree() should always be true. 2055 } else if (childNeedsReattachLayoutTree()) {
2050 DCHECK(parentNode()); 2056 DCHECK(!needsReattachLayoutTree());
2051 DCHECK(parentNode()->childNeedsReattachLayoutTree()); 2057 SelectorFilterParentScope filterScope(*this);
2052 DCHECK(needsReattachLayoutTree()); 2058 StyleSharingDepthScope sharingScope(*this);
2053 reattachLayoutTree(reattachContext); 2059 reattachPseudoElementLayoutTree(PseudoIdBefore);
2054 // Since needsReattachLayoutTree() is always true we go into 2060 rebuildShadowRootLayoutTree();
2055 // reattachLayoutTree() which reattaches all the descendant 2061 rebuildChildrenLayoutTrees();
2056 // sub-trees. At this point no child should need reattaching. 2062 reattachPseudoElementLayoutTree(PseudoIdAfter);
2063 reattachPseudoElementLayoutTree(PseudoIdBackdrop);
2064 reattachPseudoElementLayoutTree(PseudoIdFirstLetter);
2065 DCHECK(!needsStyleRecalc());
esprehn 2017/02/16 05:49:25 remove, you check it below
nainar 2017/02/16 06:47:54 Done.
2066 }
2067 DCHECK(!needsStyleRecalc());
2068 DCHECK(!childNeedsStyleRecalc());
2069 DCHECK(!needsReattachLayoutTree());
2057 DCHECK(!childNeedsReattachLayoutTree()); 2070 DCHECK(!childNeedsReattachLayoutTree());
2058 2071
2059 if (layoutObjectWillChange || layoutObject()) { 2072 if (layoutObjectWillChange || layoutObject()) {
2060 // nextTextSibling is passed on to recalcStyle from recalcDescendantStyles 2073 // nextTextSibling is passed on to recalcStyle from recalcDescendantStyles
2061 // we can either traverse the current subtree from this node onwards 2074 // we can either traverse the current subtree from this node onwards
2062 // or store it. 2075 // or store it.
2063 // The choice is between increased time and increased memory complexity. 2076 // The choice is between increased time and increased memory complexity.
2064 reattachWhitespaceSiblingsIfNeeded(styleReattachData.nextTextSibling); 2077 reattachWhitespaceSiblingsIfNeeded(styleReattachData.nextTextSibling);
2065 return Reattach; 2078 } else {
2079 clearNeedsStyleRecalc();
esprehn 2017/02/16 05:49:25 You DCHECK(!needsStyleRecalc()); right above this,
nainar 2017/02/16 06:47:54 Done.
2066 } 2080 }
2067 return ReattachNoLayoutObject; 2081 }
2082
2083 void Element::rebuildShadowRootLayoutTree() {
2084 for (ShadowRoot* root = youngestShadowRoot(); root;
2085 root = root->olderShadowRoot()) {
2086 if (root->needsReattachLayoutTree() || root->childNeedsReattachLayoutTree())
2087 root->rebuildLayoutTree();
2088 }
2089 }
2090
2091 void Element::reattachPseudoElementLayoutTree(PseudoId pseudoId) {
2092 if (PseudoElement* element = pseudoElement(pseudoId)) {
2093 if (element->needsReattachLayoutTree() ||
2094 element->childNeedsReattachLayoutTree())
2095 element->rebuildLayoutTree();
2096 } else {
2097 createPseudoElementIfNeeded(pseudoId);
2098 }
2068 } 2099 }
2069 2100
2070 void Element::updateCallbackSelectors(const ComputedStyle* oldStyle, 2101 void Element::updateCallbackSelectors(const ComputedStyle* oldStyle,
2071 const ComputedStyle* newStyle) { 2102 const ComputedStyle* newStyle) {
2072 Vector<String> emptyVector; 2103 Vector<String> emptyVector;
2073 const Vector<String>& oldCallbackSelectors = 2104 const Vector<String>& oldCallbackSelectors =
2074 oldStyle ? oldStyle->callbackSelectors() : emptyVector; 2105 oldStyle ? oldStyle->callbackSelectors() : emptyVector;
2075 const Vector<String>& newCallbackSelectors = 2106 const Vector<String>& newCallbackSelectors =
2076 newStyle ? newStyle->callbackSelectors() : emptyVector; 2107 newStyle ? newStyle->callbackSelectors() : emptyVector;
2077 if (oldCallbackSelectors.isEmpty() && newCallbackSelectors.isEmpty()) 2108 if (oldCallbackSelectors.isEmpty() && newCallbackSelectors.isEmpty())
(...skipping 1129 matching lines...) Expand 10 before | Expand all | Expand 10 after
3207 Locale& Element::locale() const { 3238 Locale& Element::locale() const {
3208 return document().getCachedLocale(computeInheritedLanguage()); 3239 return document().getCachedLocale(computeInheritedLanguage());
3209 } 3240 }
3210 3241
3211 void Element::cancelFocusAppearanceUpdate() { 3242 void Element::cancelFocusAppearanceUpdate() {
3212 if (document().focusedElement() == this) 3243 if (document().focusedElement() == this)
3213 document().cancelFocusAppearanceUpdate(); 3244 document().cancelFocusAppearanceUpdate();
3214 } 3245 }
3215 3246
3216 void Element::updatePseudoElement(PseudoId pseudoId, StyleRecalcChange change) { 3247 void Element::updatePseudoElement(PseudoId pseudoId, StyleRecalcChange change) {
3217 DCHECK(!needsStyleRecalc());
3218 PseudoElement* element = pseudoElement(pseudoId); 3248 PseudoElement* element = pseudoElement(pseudoId);
3219 3249
3220 if (element && (change == UpdatePseudoElements || 3250 if (element && (change == UpdatePseudoElements ||
3221 element->shouldCallRecalcStyle(change))) { 3251 element->shouldCallRecalcStyle(change))) {
3222 if (pseudoId == PseudoIdFirstLetter && updateFirstLetter(element)) 3252 if (pseudoId == PseudoIdFirstLetter && updateFirstLetter(element))
3223 return; 3253 return;
3224 3254
3225 // Need to clear the cached style if the PseudoElement wants a recalc so it 3255 // Need to clear the cached style if the PseudoElement wants a recalc so it
3226 // computes a new style. 3256 // computes a new style.
3227 if (element->needsStyleRecalc()) 3257 if (element->needsStyleRecalc())
(...skipping 906 matching lines...) Expand 10 before | Expand all | Expand 10 after
4134 } 4164 }
4135 4165
4136 DEFINE_TRACE_WRAPPERS(Element) { 4166 DEFINE_TRACE_WRAPPERS(Element) {
4137 if (hasRareData()) { 4167 if (hasRareData()) {
4138 visitor->traceWrappers(elementRareData()); 4168 visitor->traceWrappers(elementRareData());
4139 } 4169 }
4140 ContainerNode::traceWrappers(visitor); 4170 ContainerNode::traceWrappers(visitor);
4141 } 4171 }
4142 4172
4143 } // namespace blink 4173 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698