TEST_F(StyleEngineTest, AnalyzedInject) { document().body()->setInnerHTML( "<style>div { color: red }</style><div id='t1'>Green</div><div></div>"); document().view()->updateAllLifecyclePhases(); Element* t1 = document().getElementById("t1"); ASSERT_TRUE(t1); ASSERT_TRUE(t1->computedStyle()); EXPECT_EQ(makeRGB(255, 0, 0), t1->computedStyle()->visitedDependentColor(CSSPropertyColor)); unsigned beforeCount = styleEngine().styleForElementCount(); StyleSheetContents* parsedSheet = StyleSheetContents::create(CSSParserContext(document(), nullptr)); parsedSheet->parseString("#t1 { color: green }"); styleEngine().injectAuthorSheet(parsedSheet); document().view()->updateAllLifecyclePhases(); unsigned afterCount = styleEngine().styleForElementCount(); EXPECT_EQ(1u, afterCount - beforeCount); ASSERT_TRUE(t1->computedStyle()); EXPECT_EQ(makeRGB(0, 128, 0), t1->computedStyle()->visitedDependentColor(CSSPropertyColor)); }
PassRefPtrWillBeRawPtr<ImmutableStylePropertySet> BisonCSSParser::parseInlineStyleDeclaration(const String& string, Element* element) { Document& document = element->document(); CSSParserContext context = CSSParserContext(document.elementSheet().contents()->parserContext(), UseCounter::getFrom(&document)); context.setMode((element->isHTMLElement() && !document.inQuirksMode()) ? HTMLStandardMode : HTMLQuirksMode); return BisonCSSParser(context).parseDeclaration(string, document.elementSheet().contents()); }
void CSSSelectorWatch::watchCSSSelectors(const Vector<String>& selectors) { m_watchedCallbackSelectors.clear(); BisonCSSParser parser(CSSParserContext(UASheetMode, 0)); const CSSProperty callbackProperty(CSSPropertyInternalCallback, CSSPrimitiveValue::createIdentifier(CSSValueInternalPresence)); const RefPtr<StylePropertySet> callbackPropertySet = ImmutableStylePropertySet::create(&callbackProperty, 1, UASheetMode); CSSSelectorList selectorList; for (unsigned i = 0; i < selectors.size(); ++i) { parser.parseSelector(selectors[i], selectorList); if (!selectorList.isValid()) continue; // Only accept Compound Selectors, since they're cheaper to match. if (!allCompound(selectorList)) continue; RefPtr<StyleRule> rule = StyleRule::create(); rule->wrapperAdoptSelectorList(selectorList); rule->setProperties(callbackPropertySet); m_watchedCallbackSelectors.append(rule.release()); } m_document.changedSelectorWatch(); }
SelectorQuery* SelectorQueryCache::add(const AtomicString& selectors, const Document& document, ExceptionState& exceptionState) { HashMap<AtomicString, OwnPtr<SelectorQuery> >::iterator it = m_entries.find(selectors); if (it != m_entries.end()) return it->value.get(); BisonCSSParser parser(CSSParserContext(document, 0)); CSSSelectorList selectorList; parser.parseSelector(selectors, selectorList); if (!selectorList.first()) { exceptionState.throwDOMException(SyntaxError, "'" + selectors + "' is not a valid selector."); return 0; } // throw a NamespaceError if the selector includes any namespace prefixes. if (selectorList.selectorsNeedNamespaceResolution()) { exceptionState.throwDOMException(NamespaceError, "'" + selectors + "' contains namespaces, which are not supported."); return 0; } const unsigned maximumSelectorQueryCacheSize = 256; if (m_entries.size() == maximumSelectorQueryCacheSize) m_entries.remove(m_entries.begin()); OwnPtr<SelectorQuery> selectorQuery = adoptPtr(new SelectorQuery(selectorList)); SelectorQuery* rawSelectorQuery = selectorQuery.get(); m_entries.add(selectors, selectorQuery.release()); return rawSelectorQuery; }
void addSelector(const String& selector) { StyleRuleBase* newRule = CSSParser::parseRule(CSSParserContext(HTMLStandardMode, nullptr), nullptr, selector + "{color:pink}"); m_ruleSet->addStyleRule(static_cast<StyleRule*>(newRule), RuleHasNoSpecialState); }
void WebDocument::insertStyleSheet(const WebString& sourceCode) { Document* document = unwrap<Document>(); DCHECK(document); StyleSheetContents* parsedSheet = StyleSheetContents::create(CSSParserContext(*document, nullptr)); parsedSheet->parseString(sourceCode); document->styleEngine().injectAuthorSheet(parsedSheet); }
void WebDocument::insertStyleSheet(const WebString& sourceCode) { RefPtr<Document> document = unwrap<Document>(); ASSERT(document); RefPtrWillBeRawPtr<StyleSheetContents> parsedSheet = StyleSheetContents::create(CSSParserContext(*document.get(), 0)); parsedSheet->parseString(sourceCode); document->styleEngine()->addAuthorSheet(parsedSheet); }
static CSSStyleSheet* createSheet(const String& cssText = String()) { StyleSheetContents* contents = StyleSheetContents::create(CSSParserContext(HTMLStandardMode, nullptr)); contents->parseString(cssText); contents->ensureRuleSet(MediaQueryEvaluator(), RuleHasDocumentSecurityOrigin); return CSSStyleSheet::create(contents); }
static bool test(CSSPropertyID propertyID, const String& string) { CSSParserMode parserMode = HTMLStandardMode; if (propertyID == CSSPropertyFloodColor) parserMode = SVGAttributeMode; CSSValue* value = CSSParser::parseSingleValue(propertyID, string, CSSParserContext(parserMode, nullptr)); ASSERT(value); return DeferredLegacyStyleInterpolation::interpolationRequiresStyleResolve(*value); }
TEST_F(StyleEngineTest, DocumentDirtyAfterInject) { StyleSheetContents* parsedSheet = StyleSheetContents::create(CSSParserContext(document(), nullptr)); parsedSheet->parseString("div {}"); styleEngine().injectAuthorSheet(parsedSheet); document().view()->updateAllLifecyclePhases(); EXPECT_TRUE(isDocumentStyleSheetCollectionClean()); }
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); }
PassRefPtrWillBeRawPtr<ImmutableStylePropertySet> CSSParserImpl::parseInlineStyleDeclaration(const String& string, Element* element) { Document& document = element->document(); CSSParserContext context = CSSParserContext(document.elementSheet().contents()->parserContext(), UseCounter::getFrom(&document)); CSSParserMode mode = element->isHTMLElement() && !document.inQuirksMode() ? HTMLStandardMode : HTMLQuirksMode; context.setMode(mode); CSSParserImpl parser(context, document.elementSheet().contents()); CSSTokenizer::Scope scope(string); parser.consumeDeclarationList(scope.tokenRange(), StyleRule::Style); return createStylePropertySet(parser.m_parsedProperties, mode); }
bool InspectorPageAgent::applyViewportStyleOverride(StyleResolver* resolver) { if (!m_deviceMetricsOverridden || !m_emulateViewportEnabled) return false; RefPtrWillBeRawPtr<StyleSheetContents> styleSheet = StyleSheetContents::create(CSSParserContext(UASheetMode, 0)); styleSheet->parseString(String(viewportAndroidUserAgentStyleSheet, sizeof(viewportAndroidUserAgentStyleSheet))); OwnPtrWillBeRawPtr<RuleSet> ruleSet = RuleSet::create(); ruleSet->addRulesFromSheet(styleSheet.get(), MediaQueryEvaluator("screen")); resolver->viewportStyleResolver()->collectViewportRules(ruleSet.get(), ViewportStyleResolver::UserAgentOrigin); return true; }
StyleEngineTest::RuleSetInvalidation StyleEngineTest::scheduleInvalidationsForRules(TreeScope& treeScope, const String& cssText) { StyleSheetContents* sheet = StyleSheetContents::create(CSSParserContext(HTMLStandardMode, nullptr)); sheet->parseString(cssText); HeapVector<Member<RuleSet>> ruleSets; RuleSet& ruleSet = sheet->ensureRuleSet(MediaQueryEvaluator(), RuleHasDocumentSecurityOrigin); ruleSet.compactRulesIfNeeded(); if (ruleSet.needsFullRecalcForRuleSetInvalidation()) return RuleSetInvalidationFullRecalc; ruleSets.append(&ruleSet); styleEngine().scheduleInvalidationsForRuleSets(treeScope, ruleSets); return RuleSetInvalidationsScheduled; }
const CSSParserContext& CSSLazyParsingState::context() { DCHECK(m_owningContents); if (!m_shouldUseCount) { DCHECK(!m_context.useCounter()); return m_context; } // Try as best as possible to grab a valid UseCounter if the underlying // document has gone away. if (!m_document) m_document = m_owningContents->anyOwnerDocument(); // Always refresh the UseCounter, as the Document can outlive its // underlying frame host causing a use-after-free of m_context's counter. UseCounter* useCounter = UseCounter::getFrom(m_document); if (useCounter != m_context.useCounter()) m_context = CSSParserContext(m_context, useCounter); return m_context; }
SelectorQuery* SelectorQueryCache::add(const AtomicString& selectors, const Document& document, ExceptionState& exceptionState) { HashMap<AtomicString, OwnPtr<SelectorQuery>>::iterator it = m_entries.find(selectors); if (it != m_entries.end()) return it->value.get(); CSSSelectorList selectorList = CSSParser::parseSelector(CSSParserContext(document, nullptr), nullptr, selectors); if (!selectorList.first()) { exceptionState.throwDOMException(SyntaxError, "'" + selectors + "' is not a valid selector."); return nullptr; } const unsigned maximumSelectorQueryCacheSize = 256; if (m_entries.size() == maximumSelectorQueryCacheSize) m_entries.remove(m_entries.begin()); return m_entries.add(selectors, SelectorQuery::adopt(std::move(selectorList))).storedValue->value.get(); }
void CSSSelectorWatch::watchCSSSelectors(const Vector<String>& selectors) { m_watchedCallbackSelectors.clear(); const RefPtrWillBeRawPtr<StylePropertySet> callbackPropertySet = ImmutableStylePropertySet::create(nullptr, 0, UASheetMode); CSSSelectorList selectorList; for (unsigned i = 0; i < selectors.size(); ++i) { CSSParser::parseSelector(CSSParserContext(UASheetMode, 0), selectors[i], selectorList); if (!selectorList.isValid()) continue; // Only accept Compound Selectors, since they're cheaper to match. if (!allCompound(selectorList)) continue; RefPtrWillBeRawPtr<StyleRule> rule = StyleRule::create(); rule->wrapperAdoptSelectorList(selectorList); rule->setProperties(callbackPropertySet); m_watchedCallbackSelectors.append(rule.release()); } document().changedSelectorWatch(); }
CSSParserContext PropertySetCSSStyleDeclaration::cssParserContext() const { return CSSParserContext(m_propertySet->cssParserMode()); }
static PassRefPtrWillBeRawPtr<StyleSheetContents> parseUASheet(const String& str) { RefPtrWillBeRawPtr<StyleSheetContents> sheet = StyleSheetContents::create(CSSParserContext(UASheetMode, 0)); sheet->parseString(str); return sheet.release(); }
static StyleSheetContents* parseUASheet(const String& str) { StyleSheetContents* sheet = StyleSheetContents::create(CSSParserContext(UASheetMode)).leakRef(); // leak the sheet on purpose sheet->parseString(str); return sheet; }
static CSSStyleSheet* createSheet() { return CSSStyleSheet::create(StyleSheetContents::create(CSSParserContext(HTMLStandardMode, nullptr))); }