GetSourceFragment() public method

Gets a source fragment
public GetSourceFragment ( ) : string
return string
        /// <summary>
        /// Process a start ignoring comment tags
        /// </summary>
        /// <returns>Result of processing (true - is processed; false - is not processed)</returns>
        protected bool ProcessStartIgnoringCommentTag()
        {
            string content          = _innerContext.SourceCode;
            int    startTagLength   = START_IGNORING_COMMENT_TAG.Length;
            int    endTagLength     = END_IGNORING_COMMENT_TAG.Length;
            int    startTagPosition = _innerContext.Position;
            int    endTagPosition   = content.IndexOf(END_IGNORING_COMMENT_TAG,
                                                      startTagPosition + startTagLength, StringComparison.Ordinal);

            if (endTagPosition != -1)
            {
                string fragment = content.Substring(startTagPosition + startTagLength,
                                                    endTagPosition - startTagPosition - startTagLength);

                var ignoredFragmentHandler = CommonHandlers.IgnoredFragment;
                if (ignoredFragmentHandler != null)
                {
                    ignoredFragmentHandler(_context, fragment);
                }

                _innerContext.IncreasePosition(endTagPosition + endTagLength - startTagPosition);
                return(true);
            }

            throw new MarkupParsingException(
                      Strings.ErrorMessage_NotClosedIgnoringCommentTag,
                      _innerContext.NodeCoordinates, _innerContext.GetSourceFragment());
        }
