diff options
Diffstat (limited to 'Source/WebCore/html/FTPDirectoryDocument.cpp')
-rw-r--r-- | Source/WebCore/html/FTPDirectoryDocument.cpp | 196 |
1 files changed, 96 insertions, 100 deletions
diff --git a/Source/WebCore/html/FTPDirectoryDocument.cpp b/Source/WebCore/html/FTPDirectoryDocument.cpp index ac29b3aef..86cb3024c 100644 --- a/Source/WebCore/html/FTPDirectoryDocument.cpp +++ b/Source/WebCore/html/FTPDirectoryDocument.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. + * Copyright (C) 2007-2008, 2014-2015 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -10,10 +10,10 @@ * 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 + * THIS SOFTWARE IS PROVIDED BY APPLE 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 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE 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 @@ -23,12 +23,14 @@ */ #include "config.h" -#if ENABLE(FTPDIR) #include "FTPDirectoryDocument.h" -#include "ExceptionCodePlaceholder.h" +#if ENABLE(FTPDIR) + +#include "HTMLAnchorElement.h" +#include "HTMLBodyElement.h" #include "HTMLDocumentParser.h" -#include "HTMLNames.h" +#include "HTMLTableCellElement.h" #include "HTMLTableElement.h" #include "LocalizedStrings.h" #include "Logging.h" @@ -36,10 +38,8 @@ #include "Settings.h" #include "SharedBuffer.h" #include "Text.h" -#include <wtf/CurrentTime.h> #include <wtf/GregorianDateTime.h> #include <wtf/StdLibExtras.h> -#include <wtf/text/CString.h> #include <wtf/unicode/CharacterNames.h> namespace WebCore { @@ -48,17 +48,19 @@ using namespace HTMLNames; class FTPDirectoryDocumentParser final : public HTMLDocumentParser { public: - static PassRefPtr<FTPDirectoryDocumentParser> create(HTMLDocument& document) + static Ref<FTPDirectoryDocumentParser> create(HTMLDocument& document) { - return adoptRef(new FTPDirectoryDocumentParser(document)); + return adoptRef(*new FTPDirectoryDocumentParser(document)); } - virtual void append(PassRefPtr<StringImpl>) override; - virtual void finish() override; +private: + void append(RefPtr<StringImpl>&&) override; + void finish() override; - virtual bool isWaitingForScripts() const override { return false; } + // FIXME: Why do we need this? + bool isWaitingForScripts() const override { return false; } - inline void checkBuffer(int len = 10) + void checkBuffer(int len = 10) { if ((m_dest - m_buffer) > m_size - len) { // Enlarge buffer @@ -69,8 +71,7 @@ public: m_size = newSize; } } - -private: + FTPDirectoryDocumentParser(HTMLDocument&); // The parser will attempt to load the document template specified via the preference @@ -81,13 +82,13 @@ private: void parseAndAppendOneLine(const String&); void appendEntry(const String& name, const String& size, const String& date, bool isDirectory); - PassRefPtr<Element> createTDForFilename(const String&); + Ref<Element> createTDForFilename(const String&); RefPtr<HTMLTableElement> m_tableElement; - bool m_skipLF; + bool m_skipLF { false }; - int m_size; + int m_size { 254 }; UChar* m_buffer; UChar* m_dest; String m_carryOver; @@ -97,8 +98,6 @@ private: FTPDirectoryDocumentParser::FTPDirectoryDocumentParser(HTMLDocument& document) : HTMLDocumentParser(document) - , m_skipLF(false) - , m_size(254) , m_buffer(static_cast<UChar*>(fastMalloc(sizeof(UChar) * m_size))) , m_dest(m_buffer) { @@ -106,54 +105,58 @@ FTPDirectoryDocumentParser::FTPDirectoryDocumentParser(HTMLDocument& document) void FTPDirectoryDocumentParser::appendEntry(const String& filename, const String& size, const String& date, bool isDirectory) { - RefPtr<Element> rowElement = m_tableElement->insertRow(-1, IGNORE_EXCEPTION); - rowElement->setAttribute("class", "ftpDirectoryEntryRow", IGNORE_EXCEPTION); + auto& document = *this->document(); - RefPtr<Element> element = document()->createElement(tdTag, false); - element->appendChild(Text::create(*document(), String(&noBreakSpace, 1)), IGNORE_EXCEPTION); + auto rowElement = m_tableElement->insertRow(-1).releaseReturnValue(); + rowElement->setAttributeWithoutSynchronization(HTMLNames::classAttr, AtomicString("ftpDirectoryEntryRow", AtomicString::ConstructFromLiteral)); + + auto typeElement = HTMLTableCellElement::create(tdTag, document); + typeElement->appendChild(Text::create(document, String(&noBreakSpace, 1))); if (isDirectory) - element->setAttribute("class", "ftpDirectoryIcon ftpDirectoryTypeDirectory", IGNORE_EXCEPTION); + typeElement->setAttributeWithoutSynchronization(HTMLNames::classAttr, AtomicString("ftpDirectoryIcon ftpDirectoryTypeDirectory", AtomicString::ConstructFromLiteral)); else - element->setAttribute("class", "ftpDirectoryIcon ftpDirectoryTypeFile", IGNORE_EXCEPTION); - rowElement->appendChild(element, IGNORE_EXCEPTION); - - element = createTDForFilename(filename); - element->setAttribute("class", "ftpDirectoryFileName", IGNORE_EXCEPTION); - rowElement->appendChild(element, IGNORE_EXCEPTION); - - element = document()->createElement(tdTag, false); - element->appendChild(Text::create(*document(), date), IGNORE_EXCEPTION); - element->setAttribute("class", "ftpDirectoryFileDate", IGNORE_EXCEPTION); - rowElement->appendChild(element, IGNORE_EXCEPTION); - - element = document()->createElement(tdTag, false); - element->appendChild(Text::create(*document(), size), IGNORE_EXCEPTION); - element->setAttribute("class", "ftpDirectoryFileSize", IGNORE_EXCEPTION); - rowElement->appendChild(element, IGNORE_EXCEPTION); + typeElement->setAttributeWithoutSynchronization(HTMLNames::classAttr, AtomicString("ftpDirectoryIcon ftpDirectoryTypeFile", AtomicString::ConstructFromLiteral)); + rowElement->appendChild(typeElement); + + auto nameElement = createTDForFilename(filename); + nameElement->setAttributeWithoutSynchronization(HTMLNames::classAttr, AtomicString("ftpDirectoryFileName", AtomicString::ConstructFromLiteral)); + rowElement->appendChild(nameElement); + + auto dateElement = HTMLTableCellElement::create(tdTag, document); + dateElement->appendChild(Text::create(document, date)); + dateElement->setAttributeWithoutSynchronization(HTMLNames::classAttr, AtomicString("ftpDirectoryFileDate", AtomicString::ConstructFromLiteral)); + rowElement->appendChild(dateElement); + + auto sizeElement = HTMLTableCellElement::create(tdTag, document); + sizeElement->appendChild(Text::create(document, size)); + sizeElement->setAttributeWithoutSynchronization(HTMLNames::classAttr, AtomicString("ftpDirectoryFileSize", AtomicString::ConstructFromLiteral)); + rowElement->appendChild(sizeElement); } -PassRefPtr<Element> FTPDirectoryDocumentParser::createTDForFilename(const String& filename) +Ref<Element> FTPDirectoryDocumentParser::createTDForFilename(const String& filename) { - String fullURL = document()->baseURL().string(); - if (fullURL[fullURL.length() - 1] == '/') - fullURL.append(filename); + auto& document = *this->document(); + + String fullURL = document.baseURL().string(); + if (fullURL.endsWith('/')) + fullURL = fullURL + filename; else - fullURL.append("/" + filename); + fullURL = fullURL + '/' + filename; - RefPtr<Element> anchorElement = document()->createElement(aTag, false); - anchorElement->setAttribute("href", fullURL, IGNORE_EXCEPTION); - anchorElement->appendChild(Text::create(*document(), filename), IGNORE_EXCEPTION); + auto anchorElement = HTMLAnchorElement::create(document); + anchorElement->setAttributeWithoutSynchronization(HTMLNames::hrefAttr, fullURL); + anchorElement->appendChild(Text::create(document, filename)); - RefPtr<Element> tdElement = document()->createElement(tdTag, false); - tdElement->appendChild(anchorElement, IGNORE_EXCEPTION); + auto tdElement = HTMLTableCellElement::create(tdTag, document); + tdElement->appendChild(anchorElement); - return tdElement.release(); + return WTFMove(tdElement); } static String processFilesizeString(const String& size, bool isDirectory) { if (isDirectory) - return "--"; + return ASCIILiteral("--"); bool valid; int64_t bytes = size.toUInt64(&valid); @@ -171,7 +174,7 @@ static String processFilesizeString(const String& size, bool isDirectory) static bool wasLastDayOfMonth(int year, int month, int day) { - static int lastDays[] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; + static const int lastDays[] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (month < 0 || month > 11) return false; @@ -241,9 +244,9 @@ static String processFileDateString(const FTPTime& fileTime) String dateString; if (fileTime.tm_year > -1) - dateString = String(months[month]) + " " + String::number(fileTime.tm_mday) + ", " + String::number(fileTime.tm_year); + dateString = makeString(months[month], ' ', String::number(fileTime.tm_mday), ", ", String::number(fileTime.tm_year)); else - dateString = String(months[month]) + " " + String::number(fileTime.tm_mday) + ", " + String::number(now.year()); + dateString = makeString(months[month], ' ', String::number(fileTime.tm_mday), ", ", String::number(now.year())); return dateString + timeOfDay; } @@ -261,7 +264,7 @@ void FTPDirectoryDocumentParser::parseAndAppendOneLine(const String& inputLine) String filename(result.filename, result.filenameLength); if (result.type == FTPDirectoryEntry) { - filename.append("/"); + filename.append('/'); // We have no interest in linking to "current directory" if (filename == "./") @@ -273,22 +276,19 @@ void FTPDirectoryDocumentParser::parseAndAppendOneLine(const String& inputLine) appendEntry(filename, processFilesizeString(result.fileSize, result.type == FTPDirectoryEntry), processFileDateString(result.modifiedTime), result.type == FTPDirectoryEntry); } -static inline PassRefPtr<SharedBuffer> createTemplateDocumentData(Settings* settings) +static inline RefPtr<SharedBuffer> createTemplateDocumentData(const Settings& settings) { - RefPtr<SharedBuffer> buffer = 0; - if (settings) - buffer = SharedBuffer::createWithContentsOfFile(settings->ftpDirectoryTemplatePath()); + RefPtr<SharedBuffer> buffer = SharedBuffer::createWithContentsOfFile(settings.ftpDirectoryTemplatePath()); if (buffer) LOG(FTP, "Loaded FTPDirectoryTemplate of length %i\n", buffer->size()); - return buffer.release(); + return buffer; } bool FTPDirectoryDocumentParser::loadDocumentTemplate() { static SharedBuffer* templateDocumentData = createTemplateDocumentData(document()->settings()).leakRef(); - // FIXME: Instead of storing the data, we'd rather actually parse the template data into the template Document once, - // store that document, then "copy" it whenever we get an FTP directory listing. There are complexities with this - // approach that make it worth putting this off. + // FIXME: Instead of storing the data, it would be more efficient if we could parse the template data into the + // template Document once, store that document, then "copy" it whenever we get an FTP directory listing. if (!templateDocumentData) { LOG_ERROR("Could not load templateData"); @@ -297,30 +297,27 @@ bool FTPDirectoryDocumentParser::loadDocumentTemplate() HTMLDocumentParser::insert(String(templateDocumentData->data(), templateDocumentData->size())); - RefPtr<Element> tableElement = document()->getElementById("ftpDirectoryTable"); - if (!tableElement) + auto& document = *this->document(); + + auto* foundElement = document.getElementById(String(ASCIILiteral("ftpDirectoryTable"))); + if (!foundElement) LOG_ERROR("Unable to find element by id \"ftpDirectoryTable\" in the template document."); - else if (!isHTMLTableElement(tableElement.get())) + else if (!is<HTMLTableElement>(*foundElement)) LOG_ERROR("Element of id \"ftpDirectoryTable\" is not a table element"); - else - m_tableElement = toHTMLTableElement(tableElement.get()); - - // Bail if we found the table element - if (m_tableElement) + else { + m_tableElement = downcast<HTMLTableElement>(foundElement); return true; + } - // Otherwise create one manually - tableElement = document()->createElement(tableTag, false); - m_tableElement = toHTMLTableElement(tableElement.get()); - m_tableElement->setAttribute("id", "ftpDirectoryTable", IGNORE_EXCEPTION); + m_tableElement = HTMLTableElement::create(document); + m_tableElement->setAttributeWithoutSynchronization(HTMLNames::idAttr, AtomicString("ftpDirectoryTable", AtomicString::ConstructFromLiteral)); - // If we didn't find the table element, lets try to append our own to the body - // If that fails for some reason, cram it on the end of the document as a last - // ditch effort - if (Element* body = document()->body()) - body->appendChild(m_tableElement, IGNORE_EXCEPTION); + // If we didn't find the table element, lets try to append our own to the body. + // If that fails for some reason, cram it on the end of the document as a last ditch effort. + if (auto* body = document.bodyOrFrameset()) + body->appendChild(*m_tableElement); else - document()->appendChild(m_tableElement, IGNORE_EXCEPTION); + document.appendChild(*m_tableElement); return true; } @@ -329,23 +326,22 @@ void FTPDirectoryDocumentParser::createBasicDocument() { LOG(FTP, "Creating a basic FTP document structure as no template was loaded"); - // FIXME: Make this "basic document" more acceptable + auto& document = *this->document(); - RefPtr<Element> bodyElement = document()->createElement(bodyTag, false); + auto bodyElement = HTMLBodyElement::create(document); + document.appendChild(bodyElement); - document()->appendChild(bodyElement, IGNORE_EXCEPTION); + m_tableElement = HTMLTableElement::create(document); + m_tableElement->setAttributeWithoutSynchronization(HTMLNames::idAttr, AtomicString("ftpDirectoryTable", AtomicString::ConstructFromLiteral)); + m_tableElement->setAttribute(HTMLNames::styleAttr, AtomicString("width:100%", AtomicString::ConstructFromLiteral)); - RefPtr<Element> tableElement = document()->createElement(tableTag, false); - m_tableElement = toHTMLTableElement(tableElement.get()); - m_tableElement->setAttribute("id", "ftpDirectoryTable", IGNORE_EXCEPTION); + bodyElement->appendChild(*m_tableElement); - bodyElement->appendChild(m_tableElement, IGNORE_EXCEPTION); + document.processViewport("width=device-width", ViewportArguments::ViewportMeta); } -void FTPDirectoryDocumentParser::append(PassRefPtr<StringImpl> inputSource) +void FTPDirectoryDocumentParser::append(RefPtr<StringImpl>&& inputSource) { - String source(inputSource); - // Make sure we have the table element to append to by loading the template set in the pref, or // creating a very basic document with the appropriate table if (!m_tableElement) { @@ -357,9 +353,9 @@ void FTPDirectoryDocumentParser::append(PassRefPtr<StringImpl> inputSource) bool foundNewLine = false; m_dest = m_buffer; - SegmentedString str = source; - while (!str.isEmpty()) { - UChar c = str.currentChar(); + SegmentedString string { String { WTFMove(inputSource) } }; + while (!string.isEmpty()) { + UChar c = string.currentCharacter(); if (c == '\r') { *m_dest++ = '\n'; @@ -376,7 +372,7 @@ void FTPDirectoryDocumentParser::append(PassRefPtr<StringImpl> inputSource) m_skipLF = false; } - str.advance(); + string.advance(); // Maybe enlarge the buffer checkBuffer(); @@ -415,7 +411,7 @@ void FTPDirectoryDocumentParser::finish() m_carryOver = String(); } - m_tableElement = 0; + m_tableElement = nullptr; fastFree(m_buffer); HTMLDocumentParser::finish(); @@ -429,7 +425,7 @@ FTPDirectoryDocument::FTPDirectoryDocument(Frame* frame, const URL& url) #endif } -PassRefPtr<DocumentParser> FTPDirectoryDocument::createParser() +Ref<DocumentParser> FTPDirectoryDocument::createParser() { return FTPDirectoryDocumentParser::create(*this); } |