Source code

Revision control

Other Tools

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
7
#include "nsCOMPtr.h"
8
#include "mozilla/dom/Event.h"
9
#include "mozilla/dom/XULTooltipElement.h"
10
#include "mozilla/dom/NodeInfo.h"
11
#include "nsCTooltipTextProvider.h"
12
#include "nsITooltipTextProvider.h"
13
14
namespace mozilla {
15
namespace dom {
16
17
nsXULElement* NS_NewXULTooltipElement(
18
already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo) {
19
RefPtr<XULTooltipElement> tooltip =
20
new XULTooltipElement(std::move(aNodeInfo));
21
NS_ENSURE_SUCCESS(tooltip->Init(), nullptr);
22
return tooltip;
23
}
24
25
nsresult XULTooltipElement::Init() {
26
// Create the default child label node that will contain the text of the
27
// tooltip.
28
RefPtr<mozilla::dom::NodeInfo> nodeInfo;
29
nodeInfo = mNodeInfo->NodeInfoManager()->GetNodeInfo(
30
nsGkAtoms::description, nullptr, kNameSpaceID_XUL, nsINode::ELEMENT_NODE);
31
nsCOMPtr<Element> description;
32
nsresult rv = NS_NewXULElement(getter_AddRefs(description), nodeInfo.forget(),
33
dom::NOT_FROM_PARSER);
34
NS_ENSURE_SUCCESS(rv, rv);
35
description->SetAttr(kNameSpaceID_None, nsGkAtoms::_class,
36
NS_LITERAL_STRING("tooltip-label"), false);
37
description->SetAttr(kNameSpaceID_None, nsGkAtoms::flex,
38
NS_LITERAL_STRING("true"), false);
39
ErrorResult error;
40
AppendChild(*description, error);
41
42
return error.StealNSResult();
43
}
44
45
nsresult XULTooltipElement::AfterSetAttr(int32_t aNameSpaceID, nsAtom* aName,
46
const nsAttrValue* aValue,
47
const nsAttrValue* aOldValue,
48
nsIPrincipal* aSubjectPrincipal,
49
bool aNotify) {
50
if (aNameSpaceID == kNameSpaceID_None && aName == nsGkAtoms::label) {
51
// When the label attribute of this node changes propagate the text down
52
// into child description element.
53
nsCOMPtr<nsIContent> description = GetFirstChild();
54
if (description && description->IsXULElement(nsGkAtoms::description)) {
55
nsAutoString value;
56
if (aValue) {
57
aValue->ToString(value);
58
}
59
nsContentUtils::AddScriptRunner(NS_NewRunnableFunction(
60
"XULTooltipElement::AfterSetAttr", [description, value]() {
61
Element* descriptionElement = description->AsElement();
62
descriptionElement->SetTextContent(value, IgnoreErrors());
63
}));
64
}
65
}
66
return nsXULElement::AfterSetAttr(aNameSpaceID, aName, aValue, aOldValue,
67
aSubjectPrincipal, aNotify);
68
}
69
70
nsresult XULTooltipElement::PostHandleEvent(EventChainPostVisitor& aVisitor) {
71
if (aVisitor.mEvent->mMessage == eXULPopupShowing &&
72
aVisitor.mEvent->IsTrusted() && !aVisitor.mEvent->DefaultPrevented() &&
73
AttrValueIs(kNameSpaceID_None, nsGkAtoms::page, nsGkAtoms::_true,
74
eCaseMatters)) {
75
// When the tooltip node has the "page" attribute set to "true" the
76
// tooltip text provider is used to find the tooltip text from page where
77
// mouse is hovering over.
78
nsCOMPtr<nsITooltipTextProvider> textProvider =
79
do_GetService(NS_DEFAULTTOOLTIPTEXTPROVIDER_CONTRACTID);
80
nsString text;
81
nsString direction;
82
bool shouldChange = false;
83
if (textProvider) {
84
textProvider->GetNodeText(GetTriggerNode(), getter_Copies(text),
85
getter_Copies(direction), &shouldChange);
86
}
87
if (shouldChange) {
88
SetAttr(kNameSpaceID_None, nsGkAtoms::label, text, true);
89
SetAttr(kNameSpaceID_None, nsGkAtoms::direction, direction, true);
90
} else {
91
aVisitor.mEventStatus = nsEventStatus_eConsumeNoDefault;
92
aVisitor.mEvent->PreventDefault();
93
}
94
}
95
return NS_OK;
96
}
97
98
} // namespace dom
99
} // namespace mozilla