Skip to content

Commit

Permalink
feat: add html symbols escape
Browse files Browse the repository at this point in the history
  • Loading branch information
JarKz authored and jsonmaf1a committed Aug 7, 2024
1 parent 71da339 commit f94b236
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 11 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ toml = "0.8.14"
zbus = "4.3.0"
itertools = "0.13.0"
ab_glyph = "0.2.28"
html-escape = "0.2.13"
53 changes: 42 additions & 11 deletions src/data/text.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, iter::Peekable, str::Chars};
use std::{borrow::Cow, collections::HashMap, iter::Peekable, str::Chars};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Text {
Expand All @@ -10,31 +10,28 @@ pub struct Text {

impl Text {
pub fn parse(input: String) -> Self {
let parser = Parser::new(input);
let escaped_string = html_escape::decode_html_entities(&input);
let parser = Parser::new(&escaped_string);
parser.parse()
}
}

struct Parser {
input: String,
struct Parser<'a> {
body: String,
entities: Vec<Entity>,
stack: Vec<(Tag, usize)>,
pos: usize,
chars: Peekable<Chars<'static>>,
chars: Peekable<Chars<'a>>,
}

impl Parser {
fn new(input: String) -> Self {
let input_static: &'static str = Box::leak(input.into_boxed_str());

impl<'a> Parser<'a> {
fn new(input: &'a Cow<str>) -> Self {
Self {
input: input_static.to_string(),
body: String::new(),
entities: Vec::new(),
stack: Vec::new(),
pos: 0,
chars: input_static.chars().peekable(),
chars: input.chars().peekable(),
}
}

Expand Down Expand Up @@ -474,4 +471,38 @@ mod tests {
}
)
}

#[test]
fn text_with_html_symbol() {
let input = String::from("<b>hello&quot;</b>");
let text = Text::parse(input);
assert_eq!(
text,
Text {
body: String::from("hello\""),
entities: vec![Entity {
offset: 0,
length: 6,
kind: EntityKind::Bold
}]
}
)
}

#[test]
fn text_with_chained_html_symbols() {
let input = String::from("<b>hello&amp;quot;</b>");
let text = Text::parse(input);
assert_eq!(
text,
Text {
body: String::from("hello&quot;"),
entities: vec![Entity {
offset: 0,
length: 11,
kind: EntityKind::Bold
}]
}
)
}
}

0 comments on commit f94b236

Please sign in to comment.