| struct Node { | |
| shared_ptr<Node> parent; | |
| shared_ptr<Node> first_child; | |
| TextNode* as_text_node() { assert(/*sensible check*/); return (TextNode*)this; } | |
| virtual Element* as_element() { return NULL; } | |
| }; | |
| struct TextNode: public Node { | |
| }; | |
| struct Element: public Node { | |
| vector<string, string> attrs; | |
| void set_attribute(const string& key, const string& value); | |
| virtual void before_set_attr(const string& key, const string& value); | |
| virtual void after_set_attr(const string& key, const string& value); | |
| virtual Element* as_element() { return this; } | |
| }; | |
| void Element::set_attribute(const string& key, const string& value) | |
| { | |
| before_set_attr(key, value); | |
| //...update attrs... | |
| after_set_attr(key, value); | |
| } | |
| struct HTMLImageElement: public Element { | |
| virtual void before_set_attr(const string& key, const string& value); | |
| }; | |
| void HTMLImageElement::before_set_attr(const string& key, const string& value) | |
| { | |
| if (key == "src") { | |
| //..remove cached image with url |value|... | |
| } | |
| Element::before_set_attr(key, value); | |
| } | |
| struct HTMLVideoElement: public Element { | |
| bool cross_origin; | |
| void after_set_attr(const string& key, const string& value); | |
| }; | |
| void HTMLVideoElement::after_set_attr(const string& key, const string& value) | |
| { | |
| if (key == "crossOrigin") { | |
| cross_origin = value == "true"; | |
| } | |
| Element::after_set_attr(key, value); | |
| } | |
| void process_any_element(Element* element) { | |
| //... | |
| } | |
| shared_ptr<HTMLVideoElement> videoElement = ...; | |
| process_any_element(videoElement); | |
| shared_ptr<Node> node = videoElement->first_child; | |
| shared_ptr<Element> element = node->as_element(); | |
| if (!element) { | |
| shared_ptr<TextNode> text = node->as_text_node(); | |
| } |
This comment has been minimized.
This comment has been minimized.
nrc
commented
Mar 31, 2014
|
The |
This comment has been minimized.
This comment has been minimized.
MicahChalmer
commented
Apr 6, 2014
|
While trying to translate this into rust+rust-lang/rfcs/#9, I noticed an error in this part: shared_ptr<Node> node = videoElement->first_child;
shared_ptr<Element> element = node->as_element();
if (!element) {
shared_ptr<TextNode> text = node->as_text_node();
}If This would still compile fine in C++, of course, which illustrates why we all want Rust. The proposals for modeling the DOM in rust may look different, but none of them would let something like that compile. I think this is what you want: shared_ptr<Node> node = videoElement->first_child;
Element* element = node->as_element();
if (!element) {
TextNode* text = node->as_text_node();
}where |
This comment has been minimized.
jdm commentedMar 31, 2014
One added concern with representing the DOM in Servo: the vast majority of our DOM methods take
&JS<T>smart pointer arguments instead of&T, whereTcan be a type in theNodehierarchy. It would be swell if we could pass&JS<Element>to a method taking&JS<Node>.