Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variables in script tags #1

Open
gjunge opened this issue Feb 3, 2011 · 7 comments
Open

Variables in script tags #1

gjunge opened this issue Feb 3, 2011 · 7 comments

Comments

@gjunge
Copy link

gjunge commented Feb 3, 2011

First off, thanks for the great tool!

Found a bug though:
Assume this code:

  <script>
       var x = '<%= Url.Action("index") %>';
  </script>

The converter doesn't convert it...

@tsvetomir
Copy link
Member

Hi,

This is a known limitation (see the Readme). Feel free to hit us with a patch :)

@gjunge
Copy link
Author

gjunge commented Feb 3, 2011

Don't you just hate it when people don't read the readme...

@vongruenigen
Copy link
Contributor

@tsvetomir: How would you achive that?

@ghost
Copy link

ghost commented Aug 29, 2011

I've modified the Parse function in the WebFormsParser class to process the script block after startTagOpeningBracketRegex statement. This allows the parse to process any tags within the block and output the other sections of the script as IWebFormsTextNode's. Below the modified method is a unit test which I added to the WebFormsParserTests class to validate this function works correctly. I've used this to upgrade my views, but haven't had chance to run it through a complex solution yet.

public IDocument<IWebFormsNode> Parse(string input)
{
    Match match;
    int startAt = 0;

    var root = new WebFormsNode { Type = NodeType.Document };
    IWebFormsNode parentNode = root;

    do
    {
        if ((match = textRegex.Match(input, startAt)).Success)
        {
            AppendTextNode(parentNode, match);
            startAt = match.Index + match.Length;
        }

        if (startAt != input.Length)
        {
            if ((match = directiveRegex.Match(input, startAt)).Success)
            {
                var directiveNode = NodeFactory.CreateNode(match, NodeType.Directive);
                parentNode.Children.Add(directiveNode);
            }
            else if ((match = commentRegex.Match(input, startAt)).Success)
            {
                var commentNode = NodeFactory.CreateNode(match, NodeType.Comment);
                parentNode.Children.Add(commentNode);
            }
            else if ((match = runatServerTagRegex.Match(input, startAt)).Success)
            {
                var serverControlNode = NodeFactory.CreateNode(match, NodeType.ServerControl);
                parentNode.Children.Add(serverControlNode);
                parentNode = serverControlNode;
            }
            else if ((match = doctypeRegex.Match(input, startAt)).Success)
            {
                AppendTextNode(parentNode, match);
            }
            else if ((match = startTagOpeningBracketRegex.Match(input, startAt)).Success)
            {
                AppendTextNode(parentNode, match);
            }
            else if ((match = endTagRegex.Match(input, startAt)).Success)
            {
                var tagName = match.Groups["tagname"].Captures[0].Value;
                var serverControlParent = parentNode as IWebFormsServerControlNode;
                if (serverControlParent != null && tagName.ToLowerInvariant() == serverControlParent.TagName.ToLowerInvariant())
                {
                    parentNode = parentNode.Parent;
                }
                else
                {
                    AppendTextNode(parentNode, match);
                }
            }
            else if ((match = aspExprRegex.Match(input, startAt)).Success || (match = aspEncodedExprRegex.Match(input, startAt)).Success)
            {
                var expressionBlockNode = NodeFactory.CreateNode(match, NodeType.ExpressionBlock);
                parentNode.Children.Add(expressionBlockNode);
            }
            else if ((match = aspCodeRegex.Match(input, startAt)).Success)
            {
                var codeBlockNode = NodeFactory.CreateNode(match, NodeType.CodeBlock);
                parentNode.Children.Add(codeBlockNode);
            }
            else if ((match = scriptRegex.Match(input, startAt)).Success) // Relocated to enable processing of <% %> tags within the script block.
            {
                AppendTextNode(parentNode, match);
            }
            else
            {
                throw new Exception(
                    string.Format("Unrecognized page element: {0}...", input.Substring(startAt, 20)));
            }

            startAt = match.Index + match.Length;
        }
    }
    while (startAt != input.Length);

    ApplyPostprocessingFilters(root);

    return new Document<IWebFormsNode>(root);
}

[Fact]
public void Should_parse_javascript_nested_expressions()
{
    var script = "<script type=\"javascript\">var url = '<%= Url.Action(\"Test\", \"Controller\", null)%>';</script>";
    var document = parser.Parse(script);
    document.RootNode.Children.Count.ShouldEqual(3);
    ((IWebFormsTextNode)document.RootNode.Children[0]).Text.ShouldEqual("<script type=\"javascript\">var url = '");
    ((IWebFormsExpressionBlockNode)document.RootNode.Children[1]).Expression.ShouldEqual(" Url.Action(\"Test\", \"Controller\", null)");
    ((IWebFormsTextNode)document.RootNode.Children[2]).Text.ShouldEqual("';</script>");
}

@vongruenigen
Copy link
Contributor

@SteveDavis, thanks, I'll give it a try!

sebnilsson pushed a commit to sebnilsson/razor-converter that referenced this issue Oct 24, 2011
@lukini
Copy link

lukini commented May 17, 2016

I think that moving it to the end causes it to never be hit. However, I think this is the correct behavior. Using the master branch and commenting out the scriptRegex if statement, it causes a script tag to be treated as a regular start tag. I haven't noticed any issues so far.

What caused this to be added as a "known limitation"?

tsvetomir pushed a commit that referenced this issue May 18, 2016
@tsvetomir
Copy link
Member

I listed this as a limitation originally as I couldn't quite come up with a reliable way to detect the variables in any script.

Do you still have the issue with #7 merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants