mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Fix introspection of HTML root sibling metadata (#5953)
This commit is contained in:
parent
36d83c8c09
commit
225e845021
@ -83,8 +83,8 @@ fn html_document_impl(
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
let output = handle_list(&mut engine, &mut locator, children.iter().copied())?;
|
let output = handle_list(&mut engine, &mut locator, children.iter().copied())?;
|
||||||
|
let introspector = Introspector::html(&output);
|
||||||
let root = root_element(output, &info)?;
|
let root = root_element(output, &info)?;
|
||||||
let introspector = Introspector::html(&root);
|
|
||||||
|
|
||||||
Ok(HtmlDocument { info, root, introspector })
|
Ok(HtmlDocument { info, root, introspector })
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ use typst_utils::NonZeroExt;
|
|||||||
|
|
||||||
use crate::diag::{bail, StrResult};
|
use crate::diag::{bail, StrResult};
|
||||||
use crate::foundations::{Content, Label, Repr, Selector};
|
use crate::foundations::{Content, Label, Repr, Selector};
|
||||||
use crate::html::{HtmlElement, HtmlNode};
|
use crate::html::HtmlNode;
|
||||||
use crate::introspection::{Location, Tag};
|
use crate::introspection::{Location, Tag};
|
||||||
use crate::layout::{Frame, FrameItem, Page, Point, Position, Transform};
|
use crate::layout::{Frame, FrameItem, Page, Point, Position, Transform};
|
||||||
use crate::model::Numbering;
|
use crate::model::Numbering;
|
||||||
@ -55,8 +55,8 @@ impl Introspector {
|
|||||||
|
|
||||||
/// Creates an introspector for HTML.
|
/// Creates an introspector for HTML.
|
||||||
#[typst_macros::time(name = "introspect html")]
|
#[typst_macros::time(name = "introspect html")]
|
||||||
pub fn html(root: &HtmlElement) -> Self {
|
pub fn html(output: &[HtmlNode]) -> Self {
|
||||||
IntrospectorBuilder::new().build_html(root)
|
IntrospectorBuilder::new().build_html(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterates over all locatable elements.
|
/// Iterates over all locatable elements.
|
||||||
@ -392,9 +392,9 @@ impl IntrospectorBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Build an introspector for an HTML document.
|
/// Build an introspector for an HTML document.
|
||||||
fn build_html(mut self, root: &HtmlElement) -> Introspector {
|
fn build_html(mut self, output: &[HtmlNode]) -> Introspector {
|
||||||
let mut elems = Vec::new();
|
let mut elems = Vec::new();
|
||||||
self.discover_in_html(&mut elems, root);
|
self.discover_in_html(&mut elems, output);
|
||||||
self.finalize(elems)
|
self.finalize(elems)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -434,16 +434,16 @@ impl IntrospectorBuilder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Processes the tags in the HTML element.
|
/// Processes the tags in the HTML element.
|
||||||
fn discover_in_html(&mut self, sink: &mut Vec<Pair>, elem: &HtmlElement) {
|
fn discover_in_html(&mut self, sink: &mut Vec<Pair>, nodes: &[HtmlNode]) {
|
||||||
for child in &elem.children {
|
for node in nodes {
|
||||||
match child {
|
match node {
|
||||||
HtmlNode::Tag(tag) => self.discover_in_tag(
|
HtmlNode::Tag(tag) => self.discover_in_tag(
|
||||||
sink,
|
sink,
|
||||||
tag,
|
tag,
|
||||||
Position { page: NonZeroUsize::ONE, point: Point::zero() },
|
Position { page: NonZeroUsize::ONE, point: Point::zero() },
|
||||||
),
|
),
|
||||||
HtmlNode::Text(_, _) => {}
|
HtmlNode::Text(_, _) => {}
|
||||||
HtmlNode::Element(elem) => self.discover_in_html(sink, elem),
|
HtmlNode::Element(elem) => self.discover_in_html(sink, &elem.children),
|
||||||
HtmlNode::Frame(frame) => self.discover_in_frame(
|
HtmlNode::Frame(frame) => self.discover_in_frame(
|
||||||
sink,
|
sink,
|
||||||
frame,
|
frame,
|
||||||
|
2
tests/ref/html/html-elem-metadata.html
Normal file
2
tests/ref/html/html-elem-metadata.html
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>Hi</html>
|
@ -5,3 +5,11 @@
|
|||||||
// Error: 2-19 `<html>` element must be the only element in the document
|
// Error: 2-19 `<html>` element must be the only element in the document
|
||||||
#html.elem("html")
|
#html.elem("html")
|
||||||
Text
|
Text
|
||||||
|
|
||||||
|
--- html-elem-metadata html ---
|
||||||
|
#html.elem("html", context {
|
||||||
|
let val = query(<l>).first().value
|
||||||
|
test(val, "Hi")
|
||||||
|
val
|
||||||
|
})
|
||||||
|
#metadata("Hi") <l>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user