Exemple #1
0
        private void ProcessTag(ParsedTag tag, Queue <string> contentTokens, StringBuilder sb)
        {
            var profiler = MiniProfiler.Current;

            using (profiler.Step("Process tag " + tag.TagName))
            {
                var handlers = TagProvider.GetHandlers();

                if (handlers.ContainsKey(tag.TagName))
                {
                    ITagHandler handler = handlers[tag.TagName];
                    if (handler != null)
                    {
                        string contentsFlat = string.Empty;
                        foreach (string s in contentTokens)
                        {
                            contentsFlat += s;
                        }
                        using (profiler.Step("Handler Processing: " + tag.TagName))
                        {
                            handler.Process(sb, this.MTApp, this.ViewBag, this.TagProvider, tag, contentsFlat);
                        }
                    }
                }
            }
        }
Exemple #2
0
        private ParsedTag ParseTag(string token, bool selfClosed)
        {
            ParsedTag t = new ParsedTag();

            t.TagName      = ParseTagName(token);
            t.IsSelfClosed = selfClosed;
            ParseTagAttributes(t, token);
            return(t);
        }
Exemple #3
0
        private void ParseTagAttributes(ParsedTag t, string input)
        {
            string tagname = ParseTagName(input);
            string temp    = input.TrimStart('<');

            temp = temp.TrimEnd('>');
            temp = temp.TrimEnd('/');
            //Trim off tag name
            temp = temp.Substring(tagname.Length, temp.Length - (tagname.Length));
            temp = temp.Trim();

            bool isParsingAttribute      = false;
            bool isParsingAttributeValue = false;

            TempAttr currentAttribute = null;

            // loop through all characters, splitting of attributes
            char[] characters = temp.ToCharArray();
            for (int i = 0; i < temp.Length; i++)
            {
                char current = temp[i];

                if (isParsingAttribute)
                {
                    if (isParsingAttributeValue)
                    {
                        // append the current character
                        currentAttribute.Value += current;

                        // check to see if we're done with the attribute
                        if (currentAttribute.Value.Length >= 2)
                        {
                            if (currentAttribute.Value.EndsWith("\""))
                            {
                                isParsingAttributeValue = false;
                                isParsingAttribute      = false;

                                currentAttribute.Value = currentAttribute.Value.TrimStart('"');
                                currentAttribute.Value = currentAttribute.Value.TrimEnd('"');

                                // Add or overwrite value
                                t.SetSafeAttribute(currentAttribute.Name, currentAttribute.Value);

                                currentAttribute = null;
                            }
                        }
                    }
                    else
                    {
                        // we're not parsing the value yet so check for "="
                        if (current == '=')
                        {
                            // skip this charater but enable attribute value parsing;
                            isParsingAttributeValue = true;
                        }
                        else
                        {
                            currentAttribute.Name += current;
                        }
                    }
                }
                else
                {
                    // not parsing right now, check to see if we need to start
                    if (!char.IsWhiteSpace(current))
                    {
                        // not white space so let's start our attribute name
                        currentAttribute   = new TempAttr(current.ToString(), "");
                        isParsingAttribute = true;
                    }
                }
            }
        }
Exemple #4
0
        private void ProcessTemplate(StringBuilder sb, Queue <string> tokens)
        {
            var profiler = MiniProfiler.Current;

            using (profiler.Step("Process Template"))
            {
                string tagStarter = "<";

                bool           parsingTag = false;
                Queue <string> subqueue   = new Queue <string>();
                string         startToken = string.Empty;

                while (tokens.Count > 0)
                {
                    string currentToken = tokens.Dequeue();

                    if (parsingTag)
                    {
                        if (IsClosingTag(currentToken, ParseTagName(startToken)))
                        {
                            // Yes, this tag closed the starting tag
                            ParsedTag t = ParseTag(startToken, false);
                            ProcessTag(t, subqueue, sb);

                            // reset everything since the tag is parsed below
                            parsingTag = false;
                            startToken = "";
                            subqueue   = new Queue <string>();
                        }
                        else
                        {
                            // Nope, no closing yet, just enqueue the token
                            subqueue.Enqueue(currentToken);
                        }
                    }
                    else
                    {
                        // we're not parsing, check for a tag start
                        if (currentToken.StartsWith(tagStarter))
                        {
                            if (IsAcceptedTag(currentToken))
                            {
                                parsingTag = true;

                                if (IsSelfClosed(currentToken))
                                {
                                    // Tag is self closed, just parse it
                                    ParsedTag t2 = ParseTag(currentToken, true);
                                    ProcessTag(t2, subqueue, sb);

                                    parsingTag = false;
                                }
                                else
                                {
                                    // store the starting token for later parsing
                                    startToken = currentToken;
                                }
                            }
                            else
                            {
                                // not an accepted tag, just dump the sucker
                                sb.Append(currentToken);
                            }
                        }
                        else
                        {
                            // not starting a tag, just dump the output
                            sb.Append(currentToken);
                        }
                    }
                }


                if (parsingTag)
                {
                    if (startToken.Length > 0)
                    {
                        sb.Append(startToken);
                    }
                }

                // if the subque has items in it because we didn't find a closing tag, dump them
                if (subqueue.Count > 0)
                {
                    while (subqueue.Count > 0)
                    {
                        string subqueuetoken = subqueue.Dequeue();
                        sb.Append(subqueuetoken);
                    }
                }
            } // End Profiler wrapper
        }
Exemple #5
0
        private void ParseTagAttributes(ParsedTag t, string input)
        {

            string tagname = ParseTagName(input);
            string temp = input.TrimStart('<');
            temp = temp.TrimEnd('>');
            temp = temp.TrimEnd('/');
            //Trim off tag name
            temp = temp.Substring(tagname.Length, temp.Length - (tagname.Length));
            temp = temp.Trim();

            bool isParsingAttribute = false;
            bool isParsingAttributeValue = false;

            TempAttr currentAttribute = null;

            // loop through all characters, splitting of attributes
            char[] characters = temp.ToCharArray();
            for (int i = 0; i < temp.Length; i++)
            {
                char current = temp[i];

                if (isParsingAttribute)
                {
                    if (isParsingAttributeValue)
                    {
                        // append the current character
                        currentAttribute.Value += current;

                        // check to see if we're done with the attribute
                        if (currentAttribute.Value.Length >= 2)
                        {
                            if (currentAttribute.Value.EndsWith("\""))
                            {
                                isParsingAttributeValue = false;
                                isParsingAttribute = false;

                                currentAttribute.Value = currentAttribute.Value.TrimStart('"');
                                currentAttribute.Value = currentAttribute.Value.TrimEnd('"');

                                // Add or overwrite value
                                t.SetSafeAttribute(currentAttribute.Name, currentAttribute.Value);

                                currentAttribute = null;
                            }
                        }
                    }
                    else
                    {
                        // we're not parsing the value yet so check for "="
                        if (current == '=')
                        {
                            // skip this charater but enable attribute value parsing;
                            isParsingAttributeValue = true;
                        }
                        else
                        {
                            currentAttribute.Name += current;
                        }
                    }
                }
                else
                {
                    // not parsing right now, check to see if we need to start
                    if (!char.IsWhiteSpace(current))
                    {
                        // not white space so let's start our attribute name
                        currentAttribute = new TempAttr(current.ToString(), "");
                        isParsingAttribute = true;
                    }

                }

            }
        }
Exemple #6
0
 private ParsedTag ParseTag(string token, bool selfClosed)
 {
     ParsedTag t = new ParsedTag();
     t.TagName = ParseTagName(token);
     t.IsSelfClosed = selfClosed;
     ParseTagAttributes(t, token);
     return t;
 }
Exemple #7
0
        private void ProcessTag(ParsedTag tag, Queue<string> contentTokens, StringBuilder sb)
        {
            var profiler = MiniProfiler.Current;
            using (profiler.Step("Process tag " + tag.TagName))
            {
                var handlers = TagProvider.GetHandlers();

                if (handlers.ContainsKey(tag.TagName))
                {
                    ITagHandler handler = handlers[tag.TagName];
                    if (handler != null)
                    {
                        string contentsFlat = string.Empty;
                        foreach (string s in contentTokens)
                        {
                            contentsFlat += s;
                        }
                        using (profiler.Step("Handler Processing: " + tag.TagName))
                        {
                            handler.Process(sb, this.MTApp, this.ViewBag, this.TagProvider, tag, contentsFlat);
                        }
                    }
                }
            }
        }
        private void ProcessTag(ParsedTag tag, Queue<string> contentTokens, List<ITemplateAction> actions)
        {
            var handlers = TagProvider.GetHandlers();

            if (handlers.ContainsKey(tag.TagName))
            {
                ITagHandler handler = handlers[tag.TagName];
                if (handler != null)
                {
                    string contentsFlat = string.Empty;
                    foreach (string s in contentTokens)
                    {
                        contentsFlat += s;
                    }
                    handler.Process(actions, this.MTApp, this.TagProvider, tag, contentsFlat);
                }
            }
        }