layout/style/CSS.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/layout/style/CSS.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,98 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +/* DOM object holding utility CSS functions */
    1.10 +
    1.11 +#include "CSS.h"
    1.12 +
    1.13 +#include "mozilla/dom/BindingDeclarations.h"
    1.14 +#include "nsCSSParser.h"
    1.15 +#include "nsGlobalWindow.h"
    1.16 +#include "nsIDocument.h"
    1.17 +#include "nsIURI.h"
    1.18 +#include "nsStyleUtil.h"
    1.19 +
    1.20 +namespace mozilla {
    1.21 +namespace dom {
    1.22 +
    1.23 +struct SupportsParsingInfo
    1.24 +{
    1.25 +  nsIURI* mDocURI;
    1.26 +  nsIURI* mBaseURI;
    1.27 +  nsIPrincipal* mPrincipal;
    1.28 +};
    1.29 +
    1.30 +static nsresult
    1.31 +GetParsingInfo(nsISupports* aGlobal,
    1.32 +               SupportsParsingInfo& aInfo)
    1.33 +{
    1.34 +  if (!aGlobal) {
    1.35 +    return NS_ERROR_FAILURE;
    1.36 +  }
    1.37 +
    1.38 +  nsGlobalWindow* win = nsGlobalWindow::FromSupports(aGlobal);
    1.39 +  nsCOMPtr<nsIDocument> doc = win->GetDoc();
    1.40 +  if (!doc) {
    1.41 +    return NS_ERROR_FAILURE;
    1.42 +  }
    1.43 +
    1.44 +  aInfo.mDocURI = nsCOMPtr<nsIURI>(doc->GetDocumentURI());
    1.45 +  aInfo.mBaseURI = nsCOMPtr<nsIURI>(doc->GetBaseURI());
    1.46 +  aInfo.mPrincipal = win->GetPrincipal();
    1.47 +  return NS_OK;
    1.48 +}
    1.49 +
    1.50 +/* static */ bool
    1.51 +CSS::Supports(const GlobalObject& aGlobal,
    1.52 +              const nsAString& aProperty,
    1.53 +              const nsAString& aValue,
    1.54 +              ErrorResult& aRv)
    1.55 +{
    1.56 +  nsCSSParser parser;
    1.57 +  SupportsParsingInfo info;
    1.58 +
    1.59 +  nsresult rv = GetParsingInfo(aGlobal.GetAsSupports(), info);
    1.60 +  if (NS_FAILED(rv)) {
    1.61 +    aRv.Throw(rv);
    1.62 +    return false;
    1.63 +  }
    1.64 +
    1.65 +  return parser.EvaluateSupportsDeclaration(aProperty, aValue, info.mDocURI,
    1.66 +                                            info.mBaseURI, info.mPrincipal);
    1.67 +}
    1.68 +
    1.69 +/* static */ bool
    1.70 +CSS::Supports(const GlobalObject& aGlobal,
    1.71 +              const nsAString& aCondition,
    1.72 +              ErrorResult& aRv)
    1.73 +{
    1.74 +  nsCSSParser parser;
    1.75 +  SupportsParsingInfo info;
    1.76 +
    1.77 +  nsresult rv = GetParsingInfo(aGlobal.GetAsSupports(), info);
    1.78 +  if (NS_FAILED(rv)) {
    1.79 +    aRv.Throw(rv);
    1.80 +    return false;
    1.81 +  }
    1.82 +
    1.83 +  return parser.EvaluateSupportsCondition(aCondition, info.mDocURI,
    1.84 +                                          info.mBaseURI, info.mPrincipal);
    1.85 +}
    1.86 +
    1.87 +/* static */ void
    1.88 +CSS::Escape(const GlobalObject& aGlobal,
    1.89 +            const nsAString& aIdent,
    1.90 +            nsAString& aReturn,
    1.91 +            ErrorResult& aRv)
    1.92 +{
    1.93 +  bool success = nsStyleUtil::AppendEscapedCSSIdent(aIdent, aReturn);
    1.94 +
    1.95 +  if (!success) {
    1.96 +    aRv.Throw(NS_ERROR_DOM_INVALID_CHARACTER_ERR);
    1.97 +  }
    1.98 +}
    1.99 +
   1.100 +} // dom
   1.101 +} // mozilla

mercurial