Debian Codegraph @

Server/Web/HtmlTag.h

raw symbols SCIP
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #pragma once
22
23 #include <map>
24 #include <ostream>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28
29 #include "Languages.h"
30 #include "Logger/Logger.h"
31
32 namespace Server { namespace Web
33 {
34 class HtmlTag
35 {
36 public:
37 HtmlTag& operator=(const HtmlTag&) = delete;
38
39 inline HtmlTag()
40 {
41 }
42
43 inline HtmlTag(const std::string& name)
44 : name(name)
45 {
46 }
47
48 inline virtual ~HtmlTag()
49 {
50 }
51
52 virtual HtmlTag AddAttribute(const std::string& name, const std::string& value = "");
53
54 inline virtual bool IsAttributeSet(const std::string& name)
55 {
56 return attributes.count(name) == 1;
57 }
58
59 inline virtual HtmlTag AddChildTag(const HtmlTag& child)
60 {
61 this->childTags.push_back(child);
62 return *this;
63 }
64
65 inline virtual HtmlTag AddContent(const std::string& content)
66 {
67 this->content += content;
68 return *this;
69 }
70
71 template<typename... Args>
72 inline HtmlTag AddContent(const Languages::TextSelector text, Args... args)
73 {
74 return AddContent(Logger::Logger::Format(Languages::GetText(text), args...));
75 }
76
77 inline virtual HtmlTag AddClass(const std::string& className)
78 {
79 if (className.length() > 0)
80 {
81 classes.push_back(className);
82 }
83 return *this;
84 }
85
86 inline virtual HtmlTag AddId(const std::string& id)
87 {
88 this->id = id;
89 return *this;
90 }
91
92 inline virtual size_t ContentSize() const
93 {
94 return content.size();
95 }
96
97 inline size_t ChildCount() const
98 {
99 return childTags.size();
100 }
101
102 inline operator std::string () const
103 {
104 std::stringstream ss;
105 ss << *this;
106 return ss.str();
107 }
108
109 friend std::ostream& operator<<(std::ostream& stream, const HtmlTag& tag);
110
111 protected:
112 std::string name;
113 std::vector<HtmlTag> childTags;
114 std::map<std::string, std::string> attributes;
115 std::vector<std::string> classes;
116 std::string content;
117 std::string id;
118 };
119 }} // namespace Server::Web
120