Bug 1872300 - Make `nsFocusManager::ContentRemoved()` call `HTMLEditor::FinalizeSelec...
[gecko.git] / layout / forms / ListMutationObserver.cpp
blob13f9e367a0e37095765578177ea53365042931a2
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "ListMutationObserver.h"
8 #include "mozilla/dom/HTMLInputElement.h"
9 #include "nsIFrame.h"
11 namespace mozilla {
12 NS_IMPL_ISUPPORTS(ListMutationObserver, nsIMutationObserver)
14 ListMutationObserver::~ListMutationObserver() = default;
16 void ListMutationObserver::Attach(bool aRepaint) {
17 nsAutoString id;
18 if (InputElement().GetAttr(nsGkAtoms::list_, id)) {
19 Unlink();
20 RefPtr<nsAtom> idAtom = NS_AtomizeMainThread(id);
21 ResetWithID(InputElement(), idAtom);
22 AddObserverIfNeeded();
24 if (aRepaint) {
25 mOwningElementFrame->InvalidateFrame();
29 void ListMutationObserver::AddObserverIfNeeded() {
30 if (auto* list = get()) {
31 if (list->IsHTMLElement(nsGkAtoms::datalist)) {
32 list->AddMutationObserver(this);
37 void ListMutationObserver::RemoveObserverIfNeeded(dom::Element* aList) {
38 if (aList && aList->IsHTMLElement(nsGkAtoms::datalist)) {
39 aList->RemoveMutationObserver(this);
43 void ListMutationObserver::Detach() {
44 RemoveObserverIfNeeded();
45 Unlink();
48 dom::HTMLInputElement& ListMutationObserver::InputElement() const {
49 MOZ_ASSERT(mOwningElementFrame->GetContent()->IsHTMLElement(nsGkAtoms::input),
50 "bad cast");
51 return *static_cast<dom::HTMLInputElement*>(
52 mOwningElementFrame->GetContent());
55 void ListMutationObserver::AttributeChanged(dom::Element* aElement,
56 int32_t aNameSpaceID,
57 nsAtom* aAttribute,
58 int32_t aModType,
59 const nsAttrValue* aOldValue) {
60 if (aAttribute == nsGkAtoms::value && aNameSpaceID == kNameSpaceID_None &&
61 aElement->IsHTMLElement(nsGkAtoms::option)) {
62 mOwningElementFrame->InvalidateFrame();
66 void ListMutationObserver::CharacterDataChanged(
67 nsIContent* aContent, const CharacterDataChangeInfo& aInfo) {
68 mOwningElementFrame->InvalidateFrame();
71 void ListMutationObserver::ContentAppended(nsIContent* aFirstNewContent) {
72 mOwningElementFrame->InvalidateFrame();
75 void ListMutationObserver::ContentInserted(nsIContent* aChild) {
76 mOwningElementFrame->InvalidateFrame();
79 void ListMutationObserver::ContentRemoved(nsIContent* aChild,
80 nsIContent* aPreviousSibling) {
81 mOwningElementFrame->InvalidateFrame();
84 void ListMutationObserver::ElementChanged(dom::Element* aFrom,
85 dom::Element* aTo) {
86 IDTracker::ElementChanged(aFrom, aTo);
87 RemoveObserverIfNeeded(aFrom);
88 AddObserverIfNeeded();
89 mOwningElementFrame->InvalidateFrame();
92 } // namespace mozilla