#include #include "Element.h" #include "../proc/document.cpp" #include "../proc/string.cpp" #include "../proc/uuid.cpp" using namespace cjs; void Element::_identify_and_rebase() { try { std::string uuid = this->getUUID(); this->rebase("[data-cjs-uuid='"+uuid+"']"); } catch (std::runtime_error& e) { std::string new_uuid = uuid::v4(); this->setData("cjs-uuid", new_uuid); this->rebase("[data-cjs-uuid='"+new_uuid+"']"); } } Element::Element(std::string selector, int nth_elem) { this->_selector_ref = selector; this->_selector_nth_elem = nth_elem; this->_identify_and_rebase(); } Element::Element(Element& parent, std::string innerHTML, std::string selector, int nth_elem) { this->_selector_ref = selector; this->_selector_nth_elem = nth_elem; parent.append(innerHTML); this->_identify_and_rebase(); } std::string Element::getInnerHTML() { cjs_str* html = doc::get_inner_html_of(this->_selector_ref, this->_selector_nth_elem); std::string return_str = html->to_str(); delete html; return return_str; } std::string Element::getInnerText() { cjs_str* text = doc::get_inner_text_of(this->_selector_ref, this->_selector_nth_elem); std::string return_str = text->to_str(); delete text; return return_str; } std::string Element::getID() { cjs_str* html = doc::get_id_of(this->_selector_ref, this->_selector_nth_elem); std::string return_str = html->to_str(); delete html; return return_str; } std::string Element::getUUID() { return this->getData("cjs-uuid"); } std::string Element::getClassRaw() { cjs_str* html = doc::get_class_of(this->_selector_ref, this->_selector_nth_elem); std::string return_str = html->to_str(); delete html; return return_str; } std::vector Element::getClasses() { cjs_str* html = doc::get_class_of(this->_selector_ref, this->_selector_nth_elem); std::vector result = html->split(); delete html; return result; } bool Element::hasClass(std::string cls) { std::vector classes = this->getClasses(); for ( std::string s : classes ) if ( s == cls ) return true; return false; } std::string Element::getAttribute(std::string attr) { cjs_str* value = doc::_query_select_attribute(attr, this->_selector_ref, this->_selector_nth_elem); std::string return_str = value->to_str(); delete value; return return_str; } std::string Element::getData(std::string key) { return this->getAttribute("data-"+key); } void Element::setInnerHTML(std::string html) { doc::set_inner_html_of(this->_selector_ref, html, this->_selector_nth_elem); } void Element::setInnerText(std::string text) { doc::set_inner_text_of(this->_selector_ref, text, this->_selector_nth_elem); } void Element::setID(std::string id) { doc::set_id_of(this->_selector_ref, id, this->_selector_nth_elem); } void Element::setClassRaw(std::string cls) { doc::set_class_of(this->_selector_ref, cls, this->_selector_nth_elem); } void Element::addClass(std::string cls) { if ( !this->hasClass(cls) ) { std::vector classes = this->getClasses(); classes.push_back(cls); this->setClassRaw(str::join(classes, " ")); } } void Element::removeClass(std::string cls) { if ( this->hasClass(cls) ) { std::vector old_classes = this->getClasses(); std::vector new_classes; for ( std::string maybe_cls : old_classes ) { if ( maybe_cls != cls ) new_classes.push_back(maybe_cls); } this->setClassRaw(str::join(new_classes, " ")); } } void Element::toggleClass(std::string cls) { if ( this->hasClass(cls) ) this->removeClass(cls); else this->addClass(cls); } void Element::setAttribute(std::string attr, std::string value) { doc::_query_set_attribute(attr, value, this->_selector_ref, this->_selector_nth_elem); } void Element::setData(std::string key, std::string value) { this->setAttribute("data-"+key, value); } void Element::append(std::string html) { this->setInnerHTML(this->getInnerHTML()+html); } void Element::rebase(std::string query_selector, int nth_elem) { this->_selector_ref = query_selector; this->_selector_nth_elem = nth_elem; } Element* Element::querySelectChild(std::string selector, int nth_elem) { std::string scope = "[data-cjs-uuid='" + this->getUUID() + "']"; return new Element(scope + " " + selector, nth_elem); }