Repository navigation
fix(html2md): preserve media resources and readable controls #1334
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
+1,356
−223
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
26a4f14
fix(html2md): preserve media resources and readable controls
lanyue-llk 3dcd99e
test(fetch): bind media semantics and freeze response metadata
lanyue-llk f30771a
fix(html2md): avoid inferring content from private names
lanyue-llk 33e302b
fix(html2md): distinguish live textarea values from source attributes
lanyue-llk f25b5b2
fix(html2md): base accessible fallback on emitted child content
lanyue-llk 926bb3b
fix(html2md): keep inline media flow and accessible alternatives
lanyue-llk 565963a
fix(html2md): preserve resource fallbacks and distinguish empty fields
lanyue-llk 139b801
fix(html2md): ignore whitespace-only video posters
lanyue-llk efd15b6
fix(renderer): preserve valid media alternatives and srcset candidates
lanyue-llk 0ec6219
refactor(html2md): simplify private naming exclusion predicate
lanyue-llk 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
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,102 @@ | ||
| use crate::{Dom, NodeKind}; | ||
| use std::borrow::Cow; | ||
|
|
||
| pub(crate) fn fallback_text<D: Dom + ?Sized>(dom: &D, node: D::NodeId) -> Option<String> { | ||
| if excluded_from_naming(dom, node) { | ||
| return None; | ||
| } | ||
| let named_tag = matches!( | ||
| dom.node_kind(node), | ||
| NodeKind::Element( | ||
| "a" | "button" | ||
| | "svg" | ||
| | "textarea" | ||
| | "select" | ||
| | "option" | ||
| | "optgroup" | ||
| | "h1" | ||
| | "h2" | ||
| | "h3" | ||
| | "h4" | ||
| | "h5" | ||
| | "h6" | ||
| ) | ||
| ); | ||
| let named_role = | ||
| primary_role(dom, node).is_some_and(|role| role_matches(role, NAMED_WIDGET_ROLES)); | ||
| if !named_tag && !named_role { | ||
| return None; | ||
| } | ||
| for name in ["aria-label", "title"] { | ||
| if let Some(value) = dom | ||
| .attribute(node, name) | ||
| .map(str::trim) | ||
| .filter(|value| !value.is_empty()) | ||
| { | ||
| return Some(value.to_owned()); | ||
| } | ||
| } | ||
| if matches!(dom.node_kind(node), NodeKind::Element("svg")) { | ||
| let mut child = dom.first_child(node); | ||
| while let Some(id) = child { | ||
| if dom.node_kind(id) == NodeKind::Element("title") { | ||
| let mut text = String::new(); | ||
| let mut title_child = dom.first_child(id); | ||
| while let Some(part) = title_child { | ||
| if let NodeKind::Text(value) = dom.node_kind(part) { | ||
| text.push_str(value); | ||
| } | ||
| title_child = dom.next_sibling(part); | ||
| } | ||
| if !text.trim().is_empty() { | ||
| return Some(text); | ||
| } | ||
| } | ||
| child = dom.next_sibling(id); | ||
| } | ||
| } | ||
| None | ||
| } | ||
|
|
||
| const PRESENTATIONAL_ROLES: &[&str] = &["none", "presentation"]; | ||
| const NAMED_WIDGET_ROLES: &[&str] = &[ | ||
| "button", "link", "img", "heading", "checkbox", "radio", "textbox", "combobox", "switch", "tab", | ||
| ]; | ||
|
|
||
| // This structural converter uses the first authored token as a conservative | ||
| // naming hint; it does not implement an accessibility-tree role resolver. | ||
| fn primary_role<D: Dom + ?Sized>(dom: &D, node: D::NodeId) -> Option<&str> { | ||
| dom.attribute(node, "role")?.split_ascii_whitespace().next() | ||
| } | ||
| fn role_matches(role: &str, candidates: &[&str]) -> bool { | ||
| candidates | ||
| .iter() | ||
| .any(|candidate| role.eq_ignore_ascii_case(candidate)) | ||
| } | ||
|
|
||
| fn excluded_from_naming<D: Dom + ?Sized>(dom: &D, node: D::NodeId) -> bool { | ||
| dom.attribute(node, "aria-hidden") | ||
| .is_some_and(|v| v.trim().eq_ignore_ascii_case("true")) | ||
| || dom.attribute(node, "hidden").is_some() | ||
| || primary_role(dom, node).is_some_and(|role| role_matches(role, PRESENTATIONAL_ROLES)) | ||
| } | ||
|
|
||
| pub(crate) fn image_label<D: Dom + ?Sized>(dom: &D, node: D::NodeId) -> Cow<'_, str> { | ||
| let alt = dom.attribute(node, "alt"); | ||
| if let Some(alt) = alt.filter(|alt| !alt.is_empty()) { | ||
| return Cow::Borrowed(alt); | ||
| } | ||
| if excluded_from_naming(dom, node) { | ||
| return Cow::Borrowed(""); | ||
| } | ||
| ["aria-label", "title"] | ||
| .into_iter() | ||
| .filter(|name| *name != "title" || alt.is_none()) | ||
| .find_map(|name| { | ||
| dom.attribute(node, name) | ||
| .map(str::trim) | ||
| .filter(|v| !v.is_empty()) | ||
| }) | ||
| .map(Cow::Borrowed) | ||
| .unwrap_or(Cow::Borrowed("")) | ||
| } | ||
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,56 @@ | ||
| use std::borrow::Cow; | ||
|
|
||
| use crate::Dom; | ||
|
|
||
| pub(crate) fn input_text<'a, D: Dom + ?Sized>(dom: &'a D, node: D::NodeId) -> Option<Cow<'a, str>> { | ||
| let kind = dom.attribute(node, "type").unwrap_or("text"); | ||
| if kind.eq_ignore_ascii_case("checkbox") || kind.eq_ignore_ascii_case("radio") { | ||
| return Some(Cow::Borrowed(if dom.attribute(node, "checked").is_some() { | ||
|
lanyue-llk marked this conversation as resolved.
|
||
| "☑" | ||
| } else { | ||
| "☐" | ||
| })); | ||
| } | ||
| if ["hidden", "file"] | ||
| .iter() | ||
| .any(|excluded| kind.eq_ignore_ascii_case(excluded)) | ||
| { | ||
| return None; | ||
| } | ||
| if kind.eq_ignore_ascii_case("image") { | ||
| return dom | ||
| .attribute(node, "alt") | ||
| .filter(|value| !value.is_empty()) | ||
| .map(Cow::Borrowed); | ||
| } | ||
| let button = ["submit", "button", "reset"] | ||
| .iter() | ||
| .any(|v| kind.eq_ignore_ascii_case(v)); | ||
| let value = (!kind.eq_ignore_ascii_case("password")) | ||
| .then(|| { | ||
| dom.control_value(node) | ||
| .or_else(|| dom.attribute(node, "value")) | ||
| }) | ||
| .flatten() | ||
| .filter(|value| !value.is_empty()) | ||
| .or_else(|| { | ||
| button | ||
| .then(|| dom.attribute(node, "aria-label")) | ||
| .flatten() | ||
| .filter(|value| !value.is_empty()) | ||
| }) | ||
| .or_else(|| { | ||
| button | ||
| .then(|| dom.attribute(node, "title")) | ||
| .flatten() | ||
| .filter(|value| !value.is_empty()) | ||
| }); | ||
| value.map(Cow::Borrowed) | ||
| } | ||
|
|
||
| pub(crate) fn textarea_text<'a, D: Dom + ?Sized>( | ||
| dom: &'a D, | ||
| node: D::NodeId, | ||
| ) -> Option<Cow<'a, str>> { | ||
| dom.control_value(node).map(Cow::Borrowed) | ||
| } | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.