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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/*
* Copyright (C) 2006, 2008, 2009, 2010 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "HTMLViewSourceDocument.h"
#include "Attribute.h"
#include "DOMImplementation.h"
#include "HTMLAnchorElement.h"
#include "HTMLBodyElement.h"
#include "HTMLDivElement.h"
#include "HTMLHtmlElement.h"
#include "HTMLNames.h"
#include "HTMLTableCellElement.h"
#include "HTMLTableElement.h"
#include "HTMLTableRowElement.h"
#include "HTMLTableSectionElement.h"
#include "HTMLToken.h"
#include "HTMLViewSourceParser.h"
#include "SegmentedString.h"
#include "Text.h"
#include "TextDocument.h"
namespace WebCore {
using namespace HTMLNames;
HTMLViewSourceDocument::HTMLViewSourceDocument(Frame* frame, const KURL& url, const String& mimeType)
: HTMLDocument(frame, url)
, m_type(mimeType)
{
setUsesBeforeAfterRules(true);
}
DocumentParser* HTMLViewSourceDocument::createParser()
{
// Use HTMLDocumentParser if applicable, otherwise use TextDocumentParser.
if (m_type == "text/html" || m_type == "application/xhtml+xml" || m_type == "image/svg+xml" || DOMImplementation::isXMLMIMEType(m_type)
#if ENABLE(XHTMLMP)
|| m_type == "application/vnd.wap.xhtml+xml"
#endif
)
return new HTMLViewSourceParser(this);
return createTextDocumentParser(this);
}
void HTMLViewSourceDocument::createContainingTable()
{
RefPtr<HTMLHtmlElement> html = HTMLHtmlElement::create(this);
parserAddChild(html);
html->attach();
RefPtr<HTMLBodyElement> body = HTMLBodyElement::create(this);
html->parserAddChild(body);
body->attach();
// Create a line gutter div that can be used to make sure the gutter extends down the height of the whole
// document.
RefPtr<HTMLDivElement> div = HTMLDivElement::create(this);
RefPtr<NamedNodeMap> attrs = NamedNodeMap::create();
attrs->addAttribute(Attribute::createMapped(classAttr, "webkit-line-gutter-backdrop"));
div->setAttributeMap(attrs.release());
body->parserAddChild(div);
div->attach();
RefPtr<HTMLTableElement> table = HTMLTableElement::create(this);
body->parserAddChild(table);
table->attach();
m_tbody = HTMLTableSectionElement::create(tbodyTag, this);
table->parserAddChild(m_tbody);
m_tbody->attach();
m_current = m_tbody;
}
void HTMLViewSourceDocument::addViewSourceText(const String& text)
{
if (!m_current)
createContainingTable();
addText(text, "");
}
void HTMLViewSourceDocument::addSource(const String& source, HTMLToken& token)
{
if (!m_current)
createContainingTable();
switch (token.type()) {
case HTMLToken::Uninitialized:
ASSERT_NOT_REACHED();
break;
case HTMLToken::DOCTYPE:
processDoctypeToken(source, token);
break;
case HTMLToken::EndOfFile:
break;
case HTMLToken::StartTag:
case HTMLToken::EndTag:
processTagToken(source, token);
break;
case HTMLToken::Comment:
processCommentToken(source, token);
break;
case HTMLToken::Character:
processCharacterToken(source, token);
break;
}
}
void HTMLViewSourceDocument::processDoctypeToken(const String& source, HTMLToken&)
{
if (!m_current)
createContainingTable();
m_current = addSpanWithClassName("webkit-html-doctype");
addText(source, "webkit-html-doctype");
m_current = m_td;
}
void HTMLViewSourceDocument::processTagToken(const String& source, HTMLToken& token)
{
String classNameStr = "webkit-html-tag";
m_current = addSpanWithClassName(classNameStr);
AtomicString tagName(token.name().data(), token.name().size());
unsigned index = 0;
HTMLToken::AttributeList::const_iterator iter = token.attributes().begin();
while (index < source.length()) {
if (iter == token.attributes().end()) {
// We want to show the remaining characters in the token.
index = addRange(source, index, source.length(), "");
ASSERT(index == source.length());
break;
}
AtomicString name(iter->m_name.data(), iter->m_name.size());
String value(iter->m_value.data(), iter->m_value.size());
index = addRange(source, index, iter->m_nameRange.m_start - token.startIndex(), "");
index = addRange(source, index, iter->m_nameRange.m_end - token.startIndex(), "webkit-html-attribute-name");
if (tagName == baseTag && name == hrefAttr) {
// Catch the href attribute in the base element. It will be used
// for rendering anchors created by addLink() below.
setBaseElementURL(KURL(url(), value));
}
index = addRange(source, index, iter->m_valueRange.m_start - token.startIndex(), "");
bool isLink = name == srcAttr || name == hrefAttr;
index = addRange(source, index, iter->m_valueRange.m_end - token.startIndex(), "webkit-html-attribute-value", isLink, tagName == aTag);
++iter;
}
m_current = m_td;
}
void HTMLViewSourceDocument::processCommentToken(const String& source, HTMLToken&)
{
m_current = addSpanWithClassName("webkit-html-comment");
addText(source, "webkit-html-comment");
m_current = m_td;
}
void HTMLViewSourceDocument::processCharacterToken(const String& source, HTMLToken&)
{
addText(source, "");
}
PassRefPtr<Element> HTMLViewSourceDocument::addSpanWithClassName(const String& className)
{
if (m_current == m_tbody) {
addLine(className);
return m_current;
}
RefPtr<HTMLElement> span = HTMLElement::create(spanTag, this);
RefPtr<NamedNodeMap> attrs = NamedNodeMap::create();
attrs->addAttribute(Attribute::createMapped(classAttr, className));
span->setAttributeMap(attrs.release());
m_current->parserAddChild(span);
span->attach();
return span.release();
}
void HTMLViewSourceDocument::addLine(const String& className)
{
// Create a table row.
RefPtr<HTMLTableRowElement> trow = HTMLTableRowElement::create(this);
m_tbody->parserAddChild(trow);
trow->attach();
// Create a cell that will hold the line number (it is generated in the stylesheet using counters).
RefPtr<HTMLTableCellElement> td = HTMLTableCellElement::create(tdTag, this);
RefPtr<NamedNodeMap> attrs = NamedNodeMap::create();
attrs->addAttribute(Attribute::createMapped(classAttr, "webkit-line-number"));
td->setAttributeMap(attrs.release());
trow->parserAddChild(td);
td->attach();
// Create a second cell for the line contents
td = HTMLTableCellElement::create(tdTag, this);
attrs = NamedNodeMap::create();
attrs->addAttribute(Attribute::createMapped(classAttr, "webkit-line-content"));
td->setAttributeMap(attrs.release());
trow->parserAddChild(td);
td->attach();
m_current = m_td = td;
#ifdef DEBUG_LINE_NUMBERS
RefPtr<Text> lineNumberText = Text::create(this, String::number(parser()->lineNumber() + 1) + " ");
td->addChild(lineNumberText);
lineNumberText->attach();
#endif
// Open up the needed spans.
if (!className.isEmpty()) {
if (className == "webkit-html-attribute-name" || className == "webkit-html-attribute-value")
m_current = addSpanWithClassName("webkit-html-tag");
m_current = addSpanWithClassName(className);
}
}
void HTMLViewSourceDocument::addText(const String& text, const String& className)
{
if (text.isEmpty())
return;
// Add in the content, splitting on newlines.
Vector<String> lines;
text.split('\n', true, lines);
unsigned size = lines.size();
for (unsigned i = 0; i < size; i++) {
String substring = lines[i];
if (substring.isEmpty()) {
if (i == size - 1)
break;
substring = " ";
}
if (m_current == m_tbody)
addLine(className);
RefPtr<Text> t = Text::create(this, substring);
m_current->parserAddChild(t);
t->attach();
if (i < size - 1)
m_current = m_tbody;
}
// Set current to m_tbody if the last character was a newline.
if (text[text.length() - 1] == '\n')
m_current = m_tbody;
}
int HTMLViewSourceDocument::addRange(const String& source, int start, int end, const String& className, bool isLink, bool isAnchor)
{
ASSERT(start <= end);
if (start == end)
return start;
String text = source.substring(start, end - start);
if (!className.isEmpty()) {
if (isLink)
m_current = addLink(text, isAnchor);
else
m_current = addSpanWithClassName(className);
}
addText(text, className);
if (!className.isEmpty() && m_current != m_tbody)
m_current = static_cast<Element*>(m_current->parent());
return end;
}
PassRefPtr<Element> HTMLViewSourceDocument::addLink(const String& url, bool isAnchor)
{
if (m_current == m_tbody)
addLine("webkit-html-tag");
// Now create a link for the attribute value instead of a span.
RefPtr<HTMLAnchorElement> anchor = HTMLAnchorElement::create(this);
RefPtr<NamedNodeMap> attrs = NamedNodeMap::create();
const char* classValue;
if (isAnchor)
classValue = "webkit-html-attribute-value webkit-html-external-link";
else
classValue = "webkit-html-attribute-value webkit-html-resource-link";
attrs->addAttribute(Attribute::createMapped(classAttr, classValue));
attrs->addAttribute(Attribute::createMapped(targetAttr, "_blank"));
attrs->addAttribute(Attribute::createMapped(hrefAttr, url));
anchor->setAttributeMap(attrs.release());
m_current->parserAddChild(anchor);
anchor->attach();
return anchor.release();
}
}
|