Title: [205314] trunk
Revision
205314
Author
nvasil...@apple.com
Date
2016-09-01 14:04:49 -0700 (Thu, 01 Sep 2016)

Log Message

Web Inspector: Minification detection produces false positives for small resources
https://bugs.webkit.org/show_bug.cgi?id=161159
<rdar://problem/27995306>

Reviewed by Brian Burg.

Source/WebInspectorUI:

Improve minification detection algorithm. Minified resources rarely have tabs and usually
have a very few line breaks. Counting tabs as 4 spaces and line breaks as 8 makes minification
detection heuristic more accurate. Increase ratio thresholds to accommodate for the changes in
whitespaceScore.

* UserInterface/Base/Utilities.js:
(isTextLikelyMinified):
* UserInterface/Views/SourceCodeTextEditor.js:
(WebInspector.SourceCodeTextEditor.prototype._prepareEditorForInitialContent):
(WebInspector.SourceCodeTextEditor.prototype._isLikelyMinified): Deleted.

LayoutTests:

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

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (205313 => 205314)


--- trunk/LayoutTests/ChangeLog	2016-09-01 20:40:23 UTC (rev 205313)
+++ trunk/LayoutTests/ChangeLog	2016-09-01 21:04:49 UTC (rev 205314)
@@ -1,3 +1,14 @@
+2016-09-01  Nikita Vasilyev  <nvasil...@apple.com>
+
+        Web Inspector: Minification detection produces false positives for small resources
+        https://bugs.webkit.org/show_bug.cgi?id=161159
+        <rdar://problem/27995306>
+
+        Reviewed by Brian Burg.
+
+        * inspector/formatting/is-text-likely-minified-expected.txt: Added.
+        * inspector/formatting/is-text-likely-minified.html: Added.
+
 2016-09-01  Chris Dumez  <cdu...@apple.com>
 
         Unreviewed, rolling out r205297.

Added: trunk/LayoutTests/inspector/formatting/is-text-likely-minified-expected.txt (0 => 205314)


--- trunk/LayoutTests/inspector/formatting/is-text-likely-minified-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/inspector/formatting/is-text-likely-minified-expected.txt	2016-09-01 21:04:49 UTC (rev 205314)
@@ -0,0 +1,13 @@
+Test minification detection.
+
+
+== Running test suite: Formatting.isLikelyMinified
+-- Running test case: _javascript_ShortSnippet.Unminified
+PASS: This small _javascript_ snippet should be classified as unminified.
+
+-- Running test case: JQuerySnippet.Unminified
+PASS: jQuery snippet should be classified as unminified.
+
+-- Running test case: JQuerySnippet.Minified
+PASS: jQuery snippet should be classified as minified.
+

Added: trunk/LayoutTests/inspector/formatting/is-text-likely-minified.html (0 => 205314)


--- trunk/LayoutTests/inspector/formatting/is-text-likely-minified.html	                        (rev 0)
+++ trunk/LayoutTests/inspector/formatting/is-text-likely-minified.html	2016-09-01 21:04:49 UTC (rev 205314)
@@ -0,0 +1,118 @@
+<!DOCTYPE html>
+<html>
+<head>
+<script src=""
+<script>
+function test()
+{
+    let suite = InspectorTest.createAsyncSuite("Formatting.isLikelyMinified");
+
+    suite.addTestCase({
+        name: "_javascript_ShortSnippet.Unminified",
+        test: (resolve, reject) => {
+            InspectorTest.expectThat(!isTextLikelyMinified("var x = 42;\nvar y = 24;\n"), "This small _javascript_ snippet should be classified as unminified.");
+            resolve();
+        }
+    });
+
+    let jquerySourceFragment = `/*eslint-disable no-unused-vars*/
+/*!
+ * jQuery _javascript_ Library v3.1.0
+ * https://jquery.com/
+ *
+ * Includes Sizzle.js
+ * https://sizzlejs.com/
+ *
+ * Copyright jQuery Foundation and other contributors
+ * Released under the MIT license
+ * https://jquery.org/license
+ *
+ * Date: 2016-07-07T21:44Z
+ */
+( function( global, factory ) {
+
+	"use strict";
+
+	if ( typeof module === "object" && typeof module.exports === "object" ) {
+
+		// For CommonJS and CommonJS-like environments where a proper window
+		// is present, execute the factory and get jQuery.
+		// For environments that do not have a window with a document
+		// (such as Node.js), expose a factory as module.exports.
+		// This accentuates the need for the creation of a real window.
+		// e.g. var jQuery = require("jquery")(window);
+		// See ticket #14549 for more info.
+		module.exports = global.document ?
+			factory( global, true ) :
+			function( w ) {
+				if ( !w.document ) {
+					throw new Error( "jQuery requires a window with a document" );
+				}
+				return factory( w );
+			};
+	} else {
+		factory( global );
+	}
+
+// Pass this if window is not defined yet
+} )( typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
+
+// Edge <= 12 - 13+, Firefox <=18 - 45+, IE 10 - 11, Safari 5.1 - 9+, iOS 6 - 9.1
+// throw exceptions when non-strict code (e.g., ASP.NET 4.5) accesses strict mode
+// arguments.callee.caller (trac-13335). But as of jQuery 3.0 (2016), strict mode should be common
+// enough that all such attempts are guarded in a try block.
+"use strict";
+
+var arr = [];
+
+var document = window.document;
+
+var getProto = Object.getPrototypeOf;
+
+var slice = arr.slice;
+
+var concat = arr.concat;
+
+var push = arr.push;
+
+var indexOf = arr.indexOf;
+
+var class2type = {};
+
+var toString = class2type.toString;
+
+var hasOwn = class2type.hasOwnProperty;
+
+var fnToString = hasOwn.toString;
+
+var ObjectFunctionString = fnToString.call( Object );
+
+var support = {};`;
+
+    suite.addTestCase({
+        name: "JQuerySnippet.Unminified",
+        test: (resolve, reject) => {
+            InspectorTest.expectThat(!isTextLikelyMinified(jquerySourceFragment), "jQuery snippet should be classified as unminified.");
+            resolve();
+        }
+    });
+
+    let jQueryMinifiedSourceFragment = `/*! jQuery v3.1.0 | (c) jQuery Foundation | jquery.org/license */
+!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.isPlain
 Object(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){ret
 urn 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)},gu
 id: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 `;
+
+    suite.addTestCase({
+        name: "JQuerySnippet.Minified",
+        test: (resolve, reject) => {
+            InspectorTest.expectThat(isTextLikelyMinified(jQueryMinifiedSourceFragment), "jQuery snippet should be classified as minified.");
+            resolve();
+        }
+    });
+
+    suite.runTestCasesAndFinish();
+}
+    </script>
+</head>
+<body _onload_="runTest();">
+<p>Test minification detection.</p>
+</body>
+</html>

