Changeset 169573 in webkit


Ignore:
Timestamp:
Jun 3, 2014 3:44:26 PM (10 years ago)
Author:
yoav@yoav.ws
Message:

Refactor the srcset parser into its own file
https://bugs.webkit.org/show_bug.cgi?id=133480

Reviewed by Andreas Kling.

No new tests, since this patch doesn't change the parser's behavior.

This patch moves the srcset parser into its own file,
in order to prepare the code for alignment with recent spec changes.

  • CMakeLists.txt:
  • html/HTMLImageElement.cpp:
  • html/parser/HTMLParserIdioms.cpp:

(WebCore::compareByScaleFactor): Deleted.
(WebCore::parseDescriptors): Deleted.
(WebCore::parseImagesWithScaleFromSrcsetAttribute): Deleted.
(WebCore::bestFitSourceForImageAttributes): Deleted.

  • html/parser/HTMLParserIdioms.h:

(WebCore::ImageWithScale::ImageWithScale): Deleted.
(WebCore::ImageWithScale::imageURL): Deleted.
(WebCore::ImageWithScale::scaleFactor): Deleted.

  • html/parser/HTMLPreloadScanner.cpp:
  • html/parser/HTMLSrcsetParser.cpp: Added.

(WebCore::compareByScaleFactor):
(WebCore::parseDescriptors):
(WebCore::parseImagesWithScaleFromSrcsetAttribute):
(WebCore::bestFitSourceForImageAttributes):

  • html/parser/HTMLSrcsetParser.h: Added.

(WebCore::ImageWithScale::ImageWithScale):
(WebCore::ImageWithScale::imageURL):
(WebCore::ImageWithScale::scaleFactor):

