StyledNode.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #pragma once
  2. #include <AK/AKString.h>
  3. #include <AK/HashMap.h>
  4. #include <AK/NonnullRefPtr.h>
  5. #include <AK/Optional.h>
  6. #include <LibHTML/CSS/StyleValue.h>
  7. #include <LibHTML/TreeNode.h>
  8. class Node;
  9. enum class Display {
  10. None,
  11. Block,
  12. Inline,
  13. };
  14. class StyledNode : public TreeNode<StyledNode> {
  15. public:
  16. static NonnullRefPtr<StyledNode> create(const Node& node)
  17. {
  18. return adopt(*new StyledNode(&node));
  19. }
  20. ~StyledNode();
  21. const Node* node() const { return m_node; }
  22. template<typename Callback>
  23. inline void for_each_child(Callback callback) const
  24. {
  25. for (auto* node = first_child(); node; node = node->next_sibling())
  26. callback(*node);
  27. }
  28. template<typename Callback>
  29. inline void for_each_child(Callback callback)
  30. {
  31. for (auto* node = first_child(); node; node = node->next_sibling())
  32. callback(*node);
  33. }
  34. template<typename Callback>
  35. inline void for_each_property(Callback callback) const
  36. {
  37. for (auto& it : m_property_values)
  38. callback(it.key, *it.value);
  39. }
  40. void set_property(const String& name, NonnullRefPtr<StyleValue> value)
  41. {
  42. m_property_values.set(name, move(value));
  43. }
  44. Optional<NonnullRefPtr<StyleValue>> property(const String& name) const
  45. {
  46. auto it = m_property_values.find(name);
  47. if (it == m_property_values.end())
  48. return {};
  49. return it->value;
  50. }
  51. Display display() const;
  52. Length length_or_fallback(const StringView& property_name, const Length& fallback)
  53. {
  54. auto value = property(property_name);
  55. if (!value.has_value())
  56. return fallback;
  57. return value.value()->to_length();
  58. }
  59. protected:
  60. explicit StyledNode(const Node*);
  61. private:
  62. const Node* m_node { nullptr };
  63. HashMap<String, NonnullRefPtr<StyleValue>> m_property_values;
  64. };