1 /*
2 * Copyright (C) 2006, 2008, 2009, 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
20 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25 #include "config.h"
26 #include "core/html/HTMLViewSourceDocument.h"
27
28 #include "HTMLNames.h"
29 #include "core/dom/StyleEngine.h"
30 #include "core/dom/Text.h"
31 #include "core/html/HTMLAnchorElement.h"
32 #include "core/html/HTMLBRElement.h"
33 #include "core/html/HTMLBaseElement.h"
34 #include "core/html/HTMLBodyElement.h"
35 #include "core/html/HTMLDivElement.h"
36 #include "core/html/HTMLHeadElement.h"
37 #include "core/html/HTMLHtmlElement.h"
38 #include "core/html/HTMLSpanElement.h"
39 #include "core/html/HTMLTableCellElement.h"
40 #include "core/html/HTMLTableElement.h"
41 #include "core/html/HTMLTableRowElement.h"
42 #include "core/html/HTMLTableSectionElement.h"
43 #include "core/html/parser/HTMLToken.h"
44 #include "core/html/parser/HTMLViewSourceParser.h"
45
46 namespace WebCore {
47
48 using namespace HTMLNames;
49
HTMLViewSourceDocument(const DocumentInit & initializer,const String & mimeType)50 HTMLViewSourceDocument::HTMLViewSourceDocument(const DocumentInit& initializer, const String& mimeType)
51 : HTMLDocument(initializer)
52 , m_type(mimeType)
53 {
54 setIsViewSource(true);
55
56 // FIXME: Why do view-source pages need to load in quirks mode?
57 setCompatibilityMode(QuirksMode);
58 lockCompatibilityMode();
59 }
60
createParser()61 PassRefPtr<DocumentParser> HTMLViewSourceDocument::createParser()
62 {
63 return HTMLViewSourceParser::create(this, m_type);
64 }
65
createContainingTable()66 void HTMLViewSourceDocument::createContainingTable()
67 {
68 RefPtr<HTMLHtmlElement> html = HTMLHtmlElement::create(*this);
69 parserAppendChild(html);
70 RefPtr<HTMLHeadElement> head = HTMLHeadElement::create(*this);
71 html->parserAppendChild(head);
72 RefPtr<HTMLBodyElement> body = HTMLBodyElement::create(*this);
73 html->parserAppendChild(body);
74
75 // Create a line gutter div that can be used to make sure the gutter extends down the height of the whole
76 // document.
77 RefPtr<HTMLDivElement> div = HTMLDivElement::create(*this);
78 div->setAttribute(classAttr, "webkit-line-gutter-backdrop");
79 body->parserAppendChild(div);
80
81 RefPtr<HTMLTableElement> table = HTMLTableElement::create(*this);
82 body->parserAppendChild(table);
83 m_tbody = HTMLTableSectionElement::create(tbodyTag, *this);
84 table->parserAppendChild(m_tbody);
85 m_current = m_tbody;
86 m_lineNumber = 0;
87 }
88
addSource(const String & source,HTMLToken & token)89 void HTMLViewSourceDocument::addSource(const String& source, HTMLToken& token)
90 {
91 if (!m_current)
92 createContainingTable();
93
94 switch (token.type()) {
95 case HTMLToken::Uninitialized:
96 ASSERT_NOT_REACHED();
97 break;
98 case HTMLToken::DOCTYPE:
99 processDoctypeToken(source, token);
100 break;
101 case HTMLToken::EndOfFile:
102 processEndOfFileToken(source, token);
103 break;
104 case HTMLToken::StartTag:
105 case HTMLToken::EndTag:
106 processTagToken(source, token);
107 break;
108 case HTMLToken::Comment:
109 processCommentToken(source, token);
110 break;
111 case HTMLToken::Character:
112 processCharacterToken(source, token);
113 break;
114 }
115 }
116
processDoctypeToken(const String & source,HTMLToken &)117 void HTMLViewSourceDocument::processDoctypeToken(const String& source, HTMLToken&)
118 {
119 m_current = addSpanWithClassName("webkit-html-doctype");
120 addText(source, "webkit-html-doctype");
121 m_current = m_td;
122 }
123
processEndOfFileToken(const String & source,HTMLToken &)124 void HTMLViewSourceDocument::processEndOfFileToken(const String& source, HTMLToken&)
125 {
126 m_current = addSpanWithClassName("webkit-html-end-of-file");
127 addText(source, "webkit-html-end-of-file");
128 m_current = m_td;
129 }
130
processTagToken(const String & source,HTMLToken & token)131 void HTMLViewSourceDocument::processTagToken(const String& source, HTMLToken& token)
132 {
133 m_current = addSpanWithClassName("webkit-html-tag");
134
135 AtomicString tagName(token.name());
136
137 unsigned index = 0;
138 HTMLToken::AttributeList::const_iterator iter = token.attributes().begin();
139 while (index < source.length()) {
140 if (iter == token.attributes().end()) {
141 // We want to show the remaining characters in the token.
142 index = addRange(source, index, source.length(), "");
143 ASSERT(index == source.length());
144 break;
145 }
146
147 AtomicString name(iter->name);
148 String value = StringImpl::create8BitIfPossible(iter->value);
149
150 index = addRange(source, index, iter->nameRange.start - token.startIndex(), "");
151 index = addRange(source, index, iter->nameRange.end - token.startIndex(), "webkit-html-attribute-name");
152
153 if (tagName == baseTag && name == hrefAttr)
154 addBase(value);
155
156 index = addRange(source, index, iter->valueRange.start - token.startIndex(), "");
157
158 bool isLink = name == srcAttr || name == hrefAttr;
159 index = addRange(source, index, iter->valueRange.end - token.startIndex(), "webkit-html-attribute-value", isLink, tagName == aTag, value);
160
161 ++iter;
162 }
163 m_current = m_td;
164 }
165
processCommentToken(const String & source,HTMLToken &)166 void HTMLViewSourceDocument::processCommentToken(const String& source, HTMLToken&)
167 {
168 m_current = addSpanWithClassName("webkit-html-comment");
169 addText(source, "webkit-html-comment");
170 m_current = m_td;
171 }
172
processCharacterToken(const String & source,HTMLToken &)173 void HTMLViewSourceDocument::processCharacterToken(const String& source, HTMLToken&)
174 {
175 addText(source, "");
176 }
177
addSpanWithClassName(const AtomicString & className)178 PassRefPtr<Element> HTMLViewSourceDocument::addSpanWithClassName(const AtomicString& className)
179 {
180 if (m_current == m_tbody) {
181 addLine(className);
182 return m_current;
183 }
184
185 RefPtr<HTMLSpanElement> span = HTMLSpanElement::create(*this);
186 span->setAttribute(classAttr, className);
187 m_current->parserAppendChild(span);
188 return span.release();
189 }
190
addLine(const AtomicString & className)191 void HTMLViewSourceDocument::addLine(const AtomicString& className)
192 {
193 // Create a table row.
194 RefPtr<HTMLTableRowElement> trow = HTMLTableRowElement::create(*this);
195 m_tbody->parserAppendChild(trow);
196
197 // Create a cell that will hold the line number (it is generated in the stylesheet using counters).
198 RefPtr<HTMLTableCellElement> td = HTMLTableCellElement::create(tdTag, *this);
199 td->setAttribute(classAttr, "webkit-line-number");
200 td->setIntegralAttribute(valueAttr, ++m_lineNumber);
201 trow->parserAppendChild(td);
202
203 // Create a second cell for the line contents
204 td = HTMLTableCellElement::create(tdTag, *this);
205 td->setAttribute(classAttr, "webkit-line-content");
206 trow->parserAppendChild(td);
207 m_current = m_td = td;
208
209 // Open up the needed spans.
210 if (!className.isEmpty()) {
211 if (className == "webkit-html-attribute-name" || className == "webkit-html-attribute-value")
212 m_current = addSpanWithClassName("webkit-html-tag");
213 m_current = addSpanWithClassName(className);
214 }
215 }
216
finishLine()217 void HTMLViewSourceDocument::finishLine()
218 {
219 if (!m_current->hasChildNodes()) {
220 RefPtr<HTMLBRElement> br = HTMLBRElement::create(*this);
221 m_current->parserAppendChild(br);
222 }
223 m_current = m_tbody;
224 }
225
addText(const String & text,const AtomicString & className)226 void HTMLViewSourceDocument::addText(const String& text, const AtomicString& className)
227 {
228 if (text.isEmpty())
229 return;
230
231 // Add in the content, splitting on newlines.
232 Vector<String> lines;
233 text.split('\n', true, lines);
234 unsigned size = lines.size();
235 for (unsigned i = 0; i < size; i++) {
236 String substring = lines[i];
237 if (m_current == m_tbody)
238 addLine(className);
239 if (substring.isEmpty()) {
240 if (i == size - 1)
241 break;
242 finishLine();
243 continue;
244 }
245 RefPtr<Text> t = Text::create(*this, substring);
246 m_current->parserAppendChild(t);
247 if (i < size - 1)
248 finishLine();
249 }
250 }
251
addRange(const String & source,int start,int end,const String & className,bool isLink,bool isAnchor,const String & link)252 int HTMLViewSourceDocument::addRange(const String& source, int start, int end, const String& className, bool isLink, bool isAnchor, const String& link)
253 {
254 ASSERT(start <= end);
255 if (start == end)
256 return start;
257
258 String text = source.substring(start, end - start);
259 if (!className.isEmpty()) {
260 if (isLink)
261 m_current = addLink(link, isAnchor);
262 else
263 m_current = addSpanWithClassName(className);
264 }
265 addText(text, className);
266 if (!className.isEmpty() && m_current != m_tbody)
267 m_current = toElement(m_current->parentNode());
268 return end;
269 }
270
addBase(const AtomicString & href)271 PassRefPtr<Element> HTMLViewSourceDocument::addBase(const AtomicString& href)
272 {
273 RefPtr<HTMLBaseElement> base = HTMLBaseElement::create(*this);
274 base->setAttribute(hrefAttr, href);
275 m_current->parserAppendChild(base);
276 return base.release();
277 }
278
addLink(const AtomicString & url,bool isAnchor)279 PassRefPtr<Element> HTMLViewSourceDocument::addLink(const AtomicString& url, bool isAnchor)
280 {
281 if (m_current == m_tbody)
282 addLine("webkit-html-tag");
283
284 // Now create a link for the attribute value instead of a span.
285 RefPtr<HTMLAnchorElement> anchor = HTMLAnchorElement::create(*this);
286 const char* classValue;
287 if (isAnchor)
288 classValue = "webkit-html-attribute-value webkit-html-external-link";
289 else
290 classValue = "webkit-html-attribute-value webkit-html-resource-link";
291 anchor->setAttribute(classAttr, classValue);
292 anchor->setAttribute(targetAttr, "_blank");
293 anchor->setAttribute(hrefAttr, url);
294 m_current->parserAppendChild(anchor);
295 return anchor.release();
296 }
297
298 }
299