private void ProcessEmbeddedStyleContent(MarkupParsingContext context, string content, string contentType)
{
string code;
bool isNotEmpty = false;
string processedContentType = !string.IsNullOrWhiteSpace(contentType) ?
contentType.Trim().ToLowerInvariant() : CSS_CONTENT_TYPE;
bool minifyWhitespace = _settings.WhitespaceMinificationMode != WhitespaceMinificationMode.None;
bool removeHtmlComments = _settings.RemoveHtmlCommentsFromScriptsAndStyles;
bool removeCdataSections = _settings.RemoveCdataSectionsFromScriptsAndStyles;
string startPart = string.Empty;
string endPart = string.Empty;
string newLine = string.Empty;
string beforeCodeContent = string.Empty;
if (_beginCdataSectionRegex.IsMatch(content))
{
beforeCodeContent = _beginCdataSectionRegex.Match(content).Value;
startPart = "<![CDATA[";
endPart = "]]>";
newLine = Environment.NewLine;
code = Utils.RemovePrefixAndPostfix(content, _beginCdataSectionRegex, _endCdataSectionRegex);
}
else if (_styleBeginCdataSectionRegex.IsMatch(content))
{
beforeCodeContent = _styleBeginCdataSectionRegex.Match(content).Value;
if (!removeCdataSections)
{
startPart = "/*<![CDATA[*/";
endPart = "/*]]>*/";
}
code = Utils.RemovePrefixAndPostfix(content, _styleBeginCdataSectionRegex, _styleEndCdataSectionRegex);
}
else if (_styleBeginMaxCompatibleCdataSectionRegex.IsMatch(content))
{
beforeCodeContent = _styleBeginMaxCompatibleCdataSectionRegex.Match(content).Value;
if (!removeCdataSections)
{
startPart = "<!--/*--><![CDATA[/*><!--*/";
endPart = "/*]]>*/-->";
}
code = Utils.RemovePrefixAndPostfix(content, _styleBeginMaxCompatibleCdataSectionRegex,
_styleEndMaxCompatibleCdataSectionRegex);
}
else if (_beginHtmlCommentRegex.IsMatch(content))
{
beforeCodeContent = _beginHtmlCommentRegex.Match(content).Value;
if (!removeHtmlComments)
{
startPart = "<!--";
endPart = "-->";
}
code = Utils.RemovePrefixAndPostfix(content, _beginHtmlCommentRegex, _endHtmlCommentRegex);
}
else
{
code = content;
}
if (processedContentType == CSS_CONTENT_TYPE && _settings.MinifyEmbeddedCssCode)
{
CodeMinificationResult minificationResult = _cssMinifier.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 documentNodeCoordinates = SourceCodeNavigator.CalculateAbsoluteNodeCoordinates(
context.NodeCoordinates, beforeCodeContent);
foreach (MinificationErrorInfo error in minificationResult.Errors)
{
var embeddedContentNodeCoordinates = new SourceCodeNodeCoordinates(error.LineNumber, error.ColumnNumber);
var absoluteNodeCoordinates = SourceCodeNavigator.CalculateAbsoluteNodeCoordinates(
documentNodeCoordinates, embeddedContentNodeCoordinates);
string sourceFragment = SourceCodeNavigator.GetSourceFragment(
sourceCode, absoluteNodeCoordinates);
string message = error.Message.Trim();
WriteError(LogCategoryConstants.CssMinificationError, message, _fileContext,
absoluteNodeCoordinates.LineNumber, absoluteNodeCoordinates.ColumnNumber, sourceFragment);
}
foreach (MinificationErrorInfo warning in minificationResult.Warnings)
{
var embeddedContentNodeCoordinates = new SourceCodeNodeCoordinates(warning.LineNumber, warning.ColumnNumber);
var absoluteNodeCoordinates = SourceCodeNavigator.CalculateAbsoluteNodeCoordinates(
documentNodeCoordinates, embeddedContentNodeCoordinates);
string sourceFragment = SourceCodeNavigator.GetSourceFragment(
sourceCode, absoluteNodeCoordinates);
string message = warning.Message.Trim();
WriteWarning(LogCategoryConstants.CssMinificationWarning, message, _fileContext,
absoluteNodeCoordinates.LineNumber, absoluteNodeCoordinates.ColumnNumber, sourceFragment);
}
}
}
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;
}