Bug 1839315: part 4) Link from `SheetLoadData::mWasAlternate` to spec. r=emilio DONTBUILD
[gecko.git] / layout / style / test / ParseCSS.cpp
blob04e37d48e2bb0094b9b9c3809fa4227a003de9e6
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/. */
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).
15 #include "nsXPCOM.h"
16 #include "nsCOMPtr.h"
18 #include "nsIFile.h"
19 #include "nsNetUtil.h"
21 #include "nsContentCID.h"
22 #include "mozilla/StyleSheetInlines.h"
23 #include "mozilla/css/Loader.h"
25 using namespace mozilla;
27 static already_AddRefed<nsIURI> FileToURI(const char* aFilename,
28 nsresult* aRv = 0) {
29 nsCOMPtr<nsIFile> lf(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, aRv));
30 NS_ENSURE_TRUE(lf, nullptr);
31 // XXX Handle relative paths somehow.
32 lf->InitWithNativePath(nsDependentCString(aFilename));
34 nsIURI* uri = nullptr;
35 nsresult rv = NS_NewFileURI(&uri, lf);
36 if (aRv) *aRv = rv;
37 return uri;
40 static int ParseCSSFile(nsIURI* aSheetURI) {
41 RefPtr<mozilla::css::Loader> = new mozilla::css::Loader();
42 RefPtr<CSSStyleSheet> sheet;
43 loader->LoadSheetSync(aSheetURI, getter_AddRefs(sheet));
44 NS_ASSERTION(sheet, "sheet load failed");
45 /* This can happen if the file can't be found (e.g. you
46 * ask for a relative path and xpcom/io rejects it)
48 if (!sheet) return -1;
49 bool complete;
50 sheet->GetComplete(complete);
51 NS_ASSERTION(complete, "synchronous load did not complete");
52 if (!complete) return -2;
53 return 0;
56 int main(int argc, char** argv) {
57 if (argc < 2) {
58 fprintf(stderr, "%s [FILE]...\n", argv[0]);
60 nsresult rv = NS_InitXPCOM(nullptr, nullptr, nullptr);
61 if (NS_FAILED(rv)) return (int)rv;
63 int res = 0;
64 for (int i = 1; i < argc; ++i) {
65 const char* filename = argv[i];
67 printf("\nParsing %s.\n", filename);
69 nsCOMPtr<nsIURI> uri = FileToURI(filename, &rv);
70 if (rv == NS_ERROR_OUT_OF_MEMORY) {
71 fprintf(stderr, "Out of memory.\n");
72 return 1;
74 if (uri) res = ParseCSSFile(uri);
77 NS_ShutdownXPCOM(nullptr);
79 return res;