Exemple #1
0
        /// <summary>
        /// Returns the content of specified tag.
        /// </summary>
        /// <param name="pageHtml">html code</param>
        /// <param name="justTagName">The tag name like TABLE</param>
        public static string GetTagContent(ref string pageHtml, string justTagName)
        {
            string startTag = '<' + justTagName;
            string endTag = justTagName + '>';
            int    start, end;

            start = StringCompare.IndexOfIgnoreCase(ref pageHtml, startTag);
            if (start == -1)
            {
                return("");
            }
            start = StringCompare.IndexOfMatchCase(ref pageHtml, '>', start);
            if (start == -1)
            {
                return("");
            }
            start++;

            end = StringCompare.IndexOfIgnoreCase(ref pageHtml, endTag, start);
            if (end == -1)
            {
                return("");
            }
            end = StringCompare.LastIndexOfMatchCase(ref pageHtml, '<', end);
            if (end == -1 || start > end)
            {
                return("");
            }

            return(pageHtml.Substring(start, end - start).Trim());
        }
Exemple #2
0
        public static TextRange FindCSSClassStyleUrlValuePosition(ref string pageHtml, int startindex)
        {
            int          valueStart, valueEnd;
            const string strCSSUrlValue = "url(";

            TextRange result;            // = new TextRange(-1, -1);

            result.End   = -1;
            result.Start = -1;


            //==============================
            if (startindex >= pageHtml.Length)
            {
                return(result);
            }


            // Find first position
            valueStart = StringCompare.IndexOfIgnoreCase(ref pageHtml, strCSSUrlValue, startindex);
            if (valueStart == -1)
            {
                return(result);
            }

            valueStart += strCSSUrlValue.Length;
            valueEnd    = StringCompare.IndexOfMatchCase(ref pageHtml, ")", valueStart);

            if (valueEnd == -1)
            {
                return(result);
            }

            if (valueEnd > StringCompare.IndexOfMatchCase(ref pageHtml, ";", valueStart))
            {
                return(result);
            }

            result.Start = valueStart;
            result.End   = valueEnd;
            return(result);
        }
Exemple #3
0
        /// <summary>
        /// Removes specified tag
        /// </summary>
        /// <param name="pageHtml"></param>
        /// <param name="tagName"></param>
        public static void RemoveTagOnly(ref string pageHtml, string tagName)
        {
            string tagEnd      = "</" + tagName + ">";
            string tagStart    = "<" + tagName;
            string tagStartEnd = ">";

            int tagStartPos    = 0;
            int tagStartEndPos = 0;
            int tagEndPos      = 0;

            do
            {
                tagStartPos = StringCompare.IndexOfIgnoreCase(ref pageHtml, tagStart, tagStartPos);
                if (tagStartPos == -1)
                {
                    return;
                }
                tagStartEndPos = StringCompare.IndexOfMatchCase(ref pageHtml, tagStartEnd, tagStartPos);

                if (tagStartEndPos == -1)
                {
                    return;
                }
                tagStartEndPos += tagStartEnd.Length;

                // removes tag start
                pageHtml = pageHtml.Remove(tagStartPos, tagStartEndPos - tagStartPos);


                // find tag end pos
                tagEndPos = StringCompare.IndexOfMatchCase(ref pageHtml, tagEnd, tagStartPos);

                if (tagEndPos == -1)
                {
                    return;
                }

                pageHtml = pageHtml.Remove(tagEndPos, tagEnd.Length);
            } while (tagEndPos != -1);
        }
Exemple #4
0
        /// <summary>
        /// Locates @import rule value
        /// </summary>
        /// <param name="cssCodes"></param>
        /// <param name="startindex"></param>
        /// <param name="onlyWithoutUrlOption">Specifies that operator should not catch rule with URL attribute.</param>
        /// <param name="captureExactInUrl">Specifies that operator should strip the URL attribute if there is any</param>
        /// <returns></returns>
        public static TextRange FindImportRuleUrlPosition(ref string cssCodes, int startindex, bool onlyWithoutUrlOption, bool captureExactlyInUrl)
        {
            int          valueStart, valueEnd;
            TextRange    result           = new TextRange(-1, -1);
            const string strCSSImportRule = "@import";
            const string strCSSUrlValue   = "url(";

            //==============================
            if (startindex >= cssCodes.Length)
            {
                return(result);
            }


            // Find first position
            valueStart = StringCompare.IndexOfIgnoreCase(ref cssCodes, strCSSImportRule, startindex);
            if (valueStart == -1)
            {
                return(result);
            }

            valueStart += strCSSImportRule.Length;

            //
            if (cssCodes.Substring(valueStart, 1).Trim().Length > 0)
            {
                return(result);
            }
            else
            {
                valueStart++;
            }

            valueEnd = StringCompare.IndexOfMatchCase(ref cssCodes, ";", valueStart);

            if (valueEnd == -1)
            {
                return(result);
            }
            else
            {
                int urlPos = StringCompare.IndexOfIgnoreCase(ref cssCodes, strCSSUrlValue, valueStart);
                if (urlPos != -1 && urlPos < valueEnd)
                {
                    if (onlyWithoutUrlOption)
                    {
                        result.Start = valueEnd;
                        result.End   = -1;
                        return(result);
                    }

                    if (captureExactlyInUrl)
                    {
                        valueStart = urlPos + strCSSUrlValue.Length;

                        urlPos = StringCompare.IndexOfMatchCase(ref cssCodes, ")", valueStart);
                        if (urlPos != -1 && urlPos < valueEnd)
                        {
                            valueEnd = urlPos;
                        }
                    }
                }
            }

            result.Start = valueStart;
            result.End   = valueEnd;
            return(result);
        }
