1/*
2 * Copyright (C) 2009, Martin Robinson
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include "config.h"
20#include "SelectionData.h"
21
22#include <wtf/glib/GUniquePtr.h>
23#include <wtf/text/CString.h>
24#include <wtf/text/StringBuilder.h>
25
26namespace WebCore {
27
28static void replaceNonBreakingSpaceWithSpace(String& str)
29{
30 static const UChar NonBreakingSpaceCharacter = 0xA0;
31 static const UChar SpaceCharacter = ' ';
32 str.replace(NonBreakingSpaceCharacter, SpaceCharacter);
33}
34
35void SelectionData::setText(const String& newText)
36{
37 m_text = newText;
38 replaceNonBreakingSpaceWithSpace(m_text);
39}
40
41void SelectionData::setURIList(const String& uriListString)
42{
43 m_uriList = uriListString;
44
45 // This code is originally from: platform/chromium/ChromiumDataObject.cpp.
46 // FIXME: We should make this code cross-platform eventually.
47
48 // Line separator is \r\n per RFC 2483 - however, for compatibility
49 // reasons we also allow just \n here.
50
51 // Process the input and copy the first valid URL into the url member.
52 // In case no URLs can be found, subsequent calls to getData("URL")
53 // will get an empty string. This is in line with the HTML5 spec (see
54 // "The DragEvent and DataTransfer interfaces"). Also extract all filenames
55 // from the URI list.
56 bool setURL = false;
57 for (auto& line : uriListString.split('\n')) {
58 line = line.stripWhiteSpace();
59 if (line.isEmpty())
60 continue;
61 if (line[0] == '#')
62 continue;
63
64 URL url = URL(URL(), line);
65 if (url.isValid()) {
66 if (!setURL) {
67 m_url = url;
68 setURL = true;
69 }
70
71 GUniqueOutPtr<GError> error;
72 GUniquePtr<gchar> filename(g_filename_from_uri(line.utf8().data(), 0, &error.outPtr()));
73 if (!error && filename)
74 m_filenames.append(String::fromUTF8(filename.get()));
75 }
76 }
77}
78
79void SelectionData::setURL(const URL& url, const String& label)
80{
81 m_url = url;
82 m_uriList = url;
83 setText(url.string());
84
85 String actualLabel(label);
86 if (actualLabel.isEmpty())
87 actualLabel = url;
88
89 StringBuilder markup;
90 markup.append("<a href=\"");
91 markup.append(url.string());
92 markup.append("\">");
93 GUniquePtr<gchar> escaped(g_markup_escape_text(actualLabel.utf8().data(), -1));
94 markup.append(String::fromUTF8(escaped.get()));
95 markup.append("</a>");
96 setMarkup(markup.toString());
97}
98
99const String& SelectionData::urlLabel() const
100{
101 if (hasText())
102 return text();
103
104 if (hasURL())
105 return url();
106
107 return emptyString();
108}
109
110void SelectionData::clearAllExceptFilenames()
111{
112 clearText();
113 clearMarkup();
114 clearURIList();
115 clearURL();
116 clearImage();
117
118 m_unknownTypeData.clear();
119 m_canSmartReplace = false;
120}
121
122void SelectionData::clearAll()
123{
124 clearAllExceptFilenames();
125 m_filenames.clear();
126}
127
128} // namespace WebCore
129