Example #1
0
        /// <summary>
        /// Handles beginnings and ends of elements
        /// </summary>
        private void HandleAspElements()
        {
            if (currentChar == '>' && ((withinAspElement && !withinAttributeValue) || withinScriptBlock))   // end of tag
            {
                if (!withinScriptBlock)
                {
                    HitEnd(ref externalSpan, 0);
                    StartPlainText(1, 0);
                }
                // get element name
                if (string.IsNullOrEmpty(elementName) && attributeNameBuilder.Length > 0)
                {
                    elementName = attributeNameBuilder.ToString();
                    attributeNameBuilder.Length = 0;
                }

                if (withinEndAspElement)
                {
                    // content of <script> tags report as code
                    if (elementName.ToLower() == "script" && withinScriptBlock)
                    {
                        withinCodeBlock   = false;
                        withinScriptBlock = false;
                        HitEnd(ref externalSpan, -9);
                        HitEnd(ref internalSpan, -9);
                        if (scriptBeginTagContainedRunatServer)
                        {
                            handler.OnCodeBlock(new CodeBlockContext()
                            {
                                BlockText               = codeBuilder.ToString(0, codeBuilder.Length - 9),
                                InnerBlockSpan          = internalSpan,
                                OuterBlockSpan          = externalSpan,
                                WithinClientSideComment = withinClientComment
                            });
                        }
                        scriptBeginTagContainedRunatServer = false;
                        externalSpan       = null;
                        internalSpan       = null;
                        codeBuilder.Length = 0;
                    }
                    else     // other elements
                    {
                        handler.OnElementEnd(new EndElementContext()
                        {
                            BlockSpan               = externalSpan,
                            ElementName             = elementName,
                            Prefix                  = elementPrefix,
                            WithinClientSideComment = withinClientComment
                        });
                        codeBuilder.Length = 0;
                    }

                    if (softStop)
                    {
                        hardStop = true;
                    }
                    externalSpan = null;
                }
                else
                {
                    // begin code block
                    if (elementName.ToLower() == "script")
                    {
                        withinCodeBlock   = true;
                        withinScriptBlock = true;
                        scriptBeginTagContainedRunatServer = ContainRunatServer(attributes);
                        externalSpan = null;
                        HitStart(ref externalSpan, 1);
                        HitStart(ref internalSpan, 1);
                    }
                    else     // begin standard element
                    {
                        handler.OnElementBegin(new ElementContext()
                        {
                            Attributes              = attributes,
                            BlockSpan               = externalSpan,
                            ElementName             = elementName,
                            Prefix                  = elementPrefix,
                            WithinClientSideComment = withinClientComment,
                            IsEnd = lastNonWhitespaceChar == '/'
                        });
                        externalSpan       = null;
                        codeBuilder.Length = 0;
                        if (softStop)
                        {
                            hardStop = true;
                        }
                    }
                }

                withinAspElement    = false;
                withinEndAspElement = false;
            }

            if (!withinAspElement && !withinAspTags && GetCodeBack(1) == '<' &&
                (currentChar.CanBePartOfIdentifier() || currentChar == '/' || (currentChar == '!' && GetCodeForth(1) != '-')))
            {
                EndPlainText(-1, 2);

                withinAspElement    = true;
                elementName         = null;
                elementPrefix       = null;
                withinAttributeName = true;
                HitStart(ref externalSpan, -1);

                if (currentChar == '/')
                {
                    withinEndAspElement = true;
                    attributes          = null;
                }
                else
                {
                    attributeNameBuilder.Append(currentChar);
                    attributes = new List <AttributeInfo>();
                }
            }
        }
        /// <summary>
        /// Returns list of references in given block of code
        /// </summary>
        protected List <T> LookForReferences()
        {
            bool insideComment = false, insideString = false, isVerbatimString = false;
            bool skipLine = false, toRestore = false;
            CodeReferenceTrieElement previousState = null, prevPrevState = null, cachedState = null;

            currentChar     = '?';
            stringStartChar = '?';
            List <T> list = new List <T>();
            CodeReferenceTrieElement currentElement  = Trie.Root;
            StringBuilder            prefixBuilder   = new StringBuilder();
            StringBuilder            previousBuilder = new StringBuilder();
            StringBuilder            prevPrevBuilder = new StringBuilder();
            StringBuilder            cachedBuilder   = new StringBuilder();
            bool prefixContinue = false;
            bool continueLine   = false;

            for (globalIndex = 0; globalIndex < text.Length; globalIndex++)
            {
                currentChar = text[globalIndex];

                if (skipLine)   // read until end of line
                {
                    if (currentChar == '\n')
                    {
                        skipLine    = false;
                        cachedState = Trie.Root;
                    }
                }
                else
                {
                    bool oldInsideComment = insideComment;
                    PreProcessChar(ref insideComment, ref insideString, ref isVerbatimString, out skipLine);

                    if (!insideString && !insideComment)
                    {
                        if (toRestore)
                        {
                            currentElement = cachedState;
                            prefixBuilder  = cachedBuilder;
                            toRestore      = false;
                        }
                        if (oldInsideComment && cachedState != null && cachedState != Trie.Root)
                        {
                            toRestore = true;
                        }

                        prevPrevBuilder.Length = 0;
                        prevPrevBuilder.Append(previousBuilder);

                        previousBuilder.Length = 0;
                        previousBuilder.Append(prefixBuilder);

                        if (prefixBuilder.Length > 0)
                        {
                            if (currentChar == '.')
                            {
                                prefixContinue = true;
                            }
                            else if (UnderscoreIsLineJoiningChar && currentChar == '_' && char.IsWhiteSpace(GetCharBack(1)) && GetCharBack(-1) == '\r')
                            {
                                continueLine   = true;
                                prefixContinue = true;
                            }
                            else if (!currentChar.CanBePartOfIdentifier() && !char.IsWhiteSpace(currentChar))
                            {
                                prefixBuilder.Length = 0;
                            }
                        }

                        if (currentChar.CanBePartOfIdentifier() && !GetCharBack(1).CanBePartOfIdentifier())
                        {
                            if (!prefixContinue && !continueLine)
                            {
                                ReferenceStartIndex  = CurrentIndex;
                                ReferenceStartLine   = CurrentLine;
                                ReferenceStartOffset = CurrentAbsoluteOffset;
                                prefixBuilder.Length = 0;
                            }
                            prefixContinue = false;
                        }

                        if (GetCharBack(1) == '\n' && GetCharBack(2) == '\r')
                        {
                            if (continueLine)
                            {
                                prefixContinue = true;
                            }
                            continueLine = false;
                        }

                        if (!continueLine)
                        {
                            if (currentChar.CanBePartOfIdentifier() || currentChar == '.')
                            {
                                prefixBuilder.Append(currentChar);
                            }

                            prevPrevState = previousState;
                            previousState = currentElement;

                            // use trie to get next state
                            currentElement = Trie.Step(currentElement, currentChar);

                            if (currentElement.IsTerminal && !GetCharBack(-1).CanBePartOfIdentifier())
                            {
                                AddReferenceResult(list, prefixBuilder.ToString(), currentElement.Infos);
                            }
                        }
                    }
                    else
                    {
                        if (!oldInsideComment && insideComment)
                        {
                            cachedState          = prevPrevState;
                            cachedBuilder.Length = 0;
                            cachedBuilder.Append(prevPrevBuilder);
                        }
                        currentElement = Trie.Root;
                    }
                    if (!insideString || insideComment)
                    {
                        isVerbatimString = false;
                    }
                }

                Move();
            }

            return(list);
        }