1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "core/css/parser/CSSParser.h"
#include "core/css/CSSColorValue.h"
#include "core/css/CSSKeyframeRule.h"
#include "core/css/StyleColor.h"
#include "core/css/StyleRule.h"
#include "core/css/StyleSheetContents.h"
#include "core/css/parser/CSSParserFastPaths.h"
#include "core/css/parser/CSSParserImpl.h"
#include "core/css/parser/CSSPropertyParser.h"
#include "core/css/parser/CSSSelectorParser.h"
#include "core/css/parser/CSSSupportsParser.h"
#include "core/css/parser/CSSTokenizer.h"
#include "core/css/parser/CSSVariableParser.h"
#include "core/layout/LayoutTheme.h"
#include <memory>
namespace blink {
bool CSSParser::parseDeclarationList(const CSSParserContext* context,
MutableStylePropertySet* propertySet,
const String& declaration) {
return CSSParserImpl::parseDeclarationList(propertySet, declaration, context);
}
void CSSParser::parseDeclarationListForInspector(
const CSSParserContext* context,
const String& declaration,
CSSParserObserver& observer) {
CSSParserImpl::parseDeclarationListForInspector(declaration, context,
observer);
}
CSSSelectorList CSSParser::parseSelector(const CSSParserContext* context,
StyleSheetContents* styleSheetContents,
const String& selector) {
CSSTokenizer tokenizer(selector);
return CSSSelectorParser::parseSelector(tokenizer.tokenRange(), context,
styleSheetContents);
}
CSSSelectorList CSSParser::parsePageSelector(
const CSSParserContext* context,
StyleSheetContents* styleSheetContents,
const String& selector) {
CSSTokenizer tokenizer(selector);
return CSSParserImpl::parsePageSelector(tokenizer.tokenRange(),
styleSheetContents);
}
StyleRuleBase* CSSParser::parseRule(const CSSParserContext* context,
StyleSheetContents* styleSheet,
const String& rule) {
return CSSParserImpl::parseRule(rule, context, styleSheet,
CSSParserImpl::AllowImportRules);
}
void CSSParser::parseSheet(const CSSParserContext* context,
StyleSheetContents* styleSheet,
const String& text,
bool deferPropertyParsing) {
return CSSParserImpl::parseStyleSheet(text, context, styleSheet,
deferPropertyParsing);
}
void CSSParser::parseSheetForInspector(const CSSParserContext* context,
StyleSheetContents* styleSheet,
const String& text,
CSSParserObserver& observer) {
return CSSParserImpl::parseStyleSheetForInspector(text, context, styleSheet,
observer);
}
MutableStylePropertySet::SetResult CSSParser::parseValue(
MutableStylePropertySet* declaration,
CSSPropertyID unresolvedProperty,
const String& string,
bool important) {
return parseValue(declaration, unresolvedProperty, string, important,
static_cast<StyleSheetContents*>(nullptr));
}
MutableStylePropertySet::SetResult CSSParser::parseValue(
MutableStylePropertySet* declaration,
CSSPropertyID unresolvedProperty,
const String& string,
bool important,
StyleSheetContents* styleSheet) {
if (string.isEmpty()) {
bool didParse = false;
bool didChange = false;
return MutableStylePropertySet::SetResult{didParse, didChange};
}
CSSPropertyID resolvedProperty = resolveCSSPropertyID(unresolvedProperty);
CSSParserMode parserMode = declaration->cssParserMode();
CSSValue* value =
CSSParserFastPaths::maybeParseValue(resolvedProperty, string, parserMode);
if (value) {
bool didParse = true;
bool didChange = declaration->setProperty(
CSSProperty(resolvedProperty, *value, important));
return MutableStylePropertySet::SetResult{didParse, didChange};
}
CSSParserContext* context;
if (styleSheet) {
context = CSSParserContext::create(styleSheet->parserContext(), nullptr);
context->setMode(parserMode);
} else {
context = CSSParserContext::create(parserMode);
}
return parseValue(declaration, unresolvedProperty, string, important,
context);
}
MutableStylePropertySet::SetResult CSSParser::parseValueForCustomProperty(
MutableStylePropertySet* declaration,
const AtomicString& propertyName,
const PropertyRegistry* registry,
const String& value,
bool important,
StyleSheetContents* styleSheet,
bool isAnimationTainted) {
DCHECK(CSSVariableParser::isValidVariableName(propertyName));
if (value.isEmpty()) {
bool didParse = false;
bool didChange = false;
return MutableStylePropertySet::SetResult{didParse, didChange};
}
CSSParserMode parserMode = declaration->cssParserMode();
CSSParserContext* context;
if (styleSheet) {
context = CSSParserContext::create(styleSheet->parserContext(), nullptr);
context->setMode(parserMode);
} else {
context = CSSParserContext::create(parserMode);
}
return CSSParserImpl::parseVariableValue(declaration, propertyName, registry,
value, important, context,
isAnimationTainted);
}
ImmutableStylePropertySet* CSSParser::parseCustomPropertySet(
CSSParserTokenRange range) {
return CSSParserImpl::parseCustomPropertySet(range);
}
MutableStylePropertySet::SetResult CSSParser::parseValue(
MutableStylePropertySet* declaration,
CSSPropertyID unresolvedProperty,
const String& string,
bool important,
const CSSParserContext* context) {
return CSSParserImpl::parseValue(declaration, unresolvedProperty, string,
important, context);
}
const CSSValue* CSSParser::parseSingleValue(CSSPropertyID propertyID,
const String& string,
const CSSParserContext* context) {
if (string.isEmpty())
return nullptr;
if (CSSValue* value = CSSParserFastPaths::maybeParseValue(propertyID, string,
context->mode()))
return value;
CSSTokenizer tokenizer(string);
return CSSPropertyParser::parseSingleValue(propertyID, tokenizer.tokenRange(),
context);
}
ImmutableStylePropertySet* CSSParser::parseInlineStyleDeclaration(
const String& styleString,
Element* element) {
return CSSParserImpl::parseInlineStyleDeclaration(styleString, element);
}
std::unique_ptr<Vector<double>> CSSParser::parseKeyframeKeyList(
const String& keyList) {
return CSSParserImpl::parseKeyframeKeyList(keyList);
}
StyleRuleKeyframe* CSSParser::parseKeyframeRule(const CSSParserContext* context,
const String& rule) {
StyleRuleBase* keyframe = CSSParserImpl::parseRule(
rule, context, nullptr, CSSParserImpl::KeyframeRules);
return toStyleRuleKeyframe(keyframe);
}
bool CSSParser::parseSupportsCondition(const String& condition) {
CSSTokenizer tokenizer(condition);
CSSParserImpl parser(strictCSSParserContext());
return CSSSupportsParser::supportsCondition(tokenizer.tokenRange(), parser) ==
CSSSupportsParser::Supported;
}
bool CSSParser::parseColor(Color& color, const String& string, bool strict) {
if (string.isEmpty())
return false;
// The regular color parsers don't resolve named colors, so explicitly
// handle these first.
Color namedColor;
if (namedColor.setNamedColor(string)) {
color = namedColor;
return true;
}
const CSSValue* value = CSSParserFastPaths::parseColor(
string, strict ? HTMLStandardMode : HTMLQuirksMode);
// TODO(timloh): Why is this always strict mode?
if (!value)
value =
parseSingleValue(CSSPropertyColor, string, strictCSSParserContext());
if (!value || !value->isColorValue())
return false;
color = toCSSColorValue(*value).value();
return true;
}
bool CSSParser::parseSystemColor(Color& color, const String& colorString) {
CSSValueID id = cssValueKeywordID(colorString);
if (!StyleColor::isSystemColor(id))
return false;
color = LayoutTheme::theme().systemColor(id);
return true;
}
const CSSValue* CSSParser::parseFontFaceDescriptor(
CSSPropertyID propertyID,
const String& propertyValue,
const CSSParserContext* context) {
StringBuilder builder;
builder.append("@font-face { ");
builder.append(getPropertyNameString(propertyID));
builder.append(" : ");
builder.append(propertyValue);
builder.append("; }");
StyleRuleBase* rule = parseRule(context, nullptr, builder.toString());
if (!rule || !rule->isFontFaceRule())
return nullptr;
return toStyleRuleFontFace(rule)->properties().getPropertyCSSValue(
propertyID);
}
} // namespace blink
|