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