-
Notifications
You must be signed in to change notification settings - Fork 5
Encode and decode HTML escape codes #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
xeniagda
wants to merge
11
commits into
lomirus:master
Choose a base branch
from
xeniagda:escapes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4a7aca6
Decode HTML escapes in the HTML parser
1ccc2b5
Escape HTML escapes in HTML generation
c3633ed
Test cases for encoding and decoding HTML escapes
bed0cfa
Ensure text inside script and style tags isn't escaped
069d1a1
Add test for html entities inside script and style tags
88dfaef
Add documentation to Element::Text
869abe5
Change docs
e146137
Restructure tests slightly
a3dc874
from_text → from_raw_text
25f1766
period
41c60de
Merge branch 'master' into escapes
lomirus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,4 @@ license = "MIT" | |
| keywords = ["html", "parser", "editor", "dom"] | ||
|
|
||
| [dependencies] | ||
| html-escape = "0.2.13" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| use html_editor::operation::*; | ||
| use html_editor::{parse, Node, Element}; | ||
|
|
||
| #[test] | ||
| fn test_parse() { | ||
| const HTML: &str = r#" | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>I <3 "escaping"</title> | ||
| </head> | ||
| <body> | ||
| <div id="testee" attr="id-with-"quotes"-inside"></div> | ||
| </body> | ||
| </html>"#; | ||
|
|
||
| let html = parse(HTML).unwrap(); | ||
| let title_selector = Selector::from("title"); | ||
|
|
||
| let Some(title) = html.query(&title_selector) else { | ||
| assert!(false, "title selector failed to match"); | ||
| return; | ||
| }; | ||
| assert_eq!(title.name, "title"); | ||
lomirus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| match title.children.get(0) { | ||
| Some(Node::Text(title_content)) => assert_eq!(title_content, "I <3 \"escaping\""), | ||
| _ => assert!(false, "<title> with no text child"), | ||
| } | ||
|
|
||
| let div_selector = Selector::from("#testee"); | ||
|
|
||
| match html.query(&div_selector) { | ||
| Some(div) => { | ||
| assert_eq!( | ||
| div.attrs, | ||
| vec![ | ||
| ("attr".into(), "id-with-\"quotes\"-inside".into()), | ||
| ("id".into(), "testee".into()), | ||
| ]); | ||
| } | ||
| None => assert!(false, "div selector failed to match") | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_generate() { | ||
| let element = Element::new( | ||
| "dummy-tag", | ||
| vec![("attr-1".into(), "attribute containing < and \" and &".into())], | ||
| vec![Node::Text("fake <tag>".into())], | ||
| ); | ||
|
|
||
| let generated = element.html(); | ||
| assert_eq!(generated, r#"<dummy-tag attr-1="attribute containing < and " and &">fake <tag></dummy-tag>"#); | ||
| } | ||
|
|
||
| // Nothing inside script and style tags should be escaped | ||
| #[test] | ||
| fn no_unescapes_in_script_and_style() { | ||
| const HTML: &str = r#" | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <script>let text = "this tag shouldn't be escaped -> <p> hi </p>"</script> | ||
| <style>main:before { content: "fake <b>tag</b>"; }</style> | ||
| </head> | ||
| </html>"#; | ||
|
|
||
| let html = parse(HTML).unwrap(); | ||
|
|
||
| let script_selector = Selector::from("script"); | ||
|
|
||
| let Some(script) = html.query(&script_selector) else { | ||
| assert!(false, "script selector failed to match"); | ||
| unreachable!() | ||
| }; | ||
| assert_eq!(script.name, "script"); | ||
|
|
||
| match script.children.get(0) { | ||
| Some(Node::Text(script_content)) => assert_eq!(script_content, r#"let text = "this tag shouldn't be escaped -> <p> hi </p>""#), | ||
| _ => { | ||
| assert!(false, "script had no text children"); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| let style_selector = Selector::from("style"); | ||
|
|
||
| let Some(style) = html.query(&style_selector) else { | ||
| assert!(false, "Couldn't find style"); | ||
| return; | ||
| }; | ||
|
|
||
| match style.children.get(0) { | ||
| Some(Node::Text(style_content)) => assert_eq!(style_content, r#"main:before { content: "fake <b>tag</b>"; }"#), | ||
| _ => { | ||
| assert!(false, "style had no text children"); | ||
| return; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn no_escapes_in_script_and_style() { | ||
| let element = Element::new( | ||
| "head", | ||
| vec![], | ||
| vec![ | ||
| Node::Element(Element::new( | ||
| "script", | ||
| vec![], | ||
| vec![Node::Text(r#"let text = "this tag shouldn't be escaped -> <p> hi </p>""#.into())], | ||
| )), | ||
| Node::Element(Element::new( | ||
| "style", | ||
| vec![], | ||
| vec![Node::Text(r#"main:before { content: "fake <b>tag</b>"; }"#.into())], | ||
| )), | ||
| ], | ||
| ); | ||
|
|
||
| let generated = element.html(); | ||
| assert_eq!(generated, r#"<head><script>let text = "this tag shouldn't be escaped -> <p> hi </p>"</script><style>main:before { content: "fake <b>tag</b>"; }</style></head>"#); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.