tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

CSSMozDocumentRule.cpp (4986B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "mozilla/dom/CSSMozDocumentRule.h"
      8 
      9 #include "js/RegExpFlags.h"
     10 #include "mozilla/ServoBindings.h"
     11 #include "mozilla/dom/BrowsingContext.h"
     12 #include "mozilla/dom/CSSMozDocumentRuleBinding.h"
     13 #include "nsContentUtils.h"
     14 #include "nsHTMLDocument.h"
     15 
     16 namespace mozilla::dom {
     17 
     18 using namespace mozilla::css;
     19 
     20 /* virtual */
     21 JSObject* CSSMozDocumentRule::WrapObject(JSContext* aCx,
     22                                         JS::Handle<JSObject*> aGivenProto) {
     23  return CSSMozDocumentRule_Binding::Wrap(aCx, this, aGivenProto);
     24 }
     25 
     26 bool CSSMozDocumentRule::Match(const Document* aDoc, nsIURI* aDocURI,
     27                               const nsACString& aDocURISpec,
     28                               const nsACString& aPattern,
     29                               DocumentMatchingFunction aMatchingFunction) {
     30  switch (aMatchingFunction) {
     31    case DocumentMatchingFunction::MediaDocument: {
     32      auto kind = aDoc->MediaDocumentKind();
     33      if (aPattern.EqualsLiteral("all")) {
     34        return kind != Document::MediaDocumentKind::NotMedia;
     35      }
     36      MOZ_ASSERT(aPattern.EqualsLiteral("image") ||
     37                 aPattern.EqualsLiteral("video"));
     38      switch (kind) {
     39        case Document::MediaDocumentKind::NotMedia:
     40          return false;
     41        case Document::MediaDocumentKind::Image:
     42          return aPattern.EqualsLiteral("image");
     43        case Document::MediaDocumentKind::Video:
     44          return aPattern.EqualsLiteral("video");
     45      }
     46      MOZ_ASSERT_UNREACHABLE("Unknown media document kind");
     47      return false;
     48    }
     49    case DocumentMatchingFunction::URL:
     50      return aDocURISpec == aPattern;
     51    case DocumentMatchingFunction::URLPrefix:
     52      return StringBeginsWith(aDocURISpec, aPattern);
     53    case DocumentMatchingFunction::Domain: {
     54      nsAutoCString host;
     55      if (aDocURI) {
     56        aDocURI->GetHost(host);
     57      }
     58      int32_t lenDiff = host.Length() - aPattern.Length();
     59      if (lenDiff == 0) {
     60        return host == aPattern;
     61      }
     62      return StringEndsWith(host, aPattern) && host.CharAt(lenDiff - 1) == '.';
     63    }
     64    case DocumentMatchingFunction::RegExp: {
     65      // Using JS::RegExpFlag::Unicode to allow patterns containing for example
     66      // [^/].
     67      return nsContentUtils::IsPatternMatching(
     68                 NS_ConvertUTF8toUTF16(aDocURISpec),
     69                 NS_ConvertUTF8toUTF16(aPattern), aDoc,
     70                 /* aHasMultiple = */ false, JS::RegExpFlag::Unicode)
     71          .valueOr(false);
     72    }
     73    case DocumentMatchingFunction::PlainTextDocument:
     74      return aDoc->IsHTMLOrXHTML() && aDoc->AsHTMLDocument()->IsPlainText();
     75    case DocumentMatchingFunction::UnobservableDocument: {
     76      const BrowsingContext* bc = aDoc->GetBrowsingContext();
     77      return bc && bc->IsTop() && !bc->HasOpener();
     78    }
     79  }
     80  MOZ_ASSERT_UNREACHABLE("Unknown matching function");
     81  return false;
     82 }
     83 
     84 CSSMozDocumentRule::CSSMozDocumentRule(RefPtr<StyleDocumentRule> aRawRule,
     85                                       StyleSheet* aSheet,
     86                                       css::Rule* aParentRule, uint32_t aLine,
     87                                       uint32_t aColumn)
     88    : css::ConditionRule(aSheet, aParentRule, aLine, aColumn),
     89      mRawRule(std::move(aRawRule)) {}
     90 
     91 NS_IMPL_ADDREF_INHERITED(CSSMozDocumentRule, css::ConditionRule)
     92 NS_IMPL_RELEASE_INHERITED(CSSMozDocumentRule, css::ConditionRule)
     93 
     94 // QueryInterface implementation for MozDocumentRule
     95 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CSSMozDocumentRule)
     96 NS_INTERFACE_MAP_END_INHERITING(css::ConditionRule)
     97 
     98 #ifdef DEBUG
     99 /* virtual */
    100 void CSSMozDocumentRule::List(FILE* out, int32_t aIndent) const {
    101  nsAutoCString str;
    102  for (int32_t i = 0; i < aIndent; i++) {
    103    str.AppendLiteral("  ");
    104  }
    105  Servo_DocumentRule_Debug(mRawRule, &str);
    106  fprintf_stderr(out, "%s\n", str.get());
    107 }
    108 #endif
    109 
    110 void CSSMozDocumentRule::SetRawAfterClone(RefPtr<StyleDocumentRule> aRaw) {
    111  mRawRule = std::move(aRaw);
    112  css::ConditionRule::DidSetRawAfterClone();
    113 }
    114 
    115 already_AddRefed<StyleLockedCssRules>
    116 CSSMozDocumentRule::GetOrCreateRawRules() {
    117  return Servo_DocumentRule_GetRules(mRawRule).Consume();
    118 }
    119 
    120 StyleCssRuleType CSSMozDocumentRule::Type() const {
    121  return StyleCssRuleType::Document;
    122 }
    123 
    124 void CSSMozDocumentRule::GetConditionText(nsACString& aConditionText) {
    125  Servo_DocumentRule_GetConditionText(mRawRule, &aConditionText);
    126 }
    127 
    128 /* virtual */
    129 void CSSMozDocumentRule::GetCssText(nsACString& aCssText) const {
    130  Servo_DocumentRule_GetCssText(mRawRule, &aCssText);
    131 }
    132 
    133 /* virtual */
    134 size_t CSSMozDocumentRule::SizeOfIncludingThis(
    135    MallocSizeOf aMallocSizeOf) const {
    136  // TODO Implement this!
    137  return aMallocSizeOf(this);
    138 }
    139 
    140 }  // namespace mozilla::dom