HTMLViewSourceDocument.cpp [plain text]
#include "config.h"
#include "HTMLViewSourceDocument.h"
#include "HTMLTokenizer.h"
#include "HTMLHtmlElement.h"
#include "HTMLBodyElement.h"
#include "HTMLPreElement.h"
#include "Text.h"
#include "HTMLNames.h"
namespace WebCore
{
using namespace HTMLNames;
HTMLViewSourceDocument::HTMLViewSourceDocument(DOMImplementation* implementation, FrameView* v)
: HTMLDocument(implementation, v), m_current(0)
{
}
Tokenizer* HTMLViewSourceDocument::createTokenizer()
{
return new HTMLTokenizer(this);
}
void HTMLViewSourceDocument::addViewSourceToken(Token* token)
{
if (!m_current) {
Element* html = new HTMLHtmlElement(this);
addChild(html);
html->attach();
Element* body = new HTMLBodyElement(this);
html->addChild(body);
body->attach();
Element* pre = new HTMLPreElement(preTag, this);
body->addChild(pre);
pre->attach();
m_current = pre;
}
if (token->tagName == textAtom) {
Text* t = new Text(this, token->text.get());
m_current->addChild(t);
t->attach();
} else if (token->tagName == commentAtom) {
} else {
Element* span = addSpanWithClassName("webkit-html-tag");
String text = "<";
if (!token->beginTag)
text += "/";
text += token->tagName;
if (!token->attrs)
text += ">";
Text* t = new Text(this, text);
span->addChild(t);
t->attach();
if (token->attrs) {
unsigned length = token->attrs->length();
for (unsigned i = 0; i < length; i++)
addViewSourceAttribute(token->attrs->attributeItem(i));
span = addSpanWithClassName("webkit-html-tag");
t = new Text(this, ">");
span->addChild(t);
t->attach();
}
}
}
void HTMLViewSourceDocument::addViewSourceAttribute(Attribute* attr)
{
Text* t = new Text(this, " ");
m_current->addChild(t);
t->attach();
Element* span = addSpanWithClassName("webkit-html-attribute-name");
String name = attr->name().toString();
if (!attr->value().isNull())
name += "=";
t = new Text(this, name);
span->addChild(t);
t->attach();
if (!attr->value().isNull()) {
Element* span = addSpanWithClassName("webkit-html-attribute-value");
String text = "\""; text += attr->value();
text += "\"";
t = new Text(this, text);
span->addChild(t);
t->attach();
}
}
Element* HTMLViewSourceDocument::addSpanWithClassName(const String& className)
{
Element* span = new HTMLElement(spanTag, this);
Attribute* a = new MappedAttribute(classAttr, className);
NamedMappedAttrMap* attrs = new NamedMappedAttrMap(0);
attrs->insertAttribute(a);
span->setAttributeMap(attrs);
m_current->addChild(span);
span->attach();
return span;
}
}