Beispiel #2
0
        /// <summary>
        /// Parses XML content
        /// </summary>
        /// <param name="content">XML content</param>
        public void Parse(string content)
        {
            int contentLength = content.Length;
            if (contentLength == 0)
            {
                return;
            }

            lock (_parsingSynchronizer)
            {
                _innerContext = new InnerMarkupParsingContext(content);
                _context = new MarkupParsingContext(_innerContext);

                int endPosition = contentLength - 1;
                int previousPosition = -1;

                try
                {
                    while (_innerContext.Position <= endPosition)
                    {
                        bool isProcessed = false;
                        int firstCharPosition = _innerContext.Position;
                        char firstCharValue;
                        bool firstCharExist = content.TryGetChar(firstCharPosition, out firstCharValue);

                        if (firstCharExist && firstCharValue == '<')
                        {
                            int secondCharPosition = firstCharPosition + 1;
                            char secondCharValue;
                            bool secondCharExist = content.TryGetChar(secondCharPosition, out secondCharValue);

                            if (secondCharExist)
                            {
                                if (IsTagFirstChar(secondCharValue))
                                {
                                    // Start tag
                                    isProcessed = ProcessStartTag();
                                }
                                else
                                {
                                    int thirdCharPosition = secondCharPosition + 1;
                                    char thirdCharValue;
                                    bool thirdCharExist = content.TryGetChar(thirdCharPosition, out thirdCharValue);

                                    if (thirdCharExist)
                                    {
                                        switch (secondCharValue)
                                        {
                                            case '/':
                                                if (IsTagFirstChar(thirdCharValue))
                                                {
                                                    // End tag
                                                    isProcessed = ProcessEndTag();
                                                }
                                                break;

                                            case '!':
                                                switch (thirdCharValue)
                                                {
                                                    case '-':
                                                        int fourthCharPosition = thirdCharPosition + 1;
                                                        char fourthCharValue;
                                                        bool fourthCharExist = content.TryGetChar(
                                                            fourthCharPosition, out fourthCharValue);

                                                        if (fourthCharExist && fourthCharValue == '-')
                                                        {
                                                            // XML comments
                                                            isProcessed = ProcessComment();
                                                        }
                                                        break;

                                                    case '[':
                                                        // CDATA sections
                                                        isProcessed = ProcessCdataSection();
                                                        break;

                                                    case 'D':
                                                    case 'd':
                                                        // Doctype declaration
                                                        isProcessed = ProcessDoctype();
                                                        break;
                                                }
                                                break;

                                            case '?':
                                                // XML declaration and processing instructions
                                                isProcessed = ProcessProcessingInstruction();
                                                break;
                                        }
                                    }
                                }
                            }
                        }

                        if (!isProcessed)
                        {
                            // Text
                            ProcessText();
                        }

                        if (_innerContext.Position == previousPosition)
                        {
                            throw new MarkupParsingException(
                                string.Format(Strings.ErrorMessage_MarkupParsingFailed, "XML"),
                                _innerContext.NodeCoordinates, _innerContext.GetSourceFragment());
                        }

                        previousPosition = _innerContext.Position;
                    }

                    // Check whether there were not closed tags
                    if (_tagStack.Count > 0)
                    {
                        StackedXmlTag stackedTag = _tagStack.Pop();

                        throw new MarkupParsingException(
                            string.Format(Strings.ErrorMessage_NotClosedTag, stackedTag.Name),
                            stackedTag.Coordinates,
                            SourceCodeNavigator.GetSourceFragment(_innerContext.SourceCode, stackedTag.Coordinates));
                    }
                }
                catch (MarkupParsingException)
                {
                    throw;
                }
                finally
                {
                    _tagStack.Clear();

                    _context = null;
                    _innerContext = null;
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Parses XML content
        /// </summary>
        /// <param name="content">XML content</param>
        public void Parse(string content)
        {
            int contentLength = content.Length;

            if (contentLength == 0)
            {
                return;
            }

            lock (_parsingSynchronizer)
            {
                _innerContext = new InnerMarkupParsingContext(content);
                _context      = new MarkupParsingContext(_innerContext);

                _tagStack = new Stack <StackedXmlTag>();

                int endPosition      = contentLength - 1;
                int previousPosition = -1;

                try
                {
                    while (_innerContext.Position <= endPosition)
                    {
                        bool isProcessed = false;

                        if (content.CustomStartsWith("<", _innerContext.Position, StringComparison.Ordinal))
                        {
                            if (content.CustomStartsWith("</", _innerContext.Position, StringComparison.Ordinal))
                            {
                                // End tag
                                isProcessed = ProcessEndTag();
                            }
                            else if (content.CustomStartsWith("<!", _innerContext.Position, StringComparison.Ordinal))
                            {
                                // XML comments
                                isProcessed = ProcessComment();

                                if (!isProcessed)
                                {
                                    // CDATA sections
                                    isProcessed = ProcessCdataSection();
                                }

                                if (!isProcessed)
                                {
                                    // Doctype declaration
                                    isProcessed = ProcessDoctype();
                                }
                            }
                            else if (content.CustomStartsWith("<?", _innerContext.Position, StringComparison.Ordinal))
                            {
                                // XML declaration and processing instructions
                                isProcessed = ProcessProcessingInstruction();
                            }
                            else
                            {
                                // Start tag
                                isProcessed = ProcessStartTag();
                            }
                        }

                        if (!isProcessed)
                        {
                            // Text
                            ProcessText();
                        }

                        if (_innerContext.Position == previousPosition)
                        {
                            throw new XmlParsingException(
                                      string.Format(Strings.ErrorMessage_MarkupParsingFailed, "XML"),
                                      _innerContext.NodeCoordinates, _innerContext.GetSourceFragment());
                        }

                        previousPosition = _innerContext.Position;
                    }

                    // Check whether there were not closed tags
                    if (_tagStack.Count > 0)
                    {
                        StackedXmlTag stackedTag = _tagStack.Pop();

                        throw new XmlParsingException(
                                  string.Format(Strings.ErrorMessage_NotClosedTag, stackedTag.Name),
                                  stackedTag.Coordinates,
                                  SourceCodeNavigator.GetSourceFragment(_innerContext.SourceCode, stackedTag.Coordinates));
                    }
                }
                catch (XmlParsingException)
                {
                    throw;
                }
                finally
                {
                    _tagStack.Clear();

                    _context      = null;
                    _innerContext = null;
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Parses XML content
        /// </summary>
        /// <param name="content">XML content</param>
        public void Parse(string content)
        {
            int contentLength = content.Length;

            if (contentLength == 0)
            {
                return;
            }

            lock (_parsingSynchronizer)
            {
                _innerContext = new InnerMarkupParsingContext(content);
                _context      = new MarkupParsingContext(_innerContext);

                int endPosition      = contentLength - 1;
                int previousPosition = -1;

                try
                {
                    while (_innerContext.Position <= endPosition)
                    {
                        bool isProcessed = false;

                        if (_innerContext.PeekCurrentChar() == '<')
                        {
                            switch (_innerContext.PeekNextChar())
                            {
                            case char c when IsTagFirstChar(c):
                                // Start tag
                                isProcessed = ProcessStartTag();

                                break;

                            case '/':
                                if (IsTagFirstChar(_innerContext.PeekNextChar()))
                                {
                                    // End tag
                                    isProcessed = ProcessEndTag();
                                }
                                break;

                            case '!':
                                switch (_innerContext.PeekNextChar())
                                {
                                case '-':
                                    if (_innerContext.PeekNextChar() == '-')
                                    {
                                        // XML comments
                                        isProcessed = ProcessComment();
                                    }
                                    break;

                                case '[':
                                    // CDATA sections
                                    isProcessed = ProcessCdataSection();
                                    break;

                                case 'D':
                                    // Doctype declaration
                                    isProcessed = ProcessDoctype();
                                    break;
                                }
                                break;

                            case '?':
                                // XML declaration and processing instructions
                                isProcessed = ProcessProcessingInstruction();
                                break;
                            }
                        }

                        if (!isProcessed)
                        {
                            // Text
                            ProcessText();
                        }

                        if (_innerContext.Position == previousPosition)
                        {
                            throw new MarkupParsingException(
                                      string.Format(Strings.ErrorMessage_MarkupParsingFailed, "XML"),
                                      _innerContext.NodeCoordinates, _innerContext.GetSourceFragment());
                        }

                        previousPosition = _innerContext.Position;
                    }

                    // Check whether there were not closed tags
                    if (_tagStack.Count > 0)
                    {
                        StackedXmlTag stackedTag = _tagStack.Pop();

                        throw new MarkupParsingException(
                                  string.Format(Strings.ErrorMessage_NotClosedTag, stackedTag.Name),
                                  stackedTag.Coordinates,
                                  SourceCodeNavigator.GetSourceFragment(_innerContext.SourceCode, stackedTag.Coordinates));
                    }
                }
                catch (MarkupParsingException)
                {
                    throw;
                }
                finally
                {
                    _tagStack.Clear();

                    _context      = null;
                    _innerContext = null;
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Parses HTML content
        /// </summary>
        /// <param name="content">HTML content</param>
        public void Parse(string content)
        {
            int contentLength = content.Length;

            if (contentLength == 0)
            {
                return;
            }

            lock (_parsingSynchronizer)
            {
                _innerContext = new InnerMarkupParsingContext(content);
                _context      = new MarkupParsingContext(_innerContext);

                int endPosition      = contentLength - 1;
                int previousPosition = -1;

                try
                {
                    while (_innerContext.Position <= endPosition)
                    {
                        bool    isProcessed    = false;
                        HtmlTag lastStackedTag = _tagStack.LastOrDefault();

                        // Make sure we're not in a tag, that contains embedded code
                        if (lastStackedTag == null || !lastStackedTag.Flags.IsSet(HtmlTagFlags.EmbeddedCode))
                        {
                            if (_innerContext.PeekCurrentChar() == '<')
                            {
                                switch (_innerContext.PeekNextChar())
                                {
                                case char c when c.IsAlphaNumeric():
                                    // Start tag
                                    isProcessed = ProcessStartTag();

                                    break;

                                case '/':
                                    if (_innerContext.PeekNextChar().IsAlphaNumeric())
                                    {
                                        // End tag
                                        isProcessed = ProcessEndTag();
                                    }
                                    break;

                                case '!':
                                    switch (_innerContext.PeekNextChar())
                                    {
                                    case '-':
                                        if (_innerContext.PeekNextChar() == '-')
                                        {
                                            // Comments
                                            if (_innerContext.PeekNextChar() == '[')
                                            {
                                                // Revealed validating If conditional comments
                                                // (e.g. <!--[if ... ]><!--> or <!--[if ... ]>-->)
                                                isProcessed = ProcessRevealedValidatingIfComment();

                                                if (!isProcessed)
                                                {
                                                    // Hidden If conditional comments (e.g. <!--[if ... ]>)
                                                    isProcessed = ProcessHiddenIfComment();
                                                }
                                            }
                                            else
                                            {
                                                // Revealed validating End If conditional comments
                                                // (e.g. <!--<![endif]-->)
                                                isProcessed = ProcessRevealedValidatingEndIfComment();
                                            }

                                            if (!isProcessed)
                                            {
                                                // HTML comments
                                                isProcessed = ProcessComment();
                                            }
                                        }
                                        break;

                                    case '[':
                                        switch (_innerContext.PeekNextChar())
                                        {
                                        case 'i':
                                        case 'I':
                                            // Revealed If conditional comment (e.g. <![if ... ]>)
                                            isProcessed = ProcessRevealedIfComment();
                                            break;

                                        case 'e':
                                        case 'E':
                                            // Hidden End If conditional comment (e.g. <![endif]-->)
                                            isProcessed = ProcessHiddenEndIfComment();

                                            if (!isProcessed)
                                            {
                                                // Revealed End If conditional comment (e.g. <![endif]>)
                                                isProcessed = ProcessRevealedEndIfComment();
                                            }
                                            break;

                                        case 'C':
                                            // CDATA sections
                                            isProcessed = ProcessCdataSection();
                                            break;
                                        }
                                        break;

                                    case 'D':
                                    case 'd':
                                        // Doctype declaration
                                        isProcessed = ProcessDoctype();
                                        break;
                                    }
                                    break;

                                case '?':
                                    // XML declaration
                                    isProcessed = ProcessXmlDeclaration();
                                    break;
                                }
                            }

                            if (!isProcessed)
                            {
                                // Text
                                ProcessText();
                            }
                        }
                        else
                        {
                            // Embedded code
                            ProcessEmbeddedCode();
                        }

                        if (_innerContext.Position == previousPosition)
                        {
                            throw new MarkupParsingException(
                                      string.Format(Strings.ErrorMessage_MarkupParsingFailed, "HTML"),
                                      _innerContext.NodeCoordinates, _innerContext.GetSourceFragment());
                        }

                        previousPosition = _innerContext.Position;
                    }

                    // Clean up any remaining tags
                    ParseEndTag();

                    // Check whether there were not closed conditional comment
                    if (_conditionalCommentStack.Count > 0)
                    {
                        throw new MarkupParsingException(
                                  Strings.ErrorMessage_NotClosedConditionalComment,
                                  _innerContext.NodeCoordinates, _innerContext.GetSourceFragment());
                    }
                }
                catch (MarkupParsingException)
                {
                    throw;
                }
                finally
                {
                    _tagStack.Clear();
                    _tempAttributes.Clear();
                    _conditionalCommentStack.Clear();
                    _conditionalCommentOpened = false;
                    _xmlTagStack.Clear();
                    _context      = null;
                    _innerContext = null;
                }
            }
        }
Beispiel #6
0
        /// <summary>
        /// Parses HTML content
        /// </summary>
        /// <param name="content">HTML content</param>
        public void Parse(string content)
        {
            int contentLength = content.Length;
            if (contentLength == 0)
            {
                return;
            }

            lock (_parsingSynchronizer)
            {
                _innerContext = new InnerMarkupParsingContext(content);
                _context = new MarkupParsingContext(_innerContext);

                int endPosition = contentLength - 1;
                int previousPosition = -1;

                try
                {
                    while (_innerContext.Position <= endPosition)
                    {
                        bool isProcessed = false;
                        HtmlTag lastStackedTag = _tagStack.LastOrDefault();

                        // Make sure we're not in a tag, that contains embedded code
                        if (lastStackedTag == null || !lastStackedTag.Flags.HasFlag(HtmlTagFlags.EmbeddedCode))
                        {
                            int firstCharPosition = _innerContext.Position;
                            char firstCharValue;
                            bool firstCharExist = content.TryGetChar(firstCharPosition, out firstCharValue);

                            if (firstCharExist && firstCharValue == '<')
                            {
                                int secondCharPosition = firstCharPosition + 1;
                                char secondCharValue;
                                bool secondCharExist = content.TryGetChar(secondCharPosition, out secondCharValue);

                                if (secondCharExist)
                                {
                                    if (secondCharValue.IsAlphaNumeric())
                                    {
                                        // Start tag
                                        isProcessed = ProcessStartTag();
                                    }
                                    else
                                    {
                                        int thirdCharPosition = secondCharPosition + 1;
                                        char thirdCharValue;
                                        bool thirdCharExist = content.TryGetChar(thirdCharPosition, out thirdCharValue);

                                        if (thirdCharExist)
                                        {
                                            switch (secondCharValue)
                                            {
                                                case '/':
                                                    if (thirdCharValue.IsAlphaNumeric())
                                                    {
                                                        isProcessed = ProcessEndTag();
                                                    }
                                                    break;

                                                case '!':
                                                    switch (thirdCharValue)
                                                    {
                                                        case '-':
                                                            int fourthCharPosition = thirdCharPosition + 1;
                                                            char fourthCharValue;
                                                            bool fourthCharExist = content.TryGetChar(fourthCharPosition, out fourthCharValue);

                                                            if (fourthCharExist && fourthCharValue == '-')
                                                            {
                                                                // Comments
                                                                int fifthCharPosition = fourthCharPosition + 1;
                                                                char fifthCharValue;
                                                                bool fifthCharExist = content.TryGetChar(fifthCharPosition, out fifthCharValue);

                                                                if (fifthCharExist)
                                                                {
                                                                    if (fifthCharValue == '[')
                                                                    {
                                                                        // Revealed validating If conditional comments
                                                                        // (e.g. <!--[if ... ]><!--> or <!--[if ... ]>-->)
                                                                        isProcessed = ProcessRevealedValidatingIfComment();

                                                                        if (!isProcessed)
                                                                        {
                                                                            // Hidden If conditional comments (e.g. <!--[if ... ]>)
                                                                            isProcessed = ProcessHiddenIfComment();
                                                                        }
                                                                    }
                                                                    else
                                                                    {
                                                                        // Revealed validating End If conditional comments
                                                                        // (e.g. <!--<![endif]-->)
                                                                        isProcessed = ProcessRevealedValidatingEndIfComment();
                                                                    }
                                                                }

                                                                if (!isProcessed)
                                                                {
                                                                    // HTML comments
                                                                    isProcessed = ProcessComment();
                                                                }
                                                            }
                                                            break;

                                                        case '[':
                                                            // Remaining conditional comments

                                                            // Hidden End If conditional comment (e.g. <![endif]-->)
                                                            isProcessed = ProcessHiddenEndIfComment();

                                                            if (!isProcessed)
                                                            {
                                                                // Revealed If conditional comment (e.g. <![if ... ]>)
                                                                isProcessed = ProcessRevealedIfComment();
                                                            }

                                                            if (!isProcessed)
                                                            {
                                                                // Revealed End If conditional comment (e.g. <![endif]>)
                                                                isProcessed = ProcessRevealedEndIfComment();
                                                            }
                                                            break;

                                                        case 'D':
                                                        case 'd':
                                                            // Doctype declaration
                                                            isProcessed = ProcessDoctype();
                                                            break;
                                                    }
                                                    break;

                                                case '?':
                                                    // XML declaration
                                                    isProcessed = ProcessXmlDeclaration();
                                                    break;
                                            }
                                        }
                                    }
                                }
                            }

                            if (!isProcessed)
                            {
                                // Text
                                ProcessText();
                            }
                        }
                        else
                        {
                            // Embedded code
                            ProcessEmbeddedCode();
                        }

                        if (_innerContext.Position == previousPosition)
                        {
                            throw new MarkupParsingException(
                                string.Format(Strings.ErrorMessage_MarkupParsingFailed, "HTML"),
                                _innerContext.NodeCoordinates, _innerContext.GetSourceFragment());
                        }

                        previousPosition = _innerContext.Position;
                    }

                    // Clean up any remaining tags
                    ParseEndTag();

                    // Check whether there were not closed conditional comment
                    if (_conditionalCommentStack.Count > 0)
                    {
                        throw new MarkupParsingException(
                            Strings.ErrorMessage_NotClosedConditionalComment,
                            _innerContext.NodeCoordinates, _innerContext.GetSourceFragment());
                    }
                }
                catch (MarkupParsingException)
                {
                    throw;
                }
                finally
                {
                    _tagStack.Clear();
                    _htmlTagFlagsCache.Clear();
                    _customHtmlTagFlagsCache.Clear();
                    _conditionalCommentStack.Clear();
                    _conditionalCommentOpened = false;
                    _xmlTagStack.Clear();
                    _context = null;
                    _innerContext = null;
                }
            }
        }
Beispiel #7
0
        /// <summary>
        /// Parses XML content
        /// </summary>
        /// <param name="content">XML content</param>
        public void Parse(string content)
        {
            int contentLength = content.Length;
            if (contentLength == 0)
            {
                return;
            }

            lock (_parsingSynchronizer)
            {
                _innerContext = new InnerMarkupParsingContext(content);
                _context = new MarkupParsingContext(_innerContext);

                _tagStack = new Stack<StackedXmlTag>();

                int endPosition = contentLength - 1;
                int previousPosition = -1;

                try
                {
                    while (_innerContext.Position <= endPosition)
                    {
                        bool isProcessed = false;

                        if (content.CustomStartsWith("<", _innerContext.Position, StringComparison.Ordinal))
                        {
                            if (content.CustomStartsWith("</", _innerContext.Position, StringComparison.Ordinal))
                            {
                                // End tag
                                isProcessed = ProcessEndTag();
                            }
                            else if (content.CustomStartsWith("<!", _innerContext.Position, StringComparison.Ordinal))
                            {
                                // XML comments
                                isProcessed = ProcessComment();

                                if (!isProcessed)
                                {
                                    // CDATA sections
                                    isProcessed = ProcessCdataSection();
                                }

                                if (!isProcessed)
                                {
                                    // Doctype declaration
                                    isProcessed = ProcessDoctype();
                                }
                            }
                            else if (content.CustomStartsWith("<?", _innerContext.Position, StringComparison.Ordinal))
                            {
                                // XML declaration and processing instructions
                                isProcessed = ProcessProcessingInstruction();
                            }
                            else
                            {
                                // Start tag
                                isProcessed = ProcessStartTag();
                            }
                        }

                        if (!isProcessed)
                        {
                            // Text
                            ProcessText();
                        }

                        if (_innerContext.Position == previousPosition)
                        {
                            throw new XmlParsingException(
                                string.Format(Strings.ErrorMessage_MarkupParsingFailed, "XML"),
                                _innerContext.NodeCoordinates, _innerContext.GetSourceFragment());
                        }

                        previousPosition = _innerContext.Position;
                    }

                    // Check whether there were not closed tags
                    if (_tagStack.Count > 0)
                    {
                        StackedXmlTag stackedTag = _tagStack.Pop();

                        throw new XmlParsingException(
                            string.Format(Strings.ErrorMessage_NotClosedTag, stackedTag.Name),
                            stackedTag.Coordinates,
                            SourceCodeNavigator.GetSourceFragment(_innerContext.SourceCode, stackedTag.Coordinates));
                    }
                }
                catch (XmlParsingException)
                {
                    throw;
                }
                finally
                {
                    _tagStack.Clear();

                    _context = null;
                    _innerContext = null;
                }
            }
        }