-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathWAnchor.C
More file actions
299 lines (245 loc) · 6.38 KB
/
WAnchor.C
File metadata and controls
299 lines (245 loc) · 6.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright (C) 2008 Emweb bv, Herent, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include "Wt/WAnchor.h"
#include "Wt/WApplication.h"
#include "Wt/WEnvironment.h"
#include "Wt/WImage.h"
#include "Wt/WResource.h"
#include "Wt/WText.h"
#include "DomElement.h"
#include "WebUtils.h"
#include "Wt/Utils.h"
#include "boost/algorithm/string.hpp"
namespace Wt {
WAnchor::LinkState::LinkState()
: clickJS(nullptr)
{ }
WAnchor::LinkState::~LinkState()
{
delete clickJS;
}
WAnchor::WAnchor()
{
setInline(true);
}
WAnchor::WAnchor(const WLink& link)
{
setInline(true);
setLink(link);
}
WAnchor::WAnchor(const WLink& link, const WString& text)
{
setInline(true);
setLink(link);
text_ = new WText(text);
addWidget(std::unique_ptr<WWidget>(text_.get()));
}
WAnchor::WAnchor(const WLink& link, std::unique_ptr<WImage> image)
{
setInline(true);
setLink(link);
if (image) {
image_ = image.get();
addWidget(std::move(image));
}
}
void WAnchor::setLink(const WLink& link)
{
if (linkState_.link.type() != LinkType::Resource &&
linkState_.link == link)
return;
linkState_.link = link;
flags_.set(BIT_LINK_CHANGED);
repaint();
switch (linkState_.link.type()) {
case LinkType::Resource:
linkState_.link.resource()->dataChanged().connect
(this, &WAnchor::resourceChanged);
break;
case LinkType::InternalPath:
WApplication::instance()->enableInternalPaths();
break;
default:
break;
}
}
const WString& WAnchor::text() const
{
static WString empty("");
if (text_)
return text_->text();
else
return empty;
}
void WAnchor::setText(const WString& text)
{
if (!text_) {
std::unique_ptr<WText> t(new WText(text));
text_ = t.get();
addWidget(std::move(t));
} else
text_->setText(text);
}
void WAnchor::setWordWrap(bool wordWrap)
{
if (!text_)
setText(WString());
text_->setWordWrap(wordWrap);
}
bool WAnchor::wordWrap() const
{
return text_ ? text_->wordWrap() : true;
}
void WAnchor::setTextFormat(TextFormat textFormat)
{
if (!text_)
setText(WString());
text_->setTextFormat(textFormat);
}
TextFormat WAnchor::textFormat() const
{
return text_ ? text_->textFormat() : TextFormat::XHTML;
}
void WAnchor::setImage(std::unique_ptr<WImage> image)
{
image_ = image.get();
if (image)
addWidget(std::move(image));
}
void WAnchor::resourceChanged()
{
flags_.set(BIT_LINK_CHANGED);
repaint();
}
void WAnchor::enableAjax()
{
if (linkState_.link.type() == LinkType::InternalPath) {
flags_.set(BIT_LINK_CHANGED);
repaint();
}
WContainerWidget::enableAjax();
}
bool WAnchor::canReceiveFocus() const
{
return true;
}
int WAnchor::tabIndex() const
{
int result = WContainerWidget::tabIndex();
if (result == std::numeric_limits<int>::min())
return 0;
else
return result;
}
bool WAnchor::setFirstFocus()
{
return false;
}
void WAnchor::updateDom(DomElement& element, bool all)
{
bool needsUrlResolution = false;
if (flags_.test(BIT_LINK_CHANGED) || all) {
needsUrlResolution = renderHRef(this, linkState_, element);
flags_.reset(BIT_LINK_CHANGED);
}
if (flags_.test(BIT_TARGET_CHANGED) || all) {
renderHTarget(linkState_, element, all);
/*
* TODO(Benoit)
* We do that here because of the static method,
* We should maybe move the code to renderHTarget()
* and make it non static ?
*/
flags_.reset(BIT_TARGET_CHANGED);
}
WContainerWidget::updateDom(element, all);
if (needsUrlResolution)
renderUrlResolution(this, element, all);
}
bool WAnchor::renderHRef(WInteractWidget *widget,
LinkState& linkState, DomElement& element)
{
WApplication *app = WApplication::instance();
if (linkState.link.isNull() || widget->isDisabled()) {
// WT-7645: Specific empty href attribute for JS loaded content to avoid tab chain breaking
if (app->environment().javaScript()) {
element.setAttribute("href", "#");
element.addPropertyWord(Property::Class, WInteractWidget::noDefault);
// Add back all of the style classes to avoid removing them.
auto styleClassesVector = Utils::getWidgetStyleClasses(widget);
for (const auto& className : styleClassesVector) {
element.addPropertyWord(Property::Class, className);
}
} else {
element.removeAttribute("href");
}
}
else {
std::string url = linkState.link.resolveUrl(app);
/*
* From 但浩亮: setRefInternalPath() and setTarget(LinkTarget::NewWindow)
* does not work without the check below:
*/
if (linkState.link.target() == LinkTarget::Self) {
linkState.clickJS
= linkState.link.manageInternalPathChange(app, widget,
linkState.clickJS);
} else {
delete linkState.clickJS;
linkState.clickJS = nullptr;
}
url = app->encodeUntrustedUrl(url);
std::string href = url;
element.setAttribute("href", href);
return !app->environment().internalPathUsingFragments()
&& href.find("://") == std::string::npos && href[0] != '/';
}
return false;
}
void WAnchor::renderHTarget(LinkState& linkState, DomElement& element, bool all)
{
switch (linkState.link.target()) {
case LinkTarget::Self:
if (!all)
element.setProperty(Property::Target, "_self");
break;
case LinkTarget::ThisWindow:
element.setProperty(Property::Target, "_top");
break;
case LinkTarget::NewWindow:
element.setProperty(Property::Target, "_blank");
break;
case LinkTarget::Download:
element.setProperty(Property::Target, "wt_iframe_dl");
element.setProperty(Property::Download, ""); // Only works on some browsers (FF, Chrome)
break;
}
}
void WAnchor::renderUrlResolution(WWidget *widget, DomElement& element,
bool all)
{
if (all)
element.setProperty(Property::Class,
Utils::addWord(widget->styleClass().toUTF8(), "Wt-rr"));
else
element.callJavaScript(WT_CLASS ".$('" + widget->id() + "').classList.add('Wt-rr');");
}
void WAnchor::propagateRenderOk(bool deep)
{
flags_.reset(BIT_LINK_CHANGED);
flags_.reset(BIT_TARGET_CHANGED);
WContainerWidget::propagateRenderOk(deep);
}
void WAnchor::propagateSetEnabled(bool enabled)
{
WContainerWidget::propagateSetEnabled(enabled);
resourceChanged();
}
DomElementType WAnchor::domElementType() const
{
return DomElementType::A;
}
}