OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/css/parser/CSSPropertyParser.h" | 5 #include "core/css/parser/CSSPropertyParser.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include "core/StylePropertyShorthand.h" | 8 #include "core/StylePropertyShorthand.h" |
9 #include "core/css/CSSBasicShapeValues.h" | 9 #include "core/css/CSSBasicShapeValues.h" |
10 #include "core/css/CSSContentDistributionValue.h" | 10 #include "core/css/CSSContentDistributionValue.h" |
(...skipping 1866 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1877 AddParsedProperty(CSSPropertyColumnWidth, CSSPropertyInvalid, *column_width, | 1877 AddParsedProperty(CSSPropertyColumnWidth, CSSPropertyInvalid, *column_width, |
1878 important); | 1878 important); |
1879 AddParsedProperty(CSSPropertyColumnCount, CSSPropertyInvalid, *column_count, | 1879 AddParsedProperty(CSSPropertyColumnCount, CSSPropertyInvalid, *column_count, |
1880 important); | 1880 important); |
1881 return true; | 1881 return true; |
1882 } | 1882 } |
1883 | 1883 |
1884 bool CSSPropertyParser::ConsumeShorthandGreedily( | 1884 bool CSSPropertyParser::ConsumeShorthandGreedily( |
1885 const StylePropertyShorthand& shorthand, | 1885 const StylePropertyShorthand& shorthand, |
1886 bool important) { | 1886 bool important) { |
| 1887 return ConsumeShorthandViaLonghand(shorthand, important, |
| 1888 LonghandsOrder::kNoOrder); |
| 1889 } |
| 1890 |
| 1891 bool CSSPropertyParser::ConsumeShorthandViaLonghand( |
| 1892 const StylePropertyShorthand& shorthand, |
| 1893 bool important, |
| 1894 LonghandsOrder order) { |
1887 // Existing shorthands have at most 6 longhands. | 1895 // Existing shorthands have at most 6 longhands. |
1888 DCHECK_LE(shorthand.length(), 6u); | 1896 DCHECK_LE(shorthand.length(), 6u); |
| 1897 |
| 1898 const bool ordered_parsing = order != LonghandsOrder::kNoOrder; |
| 1899 |
1889 const CSSValue* longhands[6] = {nullptr, nullptr, nullptr, | 1900 const CSSValue* longhands[6] = {nullptr, nullptr, nullptr, |
1890 nullptr, nullptr, nullptr}; | 1901 nullptr, nullptr, nullptr}; |
| 1902 |
1891 const CSSPropertyID* shorthand_properties = shorthand.properties(); | 1903 const CSSPropertyID* shorthand_properties = shorthand.properties(); |
1892 do { | 1904 |
| 1905 if (range_.AtEnd()) { |
| 1906 return false; |
| 1907 } |
| 1908 |
| 1909 // Only set "next" to the next ordered longhand. It will remain 0 for |
| 1910 // unordered case. |
| 1911 size_t next = 0; |
| 1912 // For ordered longhands only. It becomes false when the parser fails to parse |
| 1913 // a longhand. In this case, we want to stop subsequent parsing immediately. |
| 1914 bool continue_parsing = true; |
| 1915 |
| 1916 while (!range_.AtEnd()) { |
| 1917 size_t i = next; |
1893 bool found_longhand = false; | 1918 bool found_longhand = false; |
1894 for (size_t i = 0; !found_longhand && i < shorthand.length(); ++i) { | 1919 |
1895 if (longhands[i]) | 1920 while (i < shorthand.length() && continue_parsing) { |
| 1921 if (longhands[i]) { |
| 1922 DCHECK(!ordered_parsing); |
| 1923 ++i; |
1896 continue; | 1924 continue; |
| 1925 } |
1897 longhands[i] = ParseSingleValue(shorthand_properties[i], shorthand.id()); | 1926 longhands[i] = ParseSingleValue(shorthand_properties[i], shorthand.id()); |
1898 if (longhands[i]) | 1927 if (longhands[i]) { |
1899 found_longhand = true; | 1928 found_longhand = true; |
| 1929 if (ordered_parsing) { |
| 1930 ++next; |
| 1931 } |
| 1932 break; |
| 1933 } |
| 1934 if (ordered_parsing) { |
| 1935 if (i == 0) { |
| 1936 return false; |
| 1937 } |
| 1938 continue_parsing = false; |
| 1939 break; |
| 1940 } |
| 1941 ++i; |
1900 } | 1942 } |
1901 if (!found_longhand) | 1943 |
| 1944 if (!found_longhand && !ordered_parsing) { |
1902 return false; | 1945 return false; |
1903 } while (!range_.AtEnd()); | 1946 } |
| 1947 if (!continue_parsing || next == shorthand.length()) { |
| 1948 DCHECK_GT(next, 0u); |
| 1949 break; |
| 1950 } |
| 1951 } |
| 1952 |
| 1953 if (ordered_parsing) { |
| 1954 DCHECK(longhands[0]); |
| 1955 } |
| 1956 |
| 1957 std::map<size_t, size_t> default_property_index = {{1u, 0}}; |
| 1958 if (order == LonghandsOrder::kOrderBy4Values) { |
| 1959 default_property_index[2u] = 0; |
| 1960 default_property_index[3u] = 1u; |
| 1961 } |
1904 | 1962 |
1905 for (size_t i = 0; i < shorthand.length(); ++i) { | 1963 for (size_t i = 0; i < shorthand.length(); ++i) { |
1906 if (longhands[i]) { | 1964 if (!longhands[i]) { |
1907 AddParsedProperty(shorthand_properties[i], shorthand.id(), *longhands[i], | 1965 if (ordered_parsing) { |
1908 important); | 1966 std::map<size_t, size_t>::iterator iter = |
1909 } else { | 1967 default_property_index.find(i); |
1910 AddParsedProperty(shorthand_properties[i], shorthand.id(), | 1968 DCHECK(iter != default_property_index.end()); |
1911 *CSSInitialValue::Create(), important); | 1969 longhands[i] = longhands[iter->second]; |
| 1970 } else { |
| 1971 longhands[i] = CSSInitialValue::Create(); |
| 1972 } |
1912 } | 1973 } |
| 1974 AddParsedProperty(shorthand_properties[i], shorthand.id(), *longhands[i], |
| 1975 important); |
1913 } | 1976 } |
1914 return true; | 1977 |
| 1978 return range_.AtEnd(); |
1915 } | 1979 } |
1916 | 1980 |
1917 bool CSSPropertyParser::ConsumeFlex(bool important) { | 1981 bool CSSPropertyParser::ConsumeFlex(bool important) { |
1918 static const double kUnsetValue = -1; | 1982 static const double kUnsetValue = -1; |
1919 double flex_grow = kUnsetValue; | 1983 double flex_grow = kUnsetValue; |
1920 double flex_shrink = kUnsetValue; | 1984 double flex_shrink = kUnsetValue; |
1921 CSSValue* flex_basis = nullptr; | 1985 CSSValue* flex_basis = nullptr; |
1922 | 1986 |
1923 if (range_.Peek().Id() == CSSValueNone) { | 1987 if (range_.Peek().Id() == CSSValueNone) { |
1924 flex_grow = 0; | 1988 flex_grow = 0; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2020 AddExpandedPropertyForValue(CSSPropertyBorderStyle, *style, important); | 2084 AddExpandedPropertyForValue(CSSPropertyBorderStyle, *style, important); |
2021 AddExpandedPropertyForValue(CSSPropertyBorderColor, *color, important); | 2085 AddExpandedPropertyForValue(CSSPropertyBorderColor, *color, important); |
2022 AddExpandedPropertyForValue(CSSPropertyBorderImage, | 2086 AddExpandedPropertyForValue(CSSPropertyBorderImage, |
2023 *CSSInitialValue::Create(), important); | 2087 *CSSInitialValue::Create(), important); |
2024 | 2088 |
2025 return range_.AtEnd(); | 2089 return range_.AtEnd(); |
2026 } | 2090 } |
2027 | 2091 |
2028 bool CSSPropertyParser::Consume2Values(const StylePropertyShorthand& shorthand, | 2092 bool CSSPropertyParser::Consume2Values(const StylePropertyShorthand& shorthand, |
2029 bool important) { | 2093 bool important) { |
2030 DCHECK_EQ(shorthand.length(), 2u); | 2094 return ConsumeShorthandViaLonghand(shorthand, important, |
2031 const CSSPropertyID* longhands = shorthand.properties(); | 2095 LonghandsOrder::kOrderBy2Values); |
2032 const CSSValue* start = ParseSingleValue(longhands[0], shorthand.id()); | |
2033 if (!start) | |
2034 return false; | |
2035 | |
2036 const CSSValue* end = ParseSingleValue(longhands[1], shorthand.id()); | |
2037 if (!end) | |
2038 end = start; | |
2039 AddParsedProperty(longhands[0], shorthand.id(), *start, important); | |
2040 AddParsedProperty(longhands[1], shorthand.id(), *end, important); | |
2041 | |
2042 return range_.AtEnd(); | |
2043 } | 2096 } |
2044 | 2097 |
2045 bool CSSPropertyParser::Consume4Values(const StylePropertyShorthand& shorthand, | 2098 bool CSSPropertyParser::Consume4Values(const StylePropertyShorthand& shorthand, |
2046 bool important) { | 2099 bool important) { |
2047 DCHECK_EQ(shorthand.length(), 4u); | 2100 return ConsumeShorthandViaLonghand(shorthand, important, |
2048 const CSSPropertyID* longhands = shorthand.properties(); | 2101 LonghandsOrder::kOrderBy4Values); |
2049 const CSSValue* top = ParseSingleValue(longhands[0], shorthand.id()); | |
2050 if (!top) | |
2051 return false; | |
2052 | |
2053 const CSSValue* right = ParseSingleValue(longhands[1], shorthand.id()); | |
2054 const CSSValue* bottom = nullptr; | |
2055 const CSSValue* left = nullptr; | |
2056 if (right) { | |
2057 bottom = ParseSingleValue(longhands[2], shorthand.id()); | |
2058 if (bottom) | |
2059 left = ParseSingleValue(longhands[3], shorthand.id()); | |
2060 } | |
2061 | |
2062 if (!right) | |
2063 right = top; | |
2064 if (!bottom) | |
2065 bottom = top; | |
2066 if (!left) | |
2067 left = right; | |
2068 | |
2069 AddParsedProperty(longhands[0], shorthand.id(), *top, important); | |
2070 AddParsedProperty(longhands[1], shorthand.id(), *right, important); | |
2071 AddParsedProperty(longhands[2], shorthand.id(), *bottom, important); | |
2072 AddParsedProperty(longhands[3], shorthand.id(), *left, important); | |
2073 | |
2074 return range_.AtEnd(); | |
2075 } | 2102 } |
2076 | 2103 |
2077 // TODO(crbug.com/668012): refactor out property specific logic from this method | 2104 // TODO(crbug.com/668012): refactor out property specific logic from this method |
2078 // and remove CSSPropetyID argument | 2105 // and remove CSSPropetyID argument |
2079 bool CSSPropertyParser::ConsumeBorderImage(CSSPropertyID property, | 2106 bool CSSPropertyParser::ConsumeBorderImage(CSSPropertyID property, |
2080 bool default_fill, | 2107 bool default_fill, |
2081 bool important) { | 2108 bool important) { |
2082 CSSValue* source = nullptr; | 2109 CSSValue* source = nullptr; |
2083 CSSValue* slice = nullptr; | 2110 CSSValue* slice = nullptr; |
2084 CSSValue* width = nullptr; | 2111 CSSValue* width = nullptr; |
(...skipping 851 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2936 case CSSPropertyScrollSnapMarginBlock: | 2963 case CSSPropertyScrollSnapMarginBlock: |
2937 return Consume2Values(scrollSnapMarginBlockShorthand(), important); | 2964 return Consume2Values(scrollSnapMarginBlockShorthand(), important); |
2938 case CSSPropertyScrollSnapMarginInline: | 2965 case CSSPropertyScrollSnapMarginInline: |
2939 return Consume2Values(scrollSnapMarginInlineShorthand(), important); | 2966 return Consume2Values(scrollSnapMarginInlineShorthand(), important); |
2940 default: | 2967 default: |
2941 return false; | 2968 return false; |
2942 } | 2969 } |
2943 } | 2970 } |
2944 | 2971 |
2945 } // namespace blink | 2972 } // namespace blink |
OLD | NEW |