Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/elements/ElementsTreeElement.js

Issue 2871593003: DevTools: correctly parse srcset attribute before linkifying urls (Closed)
Patch Set: ac Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/LayoutTests/http/tests/inspector/elements/elements-linkify-attributes-expected.txt ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com> 3 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com>
4 * Copyright (C) 2009 Joseph Pecoraro 4 * Copyright (C) 2009 Joseph Pecoraro
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 9 *
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
(...skipping 1263 matching lines...) Expand 10 before | Expand all | Expand 10 after
1274 value = value.replace(closingPunctuationRegex, '$&\u200B'); 1274 value = value.replace(closingPunctuationRegex, '$&\u200B');
1275 if (value.startsWith('data:')) 1275 if (value.startsWith('data:'))
1276 value = value.trimMiddle(60); 1276 value = value.trimMiddle(60);
1277 var link = node.nodeName().toLowerCase() === 'a' ? 1277 var link = node.nodeName().toLowerCase() === 'a' ?
1278 UI.createExternalLink(rewrittenHref, value, '', true) : 1278 UI.createExternalLink(rewrittenHref, value, '', true) :
1279 Components.Linkifier.linkifyURL(rewrittenHref, value, '', undefined, u ndefined, true); 1279 Components.Linkifier.linkifyURL(rewrittenHref, value, '', undefined, u ndefined, true);
1280 link[Elements.ElementsTreeElement.HrefSymbol] = rewrittenHref; 1280 link[Elements.ElementsTreeElement.HrefSymbol] = rewrittenHref;
1281 return link; 1281 return link;
1282 } 1282 }
1283 1283
1284 if (node && (name === 'src' || name === 'href')) { 1284 var nodeName = node ? node.nodeName().toLowerCase() : '';
1285 if (nodeName && (name === 'src' || name === 'href'))
1285 attrValueElement.appendChild(linkifyValue.call(this, value)); 1286 attrValueElement.appendChild(linkifyValue.call(this, value));
1286 } else if ( 1287 else if ((nodeName === 'img' || nodeName === 'source') && name === 'srcset')
1287 node && (node.nodeName().toLowerCase() === 'img' || node.nodeName().toLo werCase() === 'source') && 1288 attrValueElement.appendChild(linkifySrcset.call(this, value));
1288 name === 'srcset') { 1289 else
1289 var sources = value.split(',');
1290 for (var i = 0; i < sources.length; ++i) {
1291 if (i > 0)
1292 attrValueElement.createTextChild(', ');
1293 var source = sources[i].trim();
1294 var indexOfSpace = source.indexOf(' ');
1295 var url, tail;
1296
1297 if (indexOfSpace === -1) {
1298 url = source;
1299 } else {
1300 url = source.substring(0, indexOfSpace);
1301 tail = source.substring(indexOfSpace);
1302 }
1303
1304 attrValueElement.appendChild(linkifyValue.call(this, url));
1305
1306 if (tail)
1307 attrValueElement.createTextChild(tail);
1308 }
1309 } else {
1310 setValueWithEntities.call(this, attrValueElement, value); 1290 setValueWithEntities.call(this, attrValueElement, value);
1311 }
1312 1291
1313 if (hasText) 1292 if (hasText)
1314 attrSpanElement.createTextChild('"'); 1293 attrSpanElement.createTextChild('"');
1294
1295 /**
1296 * @param {string} value
1297 * @return {!DocumentFragment}
1298 * @this {!Elements.ElementsTreeElement}
1299 */
1300 function linkifySrcset(value) {
1301 // Splitting normally on commas or spaces will break on valid srcsets "foo 1x,bar 2x" and "data:,foo 1x".
1302 // 1) Let the index of the next space be `indexOfSpace`.
1303 // 2a) If the character at `indexOfSpace - 1` is a comma, collect the prec eding characters up to
1304 // `indexOfSpace - 1` as a URL and repeat step 1).
1305 // 2b) Else, collect the preceding characters as a URL.
1306 // 3) Collect the characters from `indexOfSpace` up to the next comma as t he size descriptor and repeat step 1).
1307 // https://html.spec.whatwg.org/multipage/embedded-content.html#parse-a-sr cset-attribute
1308 var fragment = createDocumentFragment();
1309 var i = 0;
1310 while (value.length) {
1311 if (i++ > 0)
1312 fragment.createTextChild(' ');
1313 value = value.trim();
1314 // The url and descriptor may end with a separating comma.
1315 var url = '';
1316 var descriptor = '';
1317 var indexOfSpace = value.search(/\s/);
1318 if (indexOfSpace === -1) {
1319 url = value;
1320 } else if (indexOfSpace > 0 && value[indexOfSpace - 1] === ',') {
1321 url = value.substring(0, indexOfSpace);
1322 } else {
1323 url = value.substring(0, indexOfSpace);
1324 var indexOfComma = value.indexOf(',', indexOfSpace);
1325 if (indexOfComma !== -1)
1326 descriptor = value.substring(indexOfSpace, indexOfComma + 1);
1327 else
1328 descriptor = value.substring(indexOfSpace);
1329 }
1330
1331 if (url) {
1332 // Up to one trailing comma should be removed from `url`.
1333 if (url.endsWith(',')) {
1334 fragment.appendChild(linkifyValue.call(this, url.substring(0, url.le ngth - 1)));
1335 fragment.createTextChild(',');
1336 } else {
1337 fragment.appendChild(linkifyValue.call(this, url));
1338 }
1339 }
1340 if (descriptor)
1341 fragment.createTextChild(descriptor);
1342 value = value.substring(url.length + descriptor.length);
1343 }
1344 return fragment;
1345 }
1315 } 1346 }
1316 1347
1317 /** 1348 /**
1318 * @param {!Node} parentElement 1349 * @param {!Node} parentElement
1319 * @param {string} pseudoElementName 1350 * @param {string} pseudoElementName
1320 */ 1351 */
1321 _buildPseudoElementDOM(parentElement, pseudoElementName) { 1352 _buildPseudoElementDOM(parentElement, pseudoElementName) {
1322 var pseudoElement = parentElement.createChild('span', 'webkit-html-pseudo-el ement'); 1353 var pseudoElement = parentElement.createChild('span', 'webkit-html-pseudo-el ement');
1323 pseudoElement.textContent = '::' + pseudoElementName; 1354 pseudoElement.textContent = '::' + pseudoElementName;
1324 parentElement.createTextChild('\u200B'); 1355 parentElement.createTextChild('\u200B');
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
1630 Elements.ElementsTreeElement.ForbiddenClosingTagElements = new Set([ 1661 Elements.ElementsTreeElement.ForbiddenClosingTagElements = new Set([
1631 'area', 'base', 'basefont', 'br', 'canvas', 'col', 'command', 'embed', 'frame', 'hr', 1662 'area', 'base', 'basefont', 'br', 'canvas', 'col', 'command', 'embed', 'frame', 'hr',
1632 'img', 'input', 'keygen', 'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr' 1663 'img', 'input', 'keygen', 'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr'
1633 ]); 1664 ]);
1634 1665
1635 // These tags we do not allow editing their tag name. 1666 // These tags we do not allow editing their tag name.
1636 Elements.ElementsTreeElement.EditTagBlacklist = new Set(['html', 'head', 'body'] ); 1667 Elements.ElementsTreeElement.EditTagBlacklist = new Set(['html', 'head', 'body'] );
1637 1668
1638 /** @typedef {{cancel: function(), commit: function(), resize: function(), edito r:!UI.TextEditor}} */ 1669 /** @typedef {{cancel: function(), commit: function(), resize: function(), edito r:!UI.TextEditor}} */
1639 Elements.MultilineEditorController; 1670 Elements.MultilineEditorController;
OLDNEW
« no previous file with comments | « third_party/WebKit/LayoutTests/http/tests/inspector/elements/elements-linkify-attributes-expected.txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698