blob: 39a723c6c0b76cdb458d9817102fea6b17979f1f [file] [log] [blame]
// Copyright 2019 The Cobalt Authors. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
#include <string>
#include "cobalt/base/token.h"
#include "cobalt/cssom/combinator_visitor.h"
#include "cobalt/cssom/selector.h"
#include "cobalt/cssom/selector_visitor.h"
namespace cobalt {
namespace cssom {
class PseudoClass;
class PseudoElement;
// Serializes various types in the CSSOM to a string. Currently only implements
// selector serialization; declarations, rules, and whole sheets aren't yet
// implemented.
class Serializer : public SelectorVisitor, public CombinatorVisitor {
Serializer(const Serializer& other) = delete;
Serializer& operator=(const Serializer& other) = delete;
explicit Serializer(std::string* output);
void SerializeIdentifier(base::Token identifier);
void SerializeString(const std::string& string);
void SerializeSelectors(const Selectors& selectors);
void SerializeSelector(const Selector& selector);
// Decode a UTF-8 character pointed to by |p| into |out_c|.
// Returns the number of bytes consumed.
// TODO: Move this to a place it can be shared, or use another shared impl.
int DecodeUTF8(const uint8_t* p, uint32_t* out_c);
// Escape a Unicode code point with its hex value.
void EscapeCodePoint(uint32_t c);
// Escape an ASCII character with a backslash.
void EscapeCharacter(uint32_t c);
// Simple selectors - Overrides from SelectorVisitor.
void VisitUniversalSelector(UniversalSelector* universal_selector) override;
void VisitTypeSelector(TypeSelector* type_selector) override;
void VisitAttributeSelector(AttributeSelector* attribute_selector) override;
void VisitClassSelector(ClassSelector* class_selector) override;
void VisitIdSelector(IdSelector* id_selector) override;
// Pseudo classes - Overrides from SelectorVisitor.
void VisitPseudoClass(PseudoClass* pseudo_class);
void VisitActivePseudoClass(ActivePseudoClass* active_pseudo_class) override;
void VisitEmptyPseudoClass(EmptyPseudoClass* empty_pseudo_class) override;
void VisitFocusPseudoClass(FocusPseudoClass* focus_pseudo_class) override;
void VisitHoverPseudoClass(HoverPseudoClass* hover_pseudo_class) override;
void VisitNotPseudoClass(NotPseudoClass* not_pseudo_class) override;
// Pseudo elements - Overrides from SelectorVisitor.
void VisitPseudoElement(PseudoElement* pseudo_element);
void VisitAfterPseudoElement(
AfterPseudoElement* after_pseudo_element) override;
void VisitBeforePseudoElement(
BeforePseudoElement* before_pseudo_element) override;
// Compound selector - Overrides from SelectorVisitor.
void VisitCompoundSelector(CompoundSelector* compound_selector) override;
// Complex selector - Overrides from SelectorVisitor.
void VisitComplexSelector(ComplexSelector* complex_selector) override;
// Combinators - Overrides from CombinatorVisitor.
void VisitChildCombinator(ChildCombinator* child_combinator) override;
void VisitNextSiblingCombinator(
NextSiblingCombinator* next_sibling_combinator) override;
void VisitDescendantCombinator(
DescendantCombinator* descendant_combinator) override;
void VisitFollowingSiblingCombinator(
FollowingSiblingCombinator* following_sibling_combinator) override;
// Pointer to the output string provided by the user.
std::string* output_;
} // namespace cssom
} // namespace cobalt