diff options
Diffstat (limited to 'src/tag.rs')
-rwxr-xr-x[-rw-r--r--] | src/tag.rs | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/tag.rs b/src/tag.rs index 0a97061..43c4e0c 100644..100755 --- a/src/tag.rs +++ b/src/tag.rs @@ -1,4 +1,5 @@ use core::fmt; +use std::{iter::Peekable, str::CharIndices}; use crate::Node; @@ -208,6 +209,8 @@ impl<'a> Iterator for TagIteratorMut<'a> { mod test { use crate::Tag; + use super::peek_skip_while; + #[test] fn tag_finds_boolen_attribute() { let tag = Tag { @@ -240,4 +243,54 @@ mod test { }; assert!(tag.get_attribute("contenteditable").is_some()); } + + // yes, this is a bad name. + #[test] + fn peek_skip_while_works() { + let str = "\t no whitespace"; + let mut chari = str.char_indices().peekable(); + + peek_skip_while(&mut chari, |c| c.is_whitespace()); + + let next_idx = chari.next().unwrap().0; + assert_eq!(&str[next_idx..], "no whitespace"); + } +} + +struct Attribute<'body> { + name: &'body str, + content: Option<&'body str>, + start: usize, + len: usize, +} + +fn find_attribute<'body>(body: &'body str, attribute: &str) -> Option<Attribute<'body>> { + let mut chari = body.char_indices().peekable(); + + 'big: loop { + // skip whitespace + peek_skip_while(&mut chari, |c| c.is_whitespace()); + + let key_start = chari.next().unwrap(); + // find end of key + peek_skip_while(&mut chari, |c| c.is_alphanumeric()); + + let key_after = chari.next().unwrap(); + } + + todo!() +} + +fn peek_skip_while<P>(iter: &mut Peekable<CharIndices>, mut predicate: P) +where + P: FnMut(&char) -> bool, +{ + loop { + match iter.peek() { + Some((_, c)) if predicate(c) => { + iter.next(); + } + _ => break, + } + } } |