ParseCSS.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. // vim:cindent:ts=8:et:sw=4:
  3. /* This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. /*
  7. * This file is meant to be used with |#define CSS_REPORT_PARSE_ERRORS|
  8. * in mozilla/dom/html/style/src/nsCSSScanner.h uncommented, and the
  9. * |#ifdef DEBUG| block in nsCSSScanner::OutputError (in
  10. * nsCSSScanner.cpp in the same directory) used (even if not a debug
  11. * build).
  12. */
  13. #include "nsXPCOM.h"
  14. #include "nsCOMPtr.h"
  15. #include "nsIFile.h"
  16. #include "nsNetUtil.h"
  17. #include "nsContentCID.h"
  18. #include "mozilla/StyleSheetInlines.h"
  19. #include "mozilla/css/Loader.h"
  20. using namespace mozilla;
  21. static already_AddRefed<nsIURI>
  22. FileToURI(const char *aFilename, nsresult *aRv = 0)
  23. {
  24. nsCOMPtr<nsIFile> lf(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, aRv));
  25. NS_ENSURE_TRUE(lf, nullptr);
  26. // XXX Handle relative paths somehow.
  27. lf->InitWithNativePath(nsDependentCString(aFilename));
  28. nsIURI *uri = nullptr;
  29. nsresult rv = NS_NewFileURI(&uri, lf);
  30. if (aRv)
  31. *aRv = rv;
  32. return uri;
  33. }
  34. static int
  35. ParseCSSFile(nsIURI *aSheetURI)
  36. {
  37. RefPtr<mozilla::css::Loader> = new mozilla::css::Loader();
  38. RefPtr<CSSStyleSheet> sheet;
  39. loader->LoadSheetSync(aSheetURI, getter_AddRefs(sheet));
  40. NS_ASSERTION(sheet, "sheet load failed");
  41. /* This can happen if the file can't be found (e.g. you
  42. * ask for a relative path and xpcom/io rejects it)
  43. */
  44. if (!sheet)
  45. return -1;
  46. bool complete;
  47. sheet->GetComplete(complete);
  48. NS_ASSERTION(complete, "synchronous load did not complete");
  49. if (!complete)
  50. return -2;
  51. return 0;
  52. }
  53. int main(int argc, char** argv)
  54. {
  55. if (argc < 2) {
  56. fprintf(stderr, "%s [FILE]...\n", argv[0]);
  57. }
  58. nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
  59. if (NS_FAILED(rv))
  60. return (int)rv;
  61. int res = 0;
  62. for (int i = 1; i < argc; ++i) {
  63. const char *filename = argv[i];
  64. printf("\nParsing %s.\n", filename);
  65. nsCOMPtr<nsIURI> uri = FileToURI(filename, &rv);
  66. if (rv == NS_ERROR_OUT_OF_MEMORY) {
  67. fprintf(stderr, "Out of memory.\n");
  68. return 1;
  69. }
  70. if (uri)
  71. res = ParseCSSFile(uri);
  72. }
  73. NS_ShutdownXPCOM(nullptr);
  74. return res;
  75. }