Skip to content

Commit

Permalink
add samples to the readme; rename HTMLTag.Name to Type; make HTMLDocu…
Browse files Browse the repository at this point in the history
…ment point at the first actual node not #document; add HTMLTagTypeFilter
  • Loading branch information
Clinton Sheppard committed Jul 16, 2010
1 parent 16b3c3e commit 6fbff4b
Show file tree
Hide file tree
Showing 16 changed files with 538 additions and 150 deletions.
40 changes: 38 additions & 2 deletions README.md
@@ -1,10 +1,46 @@
A simple utility that lets you use LINQ syntax to query HTML instead of XSLT. We use it in unit tests to verify that generated HTML correctly represents the generation inputs. A simple utility that lets you use LINQ syntax to query HTML.


## Samples ## Samples


const string html = "<html><head><TITLE>The Title</TITLE></head><body>Hello World</body></html>";
var parsed = HTMLParser.Parse(html);

parsed
.DescendantTags
.OfType("title").IgnoreCase()
.Any()
.ShouldBeTrue();


parsed
.DescendantTags
.OfType("title")
.Any()
.ShouldBeFalse();


## License parsed
.ChildTags
.Count()
.ShouldBeEqualTo(2); // head, body
parsed
.DescendantTags
.OfType("title").IgnoreCase()
.First()
.Parent
.TypeEqualsIgnoreCase("HEAD")
.ShouldBeTrue();

parsed
.DescendantTags
.Count(x => x.Parent.TypeEqualsIgnoreCase("HTML"))
.ShouldBeEqualTo(2); // head, body
parsed
.Body
.Content
.ShouldBeEqualTo("Hello World");
## License


[MIT License][mitlicense] [MIT License][mitlicense]


Expand Down
20 changes: 10 additions & 10 deletions src/LinqToHtml.Tests/HTMLDocumentTests.cs
Expand Up @@ -10,7 +10,7 @@ namespace LinqToHtml.Tests
public class HTMLDocumentTests public class HTMLDocumentTests
{ {
[TestFixture] [TestFixture]
public class When_asked_for_all_descendant_tags public class When_asked_for_its_descendant_tags
{ {
private int _expectedTagCount; private int _expectedTagCount;
private string _html; private string _html;
Expand All @@ -21,7 +21,7 @@ public void Given_a_basic_html_document()
{ {
Test.Verify( Test.Verify(
with_a_basic_html_document, with_a_basic_html_document,
when_asked_for_all_descendant_tags, when_asked_for_its_descendant_tags,
should_not_return_null, should_not_return_null,
should_not_return_an_empty_list, should_not_return_an_empty_list,
should_return_the_correct_number_of_tags, should_return_the_correct_number_of_tags,
Expand All @@ -43,7 +43,7 @@ private void should_not_return_null()


private void should_return_the_body_tag() private void should_return_the_body_tag()
{ {
_result.Single(x => x.Name == "body"); _result.Single(x => x.Type == "body");
} }


private void should_return_the_correct_number_of_tags() private void should_return_the_correct_number_of_tags()
Expand All @@ -53,24 +53,24 @@ private void should_return_the_correct_number_of_tags()


private void should_return_the_head_tag() private void should_return_the_head_tag()
{ {
_result.Single(x => x.Name == "head"); _result.Single(x => x.Type == "head");
} }


private void should_return_the_title_tag() private void should_return_the_title_tag()
{ {
_result.Single(x => x.Name == "title"); _result.Single(x => x.Type == "title");
} }


private void when_asked_for_all_descendant_tags() private void when_asked_for_its_descendant_tags()
{ {
_result = HTMLParser.Parse(_html) _result = HTMLParser.Parse(_html)
.AllDescendantTags.ToList(); .DescendantTags.ToList();
} }


private void with_a_basic_html_document() private void with_a_basic_html_document()
{ {
_html = "<html><head><title>The Title</title></head><body>Hello World</body></html>"; _html = "<html><head><title>The Title</title></head><body>Hello World</body></html>";
_expectedTagCount = 4; // html, head, title, body _expectedTagCount = 3; // head, title, body
} }
} }


Expand Down Expand Up @@ -109,7 +109,7 @@ private void should_not_return_null()


private void should_return_the_BODY_tag() private void should_return_the_BODY_tag()
{ {
_result.NameEqualsIgnoreCase("BODY").ShouldBeTrue(); _result.TypeEqualsIgnoreCase("BODY").ShouldBeTrue();
} }


private void when_asked_for_the_BODY_tag() private void when_asked_for_the_BODY_tag()
Expand Down Expand Up @@ -163,7 +163,7 @@ private void should_not_return_null()


private void should_return_the_HEAD_tag() private void should_return_the_HEAD_tag()
{ {
_result.NameEqualsIgnoreCase("HEAD").ShouldBeTrue(); _result.TypeEqualsIgnoreCase("HEAD").ShouldBeTrue();
} }


private void when_asked_for_the_HEAD_tag() private void when_asked_for_the_HEAD_tag()
Expand Down
10 changes: 5 additions & 5 deletions src/LinqToHtml.Tests/HTMLParserTests.cs
Expand Up @@ -9,7 +9,7 @@ public class HTMLParserTests
[TestFixture] [TestFixture]
public class When_asked_to_parse_an_HTML_string public class When_asked_to_parse_an_HTML_string
{ {
private string _expectedName; private string _expectedType;
private string _html; private string _html;
private HTMLTag _result; private HTMLTag _result;


Expand All @@ -20,7 +20,7 @@ public void Given_a_basic_html_document()
with_a_basic_html_document, with_a_basic_html_document,
when_asked_to_parse_the_string, when_asked_to_parse_the_string,
should_not_return_null, should_not_return_null,
should_return_an_html_document_with_the_correct_Name should_return_an_html_document_with_the_correct_Type
); );
} }


Expand All @@ -29,9 +29,9 @@ private void should_not_return_null()
_result.ShouldNotBeNull(); _result.ShouldNotBeNull();
} }


private void should_return_an_html_document_with_the_correct_Name() private void should_return_an_html_document_with_the_correct_Type()
{ {
_result.Name.ShouldBeEqualTo(_expectedName); _result.Type.ShouldBeEqualTo(_expectedType);
} }


private void when_asked_to_parse_the_string() private void when_asked_to_parse_the_string()
Expand All @@ -42,7 +42,7 @@ private void when_asked_to_parse_the_string()
private void with_a_basic_html_document() private void with_a_basic_html_document()
{ {
_html = "<html><head><title>The Title</title></head><body>Hello World</body></html>"; _html = "<html><head><title>The Title</title></head><body>Hello World</body></html>";
_expectedName = HTMLDocument.DefaultName; _expectedType = "html";
} }
} }
} }
Expand Down
1 change: 0 additions & 1 deletion src/LinqToHtml.Tests/HTMLTagAttributeExtensionsTests.cs
Expand Up @@ -58,7 +58,6 @@ private void should_return_true()
private void when_asked_if_its_name_equals_ignore_case() private void when_asked_if_its_name_equals_ignore_case()
{ {
_result = HTMLParser.Parse("<body " + _attributeName + "='foo' />") _result = HTMLParser.Parse("<body " + _attributeName + "='foo' />")
.ChildTags.First()
.Attributes.First().NameEqualsIgnoreCase(_name); .Attributes.First().NameEqualsIgnoreCase(_name);
} }


