nsCSSRules.cpp 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* rules in a CSS stylesheet other than style rules (e.g., @import rules) */
  6. #include "mozilla/Attributes.h"
  7. #include "nsCSSRules.h"
  8. #include "nsCSSValue.h"
  9. #include "mozilla/StyleSheetInlines.h"
  10. #include "mozilla/MemoryReporting.h"
  11. #include "mozilla/css/ImportRule.h"
  12. #include "mozilla/css/NameSpaceRule.h"
  13. #include "nsString.h"
  14. #include "nsIAtom.h"
  15. #include "nsCSSProps.h"
  16. #include "nsCOMPtr.h"
  17. #include "nsIDOMCSSStyleSheet.h"
  18. #include "nsIMediaList.h"
  19. #include "mozilla/dom/CSSRuleList.h"
  20. #include "nsIDocument.h"
  21. #include "nsPresContext.h"
  22. #include "nsContentUtils.h"
  23. #include "nsError.h"
  24. #include "nsStyleUtil.h"
  25. #include "mozilla/DeclarationBlockInlines.h"
  26. #include "nsCSSParser.h"
  27. #include "nsDOMClassInfoID.h"
  28. #include "mozilla/dom/CSSStyleDeclarationBinding.h"
  29. #include "StyleRule.h"
  30. #include "nsFont.h"
  31. #include "nsIURI.h"
  32. #include "mozAutoDocUpdate.h"
  33. using namespace mozilla;
  34. using namespace mozilla::dom;
  35. #define IMPL_STYLE_RULE_INHERIT_GET_DOM_RULE_WEAK(class_, super_) \
  36. /* virtual */ nsIDOMCSSRule* class_::GetDOMRule() \
  37. { return this; } \
  38. /* virtual */ nsIDOMCSSRule* class_::GetExistingDOMRule() \
  39. { return this; }
  40. #define IMPL_STYLE_RULE_INHERIT(class_, super_) \
  41. IMPL_STYLE_RULE_INHERIT_GET_DOM_RULE_WEAK(class_, super_)
  42. // base class for all rule types in a CSS style sheet
  43. namespace mozilla {
  44. namespace css {
  45. /* virtual */ void
  46. Rule::SetStyleSheet(CSSStyleSheet* aSheet)
  47. {
  48. // We don't reference count this up reference. The style sheet
  49. // will tell us when it's going away or when we're detached from
  50. // it.
  51. mSheet = aSheet;
  52. }
  53. nsresult
  54. Rule::GetParentRule(nsIDOMCSSRule** aParentRule)
  55. {
  56. if (mParentRule) {
  57. NS_IF_ADDREF(*aParentRule = mParentRule->GetDOMRule());
  58. } else {
  59. *aParentRule = nullptr;
  60. }
  61. return NS_OK;
  62. }
  63. nsresult
  64. Rule::GetParentStyleSheet(nsIDOMCSSStyleSheet** aSheet)
  65. {
  66. NS_ENSURE_ARG_POINTER(aSheet);
  67. NS_IF_ADDREF(*aSheet = GetStyleSheet());
  68. return NS_OK;
  69. }
  70. css::Rule*
  71. Rule::GetCSSRule()
  72. {
  73. return this;
  74. }
  75. // -------------------------------
  76. // Style Rule List for group rules
  77. //
  78. class GroupRuleRuleList final : public dom::CSSRuleList
  79. {
  80. public:
  81. explicit GroupRuleRuleList(GroupRule *aGroupRule);
  82. virtual CSSStyleSheet* GetParentObject() override;
  83. virtual nsIDOMCSSRule*
  84. IndexedGetter(uint32_t aIndex, bool& aFound) override;
  85. virtual uint32_t
  86. Length() override;
  87. void DropReference() { mGroupRule = nullptr; }
  88. private:
  89. ~GroupRuleRuleList();
  90. private:
  91. GroupRule* mGroupRule;
  92. };
  93. GroupRuleRuleList::GroupRuleRuleList(GroupRule *aGroupRule)
  94. {
  95. // Not reference counted to avoid circular references.
  96. // The rule will tell us when its going away.
  97. mGroupRule = aGroupRule;
  98. }
  99. GroupRuleRuleList::~GroupRuleRuleList()
  100. {
  101. }
  102. CSSStyleSheet*
  103. GroupRuleRuleList::GetParentObject()
  104. {
  105. if (!mGroupRule) {
  106. return nullptr;
  107. }
  108. return mGroupRule->GetStyleSheet();
  109. }
  110. uint32_t
  111. GroupRuleRuleList::Length()
  112. {
  113. if (!mGroupRule) {
  114. return 0;
  115. }
  116. return AssertedCast<uint32_t>(mGroupRule->StyleRuleCount());
  117. }
  118. nsIDOMCSSRule*
  119. GroupRuleRuleList::IndexedGetter(uint32_t aIndex, bool& aFound)
  120. {
  121. aFound = false;
  122. if (mGroupRule) {
  123. RefPtr<Rule> rule = mGroupRule->GetStyleRuleAt(aIndex);
  124. if (rule) {
  125. aFound = true;
  126. return rule->GetDOMRule();
  127. }
  128. }
  129. return nullptr;
  130. }
  131. // -------------------------------------------
  132. // ImportRule
  133. //
  134. ImportRule::ImportRule(nsMediaList* aMedia, const nsString& aURLSpec,
  135. uint32_t aLineNumber, uint32_t aColumnNumber)
  136. : Rule(aLineNumber, aColumnNumber)
  137. , mURLSpec(aURLSpec)
  138. , mMedia(aMedia)
  139. {
  140. // XXXbz This is really silly.... the mMedia here will be replaced
  141. // with itself if we manage to load a sheet. Which should really
  142. // never fail nowadays, in sane cases.
  143. }
  144. ImportRule::ImportRule(const ImportRule& aCopy)
  145. : Rule(aCopy),
  146. mURLSpec(aCopy.mURLSpec)
  147. {
  148. // Whether or not an @import rule has a null sheet is a permanent
  149. // property of that @import rule, since it is null only if the target
  150. // sheet failed security checks.
  151. if (aCopy.mChildSheet) {
  152. RefPtr<CSSStyleSheet> sheet =
  153. aCopy.mChildSheet->Clone(nullptr, this, nullptr, nullptr);
  154. SetSheet(sheet);
  155. // SetSheet sets mMedia appropriately
  156. }
  157. }
  158. ImportRule::~ImportRule()
  159. {
  160. if (mChildSheet) {
  161. mChildSheet->SetOwnerRule(nullptr);
  162. }
  163. }
  164. NS_IMPL_CYCLE_COLLECTING_ADDREF(ImportRule)
  165. NS_IMPL_CYCLE_COLLECTING_RELEASE(ImportRule)
  166. // QueryInterface implementation for ImportRule
  167. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ImportRule)
  168. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSRule)
  169. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSImportRule)
  170. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozilla::css::Rule)
  171. NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(CSSImportRule)
  172. NS_INTERFACE_MAP_END
  173. IMPL_STYLE_RULE_INHERIT(ImportRule, Rule)
  174. NS_IMPL_CYCLE_COLLECTION_CLASS(ImportRule)
  175. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ImportRule)
  176. if (tmp->mChildSheet) {
  177. tmp->mChildSheet->SetOwnerRule(nullptr);
  178. tmp->mChildSheet = nullptr;
  179. }
  180. tmp->mMedia = nullptr;
  181. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  182. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ImportRule)
  183. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMedia)
  184. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mChildSheet)
  185. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  186. #ifdef DEBUG
  187. /* virtual */ void
  188. ImportRule::List(FILE* out, int32_t aIndent) const
  189. {
  190. nsAutoCString str;
  191. // Indent
  192. for (int32_t indent = aIndent; --indent >= 0; ) {
  193. str.AppendLiteral(" ");
  194. }
  195. str.AppendLiteral("@import \"");
  196. AppendUTF16toUTF8(mURLSpec, str);
  197. str.AppendLiteral("\" ");
  198. nsAutoString mediaText;
  199. mMedia->GetText(mediaText);
  200. AppendUTF16toUTF8(mediaText, str);
  201. str.AppendLiteral("\n");
  202. fprintf_stderr(out, "%s", str.get());
  203. }
  204. #endif
  205. /* virtual */ int32_t
  206. ImportRule::GetType() const
  207. {
  208. return Rule::IMPORT_RULE;
  209. }
  210. /* virtual */ already_AddRefed<Rule>
  211. ImportRule::Clone() const
  212. {
  213. RefPtr<Rule> clone = new ImportRule(*this);
  214. return clone.forget();
  215. }
  216. void
  217. ImportRule::SetSheet(CSSStyleSheet* aSheet)
  218. {
  219. NS_PRECONDITION(aSheet, "null arg");
  220. // set the new sheet
  221. mChildSheet = aSheet;
  222. aSheet->SetOwnerRule(this);
  223. // set our medialist to be the same as the sheet's medialist
  224. mMedia = mChildSheet->Media();
  225. }
  226. NS_IMETHODIMP
  227. ImportRule::GetType(uint16_t* aType)
  228. {
  229. NS_ENSURE_ARG_POINTER(aType);
  230. *aType = nsIDOMCSSRule::IMPORT_RULE;
  231. return NS_OK;
  232. }
  233. NS_IMETHODIMP
  234. ImportRule::GetCssText(nsAString& aCssText)
  235. {
  236. aCssText.AssignLiteral("@import url(");
  237. nsStyleUtil::AppendEscapedCSSString(mURLSpec, aCssText);
  238. aCssText.Append(')');
  239. if (mMedia) {
  240. nsAutoString mediaText;
  241. mMedia->GetText(mediaText);
  242. if (!mediaText.IsEmpty()) {
  243. aCssText.Append(' ');
  244. aCssText.Append(mediaText);
  245. }
  246. }
  247. aCssText.Append(';');
  248. return NS_OK;
  249. }
  250. NS_IMETHODIMP
  251. ImportRule::SetCssText(const nsAString& aCssText)
  252. {
  253. return NS_ERROR_NOT_IMPLEMENTED;
  254. }
  255. NS_IMETHODIMP
  256. ImportRule::GetParentStyleSheet(nsIDOMCSSStyleSheet** aSheet)
  257. {
  258. return Rule::GetParentStyleSheet(aSheet);
  259. }
  260. NS_IMETHODIMP
  261. ImportRule::GetParentRule(nsIDOMCSSRule** aParentRule)
  262. {
  263. return Rule::GetParentRule(aParentRule);
  264. }
  265. css::Rule*
  266. ImportRule::GetCSSRule()
  267. {
  268. return Rule::GetCSSRule();
  269. }
  270. NS_IMETHODIMP
  271. ImportRule::GetHref(nsAString & aHref)
  272. {
  273. aHref = mURLSpec;
  274. return NS_OK;
  275. }
  276. NS_IMETHODIMP
  277. ImportRule::GetMedia(nsIDOMMediaList * *aMedia)
  278. {
  279. NS_ENSURE_ARG_POINTER(aMedia);
  280. NS_IF_ADDREF(*aMedia = mMedia);
  281. return NS_OK;
  282. }
  283. NS_IMETHODIMP
  284. ImportRule::GetStyleSheet(nsIDOMCSSStyleSheet * *aStyleSheet)
  285. {
  286. NS_ENSURE_ARG_POINTER(aStyleSheet);
  287. NS_IF_ADDREF(*aStyleSheet = mChildSheet);
  288. return NS_OK;
  289. }
  290. /* virtual */ size_t
  291. ImportRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
  292. {
  293. return aMallocSizeOf(this);
  294. // Measurement of the following members may be added later if DMD finds it is
  295. // worthwhile:
  296. // - mURLSpec
  297. //
  298. // The following members are not measured:
  299. // - mMedia, because it is measured via CSSStyleSheet::mMedia
  300. // - mChildSheet, because it is measured via CSSStyleSheetInner::mSheets
  301. }
  302. GroupRule::GroupRule(uint32_t aLineNumber, uint32_t aColumnNumber)
  303. : Rule(aLineNumber, aColumnNumber)
  304. {
  305. }
  306. static bool
  307. SetParentRuleReference(Rule* aRule, void* aParentRule)
  308. {
  309. GroupRule* parentRule = static_cast<GroupRule*>(aParentRule);
  310. aRule->SetParentRule(parentRule);
  311. return true;
  312. }
  313. GroupRule::GroupRule(const GroupRule& aCopy)
  314. : Rule(aCopy)
  315. {
  316. const_cast<GroupRule&>(aCopy).mRules.EnumerateForwards(GroupRule::CloneRuleInto, &mRules);
  317. mRules.EnumerateForwards(SetParentRuleReference, this);
  318. }
  319. GroupRule::~GroupRule()
  320. {
  321. MOZ_ASSERT(!mSheet, "SetStyleSheet should have been called");
  322. mRules.EnumerateForwards(SetParentRuleReference, nullptr);
  323. if (mRuleCollection) {
  324. mRuleCollection->DropReference();
  325. }
  326. }
  327. NS_IMPL_CYCLE_COLLECTING_ADDREF(GroupRule)
  328. NS_IMPL_CYCLE_COLLECTING_RELEASE(GroupRule)
  329. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GroupRule)
  330. NS_INTERFACE_MAP_END
  331. static bool
  332. SetStyleSheetReference(Rule* aRule, void* aSheet)
  333. {
  334. CSSStyleSheet* sheet = (CSSStyleSheet*)aSheet;
  335. aRule->SetStyleSheet(sheet);
  336. return true;
  337. }
  338. NS_IMPL_CYCLE_COLLECTION_CLASS(GroupRule)
  339. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(GroupRule)
  340. tmp->mRules.EnumerateForwards(SetParentRuleReference, nullptr);
  341. // If tmp does not have a stylesheet, neither do its descendants. In that
  342. // case, don't try to null out their stylesheet, to avoid O(N^2) behavior in
  343. // depth of group rule nesting. But if tmp _does_ have a stylesheet (which
  344. // can happen if it gets unlinked earlier than its owning stylesheet), then we
  345. // need to null out the stylesheet pointer on descendants now, before we clear
  346. // tmp->mRules.
  347. if (tmp->GetStyleSheet()) {
  348. tmp->mRules.EnumerateForwards(SetStyleSheetReference, nullptr);
  349. }
  350. tmp->mRules.Clear();
  351. if (tmp->mRuleCollection) {
  352. tmp->mRuleCollection->DropReference();
  353. tmp->mRuleCollection = nullptr;
  354. }
  355. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  356. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(GroupRule)
  357. const nsCOMArray<Rule>& rules = tmp->mRules;
  358. for (int32_t i = 0, count = rules.Count(); i < count; ++i) {
  359. NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mRules[i]");
  360. cb.NoteXPCOMChild(rules[i]->GetExistingDOMRule());
  361. }
  362. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRuleCollection)
  363. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  364. /* virtual */ void
  365. GroupRule::SetStyleSheet(CSSStyleSheet* aSheet)
  366. {
  367. // Don't set the sheet on the kids if it's already the same as the sheet we
  368. // already have. This is needed to avoid O(N^2) behavior in group nesting
  369. // depth when seting the sheet to null during unlink, if we happen to unlin in
  370. // order from most nested rule up to least nested rule.
  371. if (aSheet != GetStyleSheet()) {
  372. mRules.EnumerateForwards(SetStyleSheetReference, aSheet);
  373. Rule::SetStyleSheet(aSheet);
  374. }
  375. }
  376. #ifdef DEBUG
  377. /* virtual */ void
  378. GroupRule::List(FILE* out, int32_t aIndent) const
  379. {
  380. for (int32_t index = 0, count = mRules.Count(); index < count; ++index) {
  381. mRules.ObjectAt(index)->List(out, aIndent + 1);
  382. }
  383. }
  384. #endif
  385. void
  386. GroupRule::AppendStyleRule(Rule* aRule)
  387. {
  388. mRules.AppendObject(aRule);
  389. CSSStyleSheet* sheet = GetStyleSheet();
  390. aRule->SetStyleSheet(sheet);
  391. aRule->SetParentRule(this);
  392. if (sheet) {
  393. sheet->SetModifiedByChildRule();
  394. }
  395. }
  396. Rule*
  397. GroupRule::GetStyleRuleAt(int32_t aIndex) const
  398. {
  399. return mRules.SafeObjectAt(aIndex);
  400. }
  401. bool
  402. GroupRule::EnumerateRulesForwards(RuleEnumFunc aFunc, void * aData) const
  403. {
  404. return
  405. const_cast<GroupRule*>(this)->mRules.EnumerateForwards(aFunc, aData);
  406. }
  407. /*
  408. * The next two methods (DeleteStyleRuleAt and InsertStyleRuleAt)
  409. * should never be called unless you have first called WillDirty() on
  410. * the parents stylesheet. After they are called, DidDirty() needs to
  411. * be called on the sheet
  412. */
  413. nsresult
  414. GroupRule::DeleteStyleRuleAt(uint32_t aIndex)
  415. {
  416. Rule* rule = mRules.SafeObjectAt(aIndex);
  417. if (rule) {
  418. rule->SetStyleSheet(nullptr);
  419. rule->SetParentRule(nullptr);
  420. }
  421. return mRules.RemoveObjectAt(aIndex) ? NS_OK : NS_ERROR_ILLEGAL_VALUE;
  422. }
  423. nsresult
  424. GroupRule::InsertStyleRuleAt(uint32_t aIndex, Rule* aRule)
  425. {
  426. aRule->SetStyleSheet(GetStyleSheet());
  427. aRule->SetParentRule(this);
  428. if (! mRules.InsertObjectAt(aRule, aIndex)) {
  429. return NS_ERROR_FAILURE;
  430. }
  431. return NS_OK;
  432. }
  433. void
  434. GroupRule::AppendRulesToCssText(nsAString& aCssText)
  435. {
  436. aCssText.AppendLiteral(" {\n");
  437. // get all the rules
  438. for (int32_t index = 0, count = mRules.Count(); index < count; ++index) {
  439. Rule* rule = mRules.ObjectAt(index);
  440. nsIDOMCSSRule* domRule = rule->GetDOMRule();
  441. if (domRule) {
  442. nsAutoString cssText;
  443. domRule->GetCssText(cssText);
  444. aCssText.AppendLiteral(" ");
  445. aCssText.Append(cssText);
  446. aCssText.Append('\n');
  447. }
  448. }
  449. aCssText.Append('}');
  450. }
  451. // nsIDOMCSSMediaRule or nsIDOMCSSMozDocumentRule methods
  452. nsresult
  453. GroupRule::GetCssRules(nsIDOMCSSRuleList* *aRuleList)
  454. {
  455. if (!mRuleCollection) {
  456. mRuleCollection = new css::GroupRuleRuleList(this);
  457. }
  458. NS_ADDREF(*aRuleList = mRuleCollection);
  459. return NS_OK;
  460. }
  461. nsresult
  462. GroupRule::InsertRule(const nsAString & aRule, uint32_t aIndex, uint32_t* _retval)
  463. {
  464. CSSStyleSheet* sheet = GetStyleSheet();
  465. NS_ENSURE_TRUE(sheet, NS_ERROR_FAILURE);
  466. if (aIndex > uint32_t(mRules.Count()))
  467. return NS_ERROR_DOM_INDEX_SIZE_ERR;
  468. NS_ASSERTION(uint32_t(mRules.Count()) <= INT32_MAX,
  469. "Too many style rules!");
  470. return sheet->InsertRuleIntoGroup(aRule, this, aIndex, _retval);
  471. }
  472. nsresult
  473. GroupRule::DeleteRule(uint32_t aIndex)
  474. {
  475. CSSStyleSheet* sheet = GetStyleSheet();
  476. NS_ENSURE_TRUE(sheet, NS_ERROR_FAILURE);
  477. if (aIndex >= uint32_t(mRules.Count()))
  478. return NS_ERROR_DOM_INDEX_SIZE_ERR;
  479. NS_ASSERTION(uint32_t(mRules.Count()) <= INT32_MAX,
  480. "Too many style rules!");
  481. return sheet->DeleteRuleFromGroup(this, aIndex);
  482. }
  483. /* virtual */ size_t
  484. GroupRule::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
  485. {
  486. size_t n = mRules.ShallowSizeOfExcludingThis(aMallocSizeOf);
  487. for (size_t i = 0; i < mRules.Length(); i++) {
  488. n += mRules[i]->SizeOfIncludingThis(aMallocSizeOf);
  489. }
  490. // Measurement of the following members may be added later if DMD finds it is
  491. // worthwhile:
  492. // - mRuleCollection
  493. return n;
  494. }
  495. // -------------------------------------------
  496. // nsICSSMediaRule
  497. //
  498. MediaRule::MediaRule(uint32_t aLineNumber, uint32_t aColumnNumber)
  499. : GroupRule(aLineNumber, aColumnNumber)
  500. {
  501. }
  502. MediaRule::MediaRule(const MediaRule& aCopy)
  503. : GroupRule(aCopy)
  504. {
  505. if (aCopy.mMedia) {
  506. mMedia = aCopy.mMedia->Clone();
  507. // XXXldb This doesn't really make sense.
  508. mMedia->SetStyleSheet(aCopy.GetStyleSheet());
  509. }
  510. }
  511. MediaRule::~MediaRule()
  512. {
  513. if (mMedia) {
  514. mMedia->SetStyleSheet(nullptr);
  515. }
  516. }
  517. NS_IMPL_ADDREF_INHERITED(MediaRule, GroupRule)
  518. NS_IMPL_RELEASE_INHERITED(MediaRule, GroupRule)
  519. // QueryInterface implementation for MediaRule
  520. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MediaRule)
  521. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSRule)
  522. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSGroupingRule)
  523. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSConditionRule)
  524. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSMediaRule)
  525. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozilla::css::Rule)
  526. NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(CSSMediaRule)
  527. NS_INTERFACE_MAP_END_INHERITING(GroupRule)
  528. NS_IMPL_CYCLE_COLLECTION_CLASS(MediaRule)
  529. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MediaRule, GroupRule)
  530. if (tmp->mMedia) {
  531. tmp->mMedia->SetStyleSheet(nullptr);
  532. tmp->mMedia = nullptr;
  533. }
  534. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  535. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MediaRule, GroupRule)
  536. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMedia)
  537. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  538. /* virtual */ void
  539. MediaRule::SetStyleSheet(CSSStyleSheet* aSheet)
  540. {
  541. if (mMedia) {
  542. // Set to null so it knows it's leaving one sheet and joining another.
  543. mMedia->SetStyleSheet(nullptr);
  544. mMedia->SetStyleSheet(aSheet);
  545. }
  546. GroupRule::SetStyleSheet(aSheet);
  547. }
  548. #ifdef DEBUG
  549. /* virtual */ void
  550. MediaRule::List(FILE* out, int32_t aIndent) const
  551. {
  552. nsAutoCString indentStr;
  553. for (int32_t indent = aIndent; --indent >= 0; ) {
  554. indentStr.AppendLiteral(" ");
  555. }
  556. nsAutoCString str(indentStr);
  557. str.AppendLiteral("@media ");
  558. if (mMedia) {
  559. nsAutoString mediaText;
  560. mMedia->GetText(mediaText);
  561. AppendUTF16toUTF8(mediaText, str);
  562. }
  563. str.AppendLiteral(" {\n");
  564. fprintf_stderr(out, "%s", str.get());
  565. GroupRule::List(out, aIndent);
  566. fprintf_stderr(out, "%s}\n", indentStr.get());
  567. }
  568. #endif
  569. /* virtual */ int32_t
  570. MediaRule::GetType() const
  571. {
  572. return Rule::MEDIA_RULE;
  573. }
  574. /* virtual */ already_AddRefed<Rule>
  575. MediaRule::Clone() const
  576. {
  577. RefPtr<Rule> clone = new MediaRule(*this);
  578. return clone.forget();
  579. }
  580. nsresult
  581. MediaRule::SetMedia(nsMediaList* aMedia)
  582. {
  583. mMedia = aMedia;
  584. if (aMedia)
  585. mMedia->SetStyleSheet(GetStyleSheet());
  586. return NS_OK;
  587. }
  588. // nsIDOMCSSRule methods
  589. NS_IMETHODIMP
  590. MediaRule::GetType(uint16_t* aType)
  591. {
  592. *aType = nsIDOMCSSRule::MEDIA_RULE;
  593. return NS_OK;
  594. }
  595. NS_IMETHODIMP
  596. MediaRule::GetCssText(nsAString& aCssText)
  597. {
  598. aCssText.AssignLiteral("@media ");
  599. AppendConditionText(aCssText);
  600. GroupRule::AppendRulesToCssText(aCssText);
  601. return NS_OK;
  602. }
  603. NS_IMETHODIMP
  604. MediaRule::SetCssText(const nsAString& aCssText)
  605. {
  606. return NS_ERROR_NOT_IMPLEMENTED;
  607. }
  608. NS_IMETHODIMP
  609. MediaRule::GetParentStyleSheet(nsIDOMCSSStyleSheet** aSheet)
  610. {
  611. return GroupRule::GetParentStyleSheet(aSheet);
  612. }
  613. NS_IMETHODIMP
  614. MediaRule::GetParentRule(nsIDOMCSSRule** aParentRule)
  615. {
  616. return GroupRule::GetParentRule(aParentRule);
  617. }
  618. css::Rule*
  619. MediaRule::GetCSSRule()
  620. {
  621. return Rule::GetCSSRule();
  622. }
  623. // nsIDOMCSSGroupingRule methods
  624. NS_IMETHODIMP
  625. MediaRule::GetCssRules(nsIDOMCSSRuleList* *aRuleList)
  626. {
  627. return GroupRule::GetCssRules(aRuleList);
  628. }
  629. NS_IMETHODIMP
  630. MediaRule::InsertRule(const nsAString & aRule, uint32_t aIndex, uint32_t* _retval)
  631. {
  632. return GroupRule::InsertRule(aRule, aIndex, _retval);
  633. }
  634. NS_IMETHODIMP
  635. MediaRule::DeleteRule(uint32_t aIndex)
  636. {
  637. return GroupRule::DeleteRule(aIndex);
  638. }
  639. // nsIDOMCSSConditionRule methods
  640. NS_IMETHODIMP
  641. MediaRule::GetConditionText(nsAString& aConditionText)
  642. {
  643. aConditionText.Truncate(0);
  644. AppendConditionText(aConditionText);
  645. return NS_OK;
  646. }
  647. NS_IMETHODIMP
  648. MediaRule::SetConditionText(const nsAString& aConditionText)
  649. {
  650. if (!mMedia) {
  651. RefPtr<nsMediaList> media = new nsMediaList();
  652. media->SetStyleSheet(GetStyleSheet());
  653. nsresult rv = media->SetMediaText(aConditionText);
  654. if (NS_SUCCEEDED(rv)) {
  655. mMedia = media;
  656. }
  657. return rv;
  658. }
  659. return mMedia->SetMediaText(aConditionText);
  660. }
  661. // nsIDOMCSSMediaRule methods
  662. NS_IMETHODIMP
  663. MediaRule::GetMedia(nsIDOMMediaList* *aMedia)
  664. {
  665. NS_ENSURE_ARG_POINTER(aMedia);
  666. NS_IF_ADDREF(*aMedia = mMedia);
  667. return NS_OK;
  668. }
  669. // GroupRule interface
  670. /* virtual */ bool
  671. MediaRule::UseForPresentation(nsPresContext* aPresContext,
  672. nsMediaQueryResultCacheKey& aKey)
  673. {
  674. if (mMedia) {
  675. return mMedia->Matches(aPresContext, &aKey);
  676. }
  677. return true;
  678. }
  679. /* virtual */ size_t
  680. MediaRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
  681. {
  682. size_t n = aMallocSizeOf(this);
  683. n += GroupRule::SizeOfExcludingThis(aMallocSizeOf);
  684. // Measurement of the following members may be added later if DMD finds it is
  685. // worthwhile:
  686. // - mMedia
  687. return n;
  688. }
  689. void
  690. MediaRule::AppendConditionText(nsAString& aOutput)
  691. {
  692. if (mMedia) {
  693. nsAutoString mediaText;
  694. mMedia->GetText(mediaText);
  695. aOutput.Append(mediaText);
  696. }
  697. }
  698. DocumentRule::DocumentRule(uint32_t aLineNumber, uint32_t aColumnNumber)
  699. : GroupRule(aLineNumber, aColumnNumber)
  700. {
  701. }
  702. DocumentRule::DocumentRule(const DocumentRule& aCopy)
  703. : GroupRule(aCopy)
  704. , mURLs(new URL(*aCopy.mURLs))
  705. {
  706. }
  707. DocumentRule::~DocumentRule()
  708. {
  709. }
  710. NS_IMPL_ADDREF_INHERITED(DocumentRule, GroupRule)
  711. NS_IMPL_RELEASE_INHERITED(DocumentRule, GroupRule)
  712. // QueryInterface implementation for DocumentRule
  713. NS_INTERFACE_MAP_BEGIN(DocumentRule)
  714. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSRule)
  715. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSGroupingRule)
  716. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSConditionRule)
  717. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSMozDocumentRule)
  718. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozilla::css::Rule)
  719. NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(CSSMozDocumentRule)
  720. NS_INTERFACE_MAP_END_INHERITING(GroupRule)
  721. #ifdef DEBUG
  722. /* virtual */ void
  723. DocumentRule::List(FILE* out, int32_t aIndent) const
  724. {
  725. nsAutoCString indentStr;
  726. for (int32_t indent = aIndent; --indent >= 0; ) {
  727. indentStr.AppendLiteral(" ");
  728. }
  729. nsAutoCString str;
  730. str.AppendLiteral("@-moz-document ");
  731. for (URL *url = mURLs; url; url = url->next) {
  732. switch (url->func) {
  733. case eURL:
  734. str.AppendLiteral("url(\"");
  735. break;
  736. case eURLPrefix:
  737. str.AppendLiteral("url-prefix(\"");
  738. break;
  739. case eDomain:
  740. str.AppendLiteral("domain(\"");
  741. break;
  742. case eRegExp:
  743. str.AppendLiteral("regexp(\"");
  744. break;
  745. }
  746. nsAutoCString escapedURL(url->url);
  747. escapedURL.ReplaceSubstring("\"", "\\\""); // escape quotes
  748. str.Append(escapedURL);
  749. str.AppendLiteral("\"), ");
  750. }
  751. str.Cut(str.Length() - 2, 1); // remove last ,
  752. fprintf_stderr(out, "%s%s {\n", indentStr.get(), str.get());
  753. GroupRule::List(out, aIndent);
  754. fprintf_stderr(out, "%s}\n", indentStr.get());
  755. }
  756. #endif
  757. /* virtual */ int32_t
  758. DocumentRule::GetType() const
  759. {
  760. return Rule::DOCUMENT_RULE;
  761. }
  762. /* virtual */ already_AddRefed<Rule>
  763. DocumentRule::Clone() const
  764. {
  765. RefPtr<Rule> clone = new DocumentRule(*this);
  766. return clone.forget();
  767. }
  768. // nsIDOMCSSRule methods
  769. NS_IMETHODIMP
  770. DocumentRule::GetType(uint16_t* aType)
  771. {
  772. // XXX What should really happen here?
  773. *aType = nsIDOMCSSRule::UNKNOWN_RULE;
  774. return NS_OK;
  775. }
  776. NS_IMETHODIMP
  777. DocumentRule::GetCssText(nsAString& aCssText)
  778. {
  779. aCssText.AssignLiteral("@-moz-document ");
  780. AppendConditionText(aCssText);
  781. GroupRule::AppendRulesToCssText(aCssText);
  782. return NS_OK;
  783. }
  784. NS_IMETHODIMP
  785. DocumentRule::SetCssText(const nsAString& aCssText)
  786. {
  787. return NS_ERROR_NOT_IMPLEMENTED;
  788. }
  789. NS_IMETHODIMP
  790. DocumentRule::GetParentStyleSheet(nsIDOMCSSStyleSheet** aSheet)
  791. {
  792. return GroupRule::GetParentStyleSheet(aSheet);
  793. }
  794. NS_IMETHODIMP
  795. DocumentRule::GetParentRule(nsIDOMCSSRule** aParentRule)
  796. {
  797. return GroupRule::GetParentRule(aParentRule);
  798. }
  799. css::Rule*
  800. DocumentRule::GetCSSRule()
  801. {
  802. return Rule::GetCSSRule();
  803. }
  804. // nsIDOMCSSGroupingRule methods
  805. NS_IMETHODIMP
  806. DocumentRule::GetCssRules(nsIDOMCSSRuleList* *aRuleList)
  807. {
  808. return GroupRule::GetCssRules(aRuleList);
  809. }
  810. NS_IMETHODIMP
  811. DocumentRule::InsertRule(const nsAString & aRule, uint32_t aIndex, uint32_t* _retval)
  812. {
  813. return GroupRule::InsertRule(aRule, aIndex, _retval);
  814. }
  815. NS_IMETHODIMP
  816. DocumentRule::DeleteRule(uint32_t aIndex)
  817. {
  818. return GroupRule::DeleteRule(aIndex);
  819. }
  820. // nsIDOMCSSConditionRule methods
  821. NS_IMETHODIMP
  822. DocumentRule::GetConditionText(nsAString& aConditionText)
  823. {
  824. aConditionText.Truncate(0);
  825. AppendConditionText(aConditionText);
  826. return NS_OK;
  827. }
  828. NS_IMETHODIMP
  829. DocumentRule::SetConditionText(const nsAString& aConditionText)
  830. {
  831. return NS_ERROR_NOT_IMPLEMENTED;
  832. }
  833. // GroupRule interface
  834. /* virtual */ bool
  835. DocumentRule::UseForPresentation(nsPresContext* aPresContext,
  836. nsMediaQueryResultCacheKey& aKey)
  837. {
  838. return UseForPresentation(aPresContext);
  839. }
  840. bool
  841. DocumentRule::UseForPresentation(nsPresContext* aPresContext)
  842. {
  843. nsIDocument *doc = aPresContext->Document();
  844. nsIURI *docURI = doc->GetDocumentURI();
  845. nsAutoCString docURISpec;
  846. if (docURI) {
  847. // If GetSpec fails (due to OOM) just skip these URI-specific CSS rules.
  848. nsresult rv = docURI->GetSpec(docURISpec);
  849. NS_ENSURE_SUCCESS(rv, false);
  850. }
  851. for (URL *url = mURLs; url; url = url->next) {
  852. switch (url->func) {
  853. case eURL: {
  854. if (docURISpec == url->url)
  855. return true;
  856. } break;
  857. case eURLPrefix: {
  858. if (StringBeginsWith(docURISpec, url->url))
  859. return true;
  860. } break;
  861. case eDomain: {
  862. nsAutoCString host;
  863. if (docURI)
  864. docURI->GetHost(host);
  865. int32_t lenDiff = host.Length() - url->url.Length();
  866. if (lenDiff == 0) {
  867. if (host == url->url)
  868. return true;
  869. } else {
  870. if (StringEndsWith(host, url->url) &&
  871. host.CharAt(lenDiff - 1) == '.')
  872. return true;
  873. }
  874. } break;
  875. case eRegExp: {
  876. NS_ConvertUTF8toUTF16 spec(docURISpec);
  877. NS_ConvertUTF8toUTF16 regex(url->url);
  878. if (nsContentUtils::IsPatternMatching(spec, regex, doc)) {
  879. return true;
  880. }
  881. } break;
  882. }
  883. }
  884. return false;
  885. }
  886. DocumentRule::URL::~URL()
  887. {
  888. NS_CSS_DELETE_LIST_MEMBER(DocumentRule::URL, this, next);
  889. }
  890. /* virtual */ size_t
  891. DocumentRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
  892. {
  893. size_t n = aMallocSizeOf(this);
  894. n += GroupRule::SizeOfExcludingThis(aMallocSizeOf);
  895. // Measurement of the following members may be added later if DMD finds it is
  896. // worthwhile:
  897. // - mURLs
  898. return n;
  899. }
  900. void
  901. DocumentRule::AppendConditionText(nsAString& aCssText)
  902. {
  903. for (URL *url = mURLs; url; url = url->next) {
  904. switch (url->func) {
  905. case eURL:
  906. aCssText.AppendLiteral("url(");
  907. break;
  908. case eURLPrefix:
  909. aCssText.AppendLiteral("url-prefix(");
  910. break;
  911. case eDomain:
  912. aCssText.AppendLiteral("domain(");
  913. break;
  914. case eRegExp:
  915. aCssText.AppendLiteral("regexp(");
  916. break;
  917. }
  918. nsStyleUtil::AppendEscapedCSSString(NS_ConvertUTF8toUTF16(url->url),
  919. aCssText);
  920. aCssText.AppendLiteral("), ");
  921. }
  922. aCssText.Truncate(aCssText.Length() - 2); // remove last ", "
  923. }
  924. // -------------------------------------------
  925. // NameSpaceRule
  926. //
  927. NameSpaceRule::NameSpaceRule(nsIAtom* aPrefix, const nsString& aURLSpec,
  928. uint32_t aLineNumber, uint32_t aColumnNumber)
  929. : Rule(aLineNumber, aColumnNumber),
  930. mPrefix(aPrefix),
  931. mURLSpec(aURLSpec)
  932. {
  933. }
  934. NameSpaceRule::NameSpaceRule(const NameSpaceRule& aCopy)
  935. : Rule(aCopy),
  936. mPrefix(aCopy.mPrefix),
  937. mURLSpec(aCopy.mURLSpec)
  938. {
  939. }
  940. NameSpaceRule::~NameSpaceRule()
  941. {
  942. }
  943. NS_IMPL_ADDREF(NameSpaceRule)
  944. NS_IMPL_RELEASE(NameSpaceRule)
  945. // QueryInterface implementation for NameSpaceRule
  946. NS_INTERFACE_MAP_BEGIN(NameSpaceRule)
  947. if (aIID.Equals(NS_GET_IID(css::NameSpaceRule))) {
  948. *aInstancePtr = this;
  949. NS_ADDREF_THIS();
  950. return NS_OK;
  951. }
  952. else
  953. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSRule)
  954. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozilla::css::Rule)
  955. NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(CSSNameSpaceRule)
  956. NS_INTERFACE_MAP_END
  957. IMPL_STYLE_RULE_INHERIT(NameSpaceRule, Rule)
  958. #ifdef DEBUG
  959. /* virtual */ void
  960. NameSpaceRule::List(FILE* out, int32_t aIndent) const
  961. {
  962. nsAutoCString str;
  963. for (int32_t indent = aIndent; --indent >= 0; ) {
  964. str.AppendLiteral(" ");
  965. }
  966. nsAutoString buffer;
  967. str.AppendLiteral("@namespace ");
  968. if (mPrefix) {
  969. mPrefix->ToString(buffer);
  970. AppendUTF16toUTF8(buffer, str);
  971. str.Append(' ');
  972. }
  973. str.AppendLiteral("url(\"");
  974. AppendUTF16toUTF8(mURLSpec, str);
  975. str.AppendLiteral("\")\n");
  976. fprintf_stderr(out, "%s", str.get());
  977. }
  978. #endif
  979. /* virtual */ int32_t
  980. NameSpaceRule::GetType() const
  981. {
  982. return Rule::NAMESPACE_RULE;
  983. }
  984. /* virtual */ already_AddRefed<Rule>
  985. NameSpaceRule::Clone() const
  986. {
  987. RefPtr<Rule> clone = new NameSpaceRule(*this);
  988. return clone.forget();
  989. }
  990. NS_IMETHODIMP
  991. NameSpaceRule::GetType(uint16_t* aType)
  992. {
  993. *aType = nsIDOMCSSRule::NAMESPACE_RULE;
  994. return NS_OK;
  995. }
  996. NS_IMETHODIMP
  997. NameSpaceRule::GetCssText(nsAString& aCssText)
  998. {
  999. aCssText.AssignLiteral("@namespace ");
  1000. if (mPrefix) {
  1001. aCssText.Append(nsDependentAtomString(mPrefix) + NS_LITERAL_STRING(" "));
  1002. }
  1003. aCssText.AppendLiteral("url(");
  1004. nsStyleUtil::AppendEscapedCSSString(mURLSpec, aCssText);
  1005. aCssText.AppendLiteral(");");
  1006. return NS_OK;
  1007. }
  1008. NS_IMETHODIMP
  1009. NameSpaceRule::SetCssText(const nsAString& aCssText)
  1010. {
  1011. return NS_ERROR_NOT_IMPLEMENTED;
  1012. }
  1013. NS_IMETHODIMP
  1014. NameSpaceRule::GetParentStyleSheet(nsIDOMCSSStyleSheet** aSheet)
  1015. {
  1016. return Rule::GetParentStyleSheet(aSheet);
  1017. }
  1018. NS_IMETHODIMP
  1019. NameSpaceRule::GetParentRule(nsIDOMCSSRule** aParentRule)
  1020. {
  1021. return Rule::GetParentRule(aParentRule);
  1022. }
  1023. css::Rule*
  1024. NameSpaceRule::GetCSSRule()
  1025. {
  1026. return Rule::GetCSSRule();
  1027. }
  1028. /* virtual */ size_t
  1029. NameSpaceRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
  1030. {
  1031. return aMallocSizeOf(this);
  1032. // Measurement of the following members may be added later if DMD finds it is
  1033. // worthwhile:
  1034. // - mPrefix
  1035. // - mURLSpec
  1036. }
  1037. } // namespace css
  1038. } // namespace mozilla
  1039. // -------------------------------------------
  1040. // nsCSSFontFaceStyleDecl and related routines
  1041. //
  1042. // Mapping from nsCSSFontDesc codes to CSSFontFaceDescriptors fields.
  1043. nsCSSValue CSSFontFaceDescriptors::* const
  1044. CSSFontFaceDescriptors::Fields[] = {
  1045. #define CSS_FONT_DESC(name_, method_) &CSSFontFaceDescriptors::m##method_,
  1046. #include "nsCSSFontDescList.h"
  1047. #undef CSS_FONT_DESC
  1048. };
  1049. const nsCSSValue&
  1050. CSSFontFaceDescriptors::Get(nsCSSFontDesc aFontDescID) const
  1051. {
  1052. MOZ_ASSERT(aFontDescID > eCSSFontDesc_UNKNOWN &&
  1053. aFontDescID < eCSSFontDesc_COUNT);
  1054. return this->*CSSFontFaceDescriptors::Fields[aFontDescID];
  1055. }
  1056. nsCSSValue&
  1057. CSSFontFaceDescriptors::Get(nsCSSFontDesc aFontDescID)
  1058. {
  1059. MOZ_ASSERT(aFontDescID > eCSSFontDesc_UNKNOWN &&
  1060. aFontDescID < eCSSFontDesc_COUNT);
  1061. return this->*CSSFontFaceDescriptors::Fields[aFontDescID];
  1062. }
  1063. // QueryInterface implementation for nsCSSFontFaceStyleDecl
  1064. NS_INTERFACE_MAP_BEGIN(nsCSSFontFaceStyleDecl)
  1065. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  1066. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSStyleDeclaration)
  1067. NS_INTERFACE_MAP_ENTRY(nsICSSDeclaration)
  1068. NS_INTERFACE_MAP_ENTRY(nsISupports)
  1069. // We forward the cycle collection interfaces to ContainingRule(), which is
  1070. // never null (in fact, we're part of that object!)
  1071. if (aIID.Equals(NS_GET_IID(nsCycleCollectionISupports)) ||
  1072. aIID.Equals(NS_GET_IID(nsXPCOMCycleCollectionParticipant))) {
  1073. return ContainingRule()->QueryInterface(aIID, aInstancePtr);
  1074. }
  1075. else
  1076. NS_INTERFACE_MAP_END
  1077. NS_IMPL_ADDREF_USING_AGGREGATOR(nsCSSFontFaceStyleDecl, ContainingRule())
  1078. NS_IMPL_RELEASE_USING_AGGREGATOR(nsCSSFontFaceStyleDecl, ContainingRule())
  1079. // helper for string GetPropertyValue and RemovePropertyValue
  1080. nsresult
  1081. nsCSSFontFaceStyleDecl::GetPropertyValue(nsCSSFontDesc aFontDescID,
  1082. nsAString & aResult) const
  1083. {
  1084. NS_ENSURE_ARG_RANGE(aFontDescID, eCSSFontDesc_UNKNOWN,
  1085. eCSSFontDesc_COUNT - 1);
  1086. aResult.Truncate();
  1087. if (aFontDescID == eCSSFontDesc_UNKNOWN)
  1088. return NS_OK;
  1089. const nsCSSValue& val = mDescriptors.Get(aFontDescID);
  1090. if (val.GetUnit() == eCSSUnit_Null) {
  1091. // Avoid having to check no-value in the Family and Src cases below.
  1092. return NS_OK;
  1093. }
  1094. switch (aFontDescID) {
  1095. case eCSSFontDesc_Family: {
  1096. // we don't use nsCSSValue::AppendToString here because it doesn't
  1097. // canonicalize the way we want, and anyway it's overkill when
  1098. // we know we have eCSSUnit_String
  1099. NS_ASSERTION(val.GetUnit() == eCSSUnit_String, "unexpected unit");
  1100. nsDependentString family(val.GetStringBufferValue());
  1101. nsStyleUtil::AppendEscapedCSSString(family, aResult);
  1102. return NS_OK;
  1103. }
  1104. case eCSSFontDesc_Style:
  1105. val.AppendToString(eCSSProperty_font_style, aResult,
  1106. nsCSSValue::eNormalized);
  1107. return NS_OK;
  1108. case eCSSFontDesc_Weight:
  1109. val.AppendToString(eCSSProperty_font_weight, aResult,
  1110. nsCSSValue::eNormalized);
  1111. return NS_OK;
  1112. case eCSSFontDesc_Stretch:
  1113. val.AppendToString(eCSSProperty_font_stretch, aResult,
  1114. nsCSSValue::eNormalized);
  1115. return NS_OK;
  1116. case eCSSFontDesc_FontFeatureSettings:
  1117. nsStyleUtil::AppendFontFeatureSettings(val, aResult);
  1118. return NS_OK;
  1119. case eCSSFontDesc_FontLanguageOverride:
  1120. val.AppendToString(eCSSProperty_font_language_override, aResult,
  1121. nsCSSValue::eNormalized);
  1122. return NS_OK;
  1123. case eCSSFontDesc_Display:
  1124. NS_ASSERTION(val.GetUnit() == eCSSUnit_Enumerated,
  1125. "unknown unit for font-display descriptor");
  1126. AppendASCIItoUTF16(nsCSSProps::ValueToKeyword(val.GetIntValue(),
  1127. nsCSSProps::kFontDisplayKTable), aResult);
  1128. return NS_OK;
  1129. case eCSSFontDesc_Src:
  1130. nsStyleUtil::AppendSerializedFontSrc(val, aResult);
  1131. return NS_OK;
  1132. case eCSSFontDesc_UnicodeRange:
  1133. nsStyleUtil::AppendUnicodeRange(val, aResult);
  1134. return NS_OK;
  1135. case eCSSFontDesc_UNKNOWN:
  1136. case eCSSFontDesc_COUNT:
  1137. ;
  1138. }
  1139. NS_NOTREACHED("nsCSSFontFaceStyleDecl::GetPropertyValue: "
  1140. "out-of-range value got to the switch");
  1141. return NS_ERROR_INVALID_ARG;
  1142. }
  1143. NS_IMETHODIMP
  1144. nsCSSFontFaceStyleDecl::GetCssText(nsAString & aCssText)
  1145. {
  1146. nsAutoString descStr;
  1147. aCssText.Truncate();
  1148. for (nsCSSFontDesc id = nsCSSFontDesc(eCSSFontDesc_UNKNOWN + 1);
  1149. id < eCSSFontDesc_COUNT;
  1150. id = nsCSSFontDesc(id + 1)) {
  1151. if (mDescriptors.Get(id).GetUnit() != eCSSUnit_Null &&
  1152. NS_SUCCEEDED(GetPropertyValue(id, descStr))) {
  1153. NS_ASSERTION(descStr.Length() > 0,
  1154. "GetCssText: non-null unit, empty property value");
  1155. aCssText.AppendLiteral(" ");
  1156. aCssText.AppendASCII(nsCSSProps::GetStringValue(id).get());
  1157. aCssText.AppendLiteral(": ");
  1158. aCssText.Append(descStr);
  1159. aCssText.AppendLiteral(";\n");
  1160. }
  1161. }
  1162. return NS_OK;
  1163. }
  1164. NS_IMETHODIMP
  1165. nsCSSFontFaceStyleDecl::SetCssText(const nsAString & aCssText)
  1166. {
  1167. return NS_ERROR_NOT_IMPLEMENTED; // bug 443978
  1168. }
  1169. NS_IMETHODIMP
  1170. nsCSSFontFaceStyleDecl::GetPropertyValue(const nsAString & propertyName,
  1171. nsAString & aResult)
  1172. {
  1173. return GetPropertyValue(nsCSSProps::LookupFontDesc(propertyName), aResult);
  1174. }
  1175. NS_IMETHODIMP
  1176. nsCSSFontFaceStyleDecl::GetAuthoredPropertyValue(const nsAString& propertyName,
  1177. nsAString& aResult)
  1178. {
  1179. // We don't return any authored property values different from
  1180. // GetPropertyValue, currently.
  1181. return GetPropertyValue(nsCSSProps::LookupFontDesc(propertyName), aResult);
  1182. }
  1183. already_AddRefed<dom::CSSValue>
  1184. nsCSSFontFaceStyleDecl::GetPropertyCSSValue(const nsAString & propertyName,
  1185. ErrorResult& aRv)
  1186. {
  1187. // ??? nsDOMCSSDeclaration returns null/NS_OK, but that seems wrong.
  1188. aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
  1189. return nullptr;
  1190. }
  1191. NS_IMETHODIMP
  1192. nsCSSFontFaceStyleDecl::RemoveProperty(const nsAString & propertyName,
  1193. nsAString & aResult)
  1194. {
  1195. nsCSSFontDesc descID = nsCSSProps::LookupFontDesc(propertyName);
  1196. NS_ASSERTION(descID >= eCSSFontDesc_UNKNOWN &&
  1197. descID < eCSSFontDesc_COUNT,
  1198. "LookupFontDesc returned value out of range");
  1199. if (descID == eCSSFontDesc_UNKNOWN) {
  1200. aResult.Truncate();
  1201. } else {
  1202. nsresult rv = GetPropertyValue(descID, aResult);
  1203. NS_ENSURE_SUCCESS(rv, rv);
  1204. mDescriptors.Get(descID).Reset();
  1205. }
  1206. return NS_OK;
  1207. }
  1208. NS_IMETHODIMP
  1209. nsCSSFontFaceStyleDecl::GetPropertyPriority(const nsAString & propertyName,
  1210. nsAString & aResult)
  1211. {
  1212. // font descriptors do not have priorities at present
  1213. aResult.Truncate();
  1214. return NS_OK;
  1215. }
  1216. NS_IMETHODIMP
  1217. nsCSSFontFaceStyleDecl::SetProperty(const nsAString & propertyName,
  1218. const nsAString & value,
  1219. const nsAString & priority)
  1220. {
  1221. return NS_ERROR_NOT_IMPLEMENTED; // bug 443978
  1222. }
  1223. NS_IMETHODIMP
  1224. nsCSSFontFaceStyleDecl::GetLength(uint32_t *aLength)
  1225. {
  1226. uint32_t len = 0;
  1227. for (nsCSSFontDesc id = nsCSSFontDesc(eCSSFontDesc_UNKNOWN + 1);
  1228. id < eCSSFontDesc_COUNT;
  1229. id = nsCSSFontDesc(id + 1))
  1230. if (mDescriptors.Get(id).GetUnit() != eCSSUnit_Null)
  1231. len++;
  1232. *aLength = len;
  1233. return NS_OK;
  1234. }
  1235. NS_IMETHODIMP
  1236. nsCSSFontFaceStyleDecl::Item(uint32_t aIndex, nsAString& aReturn)
  1237. {
  1238. bool found;
  1239. IndexedGetter(aIndex, found, aReturn);
  1240. if (!found) {
  1241. aReturn.Truncate();
  1242. }
  1243. return NS_OK;
  1244. }
  1245. void
  1246. nsCSSFontFaceStyleDecl::IndexedGetter(uint32_t index, bool& aFound, nsAString & aResult)
  1247. {
  1248. int32_t nset = -1;
  1249. for (nsCSSFontDesc id = nsCSSFontDesc(eCSSFontDesc_UNKNOWN + 1);
  1250. id < eCSSFontDesc_COUNT;
  1251. id = nsCSSFontDesc(id + 1)) {
  1252. if (mDescriptors.Get(id).GetUnit() != eCSSUnit_Null) {
  1253. nset++;
  1254. if (nset == int32_t(index)) {
  1255. aFound = true;
  1256. aResult.AssignASCII(nsCSSProps::GetStringValue(id).get());
  1257. return;
  1258. }
  1259. }
  1260. }
  1261. aFound = false;
  1262. }
  1263. NS_IMETHODIMP
  1264. nsCSSFontFaceStyleDecl::GetParentRule(nsIDOMCSSRule** aParentRule)
  1265. {
  1266. NS_IF_ADDREF(*aParentRule = ContainingRule()->GetDOMRule());
  1267. return NS_OK;
  1268. }
  1269. NS_IMETHODIMP
  1270. nsCSSFontFaceStyleDecl::GetPropertyValue(const nsCSSPropertyID aPropID,
  1271. nsAString& aValue)
  1272. {
  1273. return
  1274. GetPropertyValue(NS_ConvertUTF8toUTF16(nsCSSProps::GetStringValue(aPropID)),
  1275. aValue);
  1276. }
  1277. NS_IMETHODIMP
  1278. nsCSSFontFaceStyleDecl::SetPropertyValue(const nsCSSPropertyID aPropID,
  1279. const nsAString& aValue)
  1280. {
  1281. return SetProperty(NS_ConvertUTF8toUTF16(nsCSSProps::GetStringValue(aPropID)),
  1282. aValue, EmptyString());
  1283. }
  1284. nsINode*
  1285. nsCSSFontFaceStyleDecl::GetParentObject()
  1286. {
  1287. return ContainingRule()->GetDocument();
  1288. }
  1289. DocGroup*
  1290. nsCSSFontFaceStyleDecl::GetDocGroup() const
  1291. {
  1292. nsIDocument* document = ContainingRule()->GetDocument();
  1293. return document ? document->GetDocGroup() : nullptr;
  1294. }
  1295. JSObject*
  1296. nsCSSFontFaceStyleDecl::WrapObject(JSContext *cx, JS::Handle<JSObject*> aGivenProto)
  1297. {
  1298. return mozilla::dom::CSSStyleDeclarationBinding::Wrap(cx, this, aGivenProto);
  1299. }
  1300. // -------------------------------------------
  1301. // nsCSSFontFaceRule
  1302. //
  1303. /* virtual */ already_AddRefed<css::Rule>
  1304. nsCSSFontFaceRule::Clone() const
  1305. {
  1306. RefPtr<css::Rule> clone = new nsCSSFontFaceRule(*this);
  1307. return clone.forget();
  1308. }
  1309. NS_IMPL_CYCLE_COLLECTING_ADDREF(nsCSSFontFaceRule)
  1310. NS_IMPL_CYCLE_COLLECTING_RELEASE(nsCSSFontFaceRule)
  1311. NS_IMPL_CYCLE_COLLECTION_CLASS(nsCSSFontFaceRule)
  1312. NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsCSSFontFaceRule)
  1313. // Trace the wrapper for our declaration. This just expands out
  1314. // NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER which we can't use
  1315. // directly because the wrapper is on the declaration, not on us.
  1316. tmp->mDecl.TraceWrapper(aCallbacks, aClosure);
  1317. NS_IMPL_CYCLE_COLLECTION_TRACE_END
  1318. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsCSSFontFaceRule)
  1319. // Unlink the wrapper for our declaraton. This just expands out
  1320. // NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER which we can't use
  1321. // directly because the wrapper is on the declaration, not on us.
  1322. tmp->mDecl.ReleaseWrapper(static_cast<nsISupports*>(p));
  1323. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  1324. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsCSSFontFaceRule)
  1325. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  1326. // QueryInterface implementation for nsCSSFontFaceRule
  1327. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsCSSFontFaceRule)
  1328. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSFontFaceRule)
  1329. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSRule)
  1330. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozilla::css::Rule)
  1331. NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(CSSFontFaceRule)
  1332. NS_INTERFACE_MAP_END
  1333. IMPL_STYLE_RULE_INHERIT(nsCSSFontFaceRule, Rule)
  1334. #ifdef DEBUG
  1335. void
  1336. nsCSSFontFaceRule::List(FILE* out, int32_t aIndent) const
  1337. {
  1338. nsCString baseInd, descInd;
  1339. for (int32_t indent = aIndent; --indent >= 0; ) {
  1340. baseInd.AppendLiteral(" ");
  1341. descInd.AppendLiteral(" ");
  1342. }
  1343. descInd.AppendLiteral(" ");
  1344. nsString descStr;
  1345. fprintf_stderr(out, "%s@font-face {\n", baseInd.get());
  1346. for (nsCSSFontDesc id = nsCSSFontDesc(eCSSFontDesc_UNKNOWN + 1);
  1347. id < eCSSFontDesc_COUNT;
  1348. id = nsCSSFontDesc(id + 1))
  1349. if (mDecl.mDescriptors.Get(id).GetUnit() != eCSSUnit_Null) {
  1350. if (NS_FAILED(mDecl.GetPropertyValue(id, descStr)))
  1351. descStr.AssignLiteral("#<serialization error>");
  1352. else if (descStr.Length() == 0)
  1353. descStr.AssignLiteral("#<serialization missing>");
  1354. fprintf_stderr(out, "%s%s: %s\n",
  1355. descInd.get(), nsCSSProps::GetStringValue(id).get(),
  1356. NS_ConvertUTF16toUTF8(descStr).get());
  1357. }
  1358. fprintf_stderr(out, "%s}\n", baseInd.get());
  1359. }
  1360. #endif
  1361. /* virtual */ int32_t
  1362. nsCSSFontFaceRule::GetType() const
  1363. {
  1364. return Rule::FONT_FACE_RULE;
  1365. }
  1366. NS_IMETHODIMP
  1367. nsCSSFontFaceRule::GetType(uint16_t* aType)
  1368. {
  1369. *aType = nsIDOMCSSRule::FONT_FACE_RULE;
  1370. return NS_OK;
  1371. }
  1372. NS_IMETHODIMP
  1373. nsCSSFontFaceRule::GetCssText(nsAString& aCssText)
  1374. {
  1375. nsAutoString propText;
  1376. mDecl.GetCssText(propText);
  1377. aCssText.AssignLiteral("@font-face {\n");
  1378. aCssText.Append(propText);
  1379. aCssText.Append('}');
  1380. return NS_OK;
  1381. }
  1382. NS_IMETHODIMP
  1383. nsCSSFontFaceRule::SetCssText(const nsAString& aCssText)
  1384. {
  1385. return NS_ERROR_NOT_IMPLEMENTED; // bug 443978
  1386. }
  1387. NS_IMETHODIMP
  1388. nsCSSFontFaceRule::GetParentStyleSheet(nsIDOMCSSStyleSheet** aSheet)
  1389. {
  1390. return Rule::GetParentStyleSheet(aSheet);
  1391. }
  1392. NS_IMETHODIMP
  1393. nsCSSFontFaceRule::GetParentRule(nsIDOMCSSRule** aParentRule)
  1394. {
  1395. return Rule::GetParentRule(aParentRule);
  1396. }
  1397. css::Rule*
  1398. nsCSSFontFaceRule::GetCSSRule()
  1399. {
  1400. return Rule::GetCSSRule();
  1401. }
  1402. NS_IMETHODIMP
  1403. nsCSSFontFaceRule::GetStyle(nsIDOMCSSStyleDeclaration** aStyle)
  1404. {
  1405. NS_IF_ADDREF(*aStyle = &mDecl);
  1406. return NS_OK;
  1407. }
  1408. // Arguably these should forward to nsCSSFontFaceStyleDecl methods.
  1409. void
  1410. nsCSSFontFaceRule::SetDesc(nsCSSFontDesc aDescID, nsCSSValue const & aValue)
  1411. {
  1412. NS_PRECONDITION(aDescID > eCSSFontDesc_UNKNOWN &&
  1413. aDescID < eCSSFontDesc_COUNT,
  1414. "aDescID out of range in nsCSSFontFaceRule::SetDesc");
  1415. // FIXME: handle dynamic changes
  1416. mDecl.mDescriptors.Get(aDescID) = aValue;
  1417. }
  1418. void
  1419. nsCSSFontFaceRule::GetDesc(nsCSSFontDesc aDescID, nsCSSValue & aValue)
  1420. {
  1421. NS_PRECONDITION(aDescID > eCSSFontDesc_UNKNOWN &&
  1422. aDescID < eCSSFontDesc_COUNT,
  1423. "aDescID out of range in nsCSSFontFaceRule::GetDesc");
  1424. aValue = mDecl.mDescriptors.Get(aDescID);
  1425. }
  1426. /* virtual */ size_t
  1427. nsCSSFontFaceRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
  1428. {
  1429. return aMallocSizeOf(this);
  1430. // Measurement of the following members may be added later if DMD finds it is
  1431. // worthwhile:
  1432. // - mDecl
  1433. }
  1434. // -----------------------------------
  1435. // nsCSSFontFeatureValuesRule
  1436. //
  1437. /* virtual */ already_AddRefed<css::Rule>
  1438. nsCSSFontFeatureValuesRule::Clone() const
  1439. {
  1440. RefPtr<css::Rule> clone = new nsCSSFontFeatureValuesRule(*this);
  1441. return clone.forget();
  1442. }
  1443. NS_IMPL_ADDREF(nsCSSFontFeatureValuesRule)
  1444. NS_IMPL_RELEASE(nsCSSFontFeatureValuesRule)
  1445. // QueryInterface implementation for nsCSSFontFeatureValuesRule
  1446. NS_INTERFACE_MAP_BEGIN(nsCSSFontFeatureValuesRule)
  1447. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSFontFeatureValuesRule)
  1448. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSRule)
  1449. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozilla::css::Rule)
  1450. NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(CSSFontFeatureValuesRule)
  1451. NS_INTERFACE_MAP_END
  1452. IMPL_STYLE_RULE_INHERIT(nsCSSFontFeatureValuesRule, Rule)
  1453. static void
  1454. FeatureValuesToString(
  1455. const nsTArray<gfxFontFeatureValueSet::FeatureValues>& aFeatureValues,
  1456. nsAString& aOutStr)
  1457. {
  1458. uint32_t i, n;
  1459. // append values
  1460. n = aFeatureValues.Length();
  1461. for (i = 0; i < n; i++) {
  1462. const gfxFontFeatureValueSet::FeatureValues& fv = aFeatureValues[i];
  1463. // @alternate
  1464. aOutStr.AppendLiteral(" @");
  1465. nsAutoString functAlt;
  1466. nsStyleUtil::GetFunctionalAlternatesName(fv.alternate, functAlt);
  1467. aOutStr.Append(functAlt);
  1468. aOutStr.AppendLiteral(" {");
  1469. // for each ident-values tuple
  1470. uint32_t j, numValues = fv.valuelist.Length();
  1471. for (j = 0; j < numValues; j++) {
  1472. aOutStr.Append(' ');
  1473. const gfxFontFeatureValueSet::ValueList& vlist = fv.valuelist[j];
  1474. nsStyleUtil::AppendEscapedCSSIdent(vlist.name, aOutStr);
  1475. aOutStr.Append(':');
  1476. uint32_t k, numSelectors = vlist.featureSelectors.Length();
  1477. for (k = 0; k < numSelectors; k++) {
  1478. aOutStr.Append(' ');
  1479. aOutStr.AppendInt(vlist.featureSelectors[k]);
  1480. }
  1481. aOutStr.Append(';');
  1482. }
  1483. aOutStr.AppendLiteral(" }\n");
  1484. }
  1485. }
  1486. static void
  1487. FontFeatureValuesRuleToString(
  1488. const mozilla::FontFamilyList& aFamilyList,
  1489. const nsTArray<gfxFontFeatureValueSet::FeatureValues>& aFeatureValues,
  1490. nsAString& aOutStr)
  1491. {
  1492. aOutStr.AssignLiteral("@font-feature-values ");
  1493. nsAutoString familyListStr, valueTextStr;
  1494. nsStyleUtil::AppendEscapedCSSFontFamilyList(aFamilyList, familyListStr);
  1495. aOutStr.Append(familyListStr);
  1496. aOutStr.AppendLiteral(" {\n");
  1497. FeatureValuesToString(aFeatureValues, valueTextStr);
  1498. aOutStr.Append(valueTextStr);
  1499. aOutStr.Append('}');
  1500. }
  1501. #ifdef DEBUG
  1502. void
  1503. nsCSSFontFeatureValuesRule::List(FILE* out, int32_t aIndent) const
  1504. {
  1505. nsAutoString text;
  1506. FontFeatureValuesRuleToString(mFamilyList, mFeatureValues, text);
  1507. NS_ConvertUTF16toUTF8 utf8(text);
  1508. // replace newlines with newlines plus indent spaces
  1509. char* indent = new char[(aIndent + 1) * 2];
  1510. int32_t i;
  1511. for (i = 1; i < (aIndent + 1) * 2 - 1; i++) {
  1512. indent[i] = 0x20;
  1513. }
  1514. indent[0] = 0xa;
  1515. indent[aIndent * 2 + 1] = 0;
  1516. utf8.ReplaceSubstring("\n", indent);
  1517. delete [] indent;
  1518. nsAutoCString indentStr;
  1519. for (i = aIndent; --i >= 0; ) {
  1520. indentStr.AppendLiteral(" ");
  1521. }
  1522. fprintf_stderr(out, "%s%s\n", indentStr.get(), utf8.get());
  1523. }
  1524. #endif
  1525. /* virtual */ int32_t
  1526. nsCSSFontFeatureValuesRule::GetType() const
  1527. {
  1528. return Rule::FONT_FEATURE_VALUES_RULE;
  1529. }
  1530. NS_IMETHODIMP
  1531. nsCSSFontFeatureValuesRule::GetType(uint16_t* aType)
  1532. {
  1533. *aType = nsIDOMCSSRule::FONT_FEATURE_VALUES_RULE;
  1534. return NS_OK;
  1535. }
  1536. NS_IMETHODIMP
  1537. nsCSSFontFeatureValuesRule::GetCssText(nsAString& aCssText)
  1538. {
  1539. FontFeatureValuesRuleToString(mFamilyList, mFeatureValues, aCssText);
  1540. return NS_OK;
  1541. }
  1542. NS_IMETHODIMP
  1543. nsCSSFontFeatureValuesRule::SetCssText(const nsAString& aCssText)
  1544. {
  1545. // FIXME: implement???
  1546. return NS_ERROR_NOT_IMPLEMENTED;
  1547. }
  1548. NS_IMETHODIMP
  1549. nsCSSFontFeatureValuesRule::GetParentStyleSheet(nsIDOMCSSStyleSheet** aSheet)
  1550. {
  1551. return Rule::GetParentStyleSheet(aSheet);
  1552. }
  1553. NS_IMETHODIMP
  1554. nsCSSFontFeatureValuesRule::GetParentRule(nsIDOMCSSRule** aParentRule)
  1555. {
  1556. return Rule::GetParentRule(aParentRule);
  1557. }
  1558. css::Rule*
  1559. nsCSSFontFeatureValuesRule::GetCSSRule()
  1560. {
  1561. return Rule::GetCSSRule();
  1562. }
  1563. NS_IMETHODIMP
  1564. nsCSSFontFeatureValuesRule::GetFontFamily(nsAString& aFamilyListStr)
  1565. {
  1566. nsStyleUtil::AppendEscapedCSSFontFamilyList(mFamilyList, aFamilyListStr);
  1567. return NS_OK;
  1568. }
  1569. NS_IMETHODIMP
  1570. nsCSSFontFeatureValuesRule::SetFontFamily(const nsAString& aFontFamily)
  1571. {
  1572. return NS_ERROR_NOT_IMPLEMENTED;
  1573. }
  1574. NS_IMETHODIMP
  1575. nsCSSFontFeatureValuesRule::GetValueText(nsAString& aValueText)
  1576. {
  1577. FeatureValuesToString(mFeatureValues, aValueText);
  1578. return NS_OK;
  1579. }
  1580. NS_IMETHODIMP
  1581. nsCSSFontFeatureValuesRule::SetValueText(const nsAString& aValueText)
  1582. {
  1583. return NS_ERROR_NOT_IMPLEMENTED;
  1584. }
  1585. struct MakeFamilyArray {
  1586. explicit MakeFamilyArray(nsTArray<nsString>& aFamilyArray)
  1587. : familyArray(aFamilyArray), hasGeneric(false)
  1588. {}
  1589. static bool
  1590. AddFamily(const nsString& aFamily, bool aGeneric, void* aData)
  1591. {
  1592. MakeFamilyArray *familyArr = reinterpret_cast<MakeFamilyArray*> (aData);
  1593. if (!aGeneric && !aFamily.IsEmpty()) {
  1594. familyArr->familyArray.AppendElement(aFamily);
  1595. }
  1596. if (aGeneric) {
  1597. familyArr->hasGeneric = true;
  1598. }
  1599. return true;
  1600. }
  1601. nsTArray<nsString>& familyArray;
  1602. bool hasGeneric;
  1603. };
  1604. void
  1605. nsCSSFontFeatureValuesRule::SetFamilyList(
  1606. const mozilla::FontFamilyList& aFamilyList)
  1607. {
  1608. mFamilyList = aFamilyList;
  1609. }
  1610. void
  1611. nsCSSFontFeatureValuesRule::AddValueList(int32_t aVariantAlternate,
  1612. nsTArray<gfxFontFeatureValueSet::ValueList>& aValueList)
  1613. {
  1614. uint32_t i, len = mFeatureValues.Length();
  1615. bool foundAlternate = false;
  1616. // add to an existing list for a given property value
  1617. for (i = 0; i < len; i++) {
  1618. gfxFontFeatureValueSet::FeatureValues& f = mFeatureValues.ElementAt(i);
  1619. if (f.alternate == uint32_t(aVariantAlternate)) {
  1620. f.valuelist.AppendElements(aValueList);
  1621. foundAlternate = true;
  1622. break;
  1623. }
  1624. }
  1625. // create a new list for a given property value
  1626. if (!foundAlternate) {
  1627. gfxFontFeatureValueSet::FeatureValues &f = *mFeatureValues.AppendElement();
  1628. f.alternate = aVariantAlternate;
  1629. f.valuelist.AppendElements(aValueList);
  1630. }
  1631. }
  1632. size_t
  1633. nsCSSFontFeatureValuesRule::SizeOfIncludingThis(
  1634. MallocSizeOf aMallocSizeOf) const
  1635. {
  1636. return aMallocSizeOf(this);
  1637. }
  1638. // -------------------------------------------
  1639. // nsCSSKeyframeStyleDeclaration
  1640. //
  1641. nsCSSKeyframeStyleDeclaration::nsCSSKeyframeStyleDeclaration(nsCSSKeyframeRule *aRule)
  1642. : mRule(aRule)
  1643. {
  1644. }
  1645. nsCSSKeyframeStyleDeclaration::~nsCSSKeyframeStyleDeclaration()
  1646. {
  1647. NS_ASSERTION(!mRule, "DropReference not called.");
  1648. }
  1649. NS_IMPL_CYCLE_COLLECTING_ADDREF(nsCSSKeyframeStyleDeclaration)
  1650. NS_IMPL_CYCLE_COLLECTING_RELEASE(nsCSSKeyframeStyleDeclaration)
  1651. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(nsCSSKeyframeStyleDeclaration)
  1652. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsCSSKeyframeStyleDeclaration)
  1653. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  1654. NS_INTERFACE_MAP_END_INHERITING(nsDOMCSSDeclaration)
  1655. DeclarationBlock*
  1656. nsCSSKeyframeStyleDeclaration::GetCSSDeclaration(Operation aOperation)
  1657. {
  1658. if (mRule) {
  1659. return mRule->Declaration();
  1660. } else {
  1661. return nullptr;
  1662. }
  1663. }
  1664. void
  1665. nsCSSKeyframeStyleDeclaration::GetCSSParsingEnvironment(CSSParsingEnvironment& aCSSParseEnv)
  1666. {
  1667. GetCSSParsingEnvironmentForRule(mRule, aCSSParseEnv);
  1668. }
  1669. NS_IMETHODIMP
  1670. nsCSSKeyframeStyleDeclaration::GetParentRule(nsIDOMCSSRule **aParent)
  1671. {
  1672. NS_ENSURE_ARG_POINTER(aParent);
  1673. NS_IF_ADDREF(*aParent = mRule);
  1674. return NS_OK;
  1675. }
  1676. nsresult
  1677. nsCSSKeyframeStyleDeclaration::SetCSSDeclaration(DeclarationBlock* aDecl)
  1678. {
  1679. MOZ_ASSERT(aDecl, "must be non-null");
  1680. mRule->ChangeDeclaration(aDecl->AsGecko());
  1681. return NS_OK;
  1682. }
  1683. nsIDocument*
  1684. nsCSSKeyframeStyleDeclaration::DocToUpdate()
  1685. {
  1686. return nullptr;
  1687. }
  1688. nsINode*
  1689. nsCSSKeyframeStyleDeclaration::GetParentObject()
  1690. {
  1691. return mRule ? mRule->GetDocument() : nullptr;
  1692. }
  1693. DocGroup*
  1694. nsCSSKeyframeStyleDeclaration::GetDocGroup() const
  1695. {
  1696. if (!mRule) {
  1697. return nullptr;
  1698. }
  1699. nsIDocument* document = mRule->GetDocument();
  1700. return document ? document->GetDocGroup() : nullptr;
  1701. }
  1702. // -------------------------------------------
  1703. // nsCSSKeyframeRule
  1704. //
  1705. nsCSSKeyframeRule::nsCSSKeyframeRule(const nsCSSKeyframeRule& aCopy)
  1706. // copy everything except our reference count and mDOMDeclaration
  1707. : Rule(aCopy)
  1708. , mKeys(aCopy.mKeys)
  1709. , mDeclaration(new css::Declaration(*aCopy.mDeclaration))
  1710. {
  1711. mDeclaration->SetOwningRule(this);
  1712. }
  1713. nsCSSKeyframeRule::~nsCSSKeyframeRule()
  1714. {
  1715. mDeclaration->SetOwningRule(nullptr);
  1716. if (mDOMDeclaration) {
  1717. mDOMDeclaration->DropReference();
  1718. }
  1719. }
  1720. /* virtual */ already_AddRefed<css::Rule>
  1721. nsCSSKeyframeRule::Clone() const
  1722. {
  1723. RefPtr<css::Rule> clone = new nsCSSKeyframeRule(*this);
  1724. return clone.forget();
  1725. }
  1726. NS_IMPL_CYCLE_COLLECTING_ADDREF(nsCSSKeyframeRule)
  1727. NS_IMPL_CYCLE_COLLECTING_RELEASE(nsCSSKeyframeRule)
  1728. NS_IMPL_CYCLE_COLLECTION_CLASS(nsCSSKeyframeRule)
  1729. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsCSSKeyframeRule)
  1730. if (tmp->mDOMDeclaration) {
  1731. tmp->mDOMDeclaration->DropReference();
  1732. tmp->mDOMDeclaration = nullptr;
  1733. }
  1734. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  1735. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsCSSKeyframeRule)
  1736. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDOMDeclaration)
  1737. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  1738. // QueryInterface implementation for nsCSSKeyframeRule
  1739. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsCSSKeyframeRule)
  1740. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSKeyframeRule)
  1741. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSRule)
  1742. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozilla::css::Rule)
  1743. NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(CSSKeyframeRule)
  1744. NS_INTERFACE_MAP_END
  1745. IMPL_STYLE_RULE_INHERIT_GET_DOM_RULE_WEAK(nsCSSKeyframeRule, Rule)
  1746. #ifdef DEBUG
  1747. void
  1748. nsCSSKeyframeRule::List(FILE* out, int32_t aIndent) const
  1749. {
  1750. nsAutoCString str;
  1751. for (int32_t index = aIndent; --index >= 0; ) {
  1752. str.AppendLiteral(" ");
  1753. }
  1754. nsAutoString tmp;
  1755. DoGetKeyText(tmp);
  1756. AppendUTF16toUTF8(tmp, str);
  1757. str.AppendLiteral(" { ");
  1758. mDeclaration->ToString(tmp);
  1759. AppendUTF16toUTF8(tmp, str);
  1760. str.AppendLiteral("}\n");
  1761. fprintf_stderr(out, "%s", str.get());
  1762. }
  1763. #endif
  1764. /* virtual */ int32_t
  1765. nsCSSKeyframeRule::GetType() const
  1766. {
  1767. return Rule::KEYFRAME_RULE;
  1768. }
  1769. NS_IMETHODIMP
  1770. nsCSSKeyframeRule::GetType(uint16_t* aType)
  1771. {
  1772. *aType = nsIDOMCSSRule::KEYFRAME_RULE;
  1773. return NS_OK;
  1774. }
  1775. NS_IMETHODIMP
  1776. nsCSSKeyframeRule::GetCssText(nsAString& aCssText)
  1777. {
  1778. DoGetKeyText(aCssText);
  1779. aCssText.AppendLiteral(" { ");
  1780. nsAutoString tmp;
  1781. mDeclaration->ToString(tmp);
  1782. aCssText.Append(tmp);
  1783. aCssText.AppendLiteral(" }");
  1784. return NS_OK;
  1785. }
  1786. NS_IMETHODIMP
  1787. nsCSSKeyframeRule::SetCssText(const nsAString& aCssText)
  1788. {
  1789. // FIXME: implement???
  1790. return NS_ERROR_NOT_IMPLEMENTED;
  1791. }
  1792. NS_IMETHODIMP
  1793. nsCSSKeyframeRule::GetParentStyleSheet(nsIDOMCSSStyleSheet** aSheet)
  1794. {
  1795. return Rule::GetParentStyleSheet(aSheet);
  1796. }
  1797. NS_IMETHODIMP
  1798. nsCSSKeyframeRule::GetParentRule(nsIDOMCSSRule** aParentRule)
  1799. {
  1800. return Rule::GetParentRule(aParentRule);
  1801. }
  1802. css::Rule*
  1803. nsCSSKeyframeRule::GetCSSRule()
  1804. {
  1805. return Rule::GetCSSRule();
  1806. }
  1807. NS_IMETHODIMP
  1808. nsCSSKeyframeRule::GetKeyText(nsAString& aKeyText)
  1809. {
  1810. DoGetKeyText(aKeyText);
  1811. return NS_OK;
  1812. }
  1813. void
  1814. nsCSSKeyframeRule::DoGetKeyText(nsAString& aKeyText) const
  1815. {
  1816. aKeyText.Truncate();
  1817. uint32_t i = 0, i_end = mKeys.Length();
  1818. MOZ_ASSERT(i_end != 0, "must have some keys");
  1819. for (;;) {
  1820. aKeyText.AppendFloat(mKeys[i] * 100.0f);
  1821. aKeyText.Append(char16_t('%'));
  1822. if (++i == i_end) {
  1823. break;
  1824. }
  1825. aKeyText.AppendLiteral(", ");
  1826. }
  1827. }
  1828. NS_IMETHODIMP
  1829. nsCSSKeyframeRule::SetKeyText(const nsAString& aKeyText)
  1830. {
  1831. nsCSSParser parser;
  1832. InfallibleTArray<float> newSelectors;
  1833. // FIXME: pass filename and line number
  1834. if (!parser.ParseKeyframeSelectorString(aKeyText, nullptr, 0, newSelectors)) {
  1835. // for now, we don't do anything if the parse fails
  1836. return NS_OK;
  1837. }
  1838. nsIDocument* doc = GetDocument();
  1839. MOZ_AUTO_DOC_UPDATE(doc, UPDATE_STYLE, true);
  1840. newSelectors.SwapElements(mKeys);
  1841. CSSStyleSheet* sheet = GetStyleSheet();
  1842. if (sheet) {
  1843. sheet->SetModifiedByChildRule();
  1844. if (doc) {
  1845. doc->StyleRuleChanged(sheet, this);
  1846. }
  1847. }
  1848. return NS_OK;
  1849. }
  1850. NS_IMETHODIMP
  1851. nsCSSKeyframeRule::GetStyle(nsIDOMCSSStyleDeclaration** aStyle)
  1852. {
  1853. if (!mDOMDeclaration) {
  1854. mDOMDeclaration = new nsCSSKeyframeStyleDeclaration(this);
  1855. }
  1856. NS_ADDREF(*aStyle = mDOMDeclaration);
  1857. return NS_OK;
  1858. }
  1859. void
  1860. nsCSSKeyframeRule::ChangeDeclaration(css::Declaration* aDeclaration)
  1861. {
  1862. // Our caller already did a BeginUpdate/EndUpdate, but with
  1863. // UPDATE_CONTENT, and we need UPDATE_STYLE to trigger work in
  1864. // PresShell::EndUpdate.
  1865. nsIDocument* doc = GetDocument();
  1866. MOZ_AUTO_DOC_UPDATE(doc, UPDATE_STYLE, true);
  1867. if (aDeclaration != mDeclaration) {
  1868. mDeclaration->SetOwningRule(nullptr);
  1869. mDeclaration = aDeclaration;
  1870. mDeclaration->SetOwningRule(this);
  1871. }
  1872. CSSStyleSheet* sheet = GetStyleSheet();
  1873. if (sheet) {
  1874. sheet->SetModifiedByChildRule();
  1875. if (doc) {
  1876. doc->StyleRuleChanged(sheet, this);
  1877. }
  1878. }
  1879. }
  1880. /* virtual */ size_t
  1881. nsCSSKeyframeRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
  1882. {
  1883. return aMallocSizeOf(this);
  1884. // Measurement of the following members may be added later if DMD finds it is
  1885. // worthwhile:
  1886. // - mKeys
  1887. // - mDeclaration
  1888. // - mDOMDeclaration
  1889. }
  1890. // -------------------------------------------
  1891. // nsCSSKeyframesRule
  1892. //
  1893. nsCSSKeyframesRule::nsCSSKeyframesRule(const nsCSSKeyframesRule& aCopy)
  1894. // copy everything except our reference count. GroupRule's copy
  1895. // constructor also doesn't copy the lazily-constructed
  1896. // mRuleCollection.
  1897. : GroupRule(aCopy),
  1898. mName(aCopy.mName)
  1899. {
  1900. }
  1901. nsCSSKeyframesRule::~nsCSSKeyframesRule()
  1902. {
  1903. }
  1904. /* virtual */ already_AddRefed<css::Rule>
  1905. nsCSSKeyframesRule::Clone() const
  1906. {
  1907. RefPtr<css::Rule> clone = new nsCSSKeyframesRule(*this);
  1908. return clone.forget();
  1909. }
  1910. NS_IMPL_ADDREF_INHERITED(nsCSSKeyframesRule, css::GroupRule)
  1911. NS_IMPL_RELEASE_INHERITED(nsCSSKeyframesRule, css::GroupRule)
  1912. // QueryInterface implementation for nsCSSKeyframesRule
  1913. NS_INTERFACE_MAP_BEGIN(nsCSSKeyframesRule)
  1914. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSRule)
  1915. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSKeyframesRule)
  1916. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozilla::css::Rule)
  1917. NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(CSSKeyframesRule)
  1918. NS_INTERFACE_MAP_END_INHERITING(GroupRule)
  1919. #ifdef DEBUG
  1920. void
  1921. nsCSSKeyframesRule::List(FILE* out, int32_t aIndent) const
  1922. {
  1923. nsAutoCString indentStr;
  1924. for (int32_t indent = aIndent; --indent >= 0; ) {
  1925. indentStr.AppendLiteral(" ");
  1926. }
  1927. fprintf_stderr(out, "%s@keyframes %s {\n",
  1928. indentStr.get(), NS_ConvertUTF16toUTF8(mName).get());
  1929. GroupRule::List(out, aIndent);
  1930. fprintf_stderr(out, "%s}\n", indentStr.get());
  1931. }
  1932. #endif
  1933. /* virtual */ int32_t
  1934. nsCSSKeyframesRule::GetType() const
  1935. {
  1936. return Rule::KEYFRAMES_RULE;
  1937. }
  1938. NS_IMETHODIMP
  1939. nsCSSKeyframesRule::GetType(uint16_t* aType)
  1940. {
  1941. *aType = nsIDOMCSSRule::KEYFRAMES_RULE;
  1942. return NS_OK;
  1943. }
  1944. NS_IMETHODIMP
  1945. nsCSSKeyframesRule::GetCssText(nsAString& aCssText)
  1946. {
  1947. aCssText.AssignLiteral("@keyframes ");
  1948. aCssText.Append(mName);
  1949. aCssText.AppendLiteral(" {\n");
  1950. nsAutoString tmp;
  1951. for (uint32_t i = 0, i_end = mRules.Count(); i != i_end; ++i) {
  1952. static_cast<nsCSSKeyframeRule*>(mRules[i])->GetCssText(tmp);
  1953. aCssText.Append(tmp);
  1954. aCssText.Append('\n');
  1955. }
  1956. aCssText.Append('}');
  1957. return NS_OK;
  1958. }
  1959. NS_IMETHODIMP
  1960. nsCSSKeyframesRule::SetCssText(const nsAString& aCssText)
  1961. {
  1962. // FIXME: implement???
  1963. return NS_ERROR_NOT_IMPLEMENTED;
  1964. }
  1965. NS_IMETHODIMP
  1966. nsCSSKeyframesRule::GetParentStyleSheet(nsIDOMCSSStyleSheet** aSheet)
  1967. {
  1968. return GroupRule::GetParentStyleSheet(aSheet);
  1969. }
  1970. NS_IMETHODIMP
  1971. nsCSSKeyframesRule::GetParentRule(nsIDOMCSSRule** aParentRule)
  1972. {
  1973. return GroupRule::GetParentRule(aParentRule);
  1974. }
  1975. css::Rule*
  1976. nsCSSKeyframesRule::GetCSSRule()
  1977. {
  1978. return GroupRule::GetCSSRule();
  1979. }
  1980. NS_IMETHODIMP
  1981. nsCSSKeyframesRule::GetName(nsAString& aName)
  1982. {
  1983. aName = mName;
  1984. return NS_OK;
  1985. }
  1986. NS_IMETHODIMP
  1987. nsCSSKeyframesRule::SetName(const nsAString& aName)
  1988. {
  1989. if (mName == aName) {
  1990. return NS_OK;
  1991. }
  1992. nsIDocument* doc = GetDocument();
  1993. MOZ_AUTO_DOC_UPDATE(doc, UPDATE_STYLE, true);
  1994. mName = aName;
  1995. CSSStyleSheet* sheet = GetStyleSheet();
  1996. if (sheet) {
  1997. sheet->SetModifiedByChildRule();
  1998. if (doc) {
  1999. doc->StyleRuleChanged(sheet, this);
  2000. }
  2001. }
  2002. return NS_OK;
  2003. }
  2004. NS_IMETHODIMP
  2005. nsCSSKeyframesRule::GetCssRules(nsIDOMCSSRuleList* *aRuleList)
  2006. {
  2007. return GroupRule::GetCssRules(aRuleList);
  2008. }
  2009. NS_IMETHODIMP
  2010. nsCSSKeyframesRule::AppendRule(const nsAString& aRule)
  2011. {
  2012. // The spec is confusing, and I think we should just append the rule,
  2013. // which also turns out to match WebKit:
  2014. // http://lists.w3.org/Archives/Public/www-style/2011Apr/0034.html
  2015. nsCSSParser parser;
  2016. // FIXME: pass filename and line number
  2017. RefPtr<nsCSSKeyframeRule> rule =
  2018. parser.ParseKeyframeRule(aRule, nullptr, 0);
  2019. if (rule) {
  2020. nsIDocument* doc = GetDocument();
  2021. MOZ_AUTO_DOC_UPDATE(doc, UPDATE_STYLE, true);
  2022. AppendStyleRule(rule);
  2023. CSSStyleSheet* sheet = GetStyleSheet();
  2024. if (sheet) {
  2025. sheet->SetModifiedByChildRule();
  2026. if (doc) {
  2027. doc->StyleRuleChanged(sheet, this);
  2028. }
  2029. }
  2030. }
  2031. return NS_OK;
  2032. }
  2033. static const uint32_t RULE_NOT_FOUND = uint32_t(-1);
  2034. uint32_t
  2035. nsCSSKeyframesRule::FindRuleIndexForKey(const nsAString& aKey)
  2036. {
  2037. nsCSSParser parser;
  2038. InfallibleTArray<float> keys;
  2039. // FIXME: pass filename and line number
  2040. if (parser.ParseKeyframeSelectorString(aKey, nullptr, 0, keys)) {
  2041. // The spec isn't clear, but we'll match on the key list, which
  2042. // mostly matches what WebKit does, except we'll do last-match
  2043. // instead of first-match, and handling parsing differences better.
  2044. // http://lists.w3.org/Archives/Public/www-style/2011Apr/0036.html
  2045. // http://lists.w3.org/Archives/Public/www-style/2011Apr/0037.html
  2046. for (uint32_t i = mRules.Count(); i-- != 0; ) {
  2047. if (static_cast<nsCSSKeyframeRule*>(mRules[i])->GetKeys() == keys) {
  2048. return i;
  2049. }
  2050. }
  2051. }
  2052. return RULE_NOT_FOUND;
  2053. }
  2054. NS_IMETHODIMP
  2055. nsCSSKeyframesRule::DeleteRule(const nsAString& aKey)
  2056. {
  2057. uint32_t index = FindRuleIndexForKey(aKey);
  2058. if (index != RULE_NOT_FOUND) {
  2059. nsIDocument* doc = GetDocument();
  2060. MOZ_AUTO_DOC_UPDATE(doc, UPDATE_STYLE, true);
  2061. DeleteStyleRuleAt(index);
  2062. CSSStyleSheet* sheet = GetStyleSheet();
  2063. if (sheet) {
  2064. sheet->SetModifiedByChildRule();
  2065. if (doc) {
  2066. doc->StyleRuleChanged(sheet, this);
  2067. }
  2068. }
  2069. }
  2070. return NS_OK;
  2071. }
  2072. NS_IMETHODIMP
  2073. nsCSSKeyframesRule::FindRule(const nsAString& aKey,
  2074. nsIDOMCSSKeyframeRule** aResult)
  2075. {
  2076. uint32_t index = FindRuleIndexForKey(aKey);
  2077. if (index == RULE_NOT_FOUND) {
  2078. *aResult = nullptr;
  2079. } else {
  2080. NS_ADDREF(*aResult = static_cast<nsCSSKeyframeRule*>(mRules[index]));
  2081. }
  2082. return NS_OK;
  2083. }
  2084. // GroupRule interface
  2085. /* virtual */ bool
  2086. nsCSSKeyframesRule::UseForPresentation(nsPresContext* aPresContext,
  2087. nsMediaQueryResultCacheKey& aKey)
  2088. {
  2089. MOZ_ASSERT(false, "should not be called");
  2090. return false;
  2091. }
  2092. /* virtual */ size_t
  2093. nsCSSKeyframesRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
  2094. {
  2095. size_t n = aMallocSizeOf(this);
  2096. n += GroupRule::SizeOfExcludingThis(aMallocSizeOf);
  2097. // Measurement of the following members may be added later if DMD finds it is
  2098. // worthwhile:
  2099. // - mName
  2100. return n;
  2101. }
  2102. // -------------------------------------------
  2103. // nsCSSPageStyleDeclaration
  2104. //
  2105. nsCSSPageStyleDeclaration::nsCSSPageStyleDeclaration(nsCSSPageRule* aRule)
  2106. : mRule(aRule)
  2107. {
  2108. }
  2109. nsCSSPageStyleDeclaration::~nsCSSPageStyleDeclaration()
  2110. {
  2111. NS_ASSERTION(!mRule, "DropReference not called.");
  2112. }
  2113. NS_IMPL_CYCLE_COLLECTING_ADDREF(nsCSSPageStyleDeclaration)
  2114. NS_IMPL_CYCLE_COLLECTING_RELEASE(nsCSSPageStyleDeclaration)
  2115. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(nsCSSPageStyleDeclaration)
  2116. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsCSSPageStyleDeclaration)
  2117. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  2118. NS_INTERFACE_MAP_END_INHERITING(nsDOMCSSDeclaration)
  2119. DeclarationBlock*
  2120. nsCSSPageStyleDeclaration::GetCSSDeclaration(Operation aOperation)
  2121. {
  2122. if (mRule) {
  2123. return mRule->Declaration();
  2124. } else {
  2125. return nullptr;
  2126. }
  2127. }
  2128. void
  2129. nsCSSPageStyleDeclaration::GetCSSParsingEnvironment(CSSParsingEnvironment& aCSSParseEnv)
  2130. {
  2131. GetCSSParsingEnvironmentForRule(mRule, aCSSParseEnv);
  2132. }
  2133. NS_IMETHODIMP
  2134. nsCSSPageStyleDeclaration::GetParentRule(nsIDOMCSSRule** aParent)
  2135. {
  2136. NS_ENSURE_ARG_POINTER(aParent);
  2137. NS_IF_ADDREF(*aParent = mRule);
  2138. return NS_OK;
  2139. }
  2140. nsresult
  2141. nsCSSPageStyleDeclaration::SetCSSDeclaration(DeclarationBlock* aDecl)
  2142. {
  2143. MOZ_ASSERT(aDecl, "must be non-null");
  2144. mRule->ChangeDeclaration(aDecl->AsGecko());
  2145. return NS_OK;
  2146. }
  2147. nsIDocument*
  2148. nsCSSPageStyleDeclaration::DocToUpdate()
  2149. {
  2150. return nullptr;
  2151. }
  2152. nsINode*
  2153. nsCSSPageStyleDeclaration::GetParentObject()
  2154. {
  2155. return mRule ? mRule->GetDocument() : nullptr;
  2156. }
  2157. DocGroup*
  2158. nsCSSPageStyleDeclaration::GetDocGroup() const
  2159. {
  2160. if (!mRule) {
  2161. return nullptr;
  2162. }
  2163. nsIDocument* document = mRule->GetDocument();
  2164. return document ? document->GetDocGroup() : nullptr;
  2165. }
  2166. // -------------------------------------------
  2167. // nsCSSPageRule
  2168. //
  2169. nsCSSPageRule::nsCSSPageRule(const nsCSSPageRule& aCopy)
  2170. // copy everything except our reference count and mDOMDeclaration
  2171. : Rule(aCopy)
  2172. , mDeclaration(new css::Declaration(*aCopy.mDeclaration))
  2173. {
  2174. mDeclaration->SetOwningRule(this);
  2175. }
  2176. nsCSSPageRule::~nsCSSPageRule()
  2177. {
  2178. mDeclaration->SetOwningRule(nullptr);
  2179. if (mDOMDeclaration) {
  2180. mDOMDeclaration->DropReference();
  2181. }
  2182. }
  2183. /* virtual */ already_AddRefed<css::Rule>
  2184. nsCSSPageRule::Clone() const
  2185. {
  2186. RefPtr<css::Rule> clone = new nsCSSPageRule(*this);
  2187. return clone.forget();
  2188. }
  2189. NS_IMPL_CYCLE_COLLECTING_ADDREF(nsCSSPageRule)
  2190. NS_IMPL_CYCLE_COLLECTING_RELEASE(nsCSSPageRule)
  2191. NS_IMPL_CYCLE_COLLECTION_CLASS(nsCSSPageRule)
  2192. NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsCSSPageRule)
  2193. if (tmp->mDOMDeclaration) {
  2194. tmp->mDOMDeclaration->DropReference();
  2195. tmp->mDOMDeclaration = nullptr;
  2196. }
  2197. NS_IMPL_CYCLE_COLLECTION_UNLINK_END
  2198. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsCSSPageRule)
  2199. NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDOMDeclaration)
  2200. NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
  2201. // QueryInterface implementation for nsCSSPageRule
  2202. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsCSSPageRule)
  2203. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSPageRule)
  2204. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSRule)
  2205. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozilla::css::Rule)
  2206. NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(CSSPageRule)
  2207. NS_INTERFACE_MAP_END
  2208. IMPL_STYLE_RULE_INHERIT_GET_DOM_RULE_WEAK(nsCSSPageRule, Rule)
  2209. #ifdef DEBUG
  2210. void
  2211. nsCSSPageRule::List(FILE* out, int32_t aIndent) const
  2212. {
  2213. nsAutoCString str;
  2214. for (int32_t indent = aIndent; --indent >= 0; ) {
  2215. str.AppendLiteral(" ");
  2216. }
  2217. str.AppendLiteral("@page { ");
  2218. nsAutoString tmp;
  2219. mDeclaration->ToString(tmp);
  2220. AppendUTF16toUTF8(tmp, str);
  2221. str.AppendLiteral("}\n");
  2222. fprintf_stderr(out, "%s", str.get());
  2223. }
  2224. #endif
  2225. /* virtual */ int32_t
  2226. nsCSSPageRule::GetType() const
  2227. {
  2228. return Rule::PAGE_RULE;
  2229. }
  2230. NS_IMETHODIMP
  2231. nsCSSPageRule::GetType(uint16_t* aType)
  2232. {
  2233. *aType = nsIDOMCSSRule::PAGE_RULE;
  2234. return NS_OK;
  2235. }
  2236. NS_IMETHODIMP
  2237. nsCSSPageRule::GetCssText(nsAString& aCssText)
  2238. {
  2239. aCssText.AppendLiteral("@page { ");
  2240. nsAutoString tmp;
  2241. mDeclaration->ToString(tmp);
  2242. aCssText.Append(tmp);
  2243. aCssText.AppendLiteral(" }");
  2244. return NS_OK;
  2245. }
  2246. NS_IMETHODIMP
  2247. nsCSSPageRule::SetCssText(const nsAString& aCssText)
  2248. {
  2249. // FIXME: implement???
  2250. return NS_ERROR_NOT_IMPLEMENTED;
  2251. }
  2252. NS_IMETHODIMP
  2253. nsCSSPageRule::GetParentStyleSheet(nsIDOMCSSStyleSheet** aSheet)
  2254. {
  2255. return Rule::GetParentStyleSheet(aSheet);
  2256. }
  2257. NS_IMETHODIMP
  2258. nsCSSPageRule::GetParentRule(nsIDOMCSSRule** aParentRule)
  2259. {
  2260. return Rule::GetParentRule(aParentRule);
  2261. }
  2262. css::Rule*
  2263. nsCSSPageRule::GetCSSRule()
  2264. {
  2265. return Rule::GetCSSRule();
  2266. }
  2267. NS_IMETHODIMP
  2268. nsCSSPageRule::GetStyle(nsIDOMCSSStyleDeclaration** aStyle)
  2269. {
  2270. if (!mDOMDeclaration) {
  2271. mDOMDeclaration = new nsCSSPageStyleDeclaration(this);
  2272. }
  2273. NS_ADDREF(*aStyle = mDOMDeclaration);
  2274. return NS_OK;
  2275. }
  2276. void
  2277. nsCSSPageRule::ChangeDeclaration(css::Declaration* aDeclaration)
  2278. {
  2279. if (aDeclaration != mDeclaration) {
  2280. mDeclaration->SetOwningRule(nullptr);
  2281. mDeclaration = aDeclaration;
  2282. mDeclaration->SetOwningRule(this);
  2283. }
  2284. CSSStyleSheet* sheet = GetStyleSheet();
  2285. if (sheet) {
  2286. sheet->SetModifiedByChildRule();
  2287. }
  2288. }
  2289. /* virtual */ size_t
  2290. nsCSSPageRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
  2291. {
  2292. return aMallocSizeOf(this);
  2293. }
  2294. namespace mozilla {
  2295. CSSSupportsRule::CSSSupportsRule(bool aConditionMet,
  2296. const nsString& aCondition,
  2297. uint32_t aLineNumber, uint32_t aColumnNumber)
  2298. : css::GroupRule(aLineNumber, aColumnNumber)
  2299. , mUseGroup(aConditionMet)
  2300. , mCondition(aCondition)
  2301. {
  2302. }
  2303. CSSSupportsRule::~CSSSupportsRule()
  2304. {
  2305. }
  2306. CSSSupportsRule::CSSSupportsRule(const CSSSupportsRule& aCopy)
  2307. : css::GroupRule(aCopy),
  2308. mUseGroup(aCopy.mUseGroup),
  2309. mCondition(aCopy.mCondition)
  2310. {
  2311. }
  2312. #ifdef DEBUG
  2313. /* virtual */ void
  2314. CSSSupportsRule::List(FILE* out, int32_t aIndent) const
  2315. {
  2316. nsAutoCString indentStr;
  2317. for (int32_t indent = aIndent; --indent >= 0; ) {
  2318. indentStr.AppendLiteral(" ");
  2319. }
  2320. fprintf_stderr(out, "%s@supports %s {\n",
  2321. indentStr.get(), NS_ConvertUTF16toUTF8(mCondition).get());
  2322. css::GroupRule::List(out, aIndent);
  2323. fprintf_stderr(out, "%s}\n", indentStr.get());
  2324. }
  2325. #endif
  2326. /* virtual */ int32_t
  2327. CSSSupportsRule::GetType() const
  2328. {
  2329. return Rule::SUPPORTS_RULE;
  2330. }
  2331. /* virtual */ already_AddRefed<mozilla::css::Rule>
  2332. CSSSupportsRule::Clone() const
  2333. {
  2334. RefPtr<css::Rule> clone = new CSSSupportsRule(*this);
  2335. return clone.forget();
  2336. }
  2337. /* virtual */ bool
  2338. CSSSupportsRule::UseForPresentation(nsPresContext* aPresContext,
  2339. nsMediaQueryResultCacheKey& aKey)
  2340. {
  2341. return mUseGroup;
  2342. }
  2343. NS_IMPL_ADDREF_INHERITED(CSSSupportsRule, css::GroupRule)
  2344. NS_IMPL_RELEASE_INHERITED(CSSSupportsRule, css::GroupRule)
  2345. // QueryInterface implementation for CSSSupportsRule
  2346. NS_INTERFACE_MAP_BEGIN(CSSSupportsRule)
  2347. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSRule)
  2348. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSGroupingRule)
  2349. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSConditionRule)
  2350. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSSupportsRule)
  2351. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozilla::css::Rule)
  2352. NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(CSSSupportsRule)
  2353. NS_INTERFACE_MAP_END_INHERITING(GroupRule)
  2354. // nsIDOMCSSRule methods
  2355. NS_IMETHODIMP
  2356. CSSSupportsRule::GetType(uint16_t* aType)
  2357. {
  2358. *aType = nsIDOMCSSRule::SUPPORTS_RULE;
  2359. return NS_OK;
  2360. }
  2361. NS_IMETHODIMP
  2362. CSSSupportsRule::GetCssText(nsAString& aCssText)
  2363. {
  2364. aCssText.AssignLiteral("@supports ");
  2365. aCssText.Append(mCondition);
  2366. css::GroupRule::AppendRulesToCssText(aCssText);
  2367. return NS_OK;
  2368. }
  2369. NS_IMETHODIMP
  2370. CSSSupportsRule::SetCssText(const nsAString& aCssText)
  2371. {
  2372. return NS_ERROR_NOT_IMPLEMENTED;
  2373. }
  2374. NS_IMETHODIMP
  2375. CSSSupportsRule::GetParentStyleSheet(nsIDOMCSSStyleSheet** aSheet)
  2376. {
  2377. return css::GroupRule::GetParentStyleSheet(aSheet);
  2378. }
  2379. NS_IMETHODIMP
  2380. CSSSupportsRule::GetParentRule(nsIDOMCSSRule** aParentRule)
  2381. {
  2382. return css::GroupRule::GetParentRule(aParentRule);
  2383. }
  2384. css::Rule*
  2385. CSSSupportsRule::GetCSSRule()
  2386. {
  2387. return css::GroupRule::GetCSSRule();
  2388. }
  2389. // nsIDOMCSSGroupingRule methods
  2390. NS_IMETHODIMP
  2391. CSSSupportsRule::GetCssRules(nsIDOMCSSRuleList* *aRuleList)
  2392. {
  2393. return css::GroupRule::GetCssRules(aRuleList);
  2394. }
  2395. NS_IMETHODIMP
  2396. CSSSupportsRule::InsertRule(const nsAString & aRule, uint32_t aIndex, uint32_t* _retval)
  2397. {
  2398. return css::GroupRule::InsertRule(aRule, aIndex, _retval);
  2399. }
  2400. NS_IMETHODIMP
  2401. CSSSupportsRule::DeleteRule(uint32_t aIndex)
  2402. {
  2403. return css::GroupRule::DeleteRule(aIndex);
  2404. }
  2405. // nsIDOMCSSConditionRule methods
  2406. NS_IMETHODIMP
  2407. CSSSupportsRule::GetConditionText(nsAString& aConditionText)
  2408. {
  2409. aConditionText.Assign(mCondition);
  2410. return NS_OK;
  2411. }
  2412. NS_IMETHODIMP
  2413. CSSSupportsRule::SetConditionText(const nsAString& aConditionText)
  2414. {
  2415. return NS_ERROR_NOT_IMPLEMENTED;
  2416. }
  2417. /* virtual */ size_t
  2418. CSSSupportsRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
  2419. {
  2420. size_t n = aMallocSizeOf(this);
  2421. n += css::GroupRule::SizeOfExcludingThis(aMallocSizeOf);
  2422. n += mCondition.SizeOfExcludingThisIfUnshared(aMallocSizeOf);
  2423. return n;
  2424. }
  2425. } // namespace mozilla
  2426. // -------------------------------------------
  2427. // nsCSSCounterStyleRule
  2428. //
  2429. nsCSSCounterStyleRule::nsCSSCounterStyleRule(const nsCSSCounterStyleRule& aCopy)
  2430. : Rule(aCopy)
  2431. , mName(aCopy.mName)
  2432. , mGeneration(aCopy.mGeneration)
  2433. {
  2434. for (size_t i = 0; i < ArrayLength(mValues); ++i) {
  2435. mValues[i] = aCopy.mValues[i];
  2436. }
  2437. }
  2438. nsCSSCounterStyleRule::~nsCSSCounterStyleRule()
  2439. {
  2440. }
  2441. /* virtual */ already_AddRefed<css::Rule>
  2442. nsCSSCounterStyleRule::Clone() const
  2443. {
  2444. RefPtr<css::Rule> clone = new nsCSSCounterStyleRule(*this);
  2445. return clone.forget();
  2446. }
  2447. nsCSSCounterStyleRule::Getter const
  2448. nsCSSCounterStyleRule::kGetters[] = {
  2449. #define CSS_COUNTER_DESC(name_, method_) &nsCSSCounterStyleRule::Get##method_,
  2450. #include "nsCSSCounterDescList.h"
  2451. #undef CSS_COUNTER_DESC
  2452. };
  2453. NS_IMPL_ADDREF(nsCSSCounterStyleRule)
  2454. NS_IMPL_RELEASE(nsCSSCounterStyleRule)
  2455. // QueryInterface implementation for nsCSSCounterStyleRule
  2456. NS_INTERFACE_MAP_BEGIN(nsCSSCounterStyleRule)
  2457. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSRule)
  2458. NS_INTERFACE_MAP_ENTRY(nsIDOMCSSCounterStyleRule)
  2459. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, mozilla::css::Rule)
  2460. NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(CSSCounterStyleRule)
  2461. NS_INTERFACE_MAP_END
  2462. IMPL_STYLE_RULE_INHERIT(nsCSSCounterStyleRule, css::Rule)
  2463. #ifdef DEBUG
  2464. void
  2465. nsCSSCounterStyleRule::List(FILE* out, int32_t aIndent) const
  2466. {
  2467. nsCString baseInd, descInd;
  2468. for (int32_t indent = aIndent; --indent >= 0; ) {
  2469. baseInd.AppendLiteral(" ");
  2470. }
  2471. descInd = baseInd;
  2472. descInd.AppendLiteral(" ");
  2473. fprintf_stderr(out, "%s@counter-style %s (rev.%u) {\n",
  2474. baseInd.get(), NS_ConvertUTF16toUTF8(mName).get(),
  2475. mGeneration);
  2476. // TODO
  2477. fprintf_stderr(out, "%s}\n", baseInd.get());
  2478. }
  2479. #endif
  2480. /* virtual */ int32_t
  2481. nsCSSCounterStyleRule::GetType() const
  2482. {
  2483. return Rule::COUNTER_STYLE_RULE;
  2484. }
  2485. // nsIDOMCSSRule methods
  2486. NS_IMETHODIMP
  2487. nsCSSCounterStyleRule::GetType(uint16_t* aType)
  2488. {
  2489. *aType = nsIDOMCSSRule::COUNTER_STYLE_RULE;
  2490. return NS_OK;
  2491. }
  2492. NS_IMETHODIMP
  2493. nsCSSCounterStyleRule::GetCssText(nsAString& aCssText)
  2494. {
  2495. aCssText.AssignLiteral(u"@counter-style ");
  2496. nsStyleUtil::AppendEscapedCSSIdent(mName, aCssText);
  2497. aCssText.AppendLiteral(u" {\n");
  2498. for (nsCSSCounterDesc id = nsCSSCounterDesc(0);
  2499. id < eCSSCounterDesc_COUNT;
  2500. id = nsCSSCounterDesc(id + 1)) {
  2501. if (mValues[id].GetUnit() != eCSSUnit_Null) {
  2502. nsAutoString tmp;
  2503. (this->*kGetters[id])(tmp);
  2504. aCssText.AppendLiteral(u" ");
  2505. AppendASCIItoUTF16(nsCSSProps::GetStringValue(id), aCssText);
  2506. aCssText.AppendLiteral(u": ");
  2507. aCssText.Append(tmp);
  2508. aCssText.AppendLiteral(u";\n");
  2509. }
  2510. }
  2511. aCssText.AppendLiteral(u"}");
  2512. return NS_OK;
  2513. }
  2514. NS_IMETHODIMP
  2515. nsCSSCounterStyleRule::SetCssText(const nsAString& aCssText)
  2516. {
  2517. // FIXME: implement???
  2518. return NS_ERROR_NOT_IMPLEMENTED;
  2519. }
  2520. NS_IMETHODIMP
  2521. nsCSSCounterStyleRule::GetParentStyleSheet(nsIDOMCSSStyleSheet** aSheet)
  2522. {
  2523. return Rule::GetParentStyleSheet(aSheet);
  2524. }
  2525. NS_IMETHODIMP
  2526. nsCSSCounterStyleRule::GetParentRule(nsIDOMCSSRule** aParentRule)
  2527. {
  2528. return Rule::GetParentRule(aParentRule);
  2529. }
  2530. css::Rule*
  2531. nsCSSCounterStyleRule::GetCSSRule()
  2532. {
  2533. return Rule::GetCSSRule();
  2534. }
  2535. // nsIDOMCSSCounterStyleRule methods
  2536. NS_IMETHODIMP
  2537. nsCSSCounterStyleRule::GetName(nsAString& aName)
  2538. {
  2539. aName.Truncate();
  2540. nsStyleUtil::AppendEscapedCSSIdent(mName, aName);
  2541. return NS_OK;
  2542. }
  2543. NS_IMETHODIMP
  2544. nsCSSCounterStyleRule::SetName(const nsAString& aName)
  2545. {
  2546. nsCSSParser parser;
  2547. nsAutoString name;
  2548. if (parser.ParseCounterStyleName(aName, nullptr, name)) {
  2549. nsIDocument* doc = GetDocument();
  2550. MOZ_AUTO_DOC_UPDATE(doc, UPDATE_STYLE, true);
  2551. mName = name;
  2552. CSSStyleSheet* sheet = GetStyleSheet();
  2553. if (sheet) {
  2554. sheet->SetModifiedByChildRule();
  2555. if (doc) {
  2556. doc->StyleRuleChanged(sheet, this);
  2557. }
  2558. }
  2559. }
  2560. return NS_OK;
  2561. }
  2562. int32_t
  2563. nsCSSCounterStyleRule::GetSystem() const
  2564. {
  2565. const nsCSSValue& system = GetDesc(eCSSCounterDesc_System);
  2566. switch (system.GetUnit()) {
  2567. case eCSSUnit_Enumerated:
  2568. return system.GetIntValue();
  2569. case eCSSUnit_Pair:
  2570. return system.GetPairValue().mXValue.GetIntValue();
  2571. default:
  2572. return NS_STYLE_COUNTER_SYSTEM_SYMBOLIC;
  2573. }
  2574. }
  2575. const nsCSSValue&
  2576. nsCSSCounterStyleRule::GetSystemArgument() const
  2577. {
  2578. const nsCSSValue& system = GetDesc(eCSSCounterDesc_System);
  2579. MOZ_ASSERT(system.GetUnit() == eCSSUnit_Pair,
  2580. "Invalid system value");
  2581. return system.GetPairValue().mYValue;
  2582. }
  2583. void
  2584. nsCSSCounterStyleRule::SetDesc(nsCSSCounterDesc aDescID, const nsCSSValue& aValue)
  2585. {
  2586. MOZ_ASSERT(aDescID >= 0 && aDescID < eCSSCounterDesc_COUNT,
  2587. "descriptor ID out of range");
  2588. nsIDocument* doc = GetDocument();
  2589. MOZ_AUTO_DOC_UPDATE(doc, UPDATE_STYLE, true);
  2590. mValues[aDescID] = aValue;
  2591. mGeneration++;
  2592. CSSStyleSheet* sheet = GetStyleSheet();
  2593. if (sheet) {
  2594. sheet->SetModifiedByChildRule();
  2595. if (doc) {
  2596. doc->StyleRuleChanged(sheet, this);
  2597. }
  2598. }
  2599. }
  2600. NS_IMETHODIMP
  2601. nsCSSCounterStyleRule::GetSystem(nsAString& aSystem)
  2602. {
  2603. const nsCSSValue& value = GetDesc(eCSSCounterDesc_System);
  2604. if (value.GetUnit() == eCSSUnit_Null) {
  2605. aSystem.Truncate();
  2606. return NS_OK;
  2607. }
  2608. aSystem = NS_ConvertASCIItoUTF16(nsCSSProps::ValueToKeyword(
  2609. GetSystem(), nsCSSProps::kCounterSystemKTable));
  2610. if (value.GetUnit() == eCSSUnit_Pair) {
  2611. aSystem.Append(' ');
  2612. GetSystemArgument().AppendToString(
  2613. eCSSProperty_UNKNOWN, aSystem, nsCSSValue::eNormalized);
  2614. }
  2615. return NS_OK;
  2616. }
  2617. NS_IMETHODIMP
  2618. nsCSSCounterStyleRule::GetSymbols(nsAString& aSymbols)
  2619. {
  2620. const nsCSSValue& value = GetDesc(eCSSCounterDesc_Symbols);
  2621. aSymbols.Truncate();
  2622. if (value.GetUnit() == eCSSUnit_List) {
  2623. for (const nsCSSValueList* item = value.GetListValue();
  2624. item; item = item->mNext) {
  2625. item->mValue.AppendToString(eCSSProperty_UNKNOWN,
  2626. aSymbols,
  2627. nsCSSValue::eNormalized);
  2628. if (item->mNext) {
  2629. aSymbols.Append(' ');
  2630. }
  2631. }
  2632. }
  2633. return NS_OK;
  2634. }
  2635. NS_IMETHODIMP
  2636. nsCSSCounterStyleRule::GetAdditiveSymbols(nsAString& aSymbols)
  2637. {
  2638. const nsCSSValue& value = GetDesc(eCSSCounterDesc_AdditiveSymbols);
  2639. aSymbols.Truncate();
  2640. if (value.GetUnit() == eCSSUnit_PairList) {
  2641. for (const nsCSSValuePairList* item = value.GetPairListValue();
  2642. item; item = item->mNext) {
  2643. item->mXValue.AppendToString(eCSSProperty_UNKNOWN,
  2644. aSymbols, nsCSSValue::eNormalized);
  2645. aSymbols.Append(' ');
  2646. item->mYValue.AppendToString(eCSSProperty_UNKNOWN,
  2647. aSymbols, nsCSSValue::eNormalized);
  2648. if (item->mNext) {
  2649. aSymbols.AppendLiteral(", ");
  2650. }
  2651. }
  2652. }
  2653. return NS_OK;
  2654. }
  2655. NS_IMETHODIMP
  2656. nsCSSCounterStyleRule::GetRange(nsAString& aRange)
  2657. {
  2658. const nsCSSValue& value = GetDesc(eCSSCounterDesc_Range);
  2659. switch (value.GetUnit()) {
  2660. case eCSSUnit_Auto:
  2661. aRange.AssignLiteral(u"auto");
  2662. break;
  2663. case eCSSUnit_PairList:
  2664. aRange.Truncate();
  2665. for (const nsCSSValuePairList* item = value.GetPairListValue();
  2666. item; item = item->mNext) {
  2667. const nsCSSValue& lower = item->mXValue;
  2668. const nsCSSValue& upper = item->mYValue;
  2669. if (lower.GetUnit() == eCSSUnit_Enumerated) {
  2670. NS_ASSERTION(lower.GetIntValue() ==
  2671. NS_STYLE_COUNTER_RANGE_INFINITE,
  2672. "Unrecognized keyword");
  2673. aRange.AppendLiteral("infinite");
  2674. } else {
  2675. aRange.AppendInt(lower.GetIntValue());
  2676. }
  2677. aRange.Append(' ');
  2678. if (upper.GetUnit() == eCSSUnit_Enumerated) {
  2679. NS_ASSERTION(upper.GetIntValue() ==
  2680. NS_STYLE_COUNTER_RANGE_INFINITE,
  2681. "Unrecognized keyword");
  2682. aRange.AppendLiteral("infinite");
  2683. } else {
  2684. aRange.AppendInt(upper.GetIntValue());
  2685. }
  2686. if (item->mNext) {
  2687. aRange.AppendLiteral(", ");
  2688. }
  2689. }
  2690. break;
  2691. default:
  2692. aRange.Truncate();
  2693. }
  2694. return NS_OK;
  2695. }
  2696. NS_IMETHODIMP
  2697. nsCSSCounterStyleRule::GetSpeakAs(nsAString& aSpeakAs)
  2698. {
  2699. const nsCSSValue& value = GetDesc(eCSSCounterDesc_SpeakAs);
  2700. switch (value.GetUnit()) {
  2701. case eCSSUnit_Enumerated:
  2702. switch (value.GetIntValue()) {
  2703. case NS_STYLE_COUNTER_SPEAKAS_BULLETS:
  2704. aSpeakAs.AssignLiteral(u"bullets");
  2705. break;
  2706. case NS_STYLE_COUNTER_SPEAKAS_NUMBERS:
  2707. aSpeakAs.AssignLiteral(u"numbers");
  2708. break;
  2709. case NS_STYLE_COUNTER_SPEAKAS_WORDS:
  2710. aSpeakAs.AssignLiteral(u"words");
  2711. break;
  2712. case NS_STYLE_COUNTER_SPEAKAS_SPELL_OUT:
  2713. aSpeakAs.AssignLiteral(u"spell-out");
  2714. break;
  2715. default:
  2716. NS_NOTREACHED("Unknown speech synthesis");
  2717. }
  2718. break;
  2719. case eCSSUnit_Auto:
  2720. case eCSSUnit_Ident:
  2721. aSpeakAs.Truncate();
  2722. value.AppendToString(eCSSProperty_UNKNOWN,
  2723. aSpeakAs, nsCSSValue::eNormalized);
  2724. break;
  2725. default:
  2726. aSpeakAs.Truncate();
  2727. }
  2728. return NS_OK;
  2729. }
  2730. nsresult
  2731. nsCSSCounterStyleRule::GetDescriptor(nsCSSCounterDesc aDescID,
  2732. nsAString& aValue)
  2733. {
  2734. NS_ASSERTION(aDescID == eCSSCounterDesc_Negative ||
  2735. aDescID == eCSSCounterDesc_Prefix ||
  2736. aDescID == eCSSCounterDesc_Suffix ||
  2737. aDescID == eCSSCounterDesc_Pad ||
  2738. aDescID == eCSSCounterDesc_Fallback,
  2739. "Unexpected descriptor");
  2740. const nsCSSValue& value = GetDesc(aDescID);
  2741. aValue.Truncate();
  2742. if (value.GetUnit() != eCSSUnit_Null) {
  2743. value.AppendToString(
  2744. eCSSProperty_UNKNOWN, aValue, nsCSSValue::eNormalized);
  2745. }
  2746. return NS_OK;
  2747. }
  2748. #define CSS_COUNTER_DESC_GETTER(name_) \
  2749. NS_IMETHODIMP \
  2750. nsCSSCounterStyleRule::Get##name_(nsAString& a##name_) \
  2751. { \
  2752. return GetDescriptor(eCSSCounterDesc_##name_, a##name_);\
  2753. }
  2754. CSS_COUNTER_DESC_GETTER(Negative)
  2755. CSS_COUNTER_DESC_GETTER(Prefix)
  2756. CSS_COUNTER_DESC_GETTER(Suffix)
  2757. CSS_COUNTER_DESC_GETTER(Pad)
  2758. CSS_COUNTER_DESC_GETTER(Fallback)
  2759. #undef CSS_COUNTER_DESC_GETTER
  2760. /* static */ bool
  2761. nsCSSCounterStyleRule::CheckDescValue(int32_t aSystem,
  2762. nsCSSCounterDesc aDescID,
  2763. const nsCSSValue& aValue)
  2764. {
  2765. switch (aDescID) {
  2766. case eCSSCounterDesc_System:
  2767. if (aValue.GetUnit() != eCSSUnit_Pair) {
  2768. return aValue.GetIntValue() == aSystem;
  2769. } else {
  2770. return aValue.GetPairValue().mXValue.GetIntValue() == aSystem;
  2771. }
  2772. case eCSSCounterDesc_Symbols:
  2773. switch (aSystem) {
  2774. case NS_STYLE_COUNTER_SYSTEM_NUMERIC:
  2775. case NS_STYLE_COUNTER_SYSTEM_ALPHABETIC:
  2776. // for these two system, the list must contain at least 2 elements
  2777. return aValue.GetListValue()->mNext;
  2778. case NS_STYLE_COUNTER_SYSTEM_EXTENDS:
  2779. // for extends system, no symbols should be set
  2780. return false;
  2781. default:
  2782. return true;
  2783. }
  2784. case eCSSCounterDesc_AdditiveSymbols:
  2785. switch (aSystem) {
  2786. case NS_STYLE_COUNTER_SYSTEM_EXTENDS:
  2787. return false;
  2788. default:
  2789. return true;
  2790. }
  2791. default:
  2792. return true;
  2793. }
  2794. }
  2795. nsresult
  2796. nsCSSCounterStyleRule::SetDescriptor(nsCSSCounterDesc aDescID,
  2797. const nsAString& aValue)
  2798. {
  2799. nsCSSParser parser;
  2800. nsCSSValue value;
  2801. CSSStyleSheet* sheet = GetStyleSheet();
  2802. nsIURI* baseURL = nullptr;
  2803. nsIPrincipal* principal = nullptr;
  2804. if (sheet) {
  2805. baseURL = sheet->GetBaseURI();
  2806. principal = sheet->Principal();
  2807. }
  2808. if (parser.ParseCounterDescriptor(aDescID, aValue, nullptr,
  2809. baseURL, principal, value)) {
  2810. if (CheckDescValue(GetSystem(), aDescID, value)) {
  2811. SetDesc(aDescID, value);
  2812. }
  2813. }
  2814. return NS_OK;
  2815. }
  2816. #define CSS_COUNTER_DESC_SETTER(name_) \
  2817. NS_IMETHODIMP \
  2818. nsCSSCounterStyleRule::Set##name_(const nsAString& a##name_) \
  2819. { \
  2820. return SetDescriptor(eCSSCounterDesc_##name_, a##name_); \
  2821. }
  2822. CSS_COUNTER_DESC_SETTER(System)
  2823. CSS_COUNTER_DESC_SETTER(Symbols)
  2824. CSS_COUNTER_DESC_SETTER(AdditiveSymbols)
  2825. CSS_COUNTER_DESC_SETTER(Negative)
  2826. CSS_COUNTER_DESC_SETTER(Prefix)
  2827. CSS_COUNTER_DESC_SETTER(Suffix)
  2828. CSS_COUNTER_DESC_SETTER(Range)
  2829. CSS_COUNTER_DESC_SETTER(Pad)
  2830. CSS_COUNTER_DESC_SETTER(Fallback)
  2831. CSS_COUNTER_DESC_SETTER(SpeakAs)
  2832. #undef CSS_COUNTER_DESC_SETTER
  2833. /* virtual */ size_t
  2834. nsCSSCounterStyleRule::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
  2835. {
  2836. return aMallocSizeOf(this);
  2837. }