/// <summary> /// End tags handler /// </summary> /// <param name="context">Markup parsing context</param> /// <param name="tagName">Tag name</param> private void EndTagHandler(MarkupParsingContext context, string tagName) { XmlNodeType previousNodeType = _currentNodeType; string previousText = _currentText; _currentNodeType = XmlNodeType.EndTag; _currentText = string.Empty; XmlMinificationOutputWriter output = _output; if (_settings.CollapseTagsWithoutContent && previousNodeType == XmlNodeType.StartTag && previousText.Length == 0) { if (output.TransformLastStartTagToEmptyTag(_settings.RenderEmptyTagsWithSpace)) { output.Flush(); return; } } if (_settings.MinifyWhitespace && previousNodeType == XmlNodeType.Text && (_endTagBeforeText || _emptyTagBeforeText)) { output.RemoveLastWhitespaceItems(); } // Add end tag to buffer output.Write("</"); output.Write(tagName); output.Write(">"); output.Flush(); }
/// <summary> /// Start tags handler /// </summary> /// <param name="context">Markup parsing context</param> /// <param name="tagName">Tag name</param> /// <param name="attributes">List of attributes</param> private void StartTagHandler(MarkupParsingContext context, string tagName, List <XmlAttribute> attributes) { XmlNodeType previousNodeType = _currentNodeType; _currentNodeType = XmlNodeType.StartTag; _currentText = string.Empty; XmlMinificationOutputWriter output = _output; if (_settings.MinifyWhitespace && previousNodeType == XmlNodeType.Text && (_startTagBeforeText || _endTagBeforeText || _emptyTagBeforeText || _ignoredFragmentBeforeText || _xmlDeclarationBeforeText || _processingInstructionBeforeText || _doctypeBeforeText)) { output.RemoveLastWhitespaceItems(); } output.Flush(); output.Write("<"); output.Write(tagName); WriteAttributes(attributes); output.Write(">"); }
/// <summary> /// Document type declaration handler /// </summary> /// <param name="context">Markup parsing context</param> /// <param name="doctype">Document type declaration</param> private void DoctypeDelegateHandler(MarkupParsingContext context, string doctype) { _currentNodeType = XmlNodeType.Doctype; XmlMinificationOutputWriter output = _output; if (_settings.MinifyWhitespace) { output.RemoveLastWhitespaceItems(); } output.Write(Utils.CollapseWhitespace(doctype)); output.Flush(); }
/// <summary> /// Ignored fragments handler /// </summary> /// <param name="context">Markup parsing context</param> /// <param name="fragment">Ignored fragment</param> private void IgnoredFragmentHandler(MarkupParsingContext context, string fragment) { _currentNodeType = XmlNodeType.IgnoredFragment; XmlMinificationOutputWriter output = _output; if (_settings.MinifyWhitespace) { output.RemoveLastWhitespaceItems(); } if (fragment.Length > 0) { output.Write(fragment); output.Flush(); } }
/// <summary> /// XML declaration handler /// </summary> /// <param name="context">Markup parsing context</param> /// <param name="attributes">List of attributes</param> private void XmlDeclarationHandler(MarkupParsingContext context, List <XmlAttribute> attributes) { _currentNodeType = XmlNodeType.XmlDeclaration; XmlMinificationOutputWriter output = _output; if (_settings.MinifyWhitespace) { output.RemoveLastWhitespaceItems(); } output.Write("<?xml"); WriteAttributes(attributes); output.Write("?>"); output.Flush(); }
/// <summary> /// Processing instruction handler /// </summary> /// <param name="context">Markup parsing context</param> /// <param name="instructionName">Instruction name</param> /// <param name="attributes">List of attributes</param> private void ProcessingInstructionHandler(MarkupParsingContext context, string instructionName, List <XmlAttribute> attributes) { _currentNodeType = XmlNodeType.ProcessingInstruction; XmlMinificationOutputWriter output = _output; if (_settings.MinifyWhitespace) { output.RemoveLastWhitespaceItems(); } output.Write("<?"); output.Write(instructionName); WriteAttributes(attributes); output.Write("?>"); output.Flush(); }
/// <summary> /// Empty tags handler /// </summary> /// <param name="context">Markup parsing context</param> /// <param name="tagName">Tag name</param> /// <param name="attributes">List of attributes</param> private void EmptyTagHandler(MarkupParsingContext context, string tagName, List <XmlAttribute> attributes) { XmlNodeType previousNodeType = _currentNodeType; _currentNodeType = XmlNodeType.EmptyTag; _currentText = string.Empty; XmlMinificationOutputWriter output = _output; if (_settings.MinifyWhitespace && previousNodeType == XmlNodeType.Text && (_startTagBeforeText || _endTagBeforeText || _emptyTagBeforeText)) { output.RemoveLastWhitespaceItems(); } output.Write("<"); output.Write(tagName); WriteAttributes(attributes); output.Write(_settings.RenderEmptyTagsWithSpace ? " />" : "/>"); output.Flush(); }
/// <summary> /// Minify XML content /// </summary> /// <param name="content">XML content</param> /// <param name="fileContext">File context</param> /// <param name="encoding">Text encoding</param> /// <param name="generateStatistics">Flag for whether to allow generate minification statistics</param> /// <returns>Minification result</returns> public MarkupMinificationResult Minify(string content, string fileContext, Encoding encoding, bool generateStatistics) { MinificationStatistics statistics = null; string cleanedContent = Utils.RemoveByteOrderMark(content); string minifiedContent = string.Empty; var stringBuilderPool = StringBuilderPool.Shared; StringBuilder sb = null; XmlMinificationOutputWriter output = _output; var errors = new List <MinificationErrorInfo>(); lock (_minificationSynchronizer) { try { if (generateStatistics) { statistics = new MinificationStatistics(encoding); statistics.Init(cleanedContent); } sb = stringBuilderPool.Rent(cleanedContent.Length); output.StringBuilder = sb; _xmlParser.Parse(cleanedContent); output.Flush(); if (_errors.Count == 0) { minifiedContent = output.ToString(); if (generateStatistics) { statistics.End(minifiedContent); } } } catch (MarkupParsingException e) { WriteError(LogCategoryConstants.XmlParsingError, e.Message, fileContext, e.LineNumber, e.ColumnNumber, e.SourceFragment); } finally { output.Clear(); output.StringBuilder = null; stringBuilderPool.Return(sb); _currentNodeType = XmlNodeType.Unknown; _currentText = string.Empty; errors.AddRange(_errors); _errors.Clear(); } if (errors.Count == 0) { _logger.Info(LogCategoryConstants.XmlMinificationSuccess, string.Format(Strings.SuccesMessage_MarkupMinificationComplete, "XML"), fileContext, statistics); } } return(new MarkupMinificationResult(minifiedContent, errors, statistics)); }