2 * Copyright (C) 2006, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 #include "core/html/HTMLViewSourceDocument.h"
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"
48 using namespace HTMLNames;
50 HTMLViewSourceDocument::HTMLViewSourceDocument(const DocumentInit& initializer, const String& mimeType)
51 : HTMLDocument(initializer)
54 setIsViewSource(true);
56 // FIXME: Why do view-source pages need to load in quirks mode?
57 setCompatibilityMode(QuirksMode);
58 lockCompatibilityMode();
61 PassRefPtr<DocumentParser> HTMLViewSourceDocument::createParser()
63 return HTMLViewSourceParser::create(this, m_type);
66 void HTMLViewSourceDocument::createContainingTable()
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);
75 // Create a line gutter div that can be used to make sure the gutter extends down the height of the whole
77 RefPtr<HTMLDivElement> div = HTMLDivElement::create(*this);
78 div->setAttribute(classAttr, "webkit-line-gutter-backdrop");
79 body->parserAppendChild(div);
81 RefPtr<HTMLTableElement> table = HTMLTableElement::create(*this);
82 body->parserAppendChild(table);
83 m_tbody = HTMLTableSectionElement::create(tbodyTag, *this);
84 table->parserAppendChild(m_tbody);
89 void HTMLViewSourceDocument::addSource(const String& source, HTMLToken& token)
92 createContainingTable();
94 switch (token.type()) {
95 case HTMLToken::Uninitialized:
98 case HTMLToken::DOCTYPE:
99 processDoctypeToken(source, token);
101 case HTMLToken::EndOfFile:
102 processEndOfFileToken(source, token);
104 case HTMLToken::StartTag:
105 case HTMLToken::EndTag:
106 processTagToken(source, token);
108 case HTMLToken::Comment:
109 processCommentToken(source, token);
111 case HTMLToken::Character:
112 processCharacterToken(source, token);
117 void HTMLViewSourceDocument::processDoctypeToken(const String& source, HTMLToken&)
119 m_current = addSpanWithClassName("webkit-html-doctype");
120 addText(source, "webkit-html-doctype");
124 void HTMLViewSourceDocument::processEndOfFileToken(const String& source, HTMLToken&)
126 m_current = addSpanWithClassName("webkit-html-end-of-file");
127 addText(source, "webkit-html-end-of-file");
131 void HTMLViewSourceDocument::processTagToken(const String& source, HTMLToken& token)
133 m_current = addSpanWithClassName("webkit-html-tag");
135 AtomicString tagName(token.name());
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(), emptyAtom);
143 ASSERT(index == source.length());
147 AtomicString name(iter->name);
148 AtomicString value(StringImpl::create8BitIfPossible(iter->value));
150 index = addRange(source, index, iter->nameRange.start - token.startIndex(), emptyAtom);
151 index = addRange(source, index, iter->nameRange.end - token.startIndex(), "webkit-html-attribute-name");
153 if (tagName == baseTag && name == hrefAttr)
156 index = addRange(source, index, iter->valueRange.start - token.startIndex(), emptyAtom);
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);
166 void HTMLViewSourceDocument::processCommentToken(const String& source, HTMLToken&)
168 m_current = addSpanWithClassName("webkit-html-comment");
169 addText(source, "webkit-html-comment");
173 void HTMLViewSourceDocument::processCharacterToken(const String& source, HTMLToken&)
178 PassRefPtr<Element> HTMLViewSourceDocument::addSpanWithClassName(const AtomicString& className)
180 if (m_current == m_tbody) {
185 RefPtr<HTMLSpanElement> span = HTMLSpanElement::create(*this);
186 span->setAttribute(classAttr, className);
187 m_current->parserAppendChild(span);
188 return span.release();
191 void HTMLViewSourceDocument::addLine(const AtomicString& className)
193 // Create a table row.
194 RefPtr<HTMLTableRowElement> trow = HTMLTableRowElement::create(*this);
195 m_tbody->parserAppendChild(trow);
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);
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;
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);
217 void HTMLViewSourceDocument::finishLine()
219 if (!m_current->hasChildren()) {
220 RefPtr<HTMLBRElement> br = HTMLBRElement::create(*this);
221 m_current->parserAppendChild(br);
226 void HTMLViewSourceDocument::addText(const String& text, const AtomicString& className)
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)
239 if (substring.isEmpty()) {
245 RefPtr<Text> t = Text::create(*this, substring);
246 m_current->parserAppendChild(t);
252 int HTMLViewSourceDocument::addRange(const String& source, int start, int end, const AtomicString& className, bool isLink, bool isAnchor, const AtomicString& link)
254 ASSERT(start <= end);
258 String text = source.substring(start, end - start);
259 if (!className.isEmpty()) {
261 m_current = addLink(link, isAnchor);
263 m_current = addSpanWithClassName(className);
265 addText(text, className);
266 if (!className.isEmpty() && m_current != m_tbody)
267 m_current = toElement(m_current->parentNode());
271 PassRefPtr<Element> HTMLViewSourceDocument::addBase(const AtomicString& href)
273 RefPtr<HTMLBaseElement> base = HTMLBaseElement::create(*this);
274 base->setAttribute(hrefAttr, href);
275 m_current->parserAppendChild(base);
276 return base.release();
279 PassRefPtr<Element> HTMLViewSourceDocument::addLink(const AtomicString& url, bool isAnchor)
281 if (m_current == m_tbody)
282 addLine("webkit-html-tag");
284 // Now create a link for the attribute value instead of a span.
285 RefPtr<HTMLAnchorElement> anchor = HTMLAnchorElement::create(*this);
286 const char* classValue;
288 classValue = "webkit-html-attribute-value webkit-html-external-link";
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();