tor-browser

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

ParseCSS.cpp (2316B)


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