Skip to content

Commit

Permalink
Merge pull request #35 from kordys/master
Browse files Browse the repository at this point in the history
Replaces Regex usages with optimized syntax
  • Loading branch information
manhhungtran authored Dec 19, 2018
2 parents 22adb47 + 7f5beac commit de07327
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
24 changes: 11 additions & 13 deletions Kentico.AcceleratedMobilePages/AmpFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private string AppendAmpHtmlLink(string finalHtml)
GetDocumentPath();
string metaTag = String.Format(Constants.AMP_AMP_HTML_LINK, ampLink) + Constants.NEW_LINE;
// Insert meta tag
finalHtml = Regex.Replace(finalHtml, "</head>", metaTag + "</head>");
finalHtml = finalHtml.Replace("</head>", metaTag + "</head>");

return finalHtml;
}
Expand Down Expand Up @@ -108,21 +108,20 @@ private void RemoveRestrictedElements(HtmlDocument doc)
/// <param name="finalHtml">Final HTML string</param>
private string PerformRegexCorrections(string finalHtml)
{
// Initial HTML amp tag - replace only first occurrence
var regex = new Regex(Constants.HTML_TAG);
finalHtml = regex.Replace(finalHtml, Constants.HTML_REPLACEMENT, 1);
// Initial HTML amp tag
finalHtml = finalHtml.Replace(Constants.HTML_TAG, Constants.HTML_REPLACEMENT);

// Remove conditional comments
finalHtml = Regex.Replace(finalHtml, Constants.REGEX_CONDITIONAL_COMMENTS, "");
finalHtml = new Regex(Constants.REGEX_CONDITIONAL_COMMENTS).Replace(finalHtml, "");

// Remove restricted attributes
finalHtml = Regex.Replace(finalHtml, Constants.REGEX_ATTR_ONANY_SUFFIX, "");
finalHtml = Regex.Replace(finalHtml, Constants.REGEX_ATTR_XML_ATTRIBUTES, "");
finalHtml = Regex.Replace(finalHtml, Constants.REGEX_ATTR_IAMPANY_SUFFIX, "");
finalHtml = new Regex(Constants.REGEX_ATTR_ONANY_SUFFIX).Replace(finalHtml, "");
finalHtml = new Regex(Constants.REGEX_ATTR_XML_ATTRIBUTES).Replace(finalHtml, "");
finalHtml = new Regex(Constants.REGEX_ATTR_IAMPANY_SUFFIX).Replace(finalHtml, "");

// Remove restricted attribute's values
finalHtml = Regex.Replace(finalHtml, Constants.REGEX_NAME_CLASS, "");
finalHtml = Regex.Replace(finalHtml, Constants.REGEX_NAME_ID, "");
finalHtml = new Regex(Constants.REGEX_NAME_CLASS).Replace(finalHtml, "");
finalHtml = new Regex(Constants.REGEX_NAME_ID).Replace(finalHtml, "");

return finalHtml;
}
Expand Down Expand Up @@ -226,7 +225,7 @@ private string InsertCompulsoryMarkupAndCss(string finalHtml)
{
// Save the original <head> tag before replacement later
String headTag = "";
Match m = Regex.Match(finalHtml, Constants.REGEX_HEAD);
Match m = Constants.HeadRegex.Match(finalHtml);
if (m.Success)
{
headTag = m.Value;
Expand All @@ -253,8 +252,7 @@ private string InsertCompulsoryMarkupAndCss(string finalHtml)
Constants.AMP_BOILERPLATE_CODE + Constants.NEW_LINE +
String.Format(Constants.AMP_CUSTOM_STYLE, styles) + Constants.NEW_LINE;

finalHtml = Regex.Replace(finalHtml, Constants.REGEX_HEAD, headTag);
return finalHtml;
return Constants.HeadRegex.Replace(finalHtml, headTag);
}


Expand Down
7 changes: 5 additions & 2 deletions Kentico.AcceleratedMobilePages/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Kentico.AcceleratedMobilePages
using System.Text.RegularExpressions;

namespace Kentico.AcceleratedMobilePages
{
public class Constants
{
Expand Down Expand Up @@ -34,7 +36,8 @@ public class Constants
public const string HTML_REPLACEMENT = "<html amp";

// Head tag - required for finding the place where all the compulsory head markup will be inserted
public const string REGEX_HEAD = "(?i)<[\\s]*?head(>|[\\s][\\s\\S]*?>)";
private const string REGEX_HEAD_PATTERN = "(?i)<[\\s]*?head(>|[\\s][\\s\\S]*?>)";
public static readonly Regex HeadRegex = new Regex(REGEX_HEAD_PATTERN);

// Internet Explorer conditional comments
public const string REGEX_CONDITIONAL_COMMENTS = "<!(|--)\\[[^\\]]+\\]>[\\s\\S]*?<!\\[endif\\](|--)>";
Expand Down

0 comments on commit de07327

Please sign in to comment.