Skip to content

Implement mutation observers #6633

Closed
Closed

Description

@dzbarsky

I've started some patches for this.

Activity

  1. jdm commented on Jul 15, 2015

    @jdm
    Member

    cc @nox

  2. nox commented on Jul 15, 2015

    @nox
    Contributor

    Colour me interested. Why do you think mutation observers are the way to go for DOM ranges?

  3. dzbarsky commented on Jul 15, 2015

    @dzbarsky
    ContributorAuthor

    Note that these aren't the DOM mutation observers, just an internal servo thing that lets ranges register a callback for when the document changes. It's similar to nsIMutationObserver in Gecko.

  4. jdm commented on Jul 15, 2015

    @jdm
    Member

    That sounds confusingly named, in that case :)

  5. dzbarsky commented on Jul 15, 2015

    @dzbarsky
    ContributorAuthor

    True it could use a different name. MutationWatcher or something.

  6. nox commented on Jul 18, 2015

    @nox
    Contributor

    @dzbarsky Would #6660 help?

  7. dzbarsky commented on Jul 18, 2015

    @dzbarsky
    ContributorAuthor

    I'm not sure how that PR helps here. There's not really a great way for nodes to know that they're the start or end of some range.

  8. nox commented on Jul 18, 2015

    @nox
    Contributor

    I made Rc<RangeInner> in Range because I intended to store a Vec<Weak<RangeInner>> in each node. I guess that's the wrong way to go at it?

  9. dzbarsky commented on Jul 18, 2015

    @dzbarsky
    ContributorAuthor

    That's a lot of storage. I think it's better to store that Vec on the document itself, that's basically what I'm doing in #6639

  10. nox commented on Jul 18, 2015

    @nox
    Contributor

    With a Vec on the document itself, children_changed() could still be used in the VirtualMethods implementation of Node to update any range associated to the context object, or actually the insert methods and friends could do it themselves too, couldn't they?

    Why the indirection with this MutationObserver thing?

  11. dzbarsky commented on Jul 19, 2015

    @dzbarsky
    ContributorAuthor

    I can see children_changed being used for some of the notifications, but I'm not sure how you'd handle some other cases. For example, there are steps to run when calling CharacterData#replaceData, or Node#normalize.
    But yes, I think it would be cleaner to just do the steps directly in insert and remove methods.

    As for the MutationObserver thing...
    In Gecko, other things than just range hook into nsIMutationObserver but perhaps we don't care about those for now.

    One thing to keep in mind is that NodeIterator also defines "removing steps" (https://dom.spec.whatwg.org/#nodeiterator).
    So we would also need to store a Vec of NodeIterators. I was hoping to combine those but it looks like they need different notifications so it doesn't really make things any cleaner.

    Having played around with it some I think the MutationObserver abstraction isn't necessary yet and just makes things more complicated for now so I'm tempted to drop it.

  12. nox commented on Oct 28, 2015

    @nox
    Contributor

    I'm on the Range part.

  13. self-assigned this
    on Oct 28, 2015
  14. added
    A-content/script
    on Oct 28, 2015
  15. changed the title [-]Implement mutation observers (and hook up DOM Range)[/-] [+]Implement mutation observers[/+] on Jan 10, 2016
  16. 39 remaining items

  17. jdm commented on Apr 30, 2017

    @jdm
    Member

    (they will need to be wrapped in Cell or DOMRefCell types to allow mutating them later)

  18. gosavipooja commented on Apr 30, 2017

    @gosavipooja

    Hi,

    There is no MutationObserverInit argument in the constructor currently and if we add it, it shoots an error in the Bindings file:

    error[E0061]: this function takes 3 parameters but 2 parameters were supplied
        --> servo/target/debug/build/script-9cf1e34119ea7ecd/out/Bindings/MutationObserverBinding.rs:1002:91
         |
    1002 |           let result: Result<Root<MutationObserver>, Error> = MutationObserver::Constructor(&global, arg0);
         |                                                                                             ^^^^^^^^^^^^^ expected 3 parameters
         | 
        ::: servo/components/script/dom/mutationobserver.rs
         |
    53   | /     pub fn Constructor(global: &Window, callback: Rc<MutationCallback>, options: DOMRefCell<MutationObserverInit>) -> Fallible<Root<MutationObserver>> {
    54   | |         let observer = MutationObserver::new(global, callback);
    55   | |         ScriptThread::add_mutation_observer(&*observer);
    56   | |         Ok(observer)
    57   | |     }
         | |___- defined here
    

    Should we create another overridden constructor which also has MutationObserverInit argument? Or what way should we follow?
    Thanks!

  19. krishnakarthick1993 commented on Apr 30, 2017

    @krishnakarthick1993

    Hi Josh,
    I am trying to implement Step 4.2 of Queuing a mutation record (If name and namespace are given, set record’s attributeName to name, and record’s attributeNamespace to namespace.)

    My enum is

    pub enum Mutation {
        Attribute { name: Atom, namespace: Namespace, oldValue: DOMString, newValue: DOMString}
    }

    Since I need name and namespace of Attribute of Mutation enum which is passed as an argument to my function , I tried using the match function like (http://rustbyexample.com/custom_types/enum.html)

    My function is

        //Queuing a mutation record
        pub fn queue_a_mutation_record(target: &Node, attr_type: Mutation) {
            // Step 1
            let mut interestedObservers: Vec<Root<MutationObserver>> = vec![];
            let mut pairedStrings: Vec<DOMString> = vec![];
            // Step 2
            let mut nodes: Vec<Root<Node>> = vec![];
            for ancestor in target.inclusive_ancestors() {
                nodes.push(ancestor);
            }
            // Step 3
            for node in &nodes {
                for registered_observer in node.registered_mutation_observers_for_type().borrow().iter() {
                   //let bool condition1 = (node!=target);
                }
            }
            // Step 4
            for observer in &interestedObservers {
                //Step 4.1
                let mut record= MutationRecord::new(target);
                //Step 4.2
                match attr_type {
                     Mutation::Attribute {name,namespace,oldValue,newValue} => { let mut given_name = name;let mut given_namespace = namespace;
                   },
                }
    
            }
            }

    On compilation I get

    error[E0382]: use of partially moved value: `attr_type`
       --> /home/kbalaji/Moz/servo/components/script/dom/mutationobserver.rs:103:19
        |
    103 |             match attr_type {
        |                   ^^^^^^^^^ value used here after move
    104 |                Mutation::Attribute {name,namespace,oldValue,newValue} => { let mut given_name = name;let mut given_namespace = namespace;
        |                                     ---- value moved here
        |
        = note: move occurs because `attr_type.name` has type `string_cache::atom::Atom<servo_atoms::AtomStaticSet>`, which does not implement the `Copy` trait
    
    error[E0382]: use of moved value: `attr_type.name`
       --> /home/kbalaji/Moz/servo/components/script/dom/mutationobserver.rs:104:37
        |
    104 |                Mutation::Attribute {name,namespace,oldValue,newValue} => { let mut given_name = name;let mut given_namespace = namespace;
        |                                     ^^^^ value moved here in previous iteration of loop
        |
        = note: move occurs because `attr_type.name` has type `string_cache::atom::Atom<servo_atoms::AtomStaticSet>`, which does not implement the `Copy` trait
    

    (similar errors for namespace,oldValue and newValue).

    I tried including #[derive(Clone,Copy)] before my mutation and the following code

    impl Copy for Mutation {}
    impl Clone for Mutation { fn clone(&self) -> Self { *self } }

    Compiler still throws

    error[E0119]: conflicting implementations of trait `core::marker::Copy` for type `dom::mutationobserver::Mutation`:
      --> /home/kbalaji/Moz/servo/components/script/dom/mutationobserver.rs:35:1
       |
    31 | #[derive(Clone,Copy)]
       |                ---- first implementation here
    ...
    35 | impl Copy for Mutation {}
       | ^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `dom::mutationobserver::Mutation`
    
    error[E0204]: the trait `Copy` may not be implemented for this type
      --> /home/kbalaji/Moz/servo/components/script/dom/mutationobserver.rs:31:16
       |
    31 | #[derive(Clone,Copy)]
       |                ^^^^
    32 | pub enum Mutation {
    33 |     Attribute { name: Atom, namespace: Namespace, oldValue: DOMString, newValue: DOMString}
       |                 ---------- this field does not implement `Copy`
    
    error[E0204]: the trait `Copy` may not be implemented for this type
      --> /home/kbalaji/Moz/servo/components/script/dom/mutationobserver.rs:35:6
       |
    33 |     Attribute { name: Atom, namespace: Namespace, oldValue: DOMString, newValue: DOMString}
       |                 ---------- this field does not implement `Copy`
    34 | }
    35 | impl Copy for Mutation {}
       |      ^^^^
    
    error[E0119]: conflicting implementations of trait `core::clone::Clone` for type `dom::mutationobserver::Mutation`:
      --> /home/kbalaji/Moz/servo/components/script/dom/mutationobserver.rs:36:1
       |
    31 | #[derive(Clone,Copy)]
       |          ----- first implementation here
    ...
    36 | impl Clone for Mutation { fn clone(&self) -> Self { *self } }
       | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `dom::mutationobserver::Mutation`
    

    Please let us know on how to reference name and namespace of attr_type ( function argument).
    Thank you.

  20. KiChjang commented on Apr 30, 2017

    @KiChjang
    Contributor

    I'm very confused why you have

    pub enum Mutation {
        Attribute { name: Atom, namespace: Namespace, oldValue: DOMString, newValue: DOMString}
    }

    Can Attribute not be a struct of its own?

  21. krishnakarthick1993 commented on Apr 30, 2017

    @krishnakarthick1993

    Hi,
    Separately, I am not clear on what "type" means in "To queue a mutation record of type for target with one or more of (depends on type) ....." .

    In Step 3 of Queuing a mutation record , type is supposed to be compared with "attributes", "characterData" and "childList".
    Does type refer to record_type of a mutation record object ?. If so , should I pass a mutation record object as well as an argument to my function queue_a_mutation_record along with target and enum. This would enable me to access the type of mutation record through that object.
    Please let us know. Thank you.

  22. jdm commented on Apr 30, 2017

    @jdm
    Member

    The "type" in the spec is equivalent to matching against the enum that we're using. To avoid the errors about partially moved values, you will want to use ref binding in the matches, and clone those values like let mut given_name = name.clone().

  23. jdm commented on Apr 30, 2017

    @jdm
    Member

    As for the question about MutationObserverInit, my mistake. I guess you will want sensible defaults for the new members in the constructor, and then set them to the real values in MutationObserver::Observe.

  24. added a commit that references this issue on May 8, 2017
    5c7b939
  25. added a commit that references this issue on May 17, 2017
  26. Xanewok commented on Oct 24, 2017

    @Xanewok
    Contributor

    @jdm just want to make sure what's the status of this issue - it seems that effectively almost all of the listed work in https://github.com/servo/servo/wiki/Mutation-observer-project is done (I tried to go over them and mark done items, excuse the crude format), mostly by #16883?

    It seems that what's left is:

    Asking because we planned to take this on as a team as a part of our university thesis project
    cc @mrowqa @marmistrz

  27. marmistrz commented on Oct 24, 2017

    @marmistrz
    Contributor

    cc @taki-jaro

  28. jdm commented on Oct 24, 2017

    @jdm
    Member

    @Xanewok Signal slots are part of a standard that we don't implement, so those can be ignored. We're also missing character data notifications, and all of the failing tests that are named MutationObserver.

  29. jdm commented on Jan 23, 2020

    @jdm
    Member

    We have implemented mutation observers. If anything is missing or not working, we should open new issues for those items.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-content/script

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions