|
| 1 | +use nom::{ |
| 2 | + branch::alt, |
| 3 | + bytes::complete::{is_not, tag}, |
| 4 | + character::complete::{alphanumeric1, multispace0}, |
| 5 | + combinator::recognize, |
| 6 | + multi::many1_count, |
| 7 | + sequence::{delimited, pair}, |
| 8 | + IResult, |
| 9 | +}; |
| 10 | + |
| 11 | +#[derive(Debug, PartialEq, Eq, Hash)] |
| 12 | +#[non_exhaustive] |
| 13 | +pub enum InlineHTMLTagType { |
| 14 | + Opening(String), |
| 15 | + //SelfClosing, |
| 16 | + Closing(String), |
| 17 | +} |
| 18 | + |
| 19 | +fn parse_html_tag_content(line: &str) -> IResult<&str, (&str, &str)> { |
| 20 | + let (remainder, tag_content) = is_not(">/")(line)?; |
| 21 | + let (attributes, (tag_name, _space)) = pair( |
| 22 | + recognize(many1_count(alt((alphanumeric1, tag("-"))))), |
| 23 | + multispace0, |
| 24 | + )(tag_content)?; |
| 25 | + Ok((remainder, (tag_name, attributes))) |
| 26 | +} |
| 27 | + |
| 28 | +fn parse_closing_html_tag(line: &str) -> IResult<&str, (&str, &str, InlineHTMLTagType)> { |
| 29 | + let (remaining_line, (tag_name, tag_attributes)) = |
| 30 | + delimited(tag("</"), parse_html_tag_content, tag(">"))(line)?; |
| 31 | + Ok(( |
| 32 | + remaining_line, |
| 33 | + ( |
| 34 | + tag_name, |
| 35 | + tag_attributes, |
| 36 | + InlineHTMLTagType::Closing(tag_name.into()), |
| 37 | + ), |
| 38 | + )) |
| 39 | +} |
| 40 | + |
| 41 | +fn parse_opening_html_tag(line: &str) -> IResult<&str, (&str, &str, InlineHTMLTagType)> { |
| 42 | + let (remaining_line, (tag_name, tag_attributes)) = |
| 43 | + delimited(tag("<"), parse_html_tag_content, tag(">"))(line)?; |
| 44 | + Ok(( |
| 45 | + remaining_line, |
| 46 | + ( |
| 47 | + tag_name, |
| 48 | + tag_attributes, |
| 49 | + InlineHTMLTagType::Opening(tag_name.into()), |
| 50 | + ), |
| 51 | + )) |
| 52 | +} |
| 53 | + |
| 54 | +pub fn parse_node(html_node: &str) -> Option<InlineHTMLTagType> { |
| 55 | + match alt((parse_opening_html_tag, parse_closing_html_tag))(html_node) { |
| 56 | + Ok((_, (_, _, tag_type))) => Some(tag_type), |
| 57 | + Err(_) => None, |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(test)] |
| 62 | +mod tests { |
| 63 | + use super::{ |
| 64 | + parse_closing_html_tag, parse_html_tag_content, parse_node, parse_opening_html_tag, |
| 65 | + InlineHTMLTagType, |
| 66 | + }; |
| 67 | + |
| 68 | + #[test] |
| 69 | + pub fn parse_html_tag_content_parses_valid_html_tag_without_attributes() { |
| 70 | + // arrange |
| 71 | + let tag_content = "abbr"; |
| 72 | + |
| 73 | + // act |
| 74 | + let result = parse_html_tag_content(tag_content); |
| 75 | + |
| 76 | + // assert |
| 77 | + assert_eq!(result, Ok(("", ("abbr", "")))); |
| 78 | + } |
| 79 | + |
| 80 | + #[test] |
| 81 | + pub fn parse_html_tag_content_parses_valid_html_tag_with_attributes() { |
| 82 | + // arrange |
| 83 | + let tag_content = r#"tool-tip inert role="tooltip""#; |
| 84 | + |
| 85 | + // act |
| 86 | + let result = parse_html_tag_content(tag_content); |
| 87 | + |
| 88 | + // assert |
| 89 | + assert_eq!(result, Ok(("", ("tool-tip", r#"inert role="tooltip""#)))); |
| 90 | + } |
| 91 | + |
| 92 | + #[test] |
| 93 | + pub fn parse_opening_html_tag_parses_valid_html_tag_without_attributes() { |
| 94 | + // arrange |
| 95 | + let tag = "<abbr>"; |
| 96 | + |
| 97 | + // act |
| 98 | + let result = parse_opening_html_tag(tag); |
| 99 | + |
| 100 | + // assert |
| 101 | + assert_eq!( |
| 102 | + result, |
| 103 | + Ok(( |
| 104 | + "", |
| 105 | + ("abbr", "", InlineHTMLTagType::Opening(String::from("abbr"))) |
| 106 | + )) |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + #[test] |
| 111 | + pub fn parse_opening_html_tag_parses_valid_html_tag_with_attributes() { |
| 112 | + // arrange |
| 113 | + let tag = r#"<tool-tip inert role="tooltip">"#; |
| 114 | + |
| 115 | + // act |
| 116 | + let result = parse_opening_html_tag(tag); |
| 117 | + |
| 118 | + // assert |
| 119 | + assert_eq!( |
| 120 | + result, |
| 121 | + Ok(( |
| 122 | + "", |
| 123 | + ( |
| 124 | + "tool-tip", |
| 125 | + r#"inert role="tooltip""#, |
| 126 | + InlineHTMLTagType::Opening(String::from("tool-tip")) |
| 127 | + ) |
| 128 | + )) |
| 129 | + ); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + pub fn parse_closing_html_tag_parses_valid_html_tag() { |
| 134 | + // arrange |
| 135 | + let tag = "</tool-tip>"; |
| 136 | + |
| 137 | + // act |
| 138 | + let result = parse_closing_html_tag(tag); |
| 139 | + |
| 140 | + // assert |
| 141 | + assert_eq!( |
| 142 | + result, |
| 143 | + Ok(( |
| 144 | + "", |
| 145 | + ( |
| 146 | + "tool-tip", |
| 147 | + "", |
| 148 | + InlineHTMLTagType::Closing(String::from("tool-tip")) |
| 149 | + ) |
| 150 | + )) |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + #[test] |
| 155 | + pub fn parse_node_parses_valid_opening_html_tag() { |
| 156 | + // arrange |
| 157 | + let tag = "</tool-tip>"; |
| 158 | + |
| 159 | + // act |
| 160 | + let result = parse_node(tag); |
| 161 | + |
| 162 | + // assert |
| 163 | + assert_eq!( |
| 164 | + result, |
| 165 | + Some(InlineHTMLTagType::Closing(String::from("tool-tip"))) |
| 166 | + ); |
| 167 | + } |
| 168 | + |
| 169 | + #[test] |
| 170 | + pub fn parse_node_parses_valid_closing_html_tag() { |
| 171 | + // arrange |
| 172 | + let tag = r#"<tool-tip inert role="tooltip">"#; |
| 173 | + |
| 174 | + // act |
| 175 | + let result = parse_node(tag); |
| 176 | + |
| 177 | + // assert |
| 178 | + assert_eq!( |
| 179 | + result, |
| 180 | + Some(InlineHTMLTagType::Opening(String::from("tool-tip"))) |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + #[test] |
| 185 | + pub fn parse_node_returns_none_for_invalid_html_tag() { |
| 186 | + // arrange |
| 187 | + let tag = "<tool-tip"; |
| 188 | + |
| 189 | + // act |
| 190 | + let result = parse_node(tag); |
| 191 | + |
| 192 | + // assert |
| 193 | + assert_eq!(result, None); |
| 194 | + } |
| 195 | +} |
0 commit comments