1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use crate::{
element::{Element, HtmlElement, HtmlElementConfig},
errors::HeadCreationError,
miscellaneous::{BaseUrlTarget, ScriptLoadMode},
tags::TagType,
};
/// Defines the html document's `<head>` tag content.
pub struct Head {
pub content: Vec<Element>,
}
impl Head {
/// Creates a blank `<head>` tag.
pub fn new() -> Self {
Self::default()
}
/// Adds an Element to the head's content.
/// If the given element is not compatible with the `<head>` html tag, the Err variant is returned.
///
/// - `element` : The element to add.
pub fn with_element(mut self, element: Element) -> Result<Self, HeadCreationError> {
if element.is_allowed_in_head() {
self.content.push(element);
Ok(self)
} else {
Err(HeadCreationError::InvalidContent(element))
}
}
/// Adds a title to the html page
///
/// - `title` : The title's content.
pub fn with_title(mut self, title: String) -> Self {
self.content.push(
Element::Element(HtmlElement::new(
TagType::Title,
HtmlElementConfig::new_empty(),
)) + Element::Text(title),
);
self
}
/// Adds a style tag to the html page.
///
/// - `content` : The css code.
pub fn with_style(mut self, content: String) -> Self {
self.content.push(
Element::Element(HtmlElement::new(
TagType::Style,
HtmlElementConfig::new_empty(),
)) + Element::Text(content),
);
self
}
/// Adds a base tag to the html page.
///
/// - `url` : The base url.
/// - `target` : The optional base url's target. If the value is None, this attribute is ignored.
pub fn with_base_url(mut self, url: String, target: Option<BaseUrlTarget>) -> Self {
let mut configs =
HtmlElementConfig::new_empty().set_attribute("href".to_string(), Some(url));
if let Some(target) = target {
configs = configs.set_attribute("target".to_string(), Some(target.to_string()))
}
self.content
.push(Element::Element(HtmlElement::new(TagType::Base, configs)));
self
}
/// Adds an ico icon to the page.
///
/// - `href` : The icon's path.
pub fn with_ico_icon(mut self, href: String) -> Self {
self.content.push(Element::Element(HtmlElement::new(
TagType::Link,
HtmlElementConfig::new_empty()
.set_attribute("rel".to_string(), Some("icon".to_string()))
.set_attribute("type".to_string(), Some("image/x-icon".to_string()))
.set_attribute("href".to_string(), Some(href)),
)));
self
}
/// Adds a css file to the page.
///
/// - `href` : The file's path.
pub fn with_css_file(mut self, href: String) -> Self {
self.content.push(Element::Element(HtmlElement::new(
TagType::Link,
HtmlElementConfig::new_empty()
.set_attribute("rel".to_string(), Some("stylesheet".to_string()))
.set_attribute("href".to_string(), Some(href)),
)));
self
}
/// Adds the `charset` meta tag to the html page.
///
/// - `charset` : The charset.
pub fn with_charset_meta(mut self, charset: String) -> Self {
self.content.push(Element::Element(HtmlElement::new(
TagType::Meta,
HtmlElementConfig::new_empty().set_attribute("charset".to_string(), Some(charset)),
)));
self
}
/// Adds the meta tag to the html page.
///
/// - `name` : The meta's name.
/// - `content` : The meta's content.
pub fn with_meta(mut self, name: String, content: String) -> Self {
self.content.push(Element::Element(HtmlElement::new(
TagType::Meta,
HtmlElementConfig::new_empty()
.set_attribute("name".to_string(), Some(name))
.set_attribute("content".to_string(), Some(content)),
)));
self
}
/// Adds a script tag to the html page with raw javascript.
///
/// - `content` : The script raw javascript content.
/// - `loading_mode` : The optional script loading mode. If the value is None, this attribute is ignored.
pub fn with_raw_javascript(
mut self,
content: String,
loading_mode: Option<ScriptLoadMode>,
) -> Self {
let mut configs = HtmlElementConfig::new_empty();
if let Some(loading_mode) = loading_mode {
configs = configs.set_attribute(loading_mode.to_string(), None);
}
self.content.push(
Element::Element(HtmlElement::new(TagType::Script, configs)) + Element::Text(content),
);
self
}
/// Adds a javascript file to the html page.
///
/// - `src` : The file's path.
/// - `loading_mode` : The optional script loading mode. If the value is None, this attribute is ignored.
pub fn with_javascript_file(
mut self,
src: String,
loading_mode: Option<ScriptLoadMode>,
) -> Self {
let mut configs =
HtmlElementConfig::new_empty().set_attribute("src".to_string(), Some(src));
if let Some(loading_mode) = loading_mode {
configs = configs.set_attribute(loading_mode.to_string(), None);
}
self.content
.push(Element::Element(HtmlElement::new(TagType::Script, configs)));
self
}
/// Adds a no script tag to the html page.
///
/// - `content` : The no script's content.
pub fn with_no_script(mut self, content: String) -> Self {
self.content.push(
Element::Element(HtmlElement::new(
TagType::NoScript,
HtmlElementConfig::new_empty(),
)) + Element::Text(content),
);
self
}
}
impl Default for Head {
fn default() -> Self {
Self {
content: Vec::new(),
}
}
}