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

Added Attribute Regex Replace #157

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions source/CsQuery.Tests/CQ_CsQuery/AttrReplace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NUnit.Framework;
using Assert = NUnit.Framework.Assert;
using CsQuery;


namespace CsQuery.Tests.CQ_CsQuery
{
[TestClass,TestFixture]
public class AttrReplace : CsQueryTest
{

[TestMethod, Test]
public void Substring()
{
var expected = CQ.Create("<p class=\"two\"></p>");
var cq = CQ.Create("<p class=\"one\"></p>");

Assert.AreEqual(expected, cq.AttrReplace("class", "one", "two"));
}

[TestMethod, Test]
public void Regex() {
var expected = CQ.Create("<p style=\"\"></p>");
var cq = CQ.Create("<p style=\"font-size: 10px;\"></p>");

Assert.AreEqual(expected, cq.AttrRegexReplace("style", @"font-size:\s+\d+px;?", ""));
}

[TestMethod, Test]
public void Regex_Evaluated() {
var expected = CQ.Create("<p style=\"font-size: 20px;\"></p>");
var cq = CQ.Create("<p style=\"font-size: 10px;\"></p>");

Assert.AreEqual(expected, cq.AttrRegexReplace("style", @"font-size:\s+(\d+)px;?", (m) => {
return m.Captures[0].Value.Replace(m.Groups[1].Value, "20");
}));
}
}
}
1 change: 1 addition & 0 deletions source/CsQuery.Tests/Csquery.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@
<Compile Include="Core\Methods\Data.cs" />
<Compile Include="Core\Selectors\ContentIsNotNumeric.cs" />
<Compile Include="Core\WebIO\BasicWeb.cs" />
<Compile Include="CQ_CsQuery\AttrReplace.cs" />
<Compile Include="ExtensionMethods\Xml.cs" />
<Compile Include="HtmlParser\Button.cs" />
<Compile Include="HtmlParser\CharacterSetEncoding.cs" />
Expand Down
59 changes: 59 additions & 0 deletions source/CsQuery/CQ_CsQuery/AttrReplace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using CsQuery.Utility;
using CsQuery.ExtensionMethods;
using CsQuery.ExtensionMethods.Internal;
Expand Down Expand Up @@ -44,5 +45,63 @@ public CQ AttrReplace(string name, string replaceWhat, string replaceWith)
}
return this;
}

/// <summary>
/// Perform a regex replace on the contents of the named attribute in each item in the
/// selection set.
/// </summary>
///
/// <param name="name">
/// The attribute name.
/// </param>
/// <param name="pattern">
/// The regex pattern.
/// </param>
/// <param name="replaceWith">
/// The value to replace each occurrence with.
/// </param>
///
/// <returns>
/// The current CQ object.
/// </returns>

public CQ AttrRegexReplace(string name, string pattern, string replaceWith) {
foreach (IDomElement item in SelectionSet) {
string val = item[name];
if (val != null) {
item[name] = Regex.Replace(val, pattern, replaceWith);
}
}
return this;
}

/// <summary>
/// Perform a regex replace on the contents of the named attribute in each item in the
/// selection set.
/// </summary>
///
/// <param name="name">
/// The attribute name.
/// </param>
/// <param name="pattern">
/// The regex pattern.
/// </param>
/// <param name="matchEvaluator">
/// The evaluator used to replace the pattern
/// </param>
///
/// <returns>
/// The current CQ object.
/// </returns>

public CQ AttrRegexReplace(string name, string pattern, MatchEvaluator matchEvaluator) {
foreach (IDomElement item in SelectionSet) {
string val = item[name];
if (val != null) {
item[name] = Regex.Replace(val, pattern, matchEvaluator);
}
}
return this;
}
}
}