Changeset 171941 in webkit


Ignore:
Timestamp:
Aug 1, 2014 12:08:59 PM (10 years ago)
Author:
mmaxfield@apple.com
Message:

URLs in srcset attributes are not made absolute upon copy and paste
https://bugs.webkit.org/show_bug.cgi?id=135448

Reviewed by Ryosuke Niwa.

Source/WebCore:
When pasting, canonicalize URLs in srcset the same way we do with src.

Test: editing/pasteboard/img-srcset-copy-paste-canonicalization.html

  • dom/Element.cpp:

(WebCore::Element::completeURLsInAttributeValue): Initial implemention, moved from markup.cpp.

  • dom/Element.h:

(WebCore::Element::attributeContainsURL): New function for completeURLs to call.
(WebCore::Element::completeURLsInAttributeValue): Only called if attributeContainsURL returns
true. Default implementation simply calls isURLAttribute().

  • editing/markup.cpp:

(WebCore::completeURLs): Call attributeContainsURL() and completeURLsInAttributeValue() to
complete the URL, so nodes can perform their own behavior.

  • html/HTMLImageElement.cpp:

(WebCore::HTMLImageElement::attributeContainsURL): Return true for srcset.
(WebCore::HTMLImageElement::completeUrlAttributeValue): Use our existing srcset parser to
parse the srcset attribute, then use its output to canonicalize URLs, and build it back up
into a string.

  • html/HTMLImageElement.h:

(WebCore::HTMLImageElement::attributeContainsURL):
(WebCore::HTMLImageElement::completeUrlAttributeValue):

  • html/parser/HTMLSrcsetParser.cpp: Make parseImageCandidatesFromSrcsetAttribute() public

and change its signature to return its result.
(WebCore::parseImageCandidatesFromSrcsetAttribute):

  • html/parser/HTMLSrcsetParser.h: Ditto.

LayoutTests:
Copy and paste a srcset image with relative URLs, and make sure that the
pasted srcset attribute doesn't match what it was before. I can't actually
dump the new srcset because it will include a full path of the file on the
user's system, and would therefore be machine-specific.

  • editing/pasteboard/img-srcset-copy-paste-canonicalization-expected.txt:
  • editing/pasteboard/img-srcset-copy-paste-canonicalization.html: Paste and check.
  • editing/pasteboard/resources/img-srcset-copy-paste-canonicalization-iframe.html:

This has to be an iframe because we don't perform any url canonicalization if we
are copying and pasting from a document into itself.