Exemple #5
0
        public static void ReplaceFormsSources(ref string htmlCodes,
                                               string pageUrlNoQuery,
                                               string newPageFormat,
                                               string pagePath,
                                               string siteRootUrl,
                                               bool encodeUrl,
                                               bool changeMethod,
                                               string extraAttributeFormat)
        {
            TextRange methodResult;            // = new TextRange();
            TextRange actionResult;            // = new TextRange();
            int       cursorPos = 0;
            string    newAttribute = "";
            string    formMethod = "";
            string    tmp, actionSrc = "";
            string    orgValue = "";

            bool addNewAttribute = false;
            bool hasNewAttribute = false;

            if (!string.IsNullOrEmpty(extraAttributeFormat))
            {
                addNewAttribute = true;
                hasNewAttribute = true;
            }


            do
            {
                addNewAttribute = hasNewAttribute;

                if (changeMethod)
                {
                    methodResult = HtmlParser.GetTagAttributeValuePos(ref htmlCodes, "<form", "method", cursorPos);
                    if (methodResult.Start > -1 && methodResult.End > -1)
                    {
                        // get the method
                        formMethod = htmlCodes.Substring(methodResult.Start, 2);

                        // validate the method
                        formMethod = WebMethods.DetectMethod(formMethod, WebMethods.DefaultMethods.GET);

                        htmlCodes = htmlCodes.Remove(methodResult.Start, methodResult.End - methodResult.Start);
                        htmlCodes = htmlCodes.Insert(methodResult.Start, "POST");
                    }
                    else
                    {
                        int formPos = StringCompare.IndexOfIgnoreCase(ref htmlCodes, "<form", cursorPos);
                        int tagEnd;
                        if (formPos != -1)
                        {
                            tagEnd = StringCompare.IndexOfMatchCase(ref htmlCodes, '>', formPos);
                            if (tagEnd != -1)
                            {
                                htmlCodes = htmlCodes.Insert(tagEnd, " method=POST ");
                            }
                        }

                        formMethod = WebMethods.GET;
                    }
                }

                actionResult = HtmlParser.GetTagAttributeValuePos(ref htmlCodes, "<form", "action", cursorPos);

                if (actionResult.Start == -1)
                {
                    break;
                }

                if (actionResult.Start > -1 && actionResult.End > -1)
                {
                    cursorPos = actionResult.Start;


                    //====== Correct value position according to quotes existence=======
                    // actionResult = ASProxyFunctions.CorrectValueIfQuoteExists(ref pageHtml, actionResult);

                    // Get the value
                    actionSrc = htmlCodes.Substring(actionResult.Start, actionResult.End - actionResult.Start);

                    // BUG fixed in v5 beta 2
                    // now supports forms with javascript
                    if (UrlProvider.IsClientSitdeUrl(actionSrc) == false)
                    {
                        //====== Convert virtual url to absolute ======
                        actionSrc = UrlProvider.JoinUrl(actionSrc, pageUrlNoQuery, pagePath, siteRootUrl);

                        //====== Delete invalid character such as tab and line feed ======
                        actionSrc = UrlProvider.IgnoreInvalidUrlCharctersInHtml(actionSrc);

                        orgValue = actionSrc;

                        //===== If another site url, has bookmark=====
                        if (actionSrc.IndexOf('#') != -1)
                        {
                            actionSrc = UrlBuilder.RemoveUrlBookmark(actionSrc, out tmp);
                        }

                        //=====Get desired address=======
                        actionSrc = HttpUtility.HtmlDecode(actionSrc);

                        //====== Encode url to make unknown it ======
                        if (encodeUrl)
                        {
                            actionSrc = UrlProvider.EncodeUrl(actionSrc);
                        }
                        else
                        {
                            // just url safe
                            actionSrc = UrlProvider.EscapeUrlQuery(actionSrc);
                        }

                        //====== Add it to our url ======
                        actionSrc = string.Format(newPageFormat, actionSrc);

                        if (changeMethod)
                        {
                            //actionSrc = UrlBuilder.AddUrlQuery(actionSrc, Consts.qIsPostForm, ((int)method).ToString());
                            actionSrc = UrlBuilder.AddUrlQueryToEnd(actionSrc, Consts.Query.WebMethod, formMethod);
                        }


                        // Make it html safe
                        actionSrc = HttpUtility.HtmlEncode(actionSrc);

                        //====== Replace it with old url ======
                        htmlCodes = htmlCodes.Remove(actionResult.Start, actionResult.End - actionResult.Start);
                        htmlCodes = htmlCodes.Insert(actionResult.Start, actionSrc);
                    }
                    else
                    {
                        // this is client side url
                        addNewAttribute = false;
                    }


                    if (addNewAttribute)
                    {
                        // Apply orginal value and encoded value to format
                        newAttribute = string.Format(extraAttributeFormat, orgValue, actionSrc, "POST");

                        // Locate end of tag
                        cursorPos = StringCompare.IndexOfMatchCase(ref htmlCodes, '>', actionResult.Start);
                        if (htmlCodes[cursorPos - 1] == '/')
                        {
                            cursorPos--;
                        }

                        // Insert to it
                        htmlCodes = htmlCodes.Insert(cursorPos, newAttribute);
                    }
                }
                else
                {
                    if (actionResult.Start != -1)
                    {
                        cursorPos = actionResult.Start;
                    }
                    cursorPos = StringCompare.IndexOfMatchCase(ref htmlCodes, ">", cursorPos);
                }
            }while (actionResult.Start != -1);
        }
