attribute.cpp (1697B)
1 #include <algorithm> 2 3 #include "hemplate/attribute.hpp" 4 5 namespace hemplate { 6 7 attributeList::attributeList(std::initializer_list<attribute> list) 8 { 9 for (const auto& attr : list) set(attr.get_name(), attr.get_value()); 10 } 11 12 attributeList::attributeList(attribute attr) // NOLINT 13 { 14 set(attr.get_name(), attr.get_value()); 15 } 16 17 bool attribute::operator!=(const attribute& rhs) const 18 { 19 return !(*this == rhs); 20 } 21 22 bool attribute::operator==(const attribute& rhs) const 23 { 24 return m_name == rhs.m_name && m_value == rhs.m_value; 25 } 26 27 bool attributeList::empty() const 28 { 29 return m_attributes.empty() && m_class.get_value().empty() 30 && m_style.get_value().empty(); 31 } 32 33 void attribute::render(std::ostream& out) const 34 { 35 out << get_name() << "=\"" << get_value() << "\""; 36 } 37 38 attributeList& attributeList::set(const std::string& name) 39 { 40 if (name != "class" && name != "style") m_attributes.emplace_back(name); 41 return *this; 42 } 43 attributeList& attributeList::set(const std::string& name, 44 const std::string& value) 45 { 46 if (name == "class") 47 { 48 if (m_class.get_value().empty()) m_class.set_value(value); 49 else m_class.set_value(m_class.get_value() + " " + value); 50 } 51 else if (name == "style") 52 { 53 if (m_style.get_value().empty()) m_style.set_value(value); 54 else m_style.set_value(m_style.get_value() + "; " + value); 55 } 56 else m_attributes.emplace_back(name, value); 57 58 return *this; 59 } 60 61 void attributeList::render(std::ostream& out) const 62 { 63 if (!m_class.get_value().empty()) out << m_class << ' '; 64 if (!m_style.get_value().empty()) out << m_style << ' '; 65 66 for (const auto& attr : m_attributes) out << attr << ' '; 67 } 68 69 } // namespace hemplate