Location:
trunk
Files:
3 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r171916 r171941  
     12014-07-30  Myles C. Maxfield  <mmaxfield@apple.com>
     2
     3        URLs in srcset attributes are not made absolute upon copy and paste
     4        https://bugs.webkit.org/show_bug.cgi?id=135448
     5
     6        Reviewed by Ryosuke Niwa.
     7
     8        Copy and paste a srcset image with relative URLs, and make sure that the
     9        pasted srcset attribute doesn't match what it was before. I can't actually
     10        dump the new srcset because it will include a full path of the file on the
     11        user's system, and would therefore be machine-specific.
     12
     13        * editing/pasteboard/img-srcset-copy-paste-canonicalization-expected.txt:
     14        * editing/pasteboard/img-srcset-copy-paste-canonicalization.html: Paste and check.
     15        * editing/pasteboard/resources/img-srcset-copy-paste-canonicalization-iframe.html:
     16        This has to be an iframe because we don't perform any url canonicalization if we
     17        are copying and pasting from a document into itself.
     18
    1192014-08-01  Michał Pakuła vel Rutka  <m.pakula@samsung.com>
    220
  • trunk/Source/WebCore/ChangeLog

    r171939 r171941  
     12014-07-30  Myles C. Maxfield  <mmaxfield@apple.com>
     2
     3        URLs in srcset attributes are not made absolute upon copy and paste
     4        https://bugs.webkit.org/show_bug.cgi?id=135448
     5
     6        Reviewed by Ryosuke Niwa.
     7
     8        When pasting, canonicalize URLs in srcset the same way we do with src.
     9
     10        Test: editing/pasteboard/img-srcset-copy-paste-canonicalization.html
     11
     12        * dom/Element.cpp:
     13        (WebCore::Element::completeURLsInAttributeValue): Initial implemention, moved from markup.cpp.
     14        * dom/Element.h:
     15        (WebCore::Element::attributeContainsURL): New function for completeURLs to call.
     16        (WebCore::Element::completeURLsInAttributeValue): Only called if attributeContainsURL returns
     17        true. Default implementation simply calls isURLAttribute().
     18        * editing/markup.cpp:
     19        (WebCore::completeURLs): Call attributeContainsURL() and completeURLsInAttributeValue() to
     20        complete the URL, so nodes can perform their own behavior.
     21        * html/HTMLImageElement.cpp:
     22        (WebCore::HTMLImageElement::attributeContainsURL): Return true for srcset.
     23        (WebCore::HTMLImageElement::completeUrlAttributeValue): Use our existing srcset parser to
     24        parse the srcset attribute, then use its output to canonicalize URLs, and build it back up
     25        into a string.
     26        * html/HTMLImageElement.h:
     27        (WebCore::HTMLImageElement::attributeContainsURL):
     28        (WebCore::HTMLImageElement::completeUrlAttributeValue):
     29        * html/parser/HTMLSrcsetParser.cpp: Make parseImageCandidatesFromSrcsetAttribute() public
     30        and change its signature to return its result.
     31        (WebCore::parseImageCandidatesFromSrcsetAttribute):
     32        * html/parser/HTMLSrcsetParser.h: Ditto.
     33
    1342014-07-31  Andreas Kling  <akling@apple.com>
    235
  • trunk/Source/WebCore/dom/Element.cpp

    r171014 r171941  
    30233023}
    30243024
     3025String Element::completeURLsInAttributeValue(const URL& base, const Attribute& attribute) const
     3026{
     3027    return URL(base, attribute.value()).string();
     3028}
     3029
    30253030} // namespace WebCore
  • trunk/Source/WebCore/dom/Element.h

    r171014 r171941  
    394394
    395395    virtual bool isURLAttribute(const Attribute&) const { return false; }
     396    virtual bool attributeContainsURL(const Attribute& attribute) const { return isURLAttribute(attribute); }
     397    virtual String completeURLsInAttributeValue(const URL& base, const Attribute&) const;
    396398    virtual bool isHTMLContentAttribute(const Attribute&) const { return false; }
    397399
  • trunk/Source/WebCore/editing/markup.cpp

    r166150 r171941  
    105105            continue;
    106106        for (const Attribute& attribute : element.attributesIterator()) {
    107             if (element.isURLAttribute(attribute) && !attribute.value().isEmpty())
    108                 changes.append(AttributeChange(&element, attribute.name(), URL(parsedBaseURL, attribute.value()).string()));
     107            if (element.attributeContainsURL(attribute) && !attribute.value().isEmpty())
     108                changes.append(AttributeChange(&element, attribute.name(), element.completeURLsInAttributeValue(parsedBaseURL, attribute)));
    109109        }
    110110    }
  • trunk/Source/WebCore/html/HTMLImageElement.cpp

    r170774 r171941  
    4040#include "ShadowRoot.h"
    4141#include "SourceSizeList.h"
     42#include <wtf/text/StringBuilder.h>
    4243
    4344#if ENABLE(SERVICE_CONTROLS)
     
    354355}
    355356
     357bool HTMLImageElement::attributeContainsURL(const Attribute& attribute) const
     358{
     359    return attribute.name() == srcsetAttr
     360        || HTMLElement::attributeContainsURL(attribute);
     361}
     362
     363String HTMLImageElement::completeURLsInAttributeValue(const URL& base, const Attribute& attribute) const
     364{
     365    if (attribute.name() == srcsetAttr) {
     366        Vector<ImageCandidate> imageCandidates = parseImageCandidatesFromSrcsetAttribute(StringView(attribute.value()));
     367        StringBuilder result;
     368        for (const auto& candidate : imageCandidates) {
     369            if (&candidate != &imageCandidates[0])
     370                result.appendLiteral(", ");
     371            result.append(URL(base, candidate.string.toString()).string());
     372            if (candidate.density != UninitializedDescriptor) {
     373                result.append(' ');
     374                result.appendNumber(candidate.density);
     375                result.append('x');
     376            }
     377            if (candidate.resourceWidth != UninitializedDescriptor) {
     378                result.append(' ');
     379                result.appendNumber(candidate.resourceWidth);
     380                result.append('x');
     381            }
     382        }
     383        return result.toString();
     384    }
     385    return HTMLElement::completeURLsInAttributeValue(base, attribute);
     386}
     387
    356388bool HTMLImageElement::matchesLowercasedUsemap(const AtomicStringImpl& name) const
    357389{
  • trunk/Source/WebCore/html/HTMLImageElement.h

    r170576 r171941  
    109109
    110110    virtual bool isURLAttribute(const Attribute&) const override;
     111    virtual bool attributeContainsURL(const Attribute&) const override;
     112    virtual String completeURLsInAttributeValue(const URL& base, const Attribute&) const override;
    111113
    112114    virtual bool draggable() const override;
  • trunk/Source/WebCore/html/parser/HTMLSrcsetParser.cpp

    r170576 r171941  
    163163// http://picture.responsiveimages.org/#parse-srcset-attr
    164164template<typename CharType>
    165 static void parseImageCandidatesFromSrcsetAttribute(const CharType* attributeStart, unsigned length, Vector<ImageCandidate>& imageCandidates)
    166 {
     165static Vector<ImageCandidate> parseImageCandidatesFromSrcsetAttribute(const CharType* attributeStart, unsigned length)
     166{
     167    Vector<ImageCandidate> imageCandidates;
     168
    167169    const CharType* attributeEnd = attributeStart + length;
    168170
     
    208210        // 11. Return to the step labeled splitting loop.
    209211    }
    210 }
    211 
    212 static void parseImageCandidatesFromSrcsetAttribute(StringView attribute, Vector<ImageCandidate>& imageCandidates)
     212    return imageCandidates;
     213}
     214
     215Vector<ImageCandidate> parseImageCandidatesFromSrcsetAttribute(StringView attribute)
    213216{
    214217    // FIXME: We should consider replacing the direct pointers in the parsing process with StringView and positions.
    215218    if (attribute.is8Bit())
    216         parseImageCandidatesFromSrcsetAttribute<LChar>(attribute.characters8(), attribute.length(), imageCandidates);
     219        return parseImageCandidatesFromSrcsetAttribute<LChar>(attribute.characters8(), attribute.length());
    217220    else
    218         parseImageCandidatesFromSrcsetAttribute<UChar>(attribute.characters16(), attribute.length(), imageCandidates);
     221        return parseImageCandidatesFromSrcsetAttribute<UChar>(attribute.characters16(), attribute.length());
    219222}
    220223
     
    276279    }
    277280
    278     Vector<ImageCandidate> imageCandidates;
    279 
    280     parseImageCandidatesFromSrcsetAttribute(StringView(srcsetAttribute), imageCandidates);
     281    Vector<ImageCandidate> imageCandidates = parseImageCandidatesFromSrcsetAttribute(StringView(srcsetAttribute));
    281282
    282283    if (!srcAttribute.isEmpty())
  • trunk/Source/WebCore/html/parser/HTMLSrcsetParser.h

    r170576 r171941  
    105105    );
    106106
     107Vector<ImageCandidate> parseImageCandidatesFromSrcsetAttribute(StringView attribute);
     108
    107109}
    108110
Note: See TracChangeset for help on using the changeset viewer.