private void ProcessEmbeddedScriptContent(MarkupParsingContext context, string content, string contentType)
{
string code = content;
bool isNotEmpty = false;
string processedContentType = !string.IsNullOrWhiteSpace(contentType) ?
contentType.Trim().ToLowerInvariant() : JS_CONTENT_TYPE;
bool isJavaScript = _jsContentTypes.Contains(processedContentType);
bool isVbScript = processedContentType == VBS_CONTENT_TYPE;
bool minifyWhitespace = _settings.WhitespaceMinificationMode != WhitespaceMinificationMode.None;
if (isJavaScript || isVbScript)
{
bool removeHtmlComments = _settings.RemoveHtmlCommentsFromScriptsAndStyles;
bool removeCdataSections = _settings.RemoveCdataSectionsFromScriptsAndStyles;
string startPart = string.Empty;
string endPart = string.Empty;
string newLine = Environment.NewLine;
string beforeCodeContent = string.Empty;
if (isJavaScript)
{
// Processing of JavaScript code
if (_beginCdataSectionRegex.IsMatch(content))
{
beforeCodeContent = _beginCdataSectionRegex.Match(content).Value;
startPart = "<![CDATA[";
endPart = "]]>";
code = Utils.RemovePrefixAndPostfix(content, _beginCdataSectionRegex, _endCdataSectionRegex);
}
else if (_scriptBeginCdataSectionRegex.IsMatch(content))
{
beforeCodeContent = _scriptBeginCdataSectionRegex.Match(content).Value;
if (!removeCdataSections)
{
startPart = "//<![CDATA[";
endPart = "//]]>";
}
code = Utils.RemovePrefixAndPostfix(content, _scriptBeginCdataSectionRegex,
_scriptEndCdataSectionRegex);
}
else if (_scriptBeginMaxCompatibleCdataSectionRegex.IsMatch(content))
{
beforeCodeContent = _scriptBeginMaxCompatibleCdataSectionRegex.Match(content).Value;
if (!removeCdataSections)
{
startPart = "<!--//--><![CDATA[//><!--";
endPart = "//--><!]]>";
}
code = Utils.RemovePrefixAndPostfix(content, _scriptBeginMaxCompatibleCdataSectionRegex,
_scriptEndMaxCompatibleCdataSectionRegex);
}
else if (_scriptBeginHtmlCommentRegex.IsMatch(content))
{
beforeCodeContent = _scriptBeginHtmlCommentRegex.Match(content).Value;
if (!removeHtmlComments)
{
startPart = "<!--";
endPart = "//-->";
}
code = Utils.RemovePrefixAndPostfix(content, _scriptBeginHtmlCommentRegex,
_scriptEndHtmlCommentRegex);
}
if (_settings.MinifyEmbeddedJsCode)
{
CodeMinificationResult minificationResult = _jsMinifier.Minify(code, false);
if (minificationResult.Errors.Count == 0)
{
code = minificationResult.MinifiedContent ?? string.Empty;
}
if (minificationResult.Errors.Count > 0 || minificationResult.Warnings.Count > 0)
{
string sourceCode = context.SourceCode;
var documentCoordinates = SourceCodeNavigator.CalculateAbsoluteNodeCoordinates(
context.NodeCoordinates, beforeCodeContent);
foreach (MinificationErrorInfo error in minificationResult.Errors)
{
var relativeErrorCoordinates = new SourceCodeNodeCoordinates(error.LineNumber, error.ColumnNumber);
var absoluteErrorCoordinates = SourceCodeNavigator.CalculateAbsoluteNodeCoordinates(
documentCoordinates, relativeErrorCoordinates);
string sourceFragment = SourceCodeNavigator.GetSourceFragment(
sourceCode, absoluteErrorCoordinates);
string message = error.Message.Trim();
WriteError(LogCategoryConstants.JsMinificationError, message, _fileContext,
absoluteErrorCoordinates.LineNumber, absoluteErrorCoordinates.ColumnNumber, sourceFragment);
}
foreach (MinificationErrorInfo warning in minificationResult.Warnings)
{
var relativeErrorCoordinates = new SourceCodeNodeCoordinates(warning.LineNumber, warning.ColumnNumber);
var absoluteErrorCoordinates = SourceCodeNavigator.CalculateAbsoluteNodeCoordinates(
documentCoordinates, relativeErrorCoordinates);
string sourceFragment = SourceCodeNavigator.GetSourceFragment(
sourceCode, absoluteErrorCoordinates);
string message = warning.Message.Trim();
WriteWarning(LogCategoryConstants.JsMinificationWarning, message, _fileContext,
absoluteErrorCoordinates.LineNumber, absoluteErrorCoordinates.ColumnNumber, sourceFragment);
}
}
}
}
else
{
// Processing of VBScript code
if (_beginCdataSectionRegex.IsMatch(content))
{
startPart = "<![CDATA[";
endPart = "]]>";
code = Utils.RemovePrefixAndPostfix(content, _beginCdataSectionRegex, _endCdataSectionRegex);
}
else if (_beginHtmlCommentRegex.IsMatch(content))
{
if (!removeHtmlComments)
{
startPart = "<!--";
endPart = "-->";
}
code = Utils.RemovePrefixAndPostfix(content, _beginHtmlCommentRegex, _endHtmlCommentRegex);
}
}
if (minifyWhitespace && code.Length > 0)
{
code = code.Trim();
}
if (startPart.Length > 0)
{
_buffer.Add(startPart);
if (newLine.Length > 0)
{
_buffer.Add(newLine);
}
isNotEmpty = true;
}
if (code.Length > 0)
{
_buffer.Add(code);
isNotEmpty = true;
}
if (endPart.Length > 0)
{
if (newLine.Length > 0)
{
_buffer.Add(newLine);
}
_buffer.Add(endPart);
isNotEmpty = true;
}
_currentText = isNotEmpty ? EMBEDDED_CODE_PLACEHOLDER : string.Empty;
return;
}
if (_settings.ProcessableScriptTypeCollection.Contains(processedContentType))
{
// Processing of JavaScript template
GenericHtmlMinifier innerHtmlMinifier = GetInnerHtmlMinifierInstance();
MarkupMinificationResult minificationResult = innerHtmlMinifier.Minify(content, false);
if (minificationResult.Errors.Count == 0)
{
code = minificationResult.MinifiedContent ?? string.Empty;
}
if (minificationResult.Errors.Count > 0 || minificationResult.Warnings.Count > 0)
{
string sourceCode = context.SourceCode;
var documentCoordinates = context.NodeCoordinates;
foreach (MinificationErrorInfo error in minificationResult.Errors)
{
var relativeErrorCoordinates = new SourceCodeNodeCoordinates(error.LineNumber, error.ColumnNumber);
var absoluteErrorCoordinates = SourceCodeNavigator.CalculateAbsoluteNodeCoordinates(
documentCoordinates, relativeErrorCoordinates);
string sourceFragment = SourceCodeNavigator.GetSourceFragment(
sourceCode, absoluteErrorCoordinates);
string message = error.Message.Trim();
WriteError(LogCategoryConstants.JsTemplateMinificationError, message, _fileContext,
absoluteErrorCoordinates.LineNumber, absoluteErrorCoordinates.ColumnNumber, sourceFragment);
}
foreach (MinificationErrorInfo warning in minificationResult.Warnings)
{
var relativeErrorCoordinates = new SourceCodeNodeCoordinates(warning.LineNumber, warning.ColumnNumber);
var absoluteErrorCoordinates = SourceCodeNavigator.CalculateAbsoluteNodeCoordinates(
documentCoordinates, relativeErrorCoordinates);
string sourceFragment = SourceCodeNavigator.GetSourceFragment(
sourceCode, absoluteErrorCoordinates);
string message = warning.Message.Trim();
WriteWarning(LogCategoryConstants.JsTemplateMinificationWarning, message, _fileContext,
absoluteErrorCoordinates.LineNumber, absoluteErrorCoordinates.ColumnNumber, sourceFragment);
}
}
if (minifyWhitespace && code.Length > 0)
{
code = code.Trim();
}
}
if (code.Length > 0)
{
_buffer.Add(code);
isNotEmpty = true;
}
_currentText = isNotEmpty ? EMBEDDED_CODE_PLACEHOLDER : string.Empty;
}