Skip to content

Commit

Permalink
Update declarative shadow dom
Browse files Browse the repository at this point in the history
Signed-off-by: batu_hoang <longvatrong111@gmail.com>
  • Loading branch information
longvatrong111 committed Feb 21, 2025
1 parent 518a3ac commit da7d3f4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
28 changes: 26 additions & 2 deletions html5ever/src/tree_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1376,6 +1376,25 @@ where
fn insert_phantom(&self, name: LocalName) -> Handle {
self.insert_element(Push, ns!(html), name, vec![])
}

fn insert_foreign_element(
&self,
tag: Tag,
ns: Namespace,
only_add_to_element_stack: bool,
) -> Handle {
let adjusted_insertion_location = self.appropriate_place_for_insertion(None);
let qname = QualName::new(None, ns, tag.name);
let elem = create_element(&self.sink, qname.clone(), tag.attrs.clone());

if !only_add_to_element_stack {
self.insert_at(adjusted_insertion_location, AppendNode(elem.clone()));
}

self.push(&elem);

elem
}
//§ END

fn should_attach_declarative_shadow(&self, tag: &Tag) -> bool {
Expand Down Expand Up @@ -1416,9 +1435,14 @@ where
is_shadow_root_mode && allow_declarative_shadow_roots && adjusted_current_node_not_topmost
}

fn attach_declarative_shadow(&self, tag: &Tag) -> Result<(), String> {
fn attach_declarative_shadow(
&self,
tag: &Tag,
shadow_host: &Handle,
template: &Handle,
) -> Result<(), String> {
self.sink
.attach_declarative_shadow(self.open_elems.borrow().last().unwrap(), tag.attrs.clone())
.attach_declarative_shadow(shadow_host, template, tag.attrs.clone())
}

fn create_formatting_element_for(&self, tag: Tag) -> Handle {
Expand Down
10 changes: 6 additions & 4 deletions html5ever/src/tree_builder/rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,12 @@ where
self.template_modes.borrow_mut().push(InTemplate);

if (self.should_attach_declarative_shadow(&tag)) {
if self.attach_declarative_shadow(&tag).is_err() {
// TODO:
// insert at the adjusted insertion location
// with the result of insert a foreign element for template tag
let shadow_host = self.open_elems.borrow().last().unwrap().clone();
let template = self.insert_foreign_element(tag.clone(), ns!(html), true);

if self.attach_declarative_shadow(&tag, &shadow_host, &template).is_err() {
let adjusted_insertion_location = self.appropriate_place_for_insertion(None);
self.insert_at(adjusted_insertion_location, AppendNode(template.clone()));
}
} else {
self.insert_element_for(tag);
Expand Down
1 change: 1 addition & 0 deletions markup5ever/interface/tree_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ pub trait TreeSink {
fn attach_declarative_shadow(
&self,
_location: &Self::Handle,
_template: &Self::Handle,
_attrs: Vec<Attribute>,
) -> Result<(), String> {
Err(String::from(
Expand Down

0 comments on commit da7d3f4

Please sign in to comment.