Changeset 239467 in webkit


Ignore:
Timestamp:
Dec 20, 2018, 3:15:56 PM (6 years ago)
Author:
Joseph Pecoraro
Message:

Web Inspector: Autoformat doesn't work on icloud.com (javascript-packed.js)
https://bugs.webkit.org/show_bug.cgi?id=192946
<rdar://problem/42546126>

Rubber-stamped by Devin Rousso.

Source/WebInspectorUI:

  • UserInterface/Base/Utilities.js:

(whitespaceRatio):
(isTextLikelyMinified):
Check the first 2500 and the last 2500 characters whitespace ratio.
If either is below 20% then treat as minified.

LayoutTests:

  • inspector/formatting/is-text-likely-minified-expected.txt:
  • inspector/formatting/is-text-likely-minified.html:

Add a test for a long header doc at the start but minified content at the end.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r239463 r239467  
     12018-12-20  Joseph Pecoraro  <pecoraro@apple.com>
     2
     3        Web Inspector: Autoformat doesn't work on icloud.com (javascript-packed.js)
     4        https://bugs.webkit.org/show_bug.cgi?id=192946
     5        <rdar://problem/42546126>
     6
     7        Rubber-stamped by Devin Rousso.
     8
     9        * inspector/formatting/is-text-likely-minified-expected.txt:
     10        * inspector/formatting/is-text-likely-minified.html:
     11        Add a test for a long header doc at the start but minified content at the end.
     12
    1132018-12-20  Ryan Haddad  <ryanhaddad@apple.com>
    214
  • trunk/LayoutTests/inspector/formatting/is-text-likely-minified-expected.txt

    r205314 r239467  
    1212PASS: jQuery snippet should be classified as minified.
    1313
     14-- Running test case: JQuerySnippet.MinifiedWithHeader
     15PASS: Source with leading unminified but trailing minified source should be classified as minified.
     16
  • trunk/LayoutTests/inspector/formatting/is-text-likely-minified.html

    r210062 r239467  
    66function test()
    77{
    8     let suite = InspectorTest.createAsyncSuite("Formatting.isLikelyMinified");
     8    let suite = InspectorTest.createSyncSuite("Formatting.isLikelyMinified");
    99
    1010    suite.addTestCase({
    1111        name: "JavaScriptShortSnippet.Unminified",
    12         test(resolve, reject) {
     12        test() {
    1313            InspectorTest.expectThat(!isTextLikelyMinified("var x = 42;\nvar y = 24;\n"), "This small JavaScript snippet should be classified as unminified.");
    14             resolve();
     14            return true;
    1515        }
    1616    });
    1717
    18     let jquerySourceFragment = `/*eslint-disable no-unused-vars*/
     18    const jquerySourceFragment = `/*eslint-disable no-unused-vars*/
    1919/*!
    2020 * jQuery JavaScript Library v3.1.0
     
    9292    suite.addTestCase({
    9393        name: "JQuerySnippet.Unminified",
    94         test(resolve, reject) {
     94        test() {
    9595            InspectorTest.expectThat(!isTextLikelyMinified(jquerySourceFragment), "jQuery snippet should be classified as unminified.");
    96             resolve();
     96            return true;
    9797        }
    9898    });
    9999
    100     let jQueryMinifiedSourceFragment = `/*! jQuery v3.1.0 | (c) jQuery Foundation | jquery.org/license */
     100    const jQueryMinifiedSourceFragment = `/*! jQuery v3.1.0 | (c) jQuery Foundation | jquery.org/license */
    101101!function(a,b){"use strict";"object"==typeof module&&"object"==typeof module.exports?module.exports=a.document?b(a,!0):function(a){if(!a.document)throw new Error("jQuery requires a window with a document");return b(a)}:b(a)}("undefined"!=typeof window?window:this,function(a,b){"use strict";var c=[],d=a.document,e=Object.getPrototypeOf,f=c.slice,g=c.concat,h=c.push,i=c.indexOf,j={},k=j.toString,l=j.hasOwnProperty,m=l.toString,n=m.call(Object),o={};function p(a,b){b=b||d;var c=b.createElement("script");c.text=a,b.head.appendChild(c).parentNode.removeChild(c)}var q="3.1.0",r=function(a,b){return new r.fn.init(a,b)},s=/^[s ]+|[s ]+$/g,t=/^-ms-/,u=/-([a-z])/g,v=function(a,b){return b.toUpperCase()};r.fn=r.prototype={jquery:q,constructor:r,length:0,toArray:function(){return f.call(this)},get:function(a){return null!=a?a<0?this[a+this.length]:this[a]:f.call(this)},pushStack:function(a){var b=r.merge(this.constructor(),a);return b.prevObject=this,b},each:function(a){return r.each(this,a)},map:function(a){return this.pushStack(r.map(this,function(b,c){return a.call(b,c,b)}))},slice:function(){return this.pushStack(f.apply(this,arguments))},first:function(){return this.eq(0)},last:function(){return this.eq(-1)},eq:function(a){var b=this.length,c=+a+(a<0?b:0);return this.pushStack(c>=0&&c<b?[this[c]]:[])},end:function(){return this.prevObject||this.constructor()},push:h,sort:c.sort,splice:c.splice},r.extend=r.fn.extend=function(){var a,b,c,d,e,f,g=arguments[0]||{},h=1,i=arguments.length,j=!1;for("boolean"==typeof g&&(j=g,g=arguments[h]||{},h++),"object"==typeof g||r.isFunction(g)||(g={}),h===i&&(g=this,h--);h<i;h++)if(null!=(a=arguments[h]))for(b in a)c=g[b],d=a[b],g!==d&&(j&&d&&(r.isPlainObject(d)||(e=r.isArray(d)))?(e?(e=!1,f=c&&r.isArray(c)?c:[]):f=c&&r.isPlainObject(c)?c:{},g[b]=r.extend(j,f,d)):void 0!==d&&(g[b]=d));return g},r.extend({expando:"jQuery"+(q+Math.random()).replace(/D/g,""),isReady:!0,error:function(a){throw new Error(a)},noop:function(){},isFunction:function(a){return"function"===r.type(a)},isArray:Array.isArray,isWindow:function(a){return null!=a&&a===a.window},isNumeric:function(a){var b=r.type(a);return("number"===b||"string"===b)&&!isNaN(a-parseFloat(a))},isPlainObject:function(a){var b,c;return!(!a||"[object Object]"!==k.call(a))&&(!(b=e(a))||(c=l.call(b,"constructor")&&b.constructor,"function"==typeof c&&m.call(c)===n))},isEmptyObject:function(a){var b;for(b in a)return!1;return!0},type:function(a){return null==a?a+"":"object"==typeof a||"function"==typeof a?j[k.call(a)]||"object":typeof a},globalEval:function(a){p(a)},camelCase:function(a){return a.replace(t,"ms-").replace(u,v)},nodeName:function(a,b){return a.nodeName&&a.nodeName.toLowerCase()===b.toLowerCase()},each:function(a,b){var c,d=0;if(w(a)){for(c=a.length;d<c;d++)if(b.call(a[d],d,a[d])===!1)break}else for(d in a)if(b.call(a[d],d,a[d])===!1)break;return a},trim:function(a){return null==a?"":(a+"").replace(s,"")},makeArray:function(a,b){var c=b||[];return null!=a&&(w(Object(a))?r.merge(c,"string"==typeof a?[a]:a):h.call(c,a)),c},inArray:function(a,b,c){return null==b?-1:i.call(b,a,c)},merge:function(a,b){for(var c=+b.length,d=0,e=a.length;d<c;d++)a[e++]=b[d];return a.length=e,a},grep:function(a,b,c){for(var d,e=[],f=0,g=a.length,h=!c;f<g;f++)d=!b(a[f],f),d!==h&&e.push(a[f]);return e},map:function(a,b,c){var d,e,f=0,h=[];if(w(a))for(d=a.length;f<d;f++)e=b(a[f],f,c),null!=e&&h.push(e);else for(f in a)e=b(a[f],f,c),null!=e&&h.push(e);return g.apply([],h)},guid:1,proxy:function(a,b){var c,d,e;if("string"==typeof b&&(c=a[b],b=a,a=c),r.isFunction(a))return d=f.call(arguments,2),e=function(){return `;
    102102
    103103    suite.addTestCase({
    104104        name: "JQuerySnippet.Minified",
    105         test(resolve, reject) {
     105        test() {
    106106            InspectorTest.expectThat(isTextLikelyMinified(jQueryMinifiedSourceFragment), "jQuery snippet should be classified as minified.");
    107             resolve();
     107            return true;
    108108        }
    109109    });
     110
     111    const sampleHeader = `
     112/*!
     113 * jQuery JavaScript Library v3.1.0
     114 * https://jquery.com/
     115 *
     116 * Includes Sizzle.js
     117 * https://sizzlejs.com/
     118 *
     119 * Copyright jQuery Foundation and other contributors
     120 * Released under the MIT license
     121 * https://jquery.org/license
     122 *
     123 * Date: 2016-07-07T21:44Z
     124 */
     125 `;
     126
     127    let longHeaderSource = sampleHeader.repeat(10) + jQueryMinifiedSourceFragment;
     128    suite.addTestCase({
     129        name: "JQuerySnippet.MinifiedWithHeader",
     130        test() {
     131            InspectorTest.expectThat(isTextLikelyMinified(longHeaderSource), "Source with leading unminified but trailing minified source should be classified as minified.");
     132            return true;
     133        }
     134    })
    110135
    111136    suite.runTestCasesAndFinish();
  • trunk/Source/WebInspectorUI/ChangeLog

    r239460 r239467  
     12018-12-20  Joseph Pecoraro  <pecoraro@apple.com>
     2
     3        Web Inspector: Autoformat doesn't work on icloud.com (javascript-packed.js)
     4        https://bugs.webkit.org/show_bug.cgi?id=192946
     5        <rdar://problem/42546126>
     6
     7        Rubber-stamped by Devin Rousso.
     8
     9        * UserInterface/Base/Utilities.js:
     10        (whitespaceRatio):
     11        (isTextLikelyMinified):
     12        Check the first 2500 and the last 2500 characters whitespace ratio.
     13        If either is below 20% then treat as minified.
     14
    1152018-12-20  Joseph Pecoraro  <pecoraro@apple.com>
    216
  • trunk/Source/WebInspectorUI/UserInterface/Base/Utilities.js

    r238563 r239467  
    16141614}
    16151615
    1616 function isTextLikelyMinified(content)
    1617 {
    1618     const autoFormatMaxCharactersToCheck = 5000;
    1619     const autoFormatWhitespaceRatio = 0.2;
    1620 
     1616function whitespaceRatio(content, start, end)
     1617{
    16211618    let whitespaceScore = 0;
    1622     let size = Math.min(autoFormatMaxCharactersToCheck, content.length);
    1623 
    1624     for (let i = 0; i < size; i++) {
     1619    let size = end - start;
     1620
     1621    for (let i = start; i < end; i++) {
    16251622        let char = content[i];
    1626 
    16271623        if (char === " ")
    16281624            whitespaceScore++;
     
    16341630
    16351631    let ratio = whitespaceScore / size;
    1636     return ratio < autoFormatWhitespaceRatio;
     1632    return ratio;
     1633}
     1634
     1635function isTextLikelyMinified(content)
     1636{
     1637    const autoFormatMaxCharactersToCheck = 2500;
     1638    const autoFormatWhitespaceRatio = 0.2;
     1639
     1640    if (content.length <= autoFormatMaxCharactersToCheck) {
     1641        let ratio = whitespaceRatio(content, 0, content.length);
     1642        return ratio < autoFormatWhitespaceRatio;
     1643    }
     1644
     1645    let startRatio = whitespaceRatio(content, 0, autoFormatMaxCharactersToCheck);
     1646    if (startRatio < autoFormatWhitespaceRatio)
     1647        return true;
     1648
     1649    let endRatio = whitespaceRatio(content, content.length - autoFormatMaxCharactersToCheck, content.length)
     1650    if (endRatio < autoFormatWhitespaceRatio)
     1651        return true;
     1652
     1653    return false;
    16371654}
    16381655
Note: See TracChangeset for help on using the changeset viewer.