3 ms·
Does anyone have a good way of representing HTML in the form of types? I'm primarily thinking about how it's tricky to pair valid attributes with the correct el
by calebh 8y ago
Does anyone have a good way of representing HTML in the form of types? I'm primarily thinking about how it's tricky to pair valid attributes with the correct elements. For example, it should be okay to use the "alt" attribute with the img tag, but not with the p tag. It seems like there should be a type-safe way to do this.
- shaabanban 8y agoIf you use JSX with typescript you effectively get type-checked html attributes.
- btown 8y agoIf you're referring to Rust's ability to infer types from macro tokens, not sure how far you'll get. But Typescript itself can absolutely track permitted attributes for different HTML tags, and it compiles JSX using these types: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/react/index.d.ts#L1723 https://github.com/DefinitelyTyped/DefinitelyTyped/blob/mast...
- evmar 8y agoTypeScript's type system can do it: http://www.typescriptlang.org/play/#src=%0D%0A%0D%0Atype%20Att%3CT%20extends%20keyof%20HTMLElementTagNameMap%3E%20%3D%20%7B%0D%0A%20%20%20%20%5BK%20in%20keyof%20HTMLElementTagNameMap%5BT%5D%5D%3F%3A%20HTMLElementTagNameMap%5BT%5D%5BK%5D%3B%0D%0A%7D%0D%0A%0D%0Afunction%20tag%3CT%20extends%20keyof%20HTMLElementTagNameMap%3E(name%3A%20T%2C%20attrs%3A%20Att%3CT%3E)%20%7B%0D%0A%20%20%20%20%2F%2F%20code%20here.%0D%0A%7D%0D%0A%0D%0Atag('a'%2C%20%7B%20href%3A%20'asdfas'%20%7D)%3B%0D%0Atag('a'%2C%20%7B%20href%3A%203%20%7D)%3B%20%2F%2F%20fields%20are%20typed%0D%0Atag('div'%2C%20%7B%20href%3A%20'adfadf'%20%7D)%3B%20%20%2F%2F%20non-anchors%20have%20no%20href%0D%0A http://www.typescriptlang.org/play/#src=%0D%0A%0D%0Atype%20A...
- dtolnay 8y agoThis library represents HTML in types. The <p> tag turns into a typed_html::elements::p [1], inside of which the attributes are held by a typed_html::elements::Attrs_p [2]. [1] https://docs.rs/typed-html/0.1.0/typed_html/elements/struct.p.html https://docs.rs/typed-html/0.1.0/typed_html/elements/struct.... [2] https://docs.rs/typed-html/0.1.0/typed_html/elements/struct.Attrs_p.html https://docs.rs/typed-html/0.1.0/typed_html/elements/struct.... let doc: DOMTree<String> = html!{ <body> <img alt="xxx" /> <p alt="xxx"></p> </body> }; If you try to give <p> an alt attribute, the program fails to compile. error[E0609]: no field `alt` on type `typed_html::elements::Attrs_p` --> src/main.rs:12:16 | 12 | <p alt="xxx"></p> | ^^^ unknown field | = note: available fields are: `accesskey`, `autocapitalize`, `class`, `contenteditable`, `contextmenu` ... and 9 others
- calebh 8y agoThis solution bundles all of the attributes into the same type, which comes with the downside that it's impossible to represent a single attribute without also receiving all the other attributes. I think that the best solution in Haskell would be to group the types in a typeclass. However then the typeclass is not directly associated with the p or img value constructors. I wonder if there is some way to use dependent types to improve this situation.
- err4nt 8y agoThis is something that's very easy to check in a web browser with DOM - Suppose you have this HTML: <p>hello</p><img src=#> If you try to access the `.alt` property of the <p> tag you get `undefined`, and if you try to access the `.alt` property of the <img> tag it returns the empty string in this case because the property exists on that tag, but hasn't been set to anything.
- setr 8y agoI don’t see any reason this wouldnt be possible, you’d just have an attribute whitelist for each tag; the biggest headache would be that you can’t really implement it incrementally since it’d be a whitelist: at best some tags turned on, others off, for attr validation And I imagine its quite a bit more work than just the tags