コード例 #1
0
ファイル: Compressor.cs プロジェクト: soywiz/NodeNetAsync
		async static public Task<string> CompressJavaScriptAsync(string JavaScriptString)
		{
			return await TaskEx.RunPropagatingExceptionsAsync(() =>
			{
				var JsWriter = new StringWriter();
				var javaScriptCompressor = new JavaScriptCompressor(new StringReader(JavaScriptString).GetJavaReader(), new ErrorReporterMock());
				javaScriptCompressor.compress(JsWriter.GetJavaWriter(), 1024, true, false, false, false);
				return JsWriter.ToString();
			});
		}
コード例 #2
0
        public void CompressFull_CodePlex48610Test()
        {
            string javascript = File.ReadAllText("jquery-1.3.1.js");

            var javaScriptCompressor = new JavaScriptCompressor(javascript);
            string compressedJavascript = javaScriptCompressor.Compress();
            Assert.IsTrue(!string.IsNullOrEmpty(compressedJavascript));
            Assert.IsTrue(javascript.Length > compressedJavascript.Length);

            // Now, check to see that we have some error logging stuff.
            var customErrorReporter = javaScriptCompressor.ErrorReporter as CustomErrorReporter;
            Assert.IsNotNull(customErrorReporter);
            Assert.IsTrue(customErrorReporter.ErrorMessages.Count > 0);
        }
コード例 #3
0
            public void The_SyntaxError_Test_File_Should_Be_Close_To_The_Java_Version()
            {
                // Arrange
                var source = File.ReadAllText(@"Compatability Test Files\_syntax_error.js", Encoding.UTF8);
                var expected = File.ReadAllText(@"Compatability Test Files\_syntax_error.js.min");

                // Act
                var actual = new JavaScriptCompressor().Compress(source);

                // Assert
                Assert.That(actual, Is.Not.Null.Or.Empty, "Null Or Empty");
                // Because the Java code uses a Hashtable to determine what variables names can be obfuscated, we can't do an exact file compare. But we can
                // do a file LENGTH compare .. which might be a bit closer to fair Assert test.
                Assert.That(actual.Length, Is.EqualTo(expected.Length), "Length mismatch");
            }
コード例 #4
0
        public void CompressTest()
        {
            // First load up some simple javascript.
            string javascript = File.ReadAllText("SampleJavaScript1.js");

            // Now compress the small javascript.
            string compressedJavascript = JavaScriptCompressor.Compress(javascript);
            Assert.IsTrue(!string.IsNullOrEmpty(compressedJavascript));
            Assert.IsTrue(javascript.Length > compressedJavascript.Length);

            // Now lets try the big mother fraker.
            javascript = File.ReadAllText("SampleJavaScript2.js");

            // Now compress the small javascript.
            compressedJavascript = JavaScriptCompressor.Compress(javascript);
            Assert.IsTrue(!string.IsNullOrEmpty(compressedJavascript));
            Assert.IsTrue(javascript.Length > compressedJavascript.Length);

            // Special Ms AJAX test.
            string original = File.ReadAllText("SampleJavaScript2.js");
            int index = original.IndexOf("Sys.Serialization.JavaScriptSerializer._stringRegEx");
            string test = original.Substring(index);

            var compressor = new JavaScriptCompressor(original);

            string minified = compressor.Compress(true, false, false, -1);
            index = minified.IndexOf("Sys.Serialization.JavaScriptSerializer._stringRegEx");
            test = minified.Substring(index);


            // And now some weird \n test.
            javascript = File.ReadAllText("SampleJavaScript3.js");

            // Now compress the small javascript.
            compressedJavascript = JavaScriptCompressor.Compress(javascript);
            Assert.IsTrue(!string.IsNullOrEmpty(compressedJavascript));
            Assert.IsTrue(javascript.Length > compressedJavascript.Length);

            // Expected failure.
            JavaScriptCompressor.Compress(null);
        }
コード例 #5
0
        public void Accents_Should_Be_Retained_In_All_Cases(string threadCulture, string compressorCulture)
        {
            // Arrange
            var originalThreadCulture = Thread.CurrentThread.CurrentCulture;
            var originalThreadUICulture = Thread.CurrentThread.CurrentUICulture;
            const string source = @"Strings = {
                                        IncorrectLogin: '******'
                                    }";
            const string expected = @"Strings={IncorrectLogin:""Felaktigt användarnamn eller lösenord. Försök igen.""};";

            try
            {
                if (threadCulture != null)
                {
                    Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(threadCulture);
                    Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(threadCulture);
                }
                var compressor = new JavaScriptCompressor();
                compressor.Encoding = Encoding.UTF8;
                if (compressorCulture != null)
                {
                    compressor.ThreadCulture = CultureInfo.CreateSpecificCulture(compressorCulture);
                }

                // Act
                var actual = compressor.Compress(source);

                // Assert.
                Assert.That(actual, Is.EqualTo(expected));
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = originalThreadCulture;
                Thread.CurrentThread.CurrentUICulture = originalThreadUICulture;
            }
        }
コード例 #6
0
 public void SetUp()
 {
     target = new JavaScriptCompressor();
 }
コード例 #7
0
        private ICompressor DetermineCompressor(CompressorConfig compressorConfig)
        {
            if (compressorConfig == null)
            {
                throw new ArgumentNullException("compressorConfig");
            }

            // Define the compressor we wish to use.
            ICompressor compressor;
            var config = _compressorConfig as CssCompressorConfig;
            if (config != null)
            {
                var cssCompressorConfig = config;
                compressor = new CssCompressor
                {
                    CompressionType = cssCompressorConfig.CompressionType,
                    LineBreakPosition = cssCompressorConfig.LineBreakPosition,
                    RemoveComments = cssCompressorConfig.RemoveComments
                };
            }
            else if (_compressorConfig is JavaScriptCompressorConfig)
            {
                var jsCompressorConfig = (JavaScriptCompressorConfig)_compressorConfig;
                compressor = new JavaScriptCompressor
                {
                    CompressionType = jsCompressorConfig.CompressionType,
                    DisableOptimizations = jsCompressorConfig.DisableOptimizations,
                    Encoding = jsCompressorConfig.Encoding,
                    ErrorReporter = jsCompressorConfig.ErrorReporter,
                    IgnoreEval = jsCompressorConfig.IgnoreEval,
                    LineBreakPosition = jsCompressorConfig.LineBreakPosition,
                    LoggingType = jsCompressorConfig.LoggingType,
                    ObfuscateJavascript = jsCompressorConfig.ObfuscateJavascript,
                    PreserveAllSemicolons = jsCompressorConfig.PreserveAllSemicolons,
                    ThreadCulture = jsCompressorConfig.ThreadCulture
                };
            }
            else
            {
                throw new InvalidOperationException("Unhandled CompressorConfig instance while trying to initialize internal compressor properties.");
            }

            return compressor;
        }