CSS.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. /* DOM object holding utility CSS functions */
  6. #include "CSS.h"
  7. #include "mozilla/dom/BindingDeclarations.h"
  8. #include "mozilla/ServoBindings.h"
  9. #include "nsCSSParser.h"
  10. #include "nsGlobalWindow.h"
  11. #include "nsIDocument.h"
  12. #include "nsIURI.h"
  13. #include "nsStyleUtil.h"
  14. #include "xpcpublic.h"
  15. namespace mozilla {
  16. namespace dom {
  17. struct SupportsParsingInfo
  18. {
  19. nsIURI* mDocURI;
  20. nsIURI* mBaseURI;
  21. nsIPrincipal* mPrincipal;
  22. StyleBackendType mStyleBackendType;
  23. };
  24. static nsresult
  25. GetParsingInfo(const GlobalObject& aGlobal,
  26. SupportsParsingInfo& aInfo)
  27. {
  28. nsGlobalWindow* win = xpc::WindowOrNull(aGlobal.Get());
  29. if (!win) {
  30. return NS_ERROR_FAILURE;
  31. }
  32. nsCOMPtr<nsIDocument> doc = win->GetDoc();
  33. if (!doc) {
  34. return NS_ERROR_FAILURE;
  35. }
  36. aInfo.mDocURI = nsCOMPtr<nsIURI>(doc->GetDocumentURI()).get();
  37. aInfo.mBaseURI = nsCOMPtr<nsIURI>(doc->GetBaseURI()).get();
  38. aInfo.mPrincipal = win->GetPrincipal();
  39. aInfo.mStyleBackendType = doc->GetStyleBackendType();
  40. return NS_OK;
  41. }
  42. /* static */ bool
  43. CSS::Supports(const GlobalObject& aGlobal,
  44. const nsAString& aProperty,
  45. const nsAString& aValue,
  46. ErrorResult& aRv)
  47. {
  48. SupportsParsingInfo info;
  49. nsresult rv = GetParsingInfo(aGlobal, info);
  50. if (NS_FAILED(rv)) {
  51. aRv.Throw(rv);
  52. return false;
  53. }
  54. if (info.mStyleBackendType == StyleBackendType::Servo) {
  55. NS_ConvertUTF16toUTF8 property(aProperty);
  56. NS_ConvertUTF16toUTF8 value(aValue);
  57. return Servo_CSSSupports(&property, &value);
  58. }
  59. nsCSSParser parser;
  60. return parser.EvaluateSupportsDeclaration(aProperty, aValue, info.mDocURI,
  61. info.mBaseURI, info.mPrincipal);
  62. }
  63. /* static */ bool
  64. CSS::Supports(const GlobalObject& aGlobal,
  65. const nsAString& aCondition,
  66. ErrorResult& aRv)
  67. {
  68. SupportsParsingInfo info;
  69. nsresult rv = GetParsingInfo(aGlobal, info);
  70. if (NS_FAILED(rv)) {
  71. aRv.Throw(rv);
  72. return false;
  73. }
  74. if (info.mStyleBackendType == StyleBackendType::Servo) {
  75. MOZ_CRASH("stylo: CSS.supports() with arguments is not yet implemented");
  76. }
  77. nsCSSParser parser;
  78. return parser.EvaluateSupportsCondition(aCondition, info.mDocURI,
  79. info.mBaseURI, info.mPrincipal);
  80. }
  81. /* static */ void
  82. CSS::Escape(const GlobalObject& aGlobal,
  83. const nsAString& aIdent,
  84. nsAString& aReturn)
  85. {
  86. nsStyleUtil::AppendEscapedCSSIdent(aIdent, aReturn);
  87. }
  88. } // namespace dom
  89. } // namespace mozilla