1use alloc::borrow::Cow;
2use alloc::vec;
3use alloc::vec::Vec;
4use core::fmt::{Display, Formatter};
5use core::fmt::Debug;
6use core::ops::AddAssign;
7
8use indexmap::{IndexMap, IndexSet};
9
10use crate::values::AttributeValue;
11
12mod edit;
13mod show;
14
15
16#[allow(unused_variables)]
18pub trait Element {
19 fn build(self) -> HtmlElement;
21
22 fn get_tag(&self) -> &str;
24
25 fn set_tag<S>(&mut self, tag: S) -> bool where S: Into<Cow<'static, str>> {
27 false
28 }
29 fn get_attribute(&self, name: &str) -> Option<&AttributeValue> {
31 None
32 }
33 fn mut_attribute(&mut self, name: &str) -> Option<&mut AttributeValue> {
35 None
36 }
37 fn set_attribute<S>(&mut self, name: S, value: AttributeValue) -> bool where S: Into<Cow<'static, str>> {
39 false
40 }
41 fn get_id(&self) -> Option<&str> {
45 match self.get_attribute("id")? {
46 AttributeValue::Constant(s) => { Some(s) }
47 AttributeValue::String(s) => { Some(s) }
48 _ => None
49 }
50 }
51 fn set_id<S>(&mut self, id: S) -> bool where S: Into<Cow<'static, str>> {
53 match id.into() {
54 Cow::Borrowed(s) => {
55 self.set_attribute("id", AttributeValue::Constant(s))
56 }
57 Cow::Owned(s) => {
58 self.set_attribute("id", AttributeValue::String(s))
59 }
60 }
61 }
62 fn get_classes(&self) -> impl Iterator<Item=&str> {
64 [].into_iter()
65 }
66 fn set_classes<I>(&mut self, classes: I) -> bool where I: Iterator<Item=Cow<'static, str>> {
68 false
69 }
70 fn add_class<S>(&mut self, class: S) -> bool where S: Into<Cow<'static, str>> {
72 false
73 }
74 fn remove_class<S>(&mut self, class: &str) -> bool {
76 false
77 }
78 fn get_child(&self, index: usize) -> Option<&HtmlNode> {
80 self.get_children().nth(index)
81 }
82 fn mut_child(&mut self, index: usize) -> Option<&mut HtmlNode> {
84 self.mut_children().nth(index)
85 }
86 fn set_child<T>(&mut self, index: usize, child: T) -> bool where T: Into<HtmlNode> {
88 match self.mut_children().nth(index) {
89 Some(s) => {
90 *s = child.into();
91 true
92 }
93 None => {
94 false
95 }
96 }
97 }
98 fn add_child<T>(&mut self, child: T) -> bool where T: Into<HtmlNode> {
100 false
101 }
102 fn get_children(&self) -> impl Iterator<Item=&HtmlNode> {
104 [].into_iter()
105 }
106 fn mut_children(&mut self) -> impl Iterator<Item=&mut HtmlNode> {
108 [].into_iter()
109 }
110 fn set_children<I>(&mut self, children: I) -> bool where I: Iterator<Item=HtmlNode> {
112 false
113 }
114}
115
116#[derive(Clone, Debug)]
118pub struct HtmlElement {
119 tag: Cow<'static, str>,
120 classes: IndexSet<Cow<'static, str>>,
121 attributes: IndexMap<Cow<'static, str>, AttributeValue>,
122 children: Vec<HtmlNode>,
123}
124
125#[derive(Clone, Debug)]
126pub enum HtmlNode {
127 Text(Cow<'static, str>),
128 Comment(Cow<'static, str>),
129 Standard(HtmlElement),
130}
131