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

Get rid of System.Web dependency #132

Open
kinguru opened this issue Jun 22, 2023 · 2 comments
Open

Get rid of System.Web dependency #132

kinguru opened this issue Jun 22, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@kinguru
Copy link

kinguru commented Jun 22, 2023

Dear Alexander,
could you please add the Raw example using "System.Net.WebUtility.HtmlEncode" as it recommended to use in non-web projects.
System.Web even not available in a non-web projects so can't encode using current Raw sample.

@adoconnection
Copy link
Owner

adoconnection commented Jun 22, 2023

Hi,
its a bit trickey, as System.Net.WebUtility has HtmlEncode but does not have HtmlAttributeEncode
it uses unsafe call of System.Web.Util.HttpEncoder.HtmlAttributeEncodeInternal
with following code

         internal static string HtmlAttributeEncode(string value)
        {
            if (string.IsNullOrEmpty(value) || HttpEncoder.IndexOfHtmlAttributeEncodingChars(value, 0) == -1)
                return value;
            StringWriter output = new StringWriter((IFormatProvider)CultureInfo.InvariantCulture);
            HttpEncoder.HtmlAttributeEncode(value, (TextWriter)output);
            return output.ToString();
        }

        internal static void HtmlAttributeEncode(string value, TextWriter output)
        {
            if (value == null)
                return;
            if (output == null)
                throw new ArgumentNullException(nameof(output));
            HttpEncoder.HtmlAttributeEncodeInternal(value, output);
        }

        private static unsafe void HtmlAttributeEncodeInternal(string s, TextWriter output)
        {
            int num1 = HttpEncoder.IndexOfHtmlAttributeEncodingChars(s, 0);
            if (num1 == -1)
            {
                output.Write(s);
            }
            else
            {
                int num2 = s.Length - num1;
                IntPtr num3;
                if (s == null)
                {
                    num3 = IntPtr.Zero;
                }
                else
                {
                    fixed (char* chPtr = &s.GetPinnableReference())
                        num3 = (IntPtr)chPtr;
                }
                char* chPtr1 = (char*)num3;
                while (num1-- > 0)
                    output.Write(*chPtr1++);
                while (num2-- > 0)
                {
                    char ch = *chPtr1++;
                    if (ch <= '<')
                    {
                        if (ch <= '&')
                        {
                            if (ch != '"')
                            {
                                if (ch == '&')
                                {
                                    output.Write("&amp;");
                                    continue;
                                }
                            }
                            else
                            {
                                output.Write("&quot;");
                                continue;
                            }
                        }
                        else if (ch != '\'')
                        {
                            if (ch == '<')
                            {
                                output.Write("&lt;");
                                continue;
                            }
                        }
                        else
                        {
                            output.Write("&#39;");
                            continue;
                        }
                        output.Write(ch);
                    }
                    else
                        output.Write(ch);
                }
                // ISSUE: fixed variable is out of scope
                // ISSUE: __unpin statement
                __unpin(chPtr);
            }
        }

I'm surprised they use unsafe in this function. I think we should upldate this code to safe calls and include in RazorEngineCore.

HtmlEncode already uses WebUtility.HtmlEncode

internal static string HtmlEncode(string value) => !string.IsNullOrEmpty(value) ? WebUtility.HtmlEncode(value) : value;

        internal static void HtmlEncode(string value, TextWriter output)
        {
            if (output == null)
                throw new ArgumentNullException(nameof(output));
            output.Write(WebUtility.HtmlEncode(value));
        }

@adoconnection adoconnection changed the title System.Net.WebUtility.HtmlEncode Raw example Get rid of System.Web dependency Jun 22, 2023
@kinguru
Copy link
Author

kinguru commented Jun 23, 2023

Thank you, currently I'd used your library without Raw, so assuming all are raw. But looking forward for updates.

@adoconnection adoconnection added the enhancement New feature or request label Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants