-
Notifications
You must be signed in to change notification settings - Fork 998
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
Add heuristic checking for HTML anchors #1716
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ include = ["src/**/*"] | |
tera = "1" | ||
unicode-segmentation = "1.2" | ||
walkdir = "2" | ||
regex="1" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: space |
||
toml = "0.5" | ||
serde = { version = "1.0", features = ["derive"] } | ||
slug = "0.1" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod de; | ||
pub mod fs; | ||
pub mod links; | ||
pub mod minify; | ||
pub mod net; | ||
pub mod site; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use regex::Regex; | ||
|
||
|
||
pub fn anchor_id_checks(anchor:&str) -> Regex { | ||
Regex::new( | ||
&format!(r#" (?i)(id|ID|name|NAME) *= *("|')*{}("|'| |>)+"#, anchor) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we're setting the |
||
).unwrap() | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests{ | ||
use super::anchor_id_checks; | ||
|
||
fn check(anchor:&str, content:&str) -> bool { | ||
anchor_id_checks(anchor).is_match(content) | ||
} | ||
|
||
#[test] | ||
fn matchers () { | ||
let m = |content| {check("fred", content)}; | ||
|
||
// Canonical match/non match | ||
assert!(m(r#"<a name="fred">"#)); | ||
assert!(m(r#"<a id="fred">"#)); | ||
assert!(!m(r#"<a name="george">"#)); | ||
|
||
// Whitespace variants | ||
assert!(m(r#"<a id ="fred">"#)); | ||
assert!(m(r#"<a id = "fred">"#)); | ||
assert!(m(r#"<a id="fred" >"#)); | ||
assert!(m(r#"<a id="fred" >"#)); | ||
|
||
// Quote variants | ||
assert!(m(r#"<a id='fred'>"#)); | ||
assert!(m(r#"<a id=fred>"#)); | ||
|
||
// Case variants | ||
assert!(m(r#"<a ID="fred">"#)); | ||
assert!(m(r#"<a iD="fred">"#)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add some other tags, so readers can easily understand, that the code also works with any element with id? |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would probably move this function to utils directly since we will only ever do
is_match