kuchiki-rs / kuchiki

(朽木) HTML/XML tree manipulation library for Rust
MIT License
470 stars 54 forks source link

How to create an empty element? #52

Closed sanmai-NL closed 1 year ago

sanmai-NL commented 5 years ago

E.g. a link element is an empty element. Looking through the API docs, examples and tests I couldn't figure out how to create such an element using kuchiki.

SimonSapin commented 5 years ago

As far as I know this is a concern for the parser and serializer for a given syntax (e.g. HTML or XML). In a DOM-like tree data structure, such elements are not special, they are represented simply as element nodes that happen not to have any children.

To create one from Rust code, call for example NodeRef::new_element and then don’t call methods like append. To create one by parsing HTML syntax, html5ever will already take care of parsing <link> correctly.

sanmai-NL commented 5 years ago

The parser used in e.g. Firefox and HTML validators will flag empty elements serialized as normal elements but empty as syntax errors in the strict sense. Using new_element without append is what I happened to do, but I did call append on the containing element in order to place the empty element as child, resulting in <link ...></link>.

SimonSapin commented 5 years ago

This sounds like a bug in html5ever’s serializier, or in kuchiki’s use of it.

vainiovano commented 5 years ago

I read some of html5ever's documentation and got a solution: use namespace html like this: QualName::new(None, ns!(html), local_name!("link")). This will make it work correctly.

SimonSapin commented 1 year ago

I will soon archive this repository and make it read-only, so this issue will not be addressed: https://github.com/kuchiki-rs/kuchiki#archived