michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* DOM object holding utility CSS functions */ michael@0: michael@0: #include "CSS.h" michael@0: michael@0: #include "mozilla/dom/BindingDeclarations.h" michael@0: #include "nsCSSParser.h" michael@0: #include "nsGlobalWindow.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsIURI.h" michael@0: #include "nsStyleUtil.h" michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: struct SupportsParsingInfo michael@0: { michael@0: nsIURI* mDocURI; michael@0: nsIURI* mBaseURI; michael@0: nsIPrincipal* mPrincipal; michael@0: }; michael@0: michael@0: static nsresult michael@0: GetParsingInfo(nsISupports* aGlobal, michael@0: SupportsParsingInfo& aInfo) michael@0: { michael@0: if (!aGlobal) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: nsGlobalWindow* win = nsGlobalWindow::FromSupports(aGlobal); michael@0: nsCOMPtr doc = win->GetDoc(); michael@0: if (!doc) { michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: aInfo.mDocURI = nsCOMPtr(doc->GetDocumentURI()); michael@0: aInfo.mBaseURI = nsCOMPtr(doc->GetBaseURI()); michael@0: aInfo.mPrincipal = win->GetPrincipal(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* static */ bool michael@0: CSS::Supports(const GlobalObject& aGlobal, michael@0: const nsAString& aProperty, michael@0: const nsAString& aValue, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsCSSParser parser; michael@0: SupportsParsingInfo info; michael@0: michael@0: nsresult rv = GetParsingInfo(aGlobal.GetAsSupports(), info); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return false; michael@0: } michael@0: michael@0: return parser.EvaluateSupportsDeclaration(aProperty, aValue, info.mDocURI, michael@0: info.mBaseURI, info.mPrincipal); michael@0: } michael@0: michael@0: /* static */ bool michael@0: CSS::Supports(const GlobalObject& aGlobal, michael@0: const nsAString& aCondition, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsCSSParser parser; michael@0: SupportsParsingInfo info; michael@0: michael@0: nsresult rv = GetParsingInfo(aGlobal.GetAsSupports(), info); michael@0: if (NS_FAILED(rv)) { michael@0: aRv.Throw(rv); michael@0: return false; michael@0: } michael@0: michael@0: return parser.EvaluateSupportsCondition(aCondition, info.mDocURI, michael@0: info.mBaseURI, info.mPrincipal); michael@0: } michael@0: michael@0: /* static */ void michael@0: CSS::Escape(const GlobalObject& aGlobal, michael@0: const nsAString& aIdent, michael@0: nsAString& aReturn, michael@0: ErrorResult& aRv) michael@0: { michael@0: bool success = nsStyleUtil::AppendEscapedCSSIdent(aIdent, aReturn); michael@0: michael@0: if (!success) { michael@0: aRv.Throw(NS_ERROR_DOM_INVALID_CHARACTER_ERR); michael@0: } michael@0: } michael@0: michael@0: } // dom michael@0: } // mozilla