home
readme
diff
tree
note
docs
0commit 18ff2ec1bfc1cf9fcd17c1acb05c3b41f8f0ed83
1Author: Mason Wright <mason@lakefox.net>
2Date: Sun Jun 15 11:50:36 2025 -0600
3
4 Parser started, can parse attributes but not closing tags
5
6diff --git a/element.cc b/element.cc
7index e987f50..0593b32 100755
8--- a/element.cc
9+++ b/element.cc
10@@ -1,66 +1,234 @@
11 #include <string>
12 #include <print>
13 #include <vector>
14+#include <fstream>
15+#include <unordered_map>
16 #include <iostream>
17+#include <memory>
18+#include <cctype>
19
20 #define GETTER_SETTER(type, name) \
21 type get##name() const { return name; } \
22 void set##name(type value) { name = value; }
23
24 struct Styles {
25- stylesheets std::vector<std::unordered_map<std::string, std::string>>;
26- inline std::unordered_map<std::string, std::string>;
27- psuedo std::unordered_map<std::string, std::unordered_map<std::string, std::string>>;
28-}
29+ std::vector<std::unordered_map<std::string, std::string>> stylesheets;
30+ std::unordered_map<std::string, std::string> inlineStyles;
31+ std::unordered_map<std::string, std::unordered_map<std::string, std::string>> psuedoStyles;
32+};
33
34 struct Bounds {
35- int Top;
36- int Right;
37- int Bottom;
38- int Left;
39-}
40+ int top;
41+ int right;
42+ int bottom;
43+ int left;
44+};
45
46 struct State {
47- Bounds offset;
48- Border border;
49- std:vector<Background> background;
50+ // Bounds offset;
51+ // Border border;
52+ // std::vector<Background> background;
53 int width;
54 int height;
55 int z;
56 bool hidden;
57 int tabIndex;
58-}
59+};
60
61 class Node {
62 private:
63- std::string tagName;
64- std::string id;
65- std::string innerText;
66-
67-
68-
69+ std::string TagName;
70+ std::string Id;
71+ std::string InnerText;
72+ std::unordered_map<std::string, std::string> Attributes;
73 public:
74 Node* parent;
75- std::vector<Node*> children;
76+ std::vector<std::unique_ptr<Node>> children;
77+
78+ Node() : parent(nullptr) {}
79+
80+ Node* createElement(std::string name) {
81+ std::unique_ptr<Node> newNode = std::make_unique<Node>();
82+ newNode->setTagName(name);
83+ newNode->parent = this;
84+ children.push_back(std::move(newNode));
85+ return children.back().get();
86+ }
87
88 // Define the getters and setters for each private property
89- GETTER_SETTER(std::string, tagname)
90- GETTER_SETTER(std::string, id)
91- GETTER_SETTER(std::string, innerText)
92+ GETTER_SETTER(std::string, TagName)
93+ GETTER_SETTER(std::string, Id)
94+ GETTER_SETTER(std::string, InnerText)
95+
96+ void setAttribute(std::string name, std::string value) {
97+ Attributes[name] = value;
98+ }
99+
100+ std::string getAttribute(std::string name) const {
101+ auto it = Attributes.find(name);
102+ if (it != Attributes.end()) {
103+ return it->second;
104+ }
105+ return ""; // Return empty string if attribute not found
106+ }
107 };
108
109+std::unordered_map<std::string, std::string> parseAttributes(std::vector<std::string> tokens) {
110+ std::unordered_map<std::string, std::string> attrs;
111+
112+ for (unsigned short i = 0; i < tokens.size(); i++) {
113+ if (i != 0) {
114+ std::string name = "";
115+ std::string value = "";
116+ bool setValue = false;
117+
118+ for (auto c : tokens[i]) {
119+ if (c == '=') {
120+ setValue = true;
121+ continue;
122+ }
123+
124+ if (setValue) {
125+ value += c;
126+ } else {
127+ name += c;
128+ }
129+ }
130+
131+ std::string trimmedName = "";
132+ for (auto c : name) {
133+ if (!std::isspace(c)) {
134+ trimmedName += c;
135+ }
136+ }
137+
138+ std::string trimmedValue = "";
139+ int sliceStart = 0;
140+ int sliceEnd = value.length();
141+
142+ if (value.length() >= 2) {
143+ for (unsigned short t = 0; t < value.length(); t++) {
144+ if (!std::isspace(value[t])) {
145+ sliceStart = t;
146+ break;
147+ }
148+ }
149+
150+ for (int t = value.length()-1; t >= 0; t--) {
151+ if (!std::isspace(value[t])) {
152+ sliceEnd = t;
153+ break;
154+ }
155+ }
156+
157+ // Add and subtract 1 from each side to remove quotes
158+ for (unsigned short t = sliceStart+1; t < sliceEnd; t++) {
159+ trimmedValue += value[t];
160+ }
161+ }
162
163-Node* parser(std::string file) {
164-
165+ attrs[trimmedName] = trimmedValue;
166+ }
167+ }
168+
169+ return attrs;
170 }
171
172-int main() {
173
174- Node document;
175+std::unique_ptr<Node> parserdocument(std::string file) {
176+ std::ifstream inputFile(file);
177+
178+ if (!inputFile.is_open()) {
179+ std::cerr << "Unable to open: " << file << std::endl;
180+ return nullptr;
181+ }
182+
183+ std::unique_ptr<Node> root = std::make_unique<Node>();
184+ root->setTagName("root");
185+
186+ std::string line;
187+ Node* currentNode = root.get();
188+ // !ISSUE: test
189+ //
190+ bool inTag = false;
191+ bool escaped = false;
192+ bool inQuote = false;
193+ char quoteType = '"';
194+
195+
196+ std::vector<std::string> tokens;
197+ std::string word = "";
198+
199+ while (std::getline(inputFile, line)) {
200+ println("{}", line);
201+
202+ int ll = line.length();
203+ for (unsigned short i = 0; i < ll; i++) {
204+ char current = line[i];
205+
206+
207+ if (current == '>') {
208+ tokens.push_back(word);
209+ word = "";
210+ inTag = false;
211+ }
212+
213+ if (inTag) {
214+
215+ if ((current == '"' || current == '\'') && !escaped) {
216+ if (inQuote && current == quoteType) {
217+ inQuote = false;
218+ } else if (!inQuote) {
219+ inQuote = true;
220+ quoteType = current;
221+ }
222+ }
223+ if (current == ' ' && !inQuote) {
224+ tokens.push_back(word);
225+ word = "";
226+ } else {
227+ word += current;
228+ }
229+ } else if (current != '<' || current != '>') {
230+ // Outside tag add to innerText
231+ currentNode->setInnerText(currentNode->getInnerText() + current);
232+ }
233
234- document.setTagName("html");
235- std::string tn = document.getTagName();
236+ if (current == '<') {
237+ inTag = true;
238+ }
239+
240+ escaped = false;
241+
242+ if (current == '\\') { //'
243+ escaped = true;
244+ }
245+
246+ if (!inTag && tokens.size() > 0) {
247+ // Build the element
248+ currentNode = currentNode->createElement(tokens[0]);
249+ auto attrs = parseAttributes(tokens);
250+ for (auto const& pair : attrs) {
251+ std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl;
252+ }
253+
254+ tokens.clear();
255+ word = "";
256+ }
257+ }
258+ }
259+
260+ for (auto t : tokens) {
261+ std::cout << t << std::endl;
262+ }
263+
264+ inputFile.close();
265+ return root;
266+}
267+
268+
269+int main() {
270
271- std::println("{}", tn);
272+ parserdocument("./index.html");
273 return 0;
274 }
275diff --git a/index.html b/index.html
276new file mode 100644
277index 0000000..0afb07f
278--- /dev/null
279+++ b/index.html
280@@ -0,0 +1,6 @@
281+<html
282+ lang="en thie i\"s\" a test"
283+ contentEditable
284+>
285+ hi
286+</html>
287diff --git a/main b/main
288index 52bf11c..b48aa86 100755
289Binary files a/main and b/main differ