tor-browser

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

CSSPageRule.cpp (6817B)


      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/CSSPageRule.h"
      8 
      9 #include "mozilla/DeclarationBlock.h"
     10 #include "mozilla/ServoBindings.h"
     11 #include "mozilla/dom/CSSPageDescriptorsBinding.h"
     12 #include "mozilla/dom/CSSPageRuleBinding.h"
     13 
     14 namespace mozilla::dom {
     15 
     16 // -- CSSPageRuleDeclaration ---------------------------------------
     17 
     18 CSSPageRuleDeclaration::CSSPageRuleDeclaration(
     19    already_AddRefed<StyleLockedDeclarationBlock> aDecls)
     20    : mDecls(new DeclarationBlock(std::move(aDecls))) {
     21  mDecls->SetOwningRule(Rule());
     22 }
     23 
     24 CSSPageRuleDeclaration::~CSSPageRuleDeclaration() {
     25  mDecls->SetOwningRule(nullptr);
     26 }
     27 
     28 // QueryInterface implementation for CSSPageRuleDeclaration
     29 NS_INTERFACE_MAP_BEGIN(CSSPageRuleDeclaration)
     30  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
     31  // We forward the cycle collection interfaces to Rule(), which is
     32  // never null (in fact, we're part of that object!)
     33  if (aIID.Equals(NS_GET_IID(nsCycleCollectionISupports)) ||
     34      aIID.Equals(NS_GET_IID(nsXPCOMCycleCollectionParticipant))) {
     35    return Rule()->QueryInterface(aIID, aInstancePtr);
     36  }
     37 NS_INTERFACE_MAP_END_INHERITING(nsDOMCSSDeclaration)
     38 
     39 NS_IMPL_ADDREF_USING_AGGREGATOR(CSSPageRuleDeclaration, Rule())
     40 NS_IMPL_RELEASE_USING_AGGREGATOR(CSSPageRuleDeclaration, Rule())
     41 
     42 /* nsDOMCSSDeclaration implementation */
     43 
     44 css::Rule* CSSPageRuleDeclaration::GetParentRule() { return Rule(); }
     45 
     46 nsINode* CSSPageRuleDeclaration::GetAssociatedNode() const {
     47  return Rule()->GetAssociatedDocumentOrShadowRoot();
     48 }
     49 
     50 nsISupports* CSSPageRuleDeclaration::GetParentObject() const {
     51  return Rule()->GetParentObject();
     52 }
     53 
     54 JSObject* CSSPageRuleDeclaration::WrapObject(
     55    JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
     56  return CSSPageDescriptors_Binding::Wrap(aCx, this, aGivenProto);
     57 }
     58 
     59 DeclarationBlock* CSSPageRuleDeclaration::GetOrCreateCSSDeclaration(
     60    Operation aOperation, DeclarationBlock** aCreated) {
     61  if (aOperation != Operation::Read) {
     62    if (StyleSheet* sheet = Rule()->GetStyleSheet()) {
     63      sheet->WillDirty();
     64    }
     65  }
     66  return mDecls;
     67 }
     68 
     69 void CSSPageRuleDeclaration::SetRawAfterClone(
     70    RefPtr<StyleLockedDeclarationBlock> aDeclarationBlock) {
     71  mDecls->SetOwningRule(nullptr);
     72  mDecls = new DeclarationBlock(aDeclarationBlock.forget());
     73  mDecls->SetOwningRule(Rule());
     74 }
     75 
     76 nsresult CSSPageRuleDeclaration::SetCSSDeclaration(
     77    DeclarationBlock* aDecl, MutationClosureData* aClosureData) {
     78  MOZ_ASSERT(aDecl, "must be non-null");
     79  CSSPageRule* rule = Rule();
     80 
     81  if (aDecl != mDecls) {
     82    mDecls->SetOwningRule(nullptr);
     83    RefPtr<DeclarationBlock> decls = aDecl;
     84    Servo_PageRule_SetStyle(rule->Raw(), decls->Raw());
     85    mDecls = std::move(decls);
     86    mDecls->SetOwningRule(rule);
     87  }
     88 
     89  return NS_OK;
     90 }
     91 
     92 nsDOMCSSDeclaration::ParsingEnvironment
     93 CSSPageRuleDeclaration::GetParsingEnvironment(
     94    nsIPrincipal* aSubjectPrincipal) const {
     95  return GetParsingEnvironmentForRule(Rule(), StyleCssRuleType::Page);
     96 }
     97 
     98 // -- CSSPageRule --------------------------------------------------
     99 
    100 CSSPageRule::CSSPageRule(RefPtr<StyleLockedPageRule> aRawRule,
    101                         StyleSheet* aSheet, css::Rule* aParentRule,
    102                         uint32_t aLine, uint32_t aColumn)
    103    : css::GroupRule(aSheet, aParentRule, aLine, aColumn),
    104      mRawRule(std::move(aRawRule)),
    105      mDecls(Servo_PageRule_GetStyle(mRawRule).Consume()) {}
    106 
    107 NS_IMPL_ADDREF_INHERITED(CSSPageRule, css::GroupRule)
    108 NS_IMPL_RELEASE_INHERITED(CSSPageRule, css::GroupRule)
    109 
    110 // QueryInterface implementation for PageRule
    111 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CSSPageRule)
    112 NS_INTERFACE_MAP_END_INHERITING(css::GroupRule)
    113 
    114 NS_IMPL_CYCLE_COLLECTION_CLASS(CSSPageRule)
    115 
    116 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(CSSPageRule, css::GroupRule)
    117  // Keep this in sync with IsCCLeaf.
    118 
    119  // Trace the wrapper for our declaration.  This just expands out
    120  // NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER which we can't use
    121  // directly because the wrapper is on the declaration, not on us.
    122  tmp->mDecls.TraceWrapper(aCallbacks, aClosure);
    123 NS_IMPL_CYCLE_COLLECTION_TRACE_END
    124 
    125 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CSSPageRule)
    126  // Keep this in sync with IsCCLeaf.
    127 
    128  // Unlink the wrapper for our declaration.
    129  //
    130  // Note that this has to happen before unlinking css::Rule.
    131  tmp->UnlinkDeclarationWrapper(tmp->mDecls);
    132  tmp->mDecls.mDecls->SetOwningRule(nullptr);
    133 NS_IMPL_CYCLE_COLLECTION_UNLINK_END_INHERITED(css::GroupRule)
    134 
    135 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CSSPageRule, css::GroupRule)
    136  // Keep this in sync with IsCCLeaf.
    137 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
    138 
    139 bool CSSPageRule::IsCCLeaf() const {
    140  if (!GroupRule::IsCCLeaf()) {
    141    return false;
    142  }
    143 
    144  return !mDecls.PreservingWrapper();
    145 }
    146 
    147 void CSSPageRule::SetRawAfterClone(RefPtr<StyleLockedPageRule> aRaw) {
    148  mRawRule = std::move(aRaw);
    149  mDecls.SetRawAfterClone(Servo_PageRule_GetStyle(mRawRule.get()).Consume());
    150 }
    151 
    152 StyleCssRuleType CSSPageRule::Type() const { return StyleCssRuleType::Page; }
    153 
    154 size_t CSSPageRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
    155  // TODO Implement this!
    156  return GroupRule::SizeOfExcludingThis(aMallocSizeOf) + aMallocSizeOf(this);
    157 }
    158 
    159 #ifdef DEBUG
    160 void CSSPageRule::List(FILE* out, int32_t aIndent) const {
    161  nsAutoCString str;
    162  for (int32_t i = 0; i < aIndent; i++) {
    163    str.AppendLiteral("  ");
    164  }
    165  Servo_PageRule_Debug(mRawRule, &str);
    166  fprintf_stderr(out, "%s\n", str.get());
    167 }
    168 #endif
    169 
    170 /* CSSRule implementation */
    171 
    172 void CSSPageRule::GetCssText(nsACString& aCssText) const {
    173  Servo_PageRule_GetCssText(mRawRule, &aCssText);
    174 }
    175 
    176 /* CSSPageRule implementation */
    177 
    178 void CSSPageRule::GetSelectorText(nsACString& aSelectorText) const {
    179  Servo_PageRule_GetSelectorText(mRawRule.get(), &aSelectorText);
    180 }
    181 
    182 void CSSPageRule::SetSelectorText(const nsACString& aSelectorText) {
    183  if (IsReadOnly()) {
    184    return;
    185  }
    186 
    187  if (StyleSheet* const sheet = GetStyleSheet()) {
    188    sheet->WillDirty();
    189    const StyleStylesheetContents* const contents = sheet->RawContents();
    190    if (Servo_PageRule_SetSelectorText(contents, mRawRule.get(),
    191                                       &aSelectorText)) {
    192      sheet->RuleChanged(this, StyleRuleChangeKind::Generic);
    193    }
    194  }
    195 }
    196 
    197 already_AddRefed<StyleLockedCssRules> CSSPageRule::GetOrCreateRawRules() {
    198  return Servo_PageRule_GetRules(mRawRule).Consume();
    199 }
    200 
    201 JSObject* CSSPageRule::WrapObject(JSContext* aCx,
    202                                  JS::Handle<JSObject*> aGivenProto) {
    203  return CSSPageRule_Binding::Wrap(aCx, this, aGivenProto);
    204 }
    205 
    206 }  // namespace mozilla::dom