Changeset 191421 in webkit


Ignore:
Timestamp:
Oct 21, 2015, 5:50:25 PM (10 years ago)
Author:
commit-queue@webkit.org
Message:

Web Inspector: srcset attributes should have hyperlinks to the resources
https://bugs.webkit.org/show_bug.cgi?id=150409

Patch by Joseph Pecoraro <Joseph Pecoraro> on 2015-10-21
Reviewed by Timothy Hatcher.

The "srcset" attribute parsing is a simplification of:
https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-srcset

The "srcset" attribute value is expected to be in comma
separated groups. Within each group we always expect a link,
and an optional descriptor. We want to linkify the link.

  • UserInterface/Views/CodeMirrorAdditions.js:

(tokenizeSrcSetString):
(extendedXMLToken):
When parsing srcset attribute, parse links as link tokens
so that they are linkified in resource content views.

  • UserInterface/Views/DOMTreeElement.js:

(WebInspector.DOMTreeElement.prototype._buildAttributeDOM):
When creating the DOMTree DOM for srcset attributes,
create link elements for the link values of attribute value.

Location:
trunk/Source/WebInspectorUI
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebInspectorUI/ChangeLog

    r191419 r191421  
     12015-10-21  Joseph Pecoraro  <pecoraro@apple.com>
     2
     3        Web Inspector: srcset attributes should have hyperlinks to the resources
     4        https://bugs.webkit.org/show_bug.cgi?id=150409
     5
     6        Reviewed by Timothy Hatcher.
     7
     8        The "srcset" attribute parsing is a simplification of:
     9        https://html.spec.whatwg.org/multipage/embedded-content.html#attr-img-srcset
     10
     11        The "srcset" attribute value is expected to be in comma
     12        separated groups. Within each group we always expect a link,
     13        and an optional descriptor. We want to linkify the link.
     14
     15        * UserInterface/Views/CodeMirrorAdditions.js:
     16        (tokenizeSrcSetString):
     17        (extendedXMLToken):
     18        When parsing srcset attribute, parse links as link tokens
     19        so that they are linkified in resource content views.
     20
     21        * UserInterface/Views/DOMTreeElement.js:
     22        (WebInspector.DOMTreeElement.prototype._buildAttributeDOM):
     23        When creating the DOMTree DOM for srcset attributes,
     24        create link elements for the link values of attribute value.
     25
    1262015-10-21  Devin Rousso  <dcrousso+webkit@gmail.com>
    227
  • trunk/Source/WebInspectorUI/UserInterface/Views/CodeMirrorAdditions.js

    r189190 r191421  
    6666        delete state._linkQuoteCharacter;
    6767        delete state._linkBaseStyle;
     68        delete state._srcSetTokenizeState;
    6869
    6970        return style;
     71    }
     72
     73    function tokenizeSrcSetString(stream, state)
     74    {
     75        console.assert(state._linkQuoteCharacter !== undefined);
     76
     77        if (state._srcSetTokenizeState === "link") {
     78            // Eat the string until a space, comma, or ending quote.
     79            // If this is unquoted, then eat until whitespace or common parse errors.
     80            if (state._linkQuoteCharacter)
     81                stream.eatWhile(new RegExp("[^\\s," + state._linkQuoteCharacter + "]"));
     82            else
     83                stream.eatWhile(/[^\s,\u00a0=<>\"\']/);
     84        } else {
     85            // Eat the string until a comma, or ending quote.
     86            // If this is unquoted, then eat until whitespace or common parse errors.
     87            stream.eatSpace();
     88            if (state._linkQuoteCharacter)
     89                stream.eatWhile(new RegExp("[^," + state._linkQuoteCharacter + "]"));
     90            else
     91                stream.eatWhile(/[^\s\u00a0=<>\"\',]/);
     92            stream.eatWhile(/[\s,]/);
     93        }
     94
     95        // If the stream isn't at the end of line and we found the end quote
     96        // change _linkTokenize to parse the end of the link next. Otherwise
     97        // _linkTokenize will stay as-is to parse more of the srcset.
     98        if (!stream.eol() && (!state._linkQuoteCharacter || stream.peek() === state._linkQuoteCharacter))
     99            state._linkTokenize = tokenizeEndOfLinkString;
     100
     101        // Link portion.
     102        if (state._srcSetTokenizeState === "link") {
     103            state._srcSetTokenizeState = "descriptor";
     104            return "link";
     105        }
     106
     107        // Descriptor portion.
     108        state._srcSetTokenizeState = "link";
     109        return state._linkBaseStyle;
    70110    }
    71111
     
    87127            if (text === "href" || text === "src")
    88128                state._expectLink = true;
    89             else
     129            else if (text === "srcset")
     130                state._expectSrcSet = true;
     131            else {
    90132                delete state._expectLink;
     133                delete state._expectSrcSet;
     134            }
    91135        } else if (state._expectLink && style === "string") {
    92136            var current = stream.current();
     
    99143                // This is a link, so setup the state to process it next.
    100144                state._linkTokenize = tokenizeLinkString;
     145                state._linkBaseStyle = style;
     146
     147                // The attribute should be quoted.
     148                var quote = current[0];
     149
     150                state._linkQuoteCharacter = quote === "'" || quote === "\"" ? quote : null;
     151
     152                // Rewind the stream to the start of this token.
     153                stream.pos = startPosition;
     154
     155                // Eat the open quote of the string so the string style
     156                // will be used for the quote character.
     157                if (state._linkQuoteCharacter)
     158                    stream.eat(state._linkQuoteCharacter);
     159            }
     160        } else if (state._expectSrcSet && style === "string") {
     161            var current = stream.current();
     162
     163            // Unless current token is empty quotes, consume quote character
     164            // and tokenize link next.
     165            if (current !== "\"\"" && current !== "''") {
     166                delete state._expectSrcSet;
     167
     168                // This is a link, so setup the state to process it next.
     169                state._srcSetTokenizeState = "link";
     170                state._linkTokenize = tokenizeSrcSetString;
    101171                state._linkBaseStyle = style;
    102172
     
    119189            // another token before a string then we stop expecting a link.
    120190            delete state._expectLink;
     191            delete state._expectSrcSet;
    121192        }
    122193
  • trunk/Source/WebInspectorUI/UserInterface/Views/DOMTreeElement.js

    r190286 r191421  
    11101110                attrSpanElement.appendChild(attrValueElement);
    11111111            }
     1112        } else if (name === "srcset") {
     1113            let baseURL = node.ownerDocument ? node.ownerDocument.documentURL : null;
     1114            attrValueElement = attrSpanElement.createChild("span", "html-attribute-value");
     1115
     1116            let groups = value.split(/\s*,\s*/);
     1117            for (let i = 0; i < groups.length; ++i) {
     1118                let string = groups[i];
     1119                let spaceIndex = string.search(/\s/);
     1120
     1121                if (spaceIndex === -1) {
     1122                    let linkText = string;
     1123                    let rewrittenURL = absoluteURL(string, baseURL);
     1124                    let linkElement = attrValueElement.appendChild(document.createElement("a"));
     1125                    linkElement.href = rewrittenURL;
     1126                    linkElement.textContent = linkText.insertWordBreakCharacters();
     1127                } else {
     1128                    let linkText = string.substring(0, spaceIndex);
     1129                    let descriptorText = string.substring(spaceIndex).insertWordBreakCharacters();
     1130                    let rewrittenURL = absoluteURL(linkText, baseURL);
     1131                    let linkElement = attrValueElement.appendChild(document.createElement("a"));
     1132                    linkElement.href = rewrittenURL;
     1133                    linkElement.textContent = linkText.insertWordBreakCharacters();
     1134                    let descriptorElement = attrValueElement.appendChild(document.createElement("span"));
     1135                    descriptorElement.textContent = string.substring(spaceIndex);
     1136                }
     1137
     1138                if (i < groups.length - 1) {
     1139                    let commaElement = attrValueElement.appendChild(document.createElement("span"));
     1140                    commaElement.textContent = ", ";
     1141                }
     1142            }
    11121143        } else {
    11131144            value = value.insertWordBreakCharacters();
Note: See TracChangeset for help on using the changeset viewer.