Modified: trunk/Source/WebInspectorUI/ChangeLog (205313 => 205314)


--- trunk/Source/WebInspectorUI/ChangeLog	2016-09-01 20:40:23 UTC (rev 205313)
+++ trunk/Source/WebInspectorUI/ChangeLog	2016-09-01 21:04:49 UTC (rev 205314)
@@ -1,5 +1,24 @@
 2016-09-01  Nikita Vasilyev  <nvasil...@apple.com>
 
+        Web Inspector: Minification detection produces false positives for small resources
+        https://bugs.webkit.org/show_bug.cgi?id=161159
+        <rdar://problem/27995306>
+
+        Reviewed by Brian Burg.
+
+        Improve minification detection algorithm. Minified resources rarely have tabs and usually
+        have a very few line breaks. Counting tabs as 4 spaces and line breaks as 8 makes minification
+        detection heuristic more accurate. Increase ratio thresholds to accommodate for the changes in
+        whitespaceScore.
+
+        * UserInterface/Base/Utilities.js:
+        (isTextLikelyMinified):
+        * UserInterface/Views/SourceCodeTextEditor.js:
+        (WebInspector.SourceCodeTextEditor.prototype._prepareEditorForInitialContent):
+        (WebInspector.SourceCodeTextEditor.prototype._isLikelyMinified): Deleted.
+
+2016-09-01  Nikita Vasilyev  <nvasil...@apple.com>
+
         REGRESSION (r201454): Web Inspector: Text caret isn't visible when editing an attribute
         https://bugs.webkit.org/show_bug.cgi?id=160986
         <rdar://problem/27916207>

Modified: trunk/Source/WebInspectorUI/UserInterface/Base/Utilities.js (205313 => 205314)


--- trunk/Source/WebInspectorUI/UserInterface/Base/Utilities.js	2016-09-01 20:40:23 UTC (rev 205313)
+++ trunk/Source/WebInspectorUI/UserInterface/Base/Utilities.js	2016-09-01 21:04:49 UTC (rev 205314)
@@ -1402,6 +1402,29 @@
     return str.endsWith("{\n    [native code]\n}");
 }
 
+function isTextLikelyMinified(content)
+{
+    const autoFormatMaxCharactersToCheck = 5000;
+    const autoFormatWhitespaceRatio = 0.2;
+
+    let whitespaceScore = 0;
+    let size = Math.min(autoFormatMaxCharactersToCheck, content.length);
+
+    for (let i = 0; i < size; i++) {
+        let char = content[i];
+
+        if (char === " ")
+            whitespaceScore++;
+        else if (char === "\t")
+            whitespaceScore += 4;
+        else if (char === "\n")
+            whitespaceScore += 8;
+    }
+
+    let ratio = whitespaceScore / size;
+    return ratio < autoFormatWhitespaceRatio;
+}
+
 function doubleQuotedString(str)
 {
     return "\"" + str.replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + "\"";

Modified: trunk/Source/WebInspectorUI/UserInterface/Views/SourceCodeTextEditor.js (205313 => 205314)


--- trunk/Source/WebInspectorUI/UserInterface/Views/SourceCodeTextEditor.js	2016-09-01 20:40:23 UTC (rev 205313)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/SourceCodeTextEditor.js	2016-09-01 21:04:49 UTC (rev 205314)
@@ -364,26 +364,6 @@
             delete this._breakpointMap[lineInfo.lineNumber];
     }
 
-    _isLikelyMinified(content)
-    {
-        let whiteSpaceCount = 0;
-        let ratio = 0;
-
-        for (let i = 0, size = Math.min(5000, content.length); i < size; i++) {
-            let char = content[i];
-            if (char === " " || char === "\n" || char === "\t")
-                whiteSpaceCount++;
-
-            if (i >= 500) {
-                ratio = whiteSpaceCount / i;
-                if (ratio < 0.05)
-                    return true;
-            }
-        }
-
-        return ratio < 0.1;
-    }
-
     _populateWithContent(content)
     {
         content = content || "";
@@ -464,7 +444,7 @@
 
         // Decide to automatically format the content if it looks minified and it can be formatted.
         console.assert(!this.formatted);
-        if (this.canBeFormatted() && this._isLikelyMinified(content)) {
+        if (this.canBeFormatted() && isTextLikelyMinified(content)) {
             this._autoFormat = true;
             this._isProbablyMinified = true;
         }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to