Repository navigation
fix(fetch): preserve rendered document content #919
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
Closed
Closed
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
934d501
fix(fetch): preserve rendered document content
lanyue-llk 92a76a7
fix(renderer): retain content behind loading veils
lanyue-llk eabea47
test(fetch): align markdown regressions with preserved semantics
lanyue-llk be2ff8d
fix(fetch): preserve interactive document semantics
lanyue-llk 35178ba
fix(fetch): close content audit regressions
lanyue-llk 7450ce0
fix(html2md): keep preformatted blocks as markdown code
lanyue-llk 9ab29d3
fix(html2md): expose table code as markdown
lanyue-llk 1486f8c
fix(content): preserve semantic pre and visibility relationships
lanyue-llk 3303fad
fix(content): preserve rendered semantics across fallbacks
lanyue-llk 766f247
fix(content): preserve completed loading transitions
lanyue-llk f1a9055
fix(content): preserve semantic streams and disclosure boundaries
lanyue-llk d69da75
fix(content): preserve proven interactive disclosures
lanyue-llk 0ac34b9
fix(renderer): retain bounded fade-in content
lanyue-llk 3be2093
fix(content): close semantic boundary regressions
lanyue-llk 7b35185
fix(content): honor effective style and disclosure facts
lanyue-llk f93a23a
fix(content): trust scoped state over handler shape
lanyue-llk c5efa39
fix(content): consume resolved style semantics
lanyue-llk 8c01910
chore(deps): retain regenerated lock resolution
lanyue-llk c078c4a
fix(content): preserve animation list semantics
lanyue-llk ee104df
fix(content): compose final animation effects
lanyue-llk 4f556a6
chore(deps): restore content style support
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
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
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,178 @@ | ||
| use std::collections::HashSet; | ||
|
|
||
| use crate::{Dom, NodeKind}; | ||
|
|
||
| pub(crate) struct Targets { | ||
| referenced: HashSet<String>, | ||
| ids: HashSet<String>, | ||
| } | ||
|
|
||
| impl Targets { | ||
| pub(crate) fn is_empty(&self) -> bool { | ||
| self.referenced.is_empty() | ||
| } | ||
|
|
||
| pub(crate) fn contains(&self, value: &str) -> bool { | ||
| self.referenced.contains(value) | ||
| } | ||
|
|
||
| pub(crate) fn targets<'a, D: Dom + ?Sized>(&self, dom: &'a D, node: D::NodeId) -> Vec<&'a str> { | ||
| let mut targets = Vec::with_capacity(2); | ||
| if let Some(id) = dom.attribute(node, "id").filter(|id| self.contains(id)) { | ||
| targets.push(id); | ||
| } | ||
| if dom.node_kind(node) == NodeKind::Element("a") | ||
| && let Some(name) = dom | ||
| .attribute(node, "name") | ||
| .filter(|name| self.contains(name) && !self.ids.contains(*name)) | ||
| && !targets.contains(&name) | ||
| { | ||
| targets.push(name); | ||
| } | ||
| targets | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn referenced<D: Dom + ?Sized>(dom: &D, root: D::NodeId, limit: usize) -> Targets { | ||
| let mut available = HashSet::new(); | ||
| let mut element_ids = HashSet::new(); | ||
| if !dom.may_have_fragment_links() { | ||
| return Targets { | ||
| referenced: available, | ||
| ids: element_ids, | ||
| }; | ||
| } | ||
| walk(dom, root, limit, |node| { | ||
| for attribute in ["id", "name"] { | ||
| if attribute == "name" && dom.node_kind(node) != NodeKind::Element("a") { | ||
| continue; | ||
| } | ||
| if let Some(value) = dom.attribute(node, attribute) { | ||
| available.insert(value.to_owned()); | ||
| if attribute == "id" { | ||
| element_ids.insert(value.to_owned()); | ||
| } | ||
| } | ||
| } | ||
| false | ||
| }); | ||
| let mut referenced = HashSet::new(); | ||
| walk(dom, root, limit, |node| { | ||
| if let Some(fragment) = dom | ||
| .attribute(node, "href") | ||
| .and_then(|href| href.strip_prefix('#')) | ||
| && !fragment.is_empty() | ||
| { | ||
| // HTML fragment navigation tries the literal fragment first and | ||
| // only percent-decodes it when no literal target exists. | ||
| if available.contains(fragment) { | ||
| referenced.insert(fragment.to_owned()); | ||
| } else { | ||
| let decoded = decode_fragment(fragment); | ||
| if available.contains(&decoded) { | ||
| referenced.insert(decoded); | ||
| } | ||
| } | ||
| } | ||
| false | ||
| }); | ||
| Targets { | ||
| referenced, | ||
| ids: element_ids, | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn contains<D: Dom + ?Sized>( | ||
| dom: &D, | ||
| root: D::NodeId, | ||
| limit: usize, | ||
| ids: &Targets, | ||
| ) -> bool { | ||
| walk(dom, root, limit, |node| !ids.targets(dom, node).is_empty()) | ||
| } | ||
|
|
||
| pub(crate) fn within<D: Dom + ?Sized>( | ||
| dom: &D, | ||
| root: D::NodeId, | ||
| limit: usize, | ||
| ids: &Targets, | ||
| ) -> Vec<String> { | ||
| let mut found = Vec::new(); | ||
| walk(dom, root, limit, |node| { | ||
| for id in ids.targets(dom, node) { | ||
| if !found.iter().any(|existing| existing == id) { | ||
| found.push(id.to_owned()); | ||
| } | ||
| } | ||
| false | ||
| }); | ||
| found | ||
| } | ||
|
|
||
| fn walk<D: Dom + ?Sized>( | ||
| dom: &D, | ||
| root: D::NodeId, | ||
| limit: usize, | ||
| mut found: impl FnMut(D::NodeId) -> bool, | ||
| ) -> bool { | ||
| let mut stack = vec![(Some(root), 0, false)]; | ||
| while let Some((node, depth, siblings)) = stack.pop() { | ||
| let Some(node) = node else { | ||
| continue; | ||
| }; | ||
| if siblings { | ||
| stack.push((dom.next_sibling(node), depth, true)); | ||
| } | ||
| if depth >= limit { | ||
| continue; | ||
| } | ||
| if matches!( | ||
| dom.node_kind(node), | ||
| NodeKind::Other | ||
| | NodeKind::Element("head" | "script" | "style" | "noscript" | "template") | ||
| ) { | ||
| continue; | ||
| } | ||
| if found(node) { | ||
| return true; | ||
| } | ||
| stack.push((dom.first_child(node), depth + 1, true)); | ||
| } | ||
| false | ||
| } | ||
|
|
||
| pub(crate) fn decode_fragment(fragment: &str) -> String { | ||
| let bytes = fragment.as_bytes(); | ||
| let mut decoded = Vec::with_capacity(bytes.len()); | ||
| let mut offset = 0; | ||
| while offset < bytes.len() { | ||
| if bytes[offset] == b'%' && offset + 2 < bytes.len() { | ||
| let hex = |byte: u8| (byte as char).to_digit(16); | ||
| if let (Some(high), Some(low)) = (hex(bytes[offset + 1]), hex(bytes[offset + 2])) { | ||
| decoded.push((high * 16 + low) as u8); | ||
| offset += 3; | ||
| continue; | ||
| } | ||
| } | ||
| decoded.push(bytes[offset]); | ||
| offset += 1; | ||
| } | ||
| String::from_utf8(decoded).unwrap_or_else(|_| fragment.to_owned()) | ||
| } | ||
|
|
||
| pub(crate) fn markup(id: &str) -> String { | ||
| let mut result = String::from("<a id=\""); | ||
| for character in id.chars() { | ||
| match character { | ||
| '&' => result.push_str("&"), | ||
| '"' => result.push_str("""), | ||
| '<' => result.push_str("<"), | ||
| '>' => result.push_str(">"), | ||
| '\n' => result.push_str(" "), | ||
| '\r' => result.push_str(" "), | ||
| character => result.push(character), | ||
| } | ||
| } | ||
| result.push_str("\"></a>"); | ||
| result | ||
| } | ||
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.