layout/style/test/ParseCSS.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/style/test/ParseCSS.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,87 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +// vim:cindent:ts=8:et:sw=4:
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/*
    1.11 + * This file is meant to be used with |#define CSS_REPORT_PARSE_ERRORS|
    1.12 + * in mozilla/content/html/style/src/nsCSSScanner.h uncommented, and the
    1.13 + * |#ifdef DEBUG| block in nsCSSScanner::OutputError (in
    1.14 + * nsCSSScanner.cpp in the same directory) used (even if not a debug
    1.15 + * build).
    1.16 + */
    1.17 +
    1.18 +#include "nsXPCOM.h"
    1.19 +#include "nsCOMPtr.h"
    1.20 +
    1.21 +#include "nsIFile.h"
    1.22 +#include "nsNetUtil.h"
    1.23 +
    1.24 +#include "nsContentCID.h"
    1.25 +#include "mozilla/css/Loader.h"
    1.26 +#include "nsCSSStyleSheet.h"
    1.27 +
    1.28 +static already_AddRefed<nsIURI>
    1.29 +FileToURI(const char *aFilename, nsresult *aRv = 0)
    1.30 +{
    1.31 +    nsCOMPtr<nsIFile> lf(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, aRv));
    1.32 +    NS_ENSURE_TRUE(lf, nullptr);
    1.33 +    // XXX Handle relative paths somehow.
    1.34 +    lf->InitWithNativePath(nsDependentCString(aFilename));
    1.35 +
    1.36 +    nsIURI *uri = nullptr;
    1.37 +    nsresult rv = NS_NewFileURI(&uri, lf);
    1.38 +    if (aRv)
    1.39 +        *aRv = rv;
    1.40 +    return uri;
    1.41 +}
    1.42 +
    1.43 +static int
    1.44 +ParseCSSFile(nsIURI *aSheetURI)
    1.45 +{
    1.46 +    nsRefPtr<mozilla::css::Loader> = new mozilla::css::Loader();
    1.47 +    nsRefPtr<nsCSSStyleSheet> sheet;
    1.48 +    loader->LoadSheetSync(aSheetURI, getter_AddRefs(sheet));
    1.49 +    NS_ASSERTION(sheet, "sheet load failed");
    1.50 +    /* This can happen if the file can't be found (e.g. you
    1.51 +     * ask for a relative path and xpcom/io rejects it)
    1.52 +     */
    1.53 +    if (!sheet)
    1.54 +        return -1;
    1.55 +    bool complete;
    1.56 +    sheet->GetComplete(complete);
    1.57 +    NS_ASSERTION(complete, "synchronous load did not complete");
    1.58 +    if (!complete)
    1.59 +        return -2;
    1.60 +    return 0;
    1.61 +}
    1.62 +
    1.63 +int main(int argc, char** argv)
    1.64 +{
    1.65 +    if (argc < 2) {
    1.66 +        fprintf(stderr, "%s [FILE]...\n", argv[0]);
    1.67 +    }
    1.68 +    nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
    1.69 +    if (NS_FAILED(rv))
    1.70 +        return (int)rv;
    1.71 +
    1.72 +    int res = 0;
    1.73 +    for (int i = 1; i < argc; ++i) {
    1.74 +        const char *filename = argv[i];
    1.75 +
    1.76 +        printf("\nParsing %s.\n", filename);
    1.77 +
    1.78 +        nsCOMPtr<nsIURI> uri = FileToURI(filename, &rv);
    1.79 +        if (rv == NS_ERROR_OUT_OF_MEMORY) {
    1.80 +            fprintf(stderr, "Out of memory.\n");
    1.81 +            return 1;
    1.82 +        }
    1.83 +        if (uri)
    1.84 +            res = ParseCSSFile(uri);
    1.85 +    }
    1.86 +
    1.87 +    NS_ShutdownXPCOM(nullptr);
    1.88 +
    1.89 +    return res;
    1.90 +}

mercurial