Changeset 63343 in webkit


Ignore:
Timestamp:
Jul 14, 2010 12:26:26 PM (14 years ago)
Author:
kinuko@chromium.org
Message:

2010-07-14 Kinuko Yasuda <kinuko@chromium.org>

Reviewed by Jian Li.

Separate line-ending conversion code from BlobItem
https://bugs.webkit.org/show_bug.cgi?id=40932

Add common line-ending normalization code under platform/text.
No new tests as it doesn't change any functionality.

  • CMakeLists.txt:
  • GNUmakefile.am:
  • WebCore.gypi:
  • WebCore.pro:
  • WebCore.vcproj/WebCore.vcproj:
  • WebCore.xcodeproj/project.pbxproj:
  • html/BlobBuilder.cpp: (WebCore::BlobBuilder::appendString):
  • html/FormDataList.cpp: (WebCore::FormDataList::appendString):
  • platform/BlobItem.cpp:
  • platform/BlobItem.h:
  • platform/text/LineEnding.cpp: Added.
  • platform/text/LineEnding.h: Added.
Location:
trunk/WebCore
Files:
2 added
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/CMakeLists.txt

    r63325 r63343  
    12921292    platform/text/Base64.cpp
    12931293    platform/text/BidiContext.cpp
     1294    platform/text/LineEnding.cpp
    12941295    platform/text/Hyphenation.cpp
    12951296    platform/text/RegularExpression.cpp
  • trunk/WebCore/ChangeLog

    r63342 r63343  
     12010-07-14  Kinuko Yasuda  <kinuko@chromium.org>
     2
     3        Reviewed by Jian Li.
     4
     5        Separate line-ending conversion code from BlobItem
     6        https://bugs.webkit.org/show_bug.cgi?id=40932
     7
     8        Add common line-ending normalization code under platform/text.
     9        No new tests as it doesn't change any functionality.
     10
     11        * CMakeLists.txt:
     12        * GNUmakefile.am:
     13        * WebCore.gypi:
     14        * WebCore.pro:
     15        * WebCore.vcproj/WebCore.vcproj:
     16        * WebCore.xcodeproj/project.pbxproj:
     17        * html/BlobBuilder.cpp:
     18        (WebCore::BlobBuilder::appendString):
     19        * html/FormDataList.cpp:
     20        (WebCore::FormDataList::appendString):
     21        * platform/BlobItem.cpp:
     22        * platform/BlobItem.h:
     23        * platform/text/LineEnding.cpp: Added.
     24        * platform/text/LineEnding.h: Added.
     25
    1262010-07-14  Erik Arvidsson  <arv@chromium.org>
    227
  • trunk/WebCore/GNUmakefile.am

    r63324 r63343  
    20922092        WebCore/platform/text/Hyphenation.cpp \
    20932093        WebCore/platform/text/Hyphenation.h \
     2094        WebCore/platform/text/LineEnding.cpp \
     2095        WebCore/platform/text/LineEnding.h \
    20942096        WebCore/platform/text/ParserUtilities.h \
    20952097        WebCore/platform/text/PlatformString.h \
  • trunk/WebCore/WebCore.gypi

    r63280 r63343  
    28372837            'platform/text/Hyphenation.cpp',
    28382838            'platform/text/Hyphenation.h',
     2839            'platform/text/LineEnding.cpp',
     2840            'platform/text/LineEnding.h',
    28392841            'platform/text/ParserUtilities.h',
    28402842            'platform/text/PlatformString.h',
  • trunk/WebCore/WebCore.pro

    r63278 r63343  
    898898    platform/KURL.cpp \
    899899    platform/Length.cpp \
     900    platform/text/LineEnding.cpp \
    900901    platform/LinkHash.cpp \
    901902    platform/Logging.cpp \
     
    16731674    platform/KURL.h \
    16741675    platform/Length.h \
     1676    platform/text/LineEnding.h \
    16751677    platform/LinkHash.h \
    16761678    platform/Logging.h \
  • trunk/WebCore/WebCore.vcproj/WebCore.vcproj

    r63278 r63343  
    2714527145                                </File>
    2714627146                                <File
     27147                                        RelativePath="..\platform\text\LineEnding.cpp"
     27148                                        >
     27149                                </File>
     27150                                <File
     27151                                        RelativePath="..\platform\text\LineEnding.h"
     27152                                        >
     27153                                </File>
     27154                                <File
    2714727155                                        RelativePath="..\platform\text\ParserUtilities.h"
    2714827156                                        >
  • trunk/WebCore/WebCore.xcodeproj/project.pbxproj

    r63278 r63343  
    23122312                8988E10E11A3508B00DB732E /* BlobItem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8988E10C11A3508B00DB732E /* BlobItem.cpp */; };
    23132313                8988E10F11A3508B00DB732E /* BlobItem.h in Headers */ = {isa = PBXBuildFile; fileRef = 8988E10D11A3508B00DB732E /* BlobItem.h */; settings = {ATTRIBUTES = (Private, ); }; };
     2314                89B5EAA111E8003D00F2367E /* LineEnding.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 89B5EA9F11E8003D00F2367E /* LineEnding.cpp */; };
     2315                89B5EAA211E8003D00F2367E /* LineEnding.h in Headers */ = {isa = PBXBuildFile; fileRef = 89B5EAA011E8003D00F2367E /* LineEnding.h */; settings = {ATTRIBUTES = (Private, ); }; };
    23142316                89BED5EB11BE11CE00448492 /* BlobBuilder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 89BED5E911BE11CE00448492 /* BlobBuilder.cpp */; };
    23152317                89BED5EC11BE11CE00448492 /* BlobBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = 89BED5EA11BE11CE00448492 /* BlobBuilder.h */; };
     
    80248026                8988E10C11A3508B00DB732E /* BlobItem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BlobItem.cpp; sourceTree = "<group>"; };
    80258027                8988E10D11A3508B00DB732E /* BlobItem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BlobItem.h; sourceTree = "<group>"; };
     8028                89B5EA9F11E8003D00F2367E /* LineEnding.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = LineEnding.cpp; sourceTree = "<group>"; };
     8029                89B5EAA011E8003D00F2367E /* LineEnding.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = LineEnding.h; sourceTree = "<group>"; };
    80268030                89BED5E911BE11CE00448492 /* BlobBuilder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = BlobBuilder.cpp; sourceTree = "<group>"; };
    80278031                89BED5EA11BE11CE00448492 /* BlobBuilder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BlobBuilder.h; sourceTree = "<group>"; };
     
    1550815512                                B2C3D9F50D006C1D00EF6F26 /* CharacterNames.h */,
    1550915513                                375CD231119D43C800A2A859 /* Hyphenation.h */,
     15514                                89B5EA9F11E8003D00F2367E /* LineEnding.cpp */,
     15515                                89B5EAA011E8003D00F2367E /* LineEnding.h */,
    1551015516                                BC76AC110DD7AD5C00415F34 /* ParserUtilities.h */,
    1551115517                                B2C3D9FB0D006C1D00EF6F26 /* PlatformString.h */,
     
    1891318919                                B22279650D00BF220071B782 /* LinearGradientAttributes.h in Headers */,
    1891418920                                AB31C91E10AE1B8E000C7B92 /* LineClampValue.h in Headers */,
     18921                                89B5EAA211E8003D00F2367E /* LineEnding.h in Headers */,
    1891518922                                A7AD2F880EC89D07008AB002 /* LinkHash.h in Headers */,
    1891618923                                BCB16C2D0979C3BD00467741 /* loader.h in Headers */,
     
    2141421421                                BCE65BEA0EACDF16007E4533 /* Length.cpp in Sources */,
    2141521422                                A7AD2F870EC89D07008AB002 /* LinkHash.cpp in Sources */,
     21423                                89B5EAA111E8003D00F2367E /* LineEnding.cpp in Sources */,
    2141621424                                BCB16C2C0979C3BD00467741 /* loader.cpp in Sources */,
    2141721425                                656D37330ADBA5DE00A4554D /* LoaderNSURLExtras.mm in Sources */,
  • trunk/WebCore/html/BlobBuilder.cpp

    r61650 r63343  
    3636#include "Blob.h"
    3737#include "ExceptionCode.h"
     38#include "LineEnding.h"
    3839#include "TextEncoding.h"
    3940
    4041namespace WebCore {
    4142
    42 static bool getLineEndingTypeFromString(const AtomicString& typeString, LineEnding& endingType)
     43static CString convertToCString(const String& text, const String& endingType, ExceptionCode& ec)
    4344{
    4445    DEFINE_STATIC_LOCAL(AtomicString, transparent, ("transparent"));
    4546    DEFINE_STATIC_LOCAL(AtomicString, native, ("native"));
    4647
    47     if (typeString.isEmpty() || typeString == transparent) {
    48         endingType = EndingTransparent;
    49         return true;
    50     }
    51     if (typeString == native) {
    52         endingType = EndingNative;
    53         return true;
    54     }
    55     return false;
     48    ec = 0;
     49
     50    if (endingType.isEmpty() || endingType == transparent)
     51        return UTF8Encoding().encode(text.characters(), text.length(), EntitiesForUnencodables);
     52    if (endingType == native)
     53        return normalizeLineEndingsToNative(UTF8Encoding().encode(text.characters(), text.length(), EntitiesForUnencodables));
     54
     55    ec = SYNTAX_ERR;
     56    return CString();
    5657}
    5758
    58 bool BlobBuilder::append(const String& text, const String& type, ExceptionCode& ec)
     59bool BlobBuilder::append(const String& text, const String& endingType, ExceptionCode& ec)
    5960{
    60     ec = 0;
    61     LineEnding endingType;
    62     if (!getLineEndingTypeFromString(type, endingType)) {
    63         ec = SYNTAX_ERR;
     61    CString cstr = convertToCString(text, endingType, ec);
     62    if (ec)
    6463        return false;
    65     }
    66     m_items.append(StringBlobItem::create(text, endingType, UTF8Encoding()));
     64
     65    m_items.append(StringBlobItem::create(cstr));
    6766    return true;
    6867}
  • trunk/WebCore/html/FormDataList.cpp

    r60799 r63343  
    2222#include "FormDataList.h"
    2323
     24#include "LineEnding.h"
     25
    2426namespace WebCore {
    2527
     
    3133void FormDataList::appendString(const String& s)
    3234{
    33     m_items.append(StringBlobItem::create(s, EndingCRLF, m_encoding));
     35    CString cstr = m_encoding.encode(s.characters(), s.length(), EntitiesForUnencodables);
     36    m_items.append(StringBlobItem::create(normalizeLineEndingsToCRLF(cstr)));
    3437}
    3538
  • trunk/WebCore/platform/BlobItem.cpp

    r62155 r63343  
    106106// StringBlobItem --------------------------------------------------------------
    107107
    108 PassRefPtr<BlobItem> StringBlobItem::create(const String& text, LineEnding ending, TextEncoding encoding)
    109 {
    110     return adoptRef(static_cast<BlobItem*>(new StringBlobItem(text, ending, encoding)));
    111 }
    112 
    113108PassRefPtr<BlobItem> StringBlobItem::create(const CString& text)
    114109{
     
    116111}
    117112
    118 StringBlobItem::StringBlobItem(const String& text, LineEnding ending, TextEncoding encoding)
    119     : m_data(StringBlobItem::convertToCString(text, ending, encoding))
    120 {
    121 }
    122 
    123113StringBlobItem::StringBlobItem(const CString& text)
    124114    : m_data(text)
    125115{
    126 }
    127 
    128 // Normalize all line-endings to CRLF.
    129 static CString convertToCRLF(const CString& from)
    130 {
    131     unsigned newLen = 0;
    132     const char* p = from.data();
    133     while (char c = *p++) {
    134         if (c == '\r') {
    135             // Safe to look ahead because of trailing '\0'.
    136             if (*p != '\n') {
    137                 // Turn CR into CRLF.
    138                 newLen += 2;
    139             }
    140         } else if (c == '\n') {
    141             // Turn LF into CRLF.
    142             newLen += 2;
    143         } else {
    144             // Leave other characters alone.
    145             newLen += 1;
    146         }
    147     }
    148     if (newLen == from.length())
    149         return from;
    150 
    151     // Make a copy of the string.
    152     p = from.data();
    153     char* q;
    154     CString result = CString::newUninitialized(newLen, q);
    155     while (char c = *p++) {
    156         if (c == '\r') {
    157             // Safe to look ahead because of trailing '\0'.
    158             if (*p != '\n') {
    159                 // Turn CR into CRLF.
    160                 *q++ = '\r';
    161                 *q++ = '\n';
    162             }
    163         } else if (c == '\n') {
    164             // Turn LF into CRLF.
    165             *q++ = '\r';
    166             *q++ = '\n';
    167         } else {
    168             // Leave other characters alone.
    169             *q++ = c;
    170         }
    171     }
    172     return result;
    173 }
    174 
    175 // Normalize all line-endings to CR or LF.
    176 static CString convertToCROrLF(const CString& from, bool toCR)
    177 {
    178     unsigned newLen = 0;
    179     bool needFix = false;
    180     const char* p = from.data();
    181     char fromEndingChar = toCR ? '\n' : '\r';
    182     char toEndingChar = toCR ? '\r' : '\n';
    183     while (char c = *p++) {
    184         if (c == '\r' && *p == '\n') {
    185             // Turn CRLF into CR or LF.
    186             p++;
    187             needFix = true;
    188         } else if (c == fromEndingChar) {
    189             // Turn CR/LF into LF/CR.
    190             needFix = true;
    191         }
    192         newLen += 1;
    193     }
    194     if (!needFix)
    195         return from;
    196 
    197     // Make a copy of the string.
    198     p = from.data();
    199     char* q;
    200     CString result = CString::newUninitialized(newLen, q);
    201     while (char c = *p++) {
    202         if (c == '\r' && *p == '\n') {
    203             // Turn CRLF or CR into CR or LF.
    204             p++;
    205             *q++ = toEndingChar;
    206         } else if (c == fromEndingChar) {
    207             // Turn CR/LF into LF/CR.
    208             *q++ = toEndingChar;
    209         } else {
    210             // Leave other characters alone.
    211             *q++ = c;
    212         }
    213     }
    214     return result;
    215 }
    216 
    217 CString StringBlobItem::convertToCString(const String& text, LineEnding ending, TextEncoding encoding)
    218 {
    219     CString from = encoding.encode(text.characters(), text.length(), EntitiesForUnencodables);
    220 
    221     if (ending == EndingNative) {
    222 #if OS(WINDOWS)
    223         ending = EndingCRLF;
    224 #else
    225         ending = EndingLF;
    226 #endif
    227     }
    228 
    229     switch (ending) {
    230     case EndingTransparent:
    231         return from;
    232     case EndingCRLF:
    233         return convertToCRLF(from);
    234     case EndingCR:
    235         return convertToCROrLF(from, true);
    236     case EndingLF:
    237         return convertToCROrLF(from, false);
    238     default:
    239         ASSERT_NOT_REACHED();
    240     }
    241 
    242     ASSERT_NOT_REACHED();
    243     return from;
    244116}
    245117
  • trunk/WebCore/platform/BlobItem.h

    r61149 r63343  
    3333
    3434#include "PlatformString.h"
    35 #include "TextEncoding.h"
    3635#include <wtf/OwnPtr.h>
    3736#include <wtf/PassRefPtr.h>
     
    4140
    4241namespace WebCore {
    43 
    44 // String ending types.
    45 enum LineEnding {
    46     EndingTransparent = 0,
    47     EndingNative,
    48     EndingLF,
    49     EndingCR,
    50     EndingCRLF,
    51 };
    5242
    5343class ByteArrayBlobItem;
     
    127117class StringBlobItem : public DataBlobItem {
    128118public:
    129     static PassRefPtr<BlobItem> create(const String&, LineEnding, TextEncoding);
    130119    static PassRefPtr<BlobItem> create(const CString&);
    131120    const CString& cstr() const { return m_data; }
     
    139128
    140129private:
    141     StringBlobItem(const String&, LineEnding, TextEncoding);
    142130    StringBlobItem(const CString&);
    143     static CString convertToCString(const String&, LineEnding, TextEncoding);
    144131    CString m_data;
    145132};
Note: See TracChangeset for help on using the changeset viewer.