gecko/layout/style/ErrorReporter.h
Cameron McCormack 4fd40c24b6 Bug 773296 - Part 18: Add error reporting for invalid variable references. r=dbaron
This adds CSS parser error reporting for parsing of custom properties
and normal properties that have variable references.

When re-parsing a normal property that had a variable reference, we
report any parse error to be at the beginning of the property value.
This is because it is difficult to keep track of where exactly each
variable substitution came from to point to the particular value
that would have caused the parse error.  For example, with this:

  :root {
    var-a: 1px 2px;
    var-b: 3px var(a);
  }
  p {
    margin: var(a) var(b);
  }

we would end up resolving the value of 'margin' to:

  "  1px 2px  3px  1px 2px"

In this string, the parse error occurs when we encounter the final
"2px", but by this point we don't know where that value came from.
So instead we just point to the line on which 'margin' was declared.

We extend ErrorReporter with an OutputError overload that takes the
specific line and column number to use in the error report to get this
right, and we store the line and column number for each token stream
we parse on the nsCSSValueTokenStream object.
2013-12-12 13:09:44 +11:00

108 lines
3.4 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* diagnostic reporting for CSS style sheet parser */
#ifndef mozilla_css_ErrorReporter_h_
#define mozilla_css_ErrorReporter_h_
// XXX turn this off for minimo builds
#define CSS_REPORT_PARSE_ERRORS
#include "nsString.h"
struct nsCSSToken;
class nsCSSStyleSheet;
class nsCSSScanner;
class nsIURI;
namespace mozilla {
namespace css {
class Loader;
// If CSS_REPORT_PARSE_ERRORS is not defined, all of this class's
// methods become inline stubs.
class MOZ_STACK_CLASS ErrorReporter {
public:
ErrorReporter(const nsCSSScanner &aScanner,
const nsCSSStyleSheet *aSheet,
const Loader *aLoader,
nsIURI *aURI);
~ErrorReporter();
static void ReleaseGlobals();
void OutputError();
void OutputError(uint32_t aLineNumber, uint32_t aLineOffset);
void ClearError();
// In all overloads of ReportUnexpected, aMessage is a stringbundle
// name, which will be processed as a format string with the
// indicated number of parameters.
// no parameters
void ReportUnexpected(const char *aMessage);
// one parameter, a string
void ReportUnexpected(const char *aMessage, const nsString& aParam);
// one parameter, a token
void ReportUnexpected(const char *aMessage, const nsCSSToken& aToken);
// two parameters, a token and a character, in that order
void ReportUnexpected(const char *aMessage, const nsCSSToken& aToken,
PRUnichar aChar);
// for ReportUnexpectedEOF, aExpected can be either a stringbundle
// name or a single character. In the former case there may not be
// any format parameters.
void ReportUnexpectedEOF(const char *aExpected);
void ReportUnexpectedEOF(PRUnichar aExpected);
private:
void AddToError(const nsString &aErrorText);
#ifdef CSS_REPORT_PARSE_ERRORS
nsAutoString mError;
nsString mErrorLine;
nsString mFileName;
const nsCSSScanner *mScanner;
const nsCSSStyleSheet *mSheet;
const Loader *mLoader;
nsIURI *mURI;
uint64_t mInnerWindowID;
uint32_t mErrorLineNumber;
uint32_t mPrevErrorLineNumber;
uint32_t mErrorColNumber;
#endif
};
#ifndef CSS_REPORT_PARSE_ERRORS
inline ErrorReporter::ErrorReporter(const nsCSSScanner&,
const nsCSSStyleSheet*,
const Loader*,
nsIURI*) {}
inline ErrorReporter::~ErrorReporter() {}
inline void ErrorReporter::ReleaseGlobals() {}
inline void ErrorReporter::OutputError() {}
inline void ErrorReporter::ClearError() {}
inline void ErrorReporter::ReportUnexpected(const char *) {}
inline void ErrorReporter::ReportUnexpected(const char *, const nsString &) {}
inline void ErrorReporter::ReportUnexpected(const char *, const nsCSSToken &) {}
inline void ErrorReporter::ReportUnexpected(const char *, const nsCSSToken &,
PRUnichar) {}
inline void ErrorReporter::ReportUnexpectedEOF(const char *) {}
inline void ErrorReporter::ReportUnexpectedEOF(PRUnichar) {}
inline void ErrorReporter::AddToError(const nsString &) {}
#endif
} // namespace css
} // namespace mozilla
#endif // mozilla_css_ErrorReporter_h_