Author: Mason Wright
Email:
[email protected]
Date: Sun, 15 Jun 2025 11:50:36 -0600
element.cc
18ff2ec1bfc1cf9fcd17c1acb05c3b41f8f0ed83
Parser started, can parse attributes but not closing tags
Clone
-
adapter.cc
-
build.sh
M
element.cc
-
events.cc
A
index.html
M
main
-
main.cc
Commits
b966b2a517365074e5c381dbdea05b3221dc0198
e840f1eeb0ae26af69e1ae146ea9938e28e9f1af
e4e05418a640eaed08cd1ec7cd8644eb1dbcca50
4e01ba8ad2c3361fa4be3d896288020948b58b5e
aae562ac1350480e4889aabb35899f776c5b59e9
6c3ae0e31eb0893f20e3872117f92cc6b9a942af
350e7d88bb2feb9db00c6e032cc6623f215b7adf
95e6c70d23e99ffcf70e5bbe12503496e5d8f232
e188783659b9bc3b9993a647e93ed110e7f41db6
5e4c38ff3c212cdd9881427ef3f8c2706539a190
e50ea9e1356a74af18fdd171337ef9dc931e1f4e
8f2e83556d12aaebe8e8597ea6923804b0eb7a43
1627c585128af263181053ab2cf1a4cdcd14ee21
def3513f75b325464ad88a33c741c4ca80572b77
a21501590980a905fa9b902897d700a42a08b7f0
56074a6bfe4498d092f3a227297c8c20e2bb962c
d9cf1485b7ae0614130494f0e73237921323b9a1
80f04b134ae32ad8a9d526007b33dd02f6600f05
23d6c65f9368d3c622a55a3068a6b2f1efa0c8d4
09c195df02536b6a796bd648fce9669397b96109
f2b5c8202fbc904e2ed78260e3fdbd55164799d2
4bfba076120f389994fc46a98e8b7a2622314400
e36ac5417e10ee9b9f94f340e1ccf28afc5705ea
d00dc89a86dd7e2fcfd4618bc3a1c8cfba9e3c3d
d9eef16adaf292f3748db5fb5aa98463de10d712
18ff2ec1bfc1cf9fcd17c1acb05c3b41f8f0ed83
9e7fd2980d723437ea621b78d395fa72ca3f4922
Diff
diff --git a/element.cc b/element.cc index 0593b32..e987f50 100755 --- a/element.cc +++ b/element.cc @@ -4,2 +3,0 @@ -#include
-#include
@@ -7,2 +4,0 @@ -#include
-#include
@@ -15,4 +11,4 @@ struct Styles { - std::vector
> stylesheets; - std::unordered_map
inlineStyles; - std::unordered_map
> psuedoStyles; -}; + stylesheets std::vector
>; + inline std::unordered_map
; + psuedo std::unordered_map
>; +} @@ -21,5 +17,5 @@ struct Bounds { - int top; - int right; - int bottom; - int left; -}; + int Top; + int Right; + int Bottom; + int Left; +} @@ -28,3 +24,3 @@ struct State { - // Bounds offset; - // Border border; - // std::vector
background; + Bounds offset; + Border border; + std:vector
background; @@ -36 +32 @@ struct State { -}; +} @@ -40,7 +36,3 @@ class Node { - std::string TagName; - std::string Id; - std::string InnerText; - std::unordered_map
Attributes; - public: - Node* parent; - std::vector
> children; + std::string tagName; + std::string id; + std::string innerText; @@ -48 +39,0 @@ class Node { - Node() : parent(nullptr) {} @@ -50,12 +40,0 @@ class Node { - Node* createElement(std::string name) { - std::unique_ptr
newNode = std::make_unique
(); - newNode->setTagName(name); - newNode->parent = this; - children.push_back(std::move(newNode)); - return children.back().get(); - } - - // Define the getters and setters for each private property - GETTER_SETTER(std::string, TagName) - GETTER_SETTER(std::string, Id) - GETTER_SETTER(std::string, InnerText) @@ -63,3 +42,3 @@ class Node { - void setAttribute(std::string name, std::string value) { - Attributes[name] = value; - } + public: + Node* parent; + std::vector
children; @@ -67,7 +46,4 @@ class Node { - std::string getAttribute(std::string name) const { - auto it = Attributes.find(name); - if (it != Attributes.end()) { - return it->second; - } - return ""; // Return empty string if attribute not found - } + // Define the getters and setters for each private property + GETTER_SETTER(std::string, tagname) + GETTER_SETTER(std::string, id) + GETTER_SETTER(std::string, innerText) @@ -76,53 +51,0 @@ class Node { -std::unordered_map
parseAttributes(std::vector
tokens) { - std::unordered_map
attrs; - - for (unsigned short i = 0; i < tokens.size(); i++) { - if (i != 0) { - std::string name = ""; - std::string value = ""; - bool setValue = false; - - for (auto c : tokens[i]) { - if (c == '=') { - setValue = true; - continue; - } - - if (setValue) { - value += c; - } else { - name += c; - } - } - - std::string trimmedName = ""; - for (auto c : name) { - if (!std::isspace(c)) { - trimmedName += c; - } - } - - std::string trimmedValue = ""; - int sliceStart = 0; - int sliceEnd = value.length(); - - if (value.length() >= 2) { - for (unsigned short t = 0; t < value.length(); t++) { - if (!std::isspace(value[t])) { - sliceStart = t; - break; - } - } - - for (int t = value.length()-1; t >= 0; t--) { - if (!std::isspace(value[t])) { - sliceEnd = t; - break; - } - } - - // Add and subtract 1 from each side to remove quotes - for (unsigned short t = sliceStart+1; t < sliceEnd; t++) { - trimmedValue += value[t]; - } - } @@ -130,5 +53,2 @@ std::unordered_map
parseAttributes(std::vector
parseAttributes(std::vector
parseAttributes(std::vector
parserdocument(std::string file) { - std::ifstream inputFile(file); - - if (!inputFile.is_open()) { - std::cerr << "Unable to open: " << file << std::endl; - return nullptr; - } - - std::unique_ptr
root = std::make_unique
(); - root->setTagName("root"); - - std::string line; - Node* currentNode = root.get(); - // !ISSUE: test - // - bool inTag = false; - bool escaped = false; - bool inQuote = false; - char quoteType = '"'; - - - std::vector
tokens; - std::string word = ""; - - while (std::getline(inputFile, line)) { - println("{}", line); - - int ll = line.length(); - for (unsigned short i = 0; i < ll; i++) { - char current = line[i]; - - - if (current == '>') { - tokens.push_back(word); - word = ""; - inTag = false; - } - - if (inTag) { - - if ((current == '"' || current == '\'') && !escaped) { - if (inQuote && current == quoteType) { - inQuote = false; - } else if (!inQuote) { - inQuote = true; - quoteType = current; - } - } - if (current == ' ' && !inQuote) { - tokens.push_back(word); - word = ""; - } else { - word += current; - } - } else if (current != '<' || current != '>') { - // Outside tag add to innerText - currentNode->setInnerText(currentNode->getInnerText() + current); - } - - if (current == '<') { - inTag = true; - } - - escaped = false; - - if (current == '\\') { //' - escaped = true; - } - - if (!inTag && tokens.size() > 0) { - // Build the element - currentNode = currentNode->createElement(tokens[0]); - auto attrs = parseAttributes(tokens); - for (auto const& pair : attrs) { - std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl; - } - - tokens.clear(); - word = ""; - } - } - } - - for (auto t : tokens) { - std::cout << t << std::endl; - } - - inputFile.close(); - return root; -} - + Node document; @@ -230 +61,2 @@ std::unique_ptr
parserdocument(std::string file) { -int main() { + document.setTagName("html"); + std::string tn = document.getTagName(); @@ -232 +64 @@ int main() { - parserdocument("./index.html"); + std::println("{}", tn);
#include
#include
#include
#include
#include
#include
#include
#include
#define GETTER_SETTER(type, name) \ type get##name() const { return name; } \ void set##name(type value) { name = value; } struct Styles { std::vector
> stylesheets; std::unordered_map
inlineStyles; std::unordered_map
> psuedoStyles; }; struct Bounds { int top; int right; int bottom; int left; }; struct State { // Bounds offset; // Border border; // std::vector
background; int width; int height; int z; bool hidden; int tabIndex; }; class Node { private: std::string TagName; std::string Id; std::string InnerText; std::unordered_map
Attributes; public: Node* parent; std::vector
> children; Node() : parent(nullptr) {} Node* createElement(std::string name) { std::unique_ptr
newNode = std::make_unique
(); newNode->setTagName(name); newNode->parent = this; children.push_back(std::move(newNode)); return children.back().get(); } // Define the getters and setters for each private property GETTER_SETTER(std::string, TagName) GETTER_SETTER(std::string, Id) GETTER_SETTER(std::string, InnerText) void setAttribute(std::string name, std::string value) { Attributes[name] = value; } std::string getAttribute(std::string name) const { auto it = Attributes.find(name); if (it != Attributes.end()) { return it->second; } return ""; // Return empty string if attribute not found } }; std::unordered_map
parseAttributes(std::vector
tokens) { std::unordered_map
attrs; for (unsigned short i = 0; i < tokens.size(); i++) { if (i != 0) { std::string name = ""; std::string value = ""; bool setValue = false; for (auto c : tokens[i]) { if (c == '=') { setValue = true; continue; } if (setValue) { value += c; } else { name += c; } } std::string trimmedName = ""; for (auto c : name) { if (!std::isspace(c)) { trimmedName += c; } } std::string trimmedValue = ""; int sliceStart = 0; int sliceEnd = value.length(); if (value.length() >= 2) { for (unsigned short t = 0; t < value.length(); t++) { if (!std::isspace(value[t])) { sliceStart = t; break; } } for (int t = value.length()-1; t >= 0; t--) { if (!std::isspace(value[t])) { sliceEnd = t; break; } } // Add and subtract 1 from each side to remove quotes for (unsigned short t = sliceStart+1; t < sliceEnd; t++) { trimmedValue += value[t]; } } attrs[trimmedName] = trimmedValue; } } return attrs; } std::unique_ptr
parserdocument(std::string file) { std::ifstream inputFile(file); if (!inputFile.is_open()) { std::cerr << "Unable to open: " << file << std::endl; return nullptr; } std::unique_ptr
root = std::make_unique
(); root->setTagName("root"); std::string line; Node* currentNode = root.get(); // !ISSUE: test // bool inTag = false; bool escaped = false; bool inQuote = false; char quoteType = '"'; std::vector
tokens; std::string word = ""; while (std::getline(inputFile, line)) { println("{}", line); int ll = line.length(); for (unsigned short i = 0; i < ll; i++) { char current = line[i]; if (current == '>') { tokens.push_back(word); word = ""; inTag = false; } if (inTag) { if ((current == '"' || current == '\'') && !escaped) { if (inQuote && current == quoteType) { inQuote = false; } else if (!inQuote) { inQuote = true; quoteType = current; } } if (current == ' ' && !inQuote) { tokens.push_back(word); word = ""; } else { word += current; } } else if (current != '<' || current != '>') { // Outside tag add to innerText currentNode->setInnerText(currentNode->getInnerText() + current); } if (current == '<') { inTag = true; } escaped = false; if (current == '\\') { //' escaped = true; } if (!inTag && tokens.size() > 0) { // Build the element currentNode = currentNode->createElement(tokens[0]); auto attrs = parseAttributes(tokens); for (auto const& pair : attrs) { std::cout << "Key: " << pair.first << ", Value: " << pair.second << std::endl; } tokens.clear(); word = ""; } } } for (auto t : tokens) { std::cout << t << std::endl; } inputFile.close(); return root; } int main() { parserdocument("./index.html"); return 0; }