private static void ReplaceTagSrcAttribute(ref string htmlCodes, string pageUrlNoQuery, string tagName, string attributeName, string pagePath, string newPageFormat, string siteRootUrl, bool encodeUrl, string extraAttributeFormat, bool canEncloseWithTags) { int index = 0; //====== In first run, index must be Zero ====== TextRange position; string oldValue, newValue; string orgValue = ""; string bookmarkPart = ""; string newAttribute = ""; bool addNewAttribute = false; bool hasNewAttribute = false; if (!string.IsNullOrEmpty(extraAttributeFormat)) { addNewAttribute = true; hasNewAttribute = true; } do { addNewAttribute = hasNewAttribute; position = HtmlParser.GetTagAttributeValuePos(ref htmlCodes, tagName, attributeName, index); if (position.Start == -1) { break; } // If requested, test statement that shouldn't enclose with specified tags bool continueLoop = true; // this causes heavy pressure //if (canEncloseWithTags && position.Start != -1 && position.End != -1) // continueLoop = !HtmlTags.IsEnclosedBy(ref htmlCodes, position.Start, "<script", "</script>"); if (continueLoop && position.Start != -1 && position.End != -1 && position.End > position.Start) { //======OK. go to end of tag============= index = StringCompare.IndexOfMatchCase(ref htmlCodes, '>', position.Start); // Replace new address //====== Correct value position according to quotes existence======= //position = ASProxyFunctions.CorrectValueIfQuoteExists(ref pageHtml, position); //====== Get the attribute value ====== oldValue = htmlCodes.Substring(position.Start, position.End - position.Start); oldValue = HtmlTags.RemoveEscapeQuotesFromTagAttributeValue(oldValue); oldValue = HtmlTags.RemoveQuotesFromTagAttributeValue(oldValue); //===== If link is a bookmark don't change it===== if (oldValue.StartsWith("#")) { continue; } if (UrlProvider.IsClientSitdeUrl(oldValue) == false) { //====== Convert virtual url to absolute ====== oldValue = UrlProvider.JoinUrl(oldValue, pageUrlNoQuery, pagePath, siteRootUrl); //====== Delete invalid character such as tab and line feed ====== oldValue = UrlProvider.IgnoreInvalidUrlCharctersInHtml(oldValue); // Save orginal value orgValue = oldValue; //===== If another site url, has bookmark if (oldValue.IndexOf('#') != -1) { oldValue = UrlBuilder.RemoveUrlBookmark(oldValue, out bookmarkPart); } //====== Get desigred url addrress oldValue = HttpUtility.HtmlDecode(oldValue); //====== Encode url to make it unknown ====== if (encodeUrl) { oldValue = UrlProvider.EncodeUrl(oldValue); } else { // just url safe oldValue = UrlProvider.EscapeUrlQuery(oldValue); } //====== Add it to our url ====== newValue = string.Format(newPageFormat, oldValue); //===== Add bookmark to last url ===== if (bookmarkPart.Length > 0) { newValue += bookmarkPart; bookmarkPart = ""; } } else { newValue = oldValue; addNewAttribute = false; } //====== Make it safe newValue = HttpUtility.HtmlEncode(newValue); //====== Replace it with old url htmlCodes = htmlCodes.Remove(position.Start, position.End - position.Start); htmlCodes = htmlCodes.Insert(position.Start, newValue); if (addNewAttribute) { // Apply original value and encoded value to format // BUG: Problem with format string that contain (') or (") characters // Bug Fixed since version 4.7 newAttribute = string.Format(extraAttributeFormat, orgValue, newValue); // Locate end of tag index = StringCompare.IndexOfMatchCase(ref htmlCodes, '>', position.Start); if (htmlCodes[index - 1] == '/') { index--; } // Insert to tag htmlCodes = htmlCodes.Insert(index, newAttribute); } //===============End of Replace new address ========= } else { if (position.Start != -1) { index = position.Start; } index = StringCompare.IndexOfMatchCase(ref htmlCodes, '>', index); } }while ((index != -1)); }
/// <summary> /// Find "BASE" tag and return "HREF" attrribute value, then remove the tag "HREF" attribute. /// </summary> /// <param name="hrefPath">href value</param> /// <returns>return if Base tag found or not</returns> public static bool ReplaceBaseSources(ref string pageHtml, bool removeHREFAttribute, out string hrefPath) { string tagName = "<base"; string attributeName = "href"; TextRange position; string oldValue; hrefPath = ""; // Find position of BASE tag position = HtmlParser.GetTagAttributeValuePos(ref pageHtml, tagName, attributeName, 0); // If any Base tag exists if (position.Start != -1 && position.End != -1) { //====== Get the attribute value ====== oldValue = pageHtml.Substring(position.Start, position.End - position.Start); // Remove unwanted characters oldValue = HtmlTags.RemoveEscapeQuotesFromTagAttributeValue(oldValue); oldValue = HtmlTags.RemoveQuotesFromTagAttributeValue(oldValue); //===== If link is a bookmark don't change it ===== if (oldValue.Length == 0 || oldValue.StartsWith("#")) { return(false); } // Browsers law!! The base url should end with slash(/) if (oldValue[oldValue.Length - 1] != '/') { // If the entered url isn't a directory specified with (/) // try to find the end a base directory int lastI = oldValue.LastIndexOf('/'); if (lastI == -1) { return(false); } lastI++; // character lenght oldValue = oldValue.Substring(0, lastI); } // Set href value hrefPath = oldValue; //====== Replace it with old url ====== if (removeHREFAttribute) { pageHtml = pageHtml.Remove(position.Start, position.End - position.Start); } return(true); } else { return(false); } }
/// <summary> /// Process the styles and replace them /// </summary> public static void ReplaceCSSClassStyleUrl(ref string htmlCode, string currentUrlWithoutParameters, string newUrl, string replacmentBasePath, string siteAddress, bool encodeUrl, bool forImportRule) { int index = 0; TextRange position; string oldValue, newValue; string bookmarkPart = ""; do { if (forImportRule) { // do not find "Import Rule"s with url option, it will done by other codes. Since v4.0 position = CSSParser.FindImportRuleUrlPosition(ref htmlCode, index, false, false); } else { position = CSSParser.FindCSSClassStyleUrlValuePosition(ref htmlCode, index); } if (position.Start == -1) { break; } if (position.Start != -1 && position.End != -1) { bool shouldAddQuote = false; //======OK. go to end of tag============= index = position.End; //========================================================// //====================Replace new address=================// //========================================================// //====== Correct value position according to quotes existence======= position = HtmlTags.CorrectValueIfQuoteExists(ref htmlCode, position); //====== Get the attribute value ====== oldValue = htmlCode.Substring(position.Start, position.End - position.Start); // Trim! oldValue = oldValue.Trim(); // Removes URL attribute if there is any if (HtmlTags.RemoveUrlAttribCssLocation(oldValue, out oldValue)) { shouldAddQuote = true; } oldValue = HtmlTags.RemoveQuotesFromTagAttributeValue(oldValue); //===== If link is a bookmark don't change it===== if (oldValue.StartsWith("#")) { continue; } //====== Convert virtual url to absolute ====== oldValue = UrlProvider.JoinUrl(oldValue, currentUrlWithoutParameters, replacmentBasePath, siteAddress); //====== Delete invalid character such as tab and line feed ====== oldValue = UrlProvider.IgnoreInvalidUrlCharctersInHtml(oldValue); //===== If another site url, has bookmark===== if (StringCompare.IndexOfMatchCase(ref oldValue, '#') != -1) { oldValue = UrlBuilder.RemoveUrlBookmark(oldValue, out bookmarkPart); } //==== Make it clear========= oldValue = HttpUtility.HtmlDecode(oldValue); //====== Encode url to make it unknown ====== if (encodeUrl) { oldValue = UrlProvider.EncodeUrl(oldValue); } else { // just url safe oldValue = UrlProvider.EscapeUrlQuery(oldValue); } //====== Add it to our url ====== newValue = string.Format(newUrl, oldValue); //===== Add bookmark to last url ===== if (bookmarkPart.Length > 0) { newValue += bookmarkPart; bookmarkPart = ""; } // Make it safe //newValue = HttpUtility.HtmlEncode(newValue); if (shouldAddQuote) { newValue = "\"" + newValue + "\""; } //====== Replace it with old url ====== htmlCode = htmlCode.Remove(position.Start, position.End - position.Start); htmlCode = htmlCode.Insert(position.Start, newValue); //========================================================// //==============End of Replace new address================// //========================================================// } else { if (position.Start != -1) { index = position.Start; } index = StringCompare.IndexOfMatchCase(ref htmlCode, ' ', index); } }while ((index != -1)); }