Repository: jaytaph/gosub-browser
File: src/html5_parser/parser/document.rs (Rust)

Code before:

        let mut document = Document::shared();

        let mut node1 = Node::new_element(&document, "div", attributes.clone(), HTML_NAMESPACE);
        let mut node2 = Node::new_element(&document, "div", attributes.clone(), HTML_NAMESPACE);

        match &mut node1.data {
            NodeData::Element(element) => {
                element.attributes.insert("id", "myid");
            }
            _ => panic!(),
        }

        match &mut node2.data {
            NodeData::Element(element) => {
                element.attributes.insert("id", "myid");
            }
            _ => panic!(),
        }

        let _ = document.get_mut().add_node(node1, NodeId::from(0));
        let _ = document.get_mut().add_node(node2, NodeId::from(1));

        // two elements here have the same ID, the ID will only be tied to NodeId(0) since
        // the HTML5 spec specifies that every ID must uniquely specify one element in the DOM
        // and we inserted NodeId(0) first
        assert_eq!(
            document.get().get_node_by_named_id("myid").unwrap().id,
            NodeId::from(0)
        );

        // however, with that in mind, NodeId(1) will still have id="myid" on the Node itself,
        // but it is not searchable in the DOM. Even if you change the id of NodeId(0), NodeId(1)
        // will still NOT be searchable under get_node_by_named_id. This behaviour can be changed
        // by using a stack/vector/queue/whatever in the HashMap, but since the spec states
        // there should be one unique ID per element, I don't think we should support it
        document
            .get_mut()
            .set_node_named_id(NodeId::from(0), "otherid");
        assert!(document.get().get_node_by_named_id("myid").is_none());
    }

    #[test]
    fn verify_node_ids_in_element_data() {
        let mut document = Document::shared();
        let document_clone = Document::clone(&document);
        document.get_mut().create_root(&document_clone);

        let node1 = Node::new_element(&document, "div", HashMap::new(), HTML_NAMESPACE);
        let node2 = Node::new_element(&document, "div", HashMap::new(), HTML_NAMESPACE);


Review comment (suggestion):
`0.into()` and `1.into()` will also work here, btw.  Although I think `NodeId::from(0)`, etc., is good.

Code after:

    #[test]
    fn duplicate_named_id_elements() {
        let attributes = HashMap::new();

        let mut document = Document::shared();

        let mut node1 = Node::new_element(&document, "div", attributes.clone(), HTML_NAMESPACE);
        let mut node2 = Node::new_element(&document, "div", attributes.clone(), HTML_NAMESPACE);

        match &mut node1.data {
            NodeData::Element(element) => {
                element.attributes.insert("id", "myid");
            }
            _ => panic!(),
        }

        match &mut node2.data {
            NodeData::Element(element) => {
                element.attributes.insert("id", "myid");
            }
            _ => panic!(),
        }

        let _ = document.get_mut().add_node(node1, NodeId::from(0));
        let _ = document.get_mut().add_node(node2, NodeId::from(1));

        // two elements here have the same ID, the ID will only be tied to NodeId(0) since
        // the HTML5 spec specifies that every ID must uniquely specify one element in the DOM
        // and we inserted NodeId(0) first
        assert_eq!(
            document.get().get_node_by_named_id("myid").unwrap().id,
            NodeId::from(0)
        );

        // however, with that in mind, NodeId(1) will still have id="myid" on the Node itself,
        // but it is not searchable in the DOM. Even if you change the id of NodeId(0), NodeId(1)
        // will still NOT be searchable under get_node_by_named_id. This behaviour can be changed
        // by using a stack/vector/queue/whatever in the HashMap, but since the spec states
        // there should be one unique ID per element, I don't think we should support it
        document
            .get_mut()
            .set_node_named_id(NodeId::from(0), "otherid");
        assert!(document.get().get_node_by_named_id("myid").is_none());
    }

    #[test]
    fn verify_node_ids_in_element_data() {
        let mut document = Document::shared();
        let document_clone = Document::clone(&document);
        document.get_mut().create_root(&document_clone);