Expand Down
55 changes: 26 additions & 29 deletions src/LinqToHtml.Tests/HTMLTagExtensionsTests.cs
@@ -1,5 +1,3 @@
using System.Linq;

using FluentAssert; using FluentAssert;


using NUnit.Framework; using NUnit.Framework;
Expand All @@ -9,39 +7,39 @@ namespace LinqToHtml.Tests
public class HTMLTagExtensionsTests public class HTMLTagExtensionsTests
{ {
[TestFixture] [TestFixture]
public class When_asked_if_its_name_equals_ignore_case public class When_asked_if_its_type_equals_ignore_case
{ {
private string _name;
private bool _result; private bool _result;
private string _tagName; private string _tagType;
private string _type;


[Test] [Test]
public void Given_the_name_has_different_case() public void Given_the_type_has_different_case()
{ {
Test.Verify( Test.Verify(
with_a_name_that_differs_only_by_case, with_a_type_that_differs_only_by_case,
when_asked_if_its_name_equals_ignore_case, when_asked_if_its_type_equals_ignore_case,
should_return_true should_return_true
); );
} }


[Test] [Test]
public void Given_the_name_matches_exactly() public void Given_the_type_is_completely_different()
{ {
Test.Verify( Test.Verify(
with_a_name_that_matches_exactly, with_a_type_that_is_completely_different,
when_asked_if_its_name_equals_ignore_case, when_asked_if_its_type_equals_ignore_case,
should_return_true should_return_false
); );
} }


[Test] [Test]
public void Given_the_name_that_is_completely_different() public void Given_the_type_matches_exactly()
{ {
Test.Verify( Test.Verify(
with_a_name_that_is_completely_different, with_a_type_that_matches_exactly,
when_asked_if_its_name_equals_ignore_case, when_asked_if_its_type_equals_ignore_case,
should_return_false should_return_true
); );
} }


Expand All @@ -55,29 +53,28 @@ private void should_return_true()
_result.ShouldBeTrue(); _result.ShouldBeTrue();
} }


private void when_asked_if_its_name_equals_ignore_case() private void when_asked_if_its_type_equals_ignore_case()
{ {
_result = HTMLParser.Parse("<" + _tagName + " />") _result = HTMLParser.Parse("<" + _tagType + " />")
.ChildTags.First() .TypeEqualsIgnoreCase(_type);
.NameEqualsIgnoreCase(_name);
} }


private void with_a_name_that_differs_only_by_case() private void with_a_type_that_differs_only_by_case()
{ {
_name = "HeAd"; _type = "HeAd";
_tagName = "head"; _tagType = "head";
} }


private void with_a_name_that_is_completely_different() private void with_a_type_that_is_completely_different()
{ {
_name = "HeAd"; _type = "HeAd";
_tagName = "tail"; _tagType = "tail";
} }


private void with_a_name_that_matches_exactly() private void with_a_type_that_matches_exactly()
{ {
_name = "HeAd"; _type = "HeAd";
_tagName = _name; _tagType = _type;
} }
} }
} }
Expand Down

0 comments on commit 6fbff4b

Please sign in to comment.