bool CSSParser::parseColor(RGBA32& color, const String& string, bool strict) { if (string.isEmpty()) return false; // The regular color parsers don't resolve all named colors, so explicitly // handle these first. Color namedColor; if (namedColor.setNamedColor(string)) { color = namedColor.rgb(); return true; } RefPtrWillBeRawPtr<CSSValue> value = CSSParserFastPaths::parseColor(string, !strict); // TODO(timloh): Why is this always strict mode? if (!value) value = parseSingleValue(CSSPropertyColor, string, strictCSSParserContext()); if (!value || !value->isPrimitiveValue()) return false; CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value.get()); if (!primitiveValue->isRGBColor()) return false; color = primitiveValue->getRGBA32Value(); return true; }
void StyleRuleImport::setCSSStyleSheet( const String& href, const KURL& baseURL, const String& charset, const CSSStyleSheetResource* cachedStyleSheet) { if (m_styleSheet) m_styleSheet->clearOwnerRule(); CSSParserContext context = m_parentStyleSheet ? m_parentStyleSheet->parserContext() : strictCSSParserContext(); context.setCharset(charset); Document* document = m_parentStyleSheet ? m_parentStyleSheet->singleOwnerDocument() : nullptr; if (!baseURL.isNull()) { context.setBaseURL(baseURL); if (document) context.setReferrer(Referrer(baseURL.strippedForUseAsReferrer(), document->getReferrerPolicy())); } m_styleSheet = StyleSheetContents::create(this, href, context); m_styleSheet->parseAuthorStyleSheet( cachedStyleSheet, document ? document->getSecurityOrigin() : 0); m_loading = false; if (m_parentStyleSheet) { m_parentStyleSheet->notifyLoadedSheet(cachedStyleSheet); m_parentStyleSheet->checkLoaded(); } }
bool CSSParser::parseColor(RGBA32& color, const String& string, bool strict) { if (string.isEmpty()) return false; // First try creating a color specified by name, rgba(), rgb() or "#" syntax. if (CSSPropertyParser::fastParseColor(color, string, strict)) return true; // In case the fast-path parser didn't understand the color, try the full parser. RefPtrWillBeRawPtr<MutableStylePropertySet> stylePropertySet = MutableStylePropertySet::create(); // FIXME: The old CSS parser is only working in strict mode ignoring the strict parameter. // It needs to be investigated why. if (!parseValue(stylePropertySet.get(), CSSPropertyColor, string, false, strictCSSParserContext())) return false; RefPtrWillBeRawPtr<CSSValue> value = stylePropertySet->getPropertyCSSValue(CSSPropertyColor); if (!value || !value->isPrimitiveValue()) return false; CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value.get()); if (!primitiveValue->isRGBColor()) return false; color = primitiveValue->getRGBA32Value(); return true; }
void updateInvalidationSets(const String& selectorText) { CSSSelectorList selectorList = CSSParser::parseSelector(strictCSSParserContext(), selectorText); RefPtrWillBeRawPtr<StyleRule> styleRule = StyleRule::create(std::move(selectorList), MutableStylePropertySet::create(HTMLStandardMode)); RuleData ruleData(styleRule.get(), 0, 0, RuleHasNoSpecialState); m_ruleFeatureSet.updateInvalidationSets(ruleData); }
PassRefPtr<CSSStyleSheet> DOMImplementation::createCSSStyleSheet(const String&, const String& media) { // FIXME: Title should be set. // FIXME: Media could have wrong syntax, in which case we should generate an exception. RefPtr<CSSStyleSheet> sheet = CSSStyleSheet::create(StyleSheetContents::create(strictCSSParserContext())); sheet->setMediaQueries(MediaQuerySet::create(media)); return sheet; }
bool CSSParser::parseSupportsCondition(const String& condition) { if (RuntimeEnabledFeatures::newCSSParserEnabled()) { CSSTokenizer::Scope scope(condition); CSSParserImpl parser(strictCSSParserContext()); return CSSSupportsParser::supportsCondition(scope.tokenRange(), parser) == CSSSupportsParser::Supported; } return BisonCSSParser(CSSParserContext(HTMLStandardMode, 0)).parseSupportsCondition(condition); }
void CSSParserImpl::parseStyleSheetForInspector(const String& string, const CSSParserContext& context, CSSParserObserver& observer) { RefPtrWillBeRawPtr<StyleSheetContents> sheet = StyleSheetContents::create(strictCSSParserContext()); CSSParserImpl parser(context, sheet.get()); CSSParserObserverWrapper wrapper(observer); parser.m_observerWrapper = &wrapper; CSSTokenizer::Scope scope(string, *parser.m_observerWrapper); parser.consumeRuleList(scope.tokenRange(), TopLevelRuleList, [](PassRefPtrWillBeRawPtr<StyleRuleBase> rule) { }); }
PassRefPtrWillBeRawPtr<MediaQuerySet> MediaQuerySet::create(const String& mediaString) { if (mediaString.isEmpty()) return MediaQuerySet::create(); if (RuntimeEnabledFeatures::mediaQueryParserEnabled()) return MediaQueryParser::parseMediaQuerySet(mediaString); BisonCSSParser parser(strictCSSParserContext()); return parser.parseMediaQueryList(mediaString); }
const Vector<double>& StyleKeyframe::keys() const { if (!m_keys) { // Keys can only be cleared by setting the key text from JavaScript // and this can never be null. ASSERT(!m_keyText.isNull()); m_keys = BisonCSSParser(strictCSSParserContext()).parseKeyframeKeyList(m_keyText); } // If an invalid key string was set, m_keys may be empty. ASSERT(m_keys); return *m_keys; }
WebString canonicalizeSelector(WebString webSelector, WebSelectorType restriction) { CSSSelectorList selectorList = CSSParser::parseSelector(strictCSSParserContext(), nullptr, webSelector); if (restriction == WebSelectorTypeCompound) { for (const CSSSelector* selector = selectorList.first(); selector; selector = selectorList.next(*selector)) { if (!selector->isCompound()) return WebString(); } } return selectorList.selectorsText(); }
RawPtr<CSSValue> CSSVariableResolver::resolveVariableReferences(StyleVariableData* styleVariableData, CSSPropertyID id, const CSSVariableReferenceValue& value) { ASSERT(!isShorthandProperty(id)); CSSVariableResolver resolver(styleVariableData); Vector<CSSParserToken> tokens; if (!resolver.resolveTokenRange(value.variableDataValue()->tokens(), tokens)) return cssValuePool().createUnsetValue(); RawPtr<CSSValue> result = CSSPropertyParser::parseSingleValue(id, tokens, strictCSSParserContext()); if (!result) return cssValuePool().createUnsetValue(); return result.release(); }
TEST(BisonCSSParserTest, MediaQuery) { struct { const char* input; const char* output; } testCases[] = { {"@media s} {}", "not all"}, {"@media } {}", "not all"}, {"@media tv {}", "tv"}, {"@media tv, screen {}", "tv, screen"}, {"@media s}, tv {}", "not all, tv"}, {"@media tv, screen and (}) {}", "tv, not all"}, }; BisonCSSParser parser(strictCSSParserContext()); for (unsigned i = 0; i < ARRAY_SIZE(testCases); ++i) { RefPtrWillBeRawPtr<StyleRuleBase> rule = parser.parseRule(nullptr, String(testCases[i].input)); EXPECT_TRUE(rule->isMediaRule()); testMediaQuery(testCases[i].output, *static_cast<StyleRuleMedia*>(rule.get())->mediaQueries()); } }
Color CSSParser::parseColor(const String& string, bool strict) { if (string.isEmpty()) return Color(); // Try named colors first. Color namedColor { string }; if (namedColor.isValid()) return namedColor; // Try the fast path to parse hex and rgb. RefPtr<CSSValue> value = CSSParserFastPaths::parseColor(string, strict ? HTMLStandardMode : HTMLQuirksMode); // If that fails, try the full parser. if (!value) value = parseSingleValue(CSSPropertyColor, string, strictCSSParserContext()); if (!value || !value->isPrimitiveValue()) return Color(); const auto& primitiveValue = downcast<CSSPrimitiveValue>(*value); if (!primitiveValue.isRGBColor()) return Color(); return primitiveValue.color(); }
// The color will only be changed when string contains a valid CSS color, so callers // can set it to a default color and ignore the boolean result. bool BisonCSSParser::parseColor(RGBA32& color, const String& string, bool strict) { // First try creating a color specified by name, rgba(), rgb() or "#" syntax. if (CSSPropertyParser::fastParseColor(color, string, strict)) return true; BisonCSSParser parser(strictCSSParserContext()); // In case the fast-path parser didn't understand the color, try the full parser. if (!parser.parseColor(string)) return false; CSSValue* value = parser.m_parsedProperties.first().value(); if (!value->isPrimitiveValue()) return false; CSSPrimitiveValue* primitiveValue = toCSSPrimitiveValue(value); if (!primitiveValue->isRGBColor()) return false; color = primitiveValue->getRGBA32Value(); return true; }
PassOwnPtr<Vector<double>> CSSParser::parseKeyframeKeyList(const String& keyList) { if (RuntimeEnabledFeatures::newCSSParserEnabled()) return CSSParserImpl::parseKeyframeKeyList(keyList); return BisonCSSParser(strictCSSParserContext()).parseKeyframeKeyList(keyList); }
const CSSParserContext& CSSRule::parserContext() const { CSSStyleSheet* styleSheet = parentStyleSheet(); return styleSheet ? styleSheet->contents()->parserContext() : strictCSSParserContext(); }
bool CSSParser::parseSupportsCondition(const String& condition) { CSSTokenizer::Scope scope(condition); CSSParserImpl parser(strictCSSParserContext()); return CSSSupportsParser::supportsCondition(scope.tokenRange(), parser) == CSSSupportsParser::Supported; }