Location:
trunk/Source/WebCore
Files:
2 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/CMakeLists.txt

    r169528 r169573  
    17071707    html/parser/HTMLScriptRunner.cpp
    17081708    html/parser/HTMLSourceTracker.cpp
     1709    html/parser/HTMLSrcsetParser.cpp
    17091710    html/parser/HTMLTokenizer.cpp
    17101711    html/parser/HTMLTreeBuilder.cpp
  • trunk/Source/WebCore/ChangeLog

    r169572 r169573  
     12014-06-03  Yoav Weiss  <yoav@yoav.ws>
     2
     3        Refactor the srcset parser into its own file
     4        https://bugs.webkit.org/show_bug.cgi?id=133480
     5
     6        Reviewed by Andreas Kling.
     7
     8        No new tests, since this patch doesn't change the parser's behavior.
     9
     10        This patch moves the srcset parser into its own file,
     11        in order to prepare the code for alignment with recent spec changes.
     12
     13        * CMakeLists.txt:
     14        * html/HTMLImageElement.cpp:
     15        * html/parser/HTMLParserIdioms.cpp:
     16        (WebCore::compareByScaleFactor): Deleted.
     17        (WebCore::parseDescriptors): Deleted.
     18        (WebCore::parseImagesWithScaleFromSrcsetAttribute): Deleted.
     19        (WebCore::bestFitSourceForImageAttributes): Deleted.
     20        * html/parser/HTMLParserIdioms.h:
     21        (WebCore::ImageWithScale::ImageWithScale): Deleted.
     22        (WebCore::ImageWithScale::imageURL): Deleted.
     23        (WebCore::ImageWithScale::scaleFactor): Deleted.
     24        * html/parser/HTMLPreloadScanner.cpp:
     25        * html/parser/HTMLSrcsetParser.cpp: Added.
     26        (WebCore::compareByScaleFactor):
     27        (WebCore::parseDescriptors):
     28        (WebCore::parseImagesWithScaleFromSrcsetAttribute):
     29        (WebCore::bestFitSourceForImageAttributes):
     30        * html/parser/HTMLSrcsetParser.h: Added.
     31        (WebCore::ImageWithScale::ImageWithScale):
     32        (WebCore::ImageWithScale::imageURL):
     33        (WebCore::ImageWithScale::scaleFactor):
     34
    1352014-06-03  Mario Sanchez Prada  <mario.prada@samsung.com>
    236
  • trunk/Source/WebCore/WebCore.vcxproj/WebCore.vcxproj

    r169380 r169573  
    1587015870    <ClCompile Include="..\html\parser\HTMLScriptRunner.cpp" />
    1587115871    <ClCompile Include="..\html\parser\HTMLSourceTracker.cpp" />
     15872    <ClCompile Include="..\html\parser\HTMLSrcsetParser.cpp" />
    1587215873    <ClCompile Include="..\html\parser\HTMLTokenizer.cpp" />
    1587315874    <ClCompile Include="..\html\parser\HTMLTreeBuilder.cpp" />
     
    2066720668    <ClInclude Include="..\html\parser\HTMLScriptRunnerHost.h" />
    2066820669    <ClInclude Include="..\html\parser\HTMLSourceTracker.h" />
     20670    <ClInclude Include="..\html\parser\HTMLSrcsetParser.h" />
    2066920671    <ClInclude Include="..\html\parser\HTMLStackItem.h" />
    2067020672    <ClInclude Include="..\html\parser\HTMLToken.h" />
  • trunk/Source/WebCore/WebCore.vcxproj/WebCore.vcxproj.filters

    r169328 r169573  
    41234123      <Filter>html\parser</Filter>
    41244124    </ClCompile>
     4125    <ClCompile Include="..\html\parser\HTMLSrcsetParser.cpp">
     4126      <Filter>html\parser</Filter>
     4127    </ClCompile>
    41254128    <ClCompile Include="..\html\parser\HTMLTokenizer.cpp">
    41264129      <Filter>html\parser</Filter>
     
    1120211205    </ClInclude>
    1120311206    <ClInclude Include="..\html\parser\HTMLSourceTracker.h">
     11207      <Filter>html\parser</Filter>
     11208    </ClInclude>
     11209    <ClInclude Include="..\html\parser\HTMLSrcsetParser.h">
    1120411210      <Filter>html\parser</Filter>
    1120511211    </ClInclude>
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r169409 r169573  
    20122012                5317612213C516690026E454 /* StyleFlexibleBoxData.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5317612013C516690026E454 /* StyleFlexibleBoxData.cpp */; };
    20132013                5317612313C516690026E454 /* StyleFlexibleBoxData.h in Headers */ = {isa = PBXBuildFile; fileRef = 5317612113C516690026E454 /* StyleFlexibleBoxData.h */; settings = {ATTRIBUTES = (Private, ); }; };
     2014                536D5A20193E18E900CE4CAB /* HTMLSrcsetParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 536D5A1F193E18E900CE4CAB /* HTMLSrcsetParser.h */; };
     2015                536D5A21193E18EE00CE4CAB /* HTMLSrcsetParser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 536D5A1E193E18D000CE4CAB /* HTMLSrcsetParser.cpp */; };
    20142016                53C8298D13D8D92700DE2DEB /* RenderFlexibleBox.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 53C8298B13D8D92700DE2DEB /* RenderFlexibleBox.cpp */; };
    20152017                53C8298E13D8D92700DE2DEB /* RenderFlexibleBox.h in Headers */ = {isa = PBXBuildFile; fileRef = 53C8298C13D8D92700DE2DEB /* RenderFlexibleBox.h */; settings = {ATTRIBUTES = (Private, ); }; };
     
    90739075                5317612013C516690026E454 /* StyleFlexibleBoxData.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = StyleFlexibleBoxData.cpp; path = style/StyleFlexibleBoxData.cpp; sourceTree = "<group>"; };
    90749076                5317612113C516690026E454 /* StyleFlexibleBoxData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = StyleFlexibleBoxData.h; path = style/StyleFlexibleBoxData.h; sourceTree = "<group>"; };
     9077                536D5A1E193E18D000CE4CAB /* HTMLSrcsetParser.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = HTMLSrcsetParser.cpp; path = parser/HTMLSrcsetParser.cpp; sourceTree = "<group>"; };
     9078                536D5A1F193E18E900CE4CAB /* HTMLSrcsetParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = HTMLSrcsetParser.h; path = parser/HTMLSrcsetParser.h; sourceTree = "<group>"; };
    90759079                53C8298B13D8D92700DE2DEB /* RenderFlexibleBox.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RenderFlexibleBox.cpp; sourceTree = "<group>"; };
    90769080                53C8298C13D8D92700DE2DEB /* RenderFlexibleBox.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RenderFlexibleBox.h; sourceTree = "<group>"; };
     
    1819018194                                977E2DCB12F0E28300C13379 /* HTMLSourceTracker.cpp */,
    1819118195                                977E2DCC12F0E28300C13379 /* HTMLSourceTracker.h */,
     18196                                536D5A1F193E18E900CE4CAB /* HTMLSrcsetParser.h */,
     18197                                536D5A1E193E18D000CE4CAB /* HTMLSrcsetParser.cpp */,
    1819218198                                97C1F552122855CB00EDE615 /* HTMLStackItem.h */,
    1819318199                                97C1F552122855CB00EDE616 /* HTMLToken.h */,
     
    2631226318                                A14832BB187F643200DA63A6 /* WAKWindow.h in Headers */,
    2631326319                                FD7F299213D4C0CB00AD9535 /* WaveShaperDSPKernel.h in Headers */,
     26320                                536D5A20193E18E900CE4CAB /* HTMLSrcsetParser.h in Headers */,
    2631426321                                FD7F299413D4C0CB00AD9535 /* WaveShaperNode.h in Headers */,
    2631526322                                FD7F299713D4C0CB00AD9535 /* WaveShaperProcessor.h in Headers */,
     
    2754427551                                6E0E569B183BFFE600E0E8D5 /* FloatRoundedRect.cpp in Sources */,
    2754527552                                B275356C0B053814002CE64F /* FloatSize.cpp in Sources */,
     27553                                536D5A21193E18EE00CE4CAB /* HTMLSrcsetParser.cpp in Sources */,
    2754627554                                B275355B0B053814002CE64F /* FloatSizeCG.cpp in Sources */,
    2754727555                                B275357A0B053814002CE64F /* FloatSizeMac.mm in Sources */,
  • trunk/Source/WebCore/html/HTMLImageElement.cpp

    r167786 r169573  
    3434#include "HTMLFormElement.h"
    3535#include "HTMLParserIdioms.h"
     36#include "HTMLSrcsetParser.h"
    3637#include "Page.h"
    3738#include "RenderImage.h"
  • trunk/Source/WebCore/html/parser/HTMLParserIdioms.cpp

    r165848 r169573  
    291291}
    292292
    293 typedef Vector<ImageWithScale> ImageCandidates;
    294 
    295 static inline bool compareByScaleFactor(const ImageWithScale& first, const ImageWithScale& second)
    296 {
    297     return first.scaleFactor() < second.scaleFactor();
    298 }
    299 
    300 static bool parseDescriptors(const String& attribute, size_t start, size_t end, float& imageScaleFactor)
    301 {
    302     size_t descriptorStart;
    303     size_t descriptorEnd;
    304     size_t position = start;
    305     bool isFoundScaleFactor = false;
    306     bool isEmptyDescriptor = !(end > start);
    307     bool isValid = false;
    308 
    309     while (position < end) {
    310         while (isHTMLSpace(attribute[position]) && position < end)
    311             ++position;
    312         descriptorStart = position;
    313         while (isNotHTMLSpace(attribute[position]) && position < end)
    314             ++position;
    315         descriptorEnd = position;
    316 
    317         // Leave if there is only whitespace at the end of the descriptors
    318         if (descriptorEnd <= descriptorStart)
    319             break;
    320 
    321         --descriptorEnd;
    322         // This part differs from the spec as the current implementation only supports pixel density descriptors.
    323         if (attribute[descriptorEnd] != 'x')
    324             continue;
    325 
    326         if (isFoundScaleFactor)
    327             return false;
    328 
    329         if (attribute.is8Bit())
    330             imageScaleFactor = charactersToFloat(attribute.characters8() + descriptorStart, descriptorEnd - descriptorStart, &isValid);
    331         else
    332             imageScaleFactor = charactersToFloat(attribute.characters16() + descriptorStart, descriptorEnd - descriptorStart, &isValid);
    333         isFoundScaleFactor = true;
    334     }
    335 
    336     return isEmptyDescriptor || isValid;
    337 }
    338 
    339 // See the specifications for more details about the algorithm to follow.
    340 // http://www.w3.org/TR/2013/WD-html-srcset-20130228/#processing-the-image-candidates.
    341 static void parseImagesWithScaleFromSrcsetAttribute(const String& srcsetAttribute, ImageCandidates& imageCandidates)
    342 {
    343     ASSERT(imageCandidates.isEmpty());
    344 
    345     size_t imageCandidateStart = 0;
    346     unsigned srcsetAttributeLength = srcsetAttribute.length();
    347 
    348     while (imageCandidateStart < srcsetAttributeLength) {
    349         float imageScaleFactor = 1;
    350         size_t separator;
    351 
    352         // 4. Splitting loop: Skip whitespace.
    353         size_t imageURLStart = srcsetAttribute.find(isNotHTMLSpace, imageCandidateStart);
    354         if (imageURLStart == notFound)
    355             break;
    356         // If The current candidate is either totally empty or only contains space, skipping.
    357         if (srcsetAttribute[imageURLStart] == ',') {
    358             imageCandidateStart = imageURLStart + 1;
    359             continue;
    360         }
    361         // 5. Collect a sequence of characters that are not space characters, and let that be url.
    362         size_t imageURLEnd = srcsetAttribute.find(isHTMLSpace, imageURLStart + 1);
    363         if (imageURLEnd == notFound) {
    364             imageURLEnd = srcsetAttributeLength;
    365             separator = srcsetAttributeLength;
    366         } else if (srcsetAttribute[imageURLEnd - 1] == ',') {
    367             --imageURLEnd;
    368             separator = imageURLEnd;
    369         } else {
    370             // 7. Collect a sequence of characters that are not "," (U+002C) characters, and let that be descriptors.
    371             size_t imageScaleStart = srcsetAttribute.find(isNotHTMLSpace, imageURLEnd + 1);
    372             imageScaleStart = (imageScaleStart == notFound) ? srcsetAttributeLength : imageScaleStart;
    373             size_t imageScaleEnd = srcsetAttribute.find(',' , imageScaleStart + 1);
    374             imageScaleEnd = (imageScaleEnd == notFound) ? srcsetAttributeLength : imageScaleEnd;
    375 
    376             if (!parseDescriptors(srcsetAttribute, imageScaleStart, imageScaleEnd, imageScaleFactor)) {
    377                 imageCandidateStart = imageScaleEnd + 1;
    378                 continue;
    379             }
    380             separator = imageScaleEnd;
    381         }
    382         ImageWithScale image(imageURLStart, imageURLEnd - imageURLStart, imageScaleFactor);
    383         imageCandidates.append(image);
    384         // 11. Return to the step labeled splitting loop.
    385         imageCandidateStart = separator + 1;
    386     }
    387 }
    388 
    389 ImageWithScale bestFitSourceForImageAttributes(float deviceScaleFactor, const String& srcAttribute, const String& srcsetAttribute)
    390 {
    391     ImageCandidates imageCandidates;
    392 
    393     parseImagesWithScaleFromSrcsetAttribute(srcsetAttribute, imageCandidates);
    394 
    395     if (!srcAttribute.isEmpty()) {
    396         ImageWithScale srcPlaceholderImage;
    397         imageCandidates.append(srcPlaceholderImage);
    398     }
    399 
    400     if (imageCandidates.isEmpty())
    401         return ImageWithScale();
    402 
    403     std::stable_sort(imageCandidates.begin(), imageCandidates.end(), compareByScaleFactor);
    404 
    405     for (size_t i = 0; i < imageCandidates.size() - 1; ++i) {
    406         if (imageCandidates[i].scaleFactor() >= deviceScaleFactor)
    407             return imageCandidates[i];
    408     }
    409 
    410     // 16. If an entry b in candidates has the same associated ... pixel density as an earlier entry a in candidates,
    411     // then remove entry b
    412     size_t winner = imageCandidates.size() - 1;
    413     size_t previousCandidate = winner;
    414     float winningScaleFactor = imageCandidates.last().scaleFactor();
    415     while ((previousCandidate > 0) && (imageCandidates[--previousCandidate].scaleFactor() == winningScaleFactor))
    416         winner = previousCandidate;
    417 
    418     return imageCandidates[winner];
    419 }
    420 
    421 }
     293}
  • trunk/Source/WebCore/html/parser/HTMLParserIdioms.h

    r163415 r169573  
    3333
    3434class Decimal;
    35 
    36 class ImageWithScale {
    37 public:
    38     ImageWithScale()
    39         : m_imageURLStart(0)
    40         , m_imageURLLength(0)
    41         , m_scaleFactor(1)
    42     {
    43     }
    44 
    45     ImageWithScale(unsigned start, unsigned length, float scaleFactor)
    46         : m_imageURLStart(start)
    47         , m_imageURLLength(length)
    48         , m_scaleFactor(scaleFactor)
    49     {
    50     }
    51 
    52     String imageURL(const String& srcAttribute, const String& srcsetAttribute) const
    53     {
    54         return m_imageURLLength ? srcsetAttribute.substringSharingImpl(m_imageURLStart, m_imageURLLength) : srcAttribute;
    55     }
    56 
    57     float scaleFactor() const
    58     {
    59         return m_scaleFactor;
    60     }
    61 
    62 private:
    63     unsigned m_imageURLStart;
    64     unsigned m_imageURLLength;
    65     float m_scaleFactor;
    66 };
    6735
    6836// Space characters as defined by the HTML specification.
     
    132100bool threadSafeMatch(const QualifiedName&, const QualifiedName&);
    133101
    134 ImageWithScale bestFitSourceForImageAttributes(float deviceScaleFactor, const String& srcAttribute, const String& sourceSetAttribute);
    135 
    136102}
    137103
  • trunk/Source/WebCore/html/parser/HTMLPreloadScanner.cpp

    r163415 r169573  
    3131#include "HTMLNames.h"
    3232#include "HTMLParserIdioms.h"
     33#include "HTMLSrcsetParser.h"
    3334#include "HTMLTokenizer.h"
    3435#include "InputTypeNames.h"
Note: See TracChangeset for help on using the changeset viewer.