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"
12 NS_IMPL_ISUPPORTS(ListMutationObserver
, nsIMutationObserver
)
14 ListMutationObserver::~ListMutationObserver() = default;
16 void ListMutationObserver::Attach(bool aRepaint
) {
18 if (InputElement().GetAttr(nsGkAtoms::list_
, id
)) {
20 RefPtr
<nsAtom
> idAtom
= NS_AtomizeMainThread(id
);
21 ResetWithID(InputElement(), idAtom
);
22 AddObserverIfNeeded();
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();
48 dom::HTMLInputElement
& ListMutationObserver::InputElement() const {
49 MOZ_ASSERT(mOwningElementFrame
->GetContent()->IsHTMLElement(nsGkAtoms::input
),
51 return *static_cast<dom::HTMLInputElement
*>(
52 mOwningElementFrame
->GetContent());
55 void ListMutationObserver::AttributeChanged(dom::Element
* aElement
,
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
,
86 IDTracker::ElementChanged(aFrom
, aTo
);
87 RemoveObserverIfNeeded(aFrom
);
88 AddObserverIfNeeded();
89 mOwningElementFrame
->InvalidateFrame();
92 } // namespace mozilla