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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
pub use crateParser;
/// Extract html tag from page
/// # Examples
/// ```
/// use tagparser::parse_tags;
///
/// let html = "<p>Test</p><a href='https://github.com/tenqz/'>Test Link 1</a><p>Another Text</p><a href='https://github.com/tenqz/'>Test Link 2</a><p>Another Text</p><a class='test' href='https://github.com/tenqz/'>Test Link 3</a><p>Another Text</p>".to_string();
/// let tag_a = "a".to_string();
/// let tag_p = "p".to_string();
/// let tags_a = parse_tags(html.clone(), tag_a);
/// let tags_p = parse_tags(html, tag_p);
/// assert_eq!(
/// vec![
/// "<a href='https://github.com/tenqz/'>Test Link 1</a>".to_string(),
/// "<a href='https://github.com/tenqz/'>Test Link 2</a>".to_string(),
/// "<a class='test' href='https://github.com/tenqz/'>Test Link 3</a>".to_string()
/// ],
/// tags_a
/// );
/// assert_eq!(
/// vec![
/// "<p>Test</p>".to_string(),
/// "<p>Another Text</p>".to_string(),
/// "<p>Another Text</p>".to_string(),
/// "<p>Another Text</p>".to_string()
/// ],
/// tags_p
/// )
///
/// ```
/// Extract HTML tags with specific attribute from page
///
/// This function allows you to filter HTML tags not only by tag name but also by their attributes.
/// You can search for tags with a specific attribute (like "href" or "class") and optionally
/// filter by the exact value of that attribute.
///
/// # Arguments
///
/// * `html` - HTML content to parse
/// * `tag` - The HTML tag name to search for (e.g., "a", "div", "img")
/// * `attr_name` - The attribute name to filter by (e.g., "href", "class", "id")
/// * `attr_value` - Optional attribute value to filter by:
/// - Pass `None` to find all tags with the specified attribute regardless of value
/// - Pass `Some("value")` to find only tags where the attribute equals "value"
///
/// # Examples
///
/// Basic usage - finding all links with href attribute:
///
/// ```
/// use tagparser::parse_tags_with_attr;
///
/// let html = "<p>Test</p><a href='https://github.com/tenqz/'>Test Link 1</a><p>Another Text</p><a href='https://example.com/'>Test Link 2</a><p>Another Text</p><a class='test' href='https://github.com/tenqz/'>Test Link 3</a><p>Another Text</p>".to_string();
///
/// // Find all 'a' tags with 'href' attribute
/// let tags_with_href = parse_tags_with_attr(html.clone(), "a".to_string(), "href", None);
/// assert_eq!(
/// vec![
/// "<a href='https://github.com/tenqz/'>Test Link 1</a>".to_string(),
/// "<a href='https://example.com/'>Test Link 2</a>".to_string(),
/// "<a class='test' href='https://github.com/tenqz/'>Test Link 3</a>".to_string()
/// ],
/// tags_with_href
/// );
///
/// // Find all 'a' tags with 'class' attribute with value 'test'
/// let tags_with_class_test = parse_tags_with_attr(html.clone(), "a".to_string(), "class", Some("test"));
/// assert_eq!(
/// vec![
/// "<a class='test' href='https://github.com/tenqz/'>Test Link 3</a>".to_string()
/// ],
/// tags_with_class_test
/// );
/// ```
///
/// # Common Use Cases
///
/// 1. Extract all links to a specific domain:
/// ```
/// # use tagparser::parse_tags_with_attr;
/// # let html = "<a href='https://github.com'>Link</a>".to_string();
/// let github_links = parse_tags_with_attr(html, "a".to_string(), "href", Some("https://github.com"));
/// ```
///
/// 2. Find all images with a specific class:
/// ```
/// # use tagparser::parse_tags_with_attr;
/// # let html = "<img class='gallery' src='image.jpg'>".to_string();
/// let gallery_images = parse_tags_with_attr(html, "img".to_string(), "class", Some("gallery"));
/// ```
///
/// 3. Extract all input fields of a form:
/// ```
/// # use tagparser::parse_tags_with_attr;
/// # let html = "<input name='username'><input name='password'>".to_string();
/// let form_inputs = parse_tags_with_attr(html, "input".to_string(), "name", None);
/// ```
/// Extract the text content from inside HTML tags
///
/// This function extracts only the text content between the opening and closing tags,
/// without the tags themselves or any HTML attributes.
///
/// # Arguments
///
/// * `html` - HTML content to parse
/// * `tag` - The HTML tag name to search for (e.g., "a", "p", "div")
///
/// # Returns
///
/// A vector of strings containing the text content of all matching tags
///
/// # Examples
///
/// Basic usage - extracting text from links and paragraphs:
///
/// ```
/// use tagparser::extract_tag_content;
///
/// let html = r#"
/// <a href='https://github.com'>GitHub</a>
/// <p>This is a <strong>paragraph</strong> with text.</p>
/// <a href='https://rust-lang.org'>Rust Language</a>
/// "#.to_string();
///
/// // Extract text from all links
/// let link_texts = extract_tag_content(html.clone(), "a".to_string());
/// assert_eq!(
/// vec!["GitHub", "Rust Language"],
/// link_texts
/// );
///
/// // Extract text from paragraphs (includes nested HTML)
/// let paragraph_texts = extract_tag_content(html.clone(), "p".to_string());
/// assert_eq!(
/// vec!["This is a <strong>paragraph</strong> with text."],
/// paragraph_texts
/// );
/// ```
///
/// # Common Use Cases
///
/// 1. Extract link text without HTML:
/// ```
/// # use tagparser::extract_tag_content;
/// # let html = "<a href='https://example.com'>Visit Example</a>".to_string();
/// let link_texts = extract_tag_content(html, "a".to_string());
/// // Returns: ["Visit Example"]
/// ```
///
/// 2. Extract headings from a page:
/// ```
/// # use tagparser::extract_tag_content;
/// # let html = "<h1>Main Title</h1><h2>Subtitle</h2>".to_string();
/// let headings = extract_tag_content(html, "h1".to_string());
/// // Returns: ["Main Title"]
/// ```
///
/// 3. Extract list items:
/// ```
/// # use tagparser::extract_tag_content;
/// # let html = "<ul><li>Item 1</li><li>Item 2</li></ul>".to_string();
/// let items = extract_tag_content(html, "li".to_string());
/// // Returns: ["Item 1", "Item 2"]
/// ```
/// Extract attribute values from HTML tags
///
/// This function extracts the values of a specified attribute from all matching tags.
///
/// # Arguments
///
/// * `html` - HTML content to parse
/// * `tag` - The HTML tag name to search for (e.g., "a", "img", "div")
/// * `attr_name` - The attribute name to extract values from (e.g., "href", "src", "class")
///
/// # Returns
///
/// A vector of strings containing the attribute values from all matching tags
///
/// # Examples
///
/// Basic usage - extracting href values from links:
///
/// ```
/// use tagparser::extract_attribute_values;
///
/// let html = r#"
/// <a href='https://github.com'>GitHub</a>
/// <a href='https://rust-lang.org' class='official'>Rust</a>
/// <a class='social' href='https://twitter.com'>Twitter</a>
/// "#.to_string();
///
/// // Extract all href values from links
/// let hrefs = extract_attribute_values(html.clone(), "a".to_string(), "href");
/// assert_eq!(
/// vec!["https://github.com", "https://rust-lang.org", "https://twitter.com"],
/// hrefs
/// );
///
/// // Extract all class values from links
/// let classes = extract_attribute_values(html.clone(), "a".to_string(), "class");
/// assert_eq!(
/// vec!["official", "social"],
/// classes
/// );
/// ```
///
/// # Common Use Cases
///
/// 1. Extract all URLs from a page:
/// ```
/// # use tagparser::extract_attribute_values;
/// # let html = "<a href='https://example.com'>Link</a>".to_string();
/// let urls = extract_attribute_values(html, "a".to_string(), "href");
/// // Returns: ["https://example.com"]
/// ```
///
/// 2. Extract all image sources:
/// ```
/// # use tagparser::extract_attribute_values;
/// # let html = "<img src='image.jpg' alt='Image'><img src='logo.png'>".to_string();
/// let image_sources = extract_attribute_values(html, "img".to_string(), "src");
/// // Returns: ["image.jpg", "logo.png"]
/// ```
///
/// 3. Extract all form input names:
/// ```
/// # use tagparser::extract_attribute_values;
/// # let html = "<input name='username'><input name='password'>".to_string();
/// let input_names = extract_attribute_values(html, "input".to_string(), "name");
/// // Returns: ["username", "password"]
/// ```