Пример #1
0
void ProcessingInstruction::setCSSStyleSheet(
    const String& href,
    const KURL& baseURL,
    const String& charset,
    const CSSStyleSheetResource* sheet) {
  if (!isConnected()) {
    DCHECK(!m_sheet);
    return;
  }

  DCHECK(m_isCSS);
  CSSParserContext parserContext(document(), nullptr, baseURL, charset);

  StyleSheetContents* newSheet =
      StyleSheetContents::create(href, parserContext);

  CSSStyleSheet* cssSheet = CSSStyleSheet::create(newSheet, *this);
  cssSheet->setDisabled(m_alternate);
  cssSheet->setTitle(m_title);
  if (!m_alternate && !m_title.isEmpty())
    document().styleEngine().setPreferredStylesheetSetNameIfNotSet(
        m_title, StyleEngine::DontUpdateActiveSheets);
  cssSheet->setMediaQueries(MediaQuerySet::create(m_media));

  m_sheet = cssSheet;

  // We don't need the cross-origin security check here because we are
  // getting the sheet text in "strict" mode. This enforces a valid CSS MIME
  // type.
  parseStyleSheet(sheet->sheetText());
}
Пример #2
0
StyleElement::ProcessingResult StyleElement::createSheet(Element& element,
                                                         const String& text) {
  DCHECK(element.isConnected());
  Document& document = element.document();

  const ContentSecurityPolicy* csp = document.contentSecurityPolicy();
  bool passesContentSecurityPolicyChecks =
      shouldBypassMainWorldCSP(element) ||
      csp->allowStyleWithHash(text, ContentSecurityPolicy::InlineType::Block) ||
      csp->allowInlineStyle(&element, document.url(),
                            element.fastGetAttribute(HTMLNames::nonceAttr),
                            m_startPosition.m_line, text);

  // Clearing the current sheet may remove the cache entry so create the new
  // sheet first
  CSSStyleSheet* newSheet = nullptr;

  // If type is empty or CSS, this is a CSS style sheet.
  const AtomicString& type = this->type();
  if (isCSS(element, type) && passesContentSecurityPolicyChecks) {
    MediaQuerySet* mediaQueries = MediaQuerySet::create(media());

    MediaQueryEvaluator screenEval("screen");
    MediaQueryEvaluator printEval("print");
    if (screenEval.eval(mediaQueries) || printEval.eval(mediaQueries)) {
      m_loading = true;
      TextPosition startPosition =
          m_startPosition == TextPosition::belowRangePosition()
              ? TextPosition::minimumPosition()
              : m_startPosition;
      newSheet = document.styleEngine().createSheet(
          element, text, startPosition, m_styleEngineContext);
      newSheet->setMediaQueries(mediaQueries);
      m_loading = false;
    }
  }

  if (m_sheet)
    clearSheet(element);

  m_sheet = newSheet;
  if (m_sheet)
    m_sheet->contents()->checkLoaded();

  return passesContentSecurityPolicyChecks ? ProcessingSuccessful
                                           : ProcessingFatalError;
}