Exemple #6
0
        /// <summary>
        /// Locate and remove a tag from html codes
        /// </summary>
        /// <param name="pageHtml">Html codes</param>
        /// <param name="tagName">Name of tags</param>
        /// <param name="removeTag">Remove the tag also</param>
        public static void RemoveTagContent(ref string pageHtml, string tagName, bool removeTag)
        {
            string tagEnd      = "</" + tagName + ">";
            string tagStart    = "<" + tagName;
            string tagStartEnd = ">";

            int tagStartPos    = 0;
            int tagStartEndPos = 0;
            int tagEndPos      = 0;

            if (removeTag)
            {
                do
                {
                    tagStartPos = StringCompare.IndexOfIgnoreCase(ref pageHtml, tagStart, tagStartPos);
                    if (tagStartPos == -1)
                    {
                        return;
                    }
                    tagStartEndPos = StringCompare.IndexOfMatchCase(ref pageHtml, tagStartEnd, tagStartPos);

                    if (tagStartEndPos == -1)
                    {
                        return;
                    }
                    tagStartEndPos += tagStartEnd.Length;

                    tagEndPos = StringCompare.IndexOfMatchCase(ref pageHtml, tagEnd, tagStartEndPos);

                    if (tagEndPos == -1)
                    {
                        return;
                    }
                    tagEndPos += tagEnd.Length;

                    pageHtml = pageHtml.Remove(tagStartPos, tagEndPos - tagStartPos);
                } while (tagEndPos != -1);
            }
            else
            {
                do
                {
                    tagStartPos = StringCompare.IndexOfIgnoreCase(ref pageHtml, tagStart, tagStartPos);
                    if (tagStartPos == -1)
                    {
                        return;
                    }
                    tagStartEndPos = StringCompare.IndexOfMatchCase(ref pageHtml, tagStartEnd, tagStartPos);

                    if (tagStartEndPos == -1)
                    {
                        return;
                    }
                    tagStartEndPos += tagStartEnd.Length;

                    tagEndPos = StringCompare.IndexOfMatchCase(ref pageHtml, tagEnd, tagStartEndPos);

                    if (tagEndPos == -1)
                    {
                        return;
                    }

                    pageHtml = pageHtml.Remove(tagStartEndPos, tagEndPos - tagStartEndPos);
                } while (tagEndPos != -1);
            }
        }
Exemple #7
0
        /// <summary>
        /// Locate value of attribute position for specified html tag
        /// </summary>
        public static TextRange GetTagAttributeValuePos(ref string pageHtml, string tagName, string attributeName, int start)
        {
            int tagStart, tagEnd;
            int attrStart;
            int valueStart;

            // Allocate result with default values
            TextRange result = new TextRange(-1, -1);

            // Incorrect input
            if (start >= pageHtml.Length)
            {
                return(result);
            }


            // Correct tag name
            if (tagName[0] != '<')
            {
                tagName = '<' + tagName;
            }

            //=============================================================//

            // Find tag first position
            tagStart = StringCompare.IndexOfIgnoreCase(ref pageHtml, tagName, start);
            if (tagStart == -1)
            {
                return(result);
            }

            // Locate end of tag
            tagEnd = StringCompare.IndexOfMatchCase(ref pageHtml, ">", tagStart);

            // Wrong html code
            if (tagEnd == -1)
            {
                return(result);
            }

            // Find the Attribute position
            attrStart = StringCompare.IndexOfIgnoreCase(ref pageHtml, attributeName, tagStart);


            // There is no attribute, so go away!
            if (attrStart == -1 || attrStart >= tagEnd)
            {
                // Set found start position to result
                result.Start = tagStart;
                result.End   = -1;
                return(result);
            }

            // Find value start position
            valueStart = StringCompare.IndexOfMatchCase(ref pageHtml, '=', attrStart);

            // There is no value, so go away!
            if (valueStart == -1 || valueStart >= tagEnd)
            {
                // Set found start position to result
                result.Start = attrStart;
                result.End   = -1;
                return(result);
            }

            // Locate value of attribute position
            result = FindAttributeValuePosition(ref pageHtml, tagEnd, valueStart);

            return(result);
        }
Exemple #8
0
        /// <summary>
        /// Finds and override url addresses within "style" tag.
        /// </summary>
        public static void ReplaceStyleTagStyleUrl(
            ref string htmlCode,
            string currentUrlWithoutParameters,
            string importRuleUrl,
            string othersNewUrl,
            string replacmentBasePath,
            string siteAddress,
            bool encodeUrl)
        {
            const string styleStart = "<style";
            const string styleEnd   = "</style>";
            const string htmlTagEnd = ">";
            TextRange    position;
            int          index = 0;

            //string bookmarkPart = "";

            do
            {
                string styleContent = "";

                // Find style tag position start
                position.Start = StringCompare.IndexOfIgnoreCase(ref htmlCode, styleStart, index);
                if (position.Start == -1)
                {
                    break;
                }

                // locate tag end
                position.Start = StringCompare.IndexOfMatchCase(ref htmlCode, htmlTagEnd, position.Start);
                if (position.Start == -1)
                {
                    break;
                }
                position.Start++;

                // Locate end tag
                position.End = StringCompare.IndexOfIgnoreCase(ref htmlCode, styleEnd, position.Start);
                if (position.End == -1)
                {
                    break;
                }

                // Set next searching start position
                index = position.End;

                // Get the content
                styleContent = htmlCode.Substring(position.Start, position.End - position.Start);

                // If there is nothing, go to next tag
                if (styleContent.Trim().Length == 0)
                {
                    continue;
                }

                // Process the style content for "Import Rule"
                // We don't need "Import Rule" process with "url" property any more. It will done by bext command. Since v4.0
                ReplaceCSSClassStyleUrl(ref styleContent, currentUrlWithoutParameters, importRuleUrl, replacmentBasePath, siteAddress, encodeUrl, true);

                // Process the style content for backgrounds.
                // So, this breach the backgound option role. Since v4.0
                ReplaceCSSClassStyleUrl(ref styleContent, currentUrlWithoutParameters, othersNewUrl, replacmentBasePath, siteAddress, encodeUrl, false);


                // Replace the new style
                htmlCode = htmlCode.Remove(position.Start, position.End - position.Start);
                htmlCode = htmlCode.Insert(position.Start, styleContent);
            } while (index != -1);
        }
Exemple #9
0
        /// <summary>
        /// Finds and encode codes within "script" tag.
        /// </summary>
        public static void ReplaceScriptTagCodes(ref string htmlCode)
        {
            const string scriptStart = "<script";
            const string scriptEnd   = "</script>";
            const string htmlTagEnd  = ">";
            TextRange    position;
            int          index = 0;

            do
            {
                string scriptContent = "";

                // Find script tag position start
                position.Start = StringCompare.IndexOfIgnoreCase(ref htmlCode, scriptStart, index);
                if (position.Start == -1)
                {
                    break;
                }

                // locate tag end
                position.Start = StringCompare.IndexOfMatchCase(ref htmlCode, htmlTagEnd, position.Start);
                if (position.Start == -1)
                {
                    break;
                }
                position.Start++;

                // Locate end tag
                position.End = StringCompare.IndexOfIgnoreCase(ref htmlCode, scriptEnd, position.Start);
                if (position.End == -1)
                {
                    break;
                }

                // Set next searching start position
                index = position.End;

                // Get the content
                scriptContent = htmlCode.Substring(position.Start, position.End - position.Start);

                // If there is nothing, go to next tag
                if (scriptContent.Trim().Length == 0)
                {
                    continue;
                }


                // Very bad use of processor, But there is no other way to do!
                // Create a new instance of processor
                JSProcessor processor = new JSProcessor();

                // Process the script content
                processor.Execute(ref scriptContent,
                                  null, null, null, null);


                // Replace the new style
                htmlCode = htmlCode.Remove(position.Start, position.End - position.Start);
                htmlCode = htmlCode.Insert(position.Start, scriptContent);
            } while (index != -1);
        }