InspectorCSSParser.cpp (2204B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 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/. */ 6 7 #include "mozilla/dom/InspectorCSSParser.h" 8 9 #include "mozilla/ServoBindings.h" 10 #include "mozilla/ServoStyleConsts.h" 11 #include "mozilla/UniquePtr.h" 12 13 namespace mozilla::dom { 14 15 InspectorCSSParser::InspectorCSSParser(const nsACString& aText) 16 : mInput(aText) { 17 mParserState = Servo_CSSParser_create(&mInput); 18 } 19 20 UniquePtr<InspectorCSSParser> InspectorCSSParser::Constructor( 21 const GlobalObject& aGlobal, const nsACString& aText) { 22 return MakeUnique<InspectorCSSParser>(aText); 23 } 24 25 InspectorCSSParser::~InspectorCSSParser() { 26 Servo_CSSParser_destroy(mParserState); 27 mParserState = nullptr; 28 } 29 30 uint32_t InspectorCSSParser::LineNumber() const { return mLineNumber; } 31 32 uint32_t InspectorCSSParser::ColumnNumber() const { 33 // mColumnNumber is 1-based, but consumers expect 0-based. 34 return mColumnNumber - 1; 35 } 36 37 void InspectorCSSParser::NextToken(Nullable<InspectorCSSToken>& aResult) { 38 StyleCSSToken cssToken; 39 if (!Servo_CSSParser_NextToken(&mInput, mParserState, &cssToken)) { 40 aResult.SetNull(); 41 42 mLineNumber = Servo_CSSParser_GetCurrentLine(mParserState); 43 mColumnNumber = Servo_CSSParser_GetCurrentColumn(mParserState); 44 45 return; 46 } 47 48 InspectorCSSToken& inspectorCssToken = aResult.SetValue(); 49 inspectorCssToken.mText.Append(cssToken.text); 50 inspectorCssToken.mTokenType.Append(cssToken.token_type); 51 if (cssToken.has_value) { 52 inspectorCssToken.mValue.Append(cssToken.value); 53 } else { 54 inspectorCssToken.mValue.SetIsVoid(true); 55 } 56 if (cssToken.has_unit) { 57 inspectorCssToken.mUnit.Append(cssToken.unit); 58 } else { 59 inspectorCssToken.mUnit.SetIsVoid(true); 60 } 61 if (cssToken.has_number) { 62 // Reduce precision to avoid floating point inprecision 63 inspectorCssToken.mNumber = round(cssToken.number * 100) / 100.0; 64 } 65 66 mLineNumber = cssToken.line; 67 mColumnNumber = cssToken.column; 68 } 69 70 } // namespace mozilla::dom