Skip to content

Commit

Permalink
Add initial graph implementation
Browse files Browse the repository at this point in the history
Contributes to #68
  • Loading branch information
baynezy committed Sep 30, 2017
1 parent a6d90a7 commit 2038024
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Html2Markdown/ContentNodeList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using AngleSharp.Dom;

namespace Html2Markdown
{
public class ContentNodeList
{
private INodeList _childNodes;

public ContentNodeList(INodeList childNodes)
{
this._childNodes = childNodes;
}

public int Size()
{
return _childNodes.Length;
}
}
}
25 changes: 25 additions & 0 deletions src/Html2Markdown/HtmlGraph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using AngleSharp.Dom.Html;
using AngleSharp.Parser.Html;

namespace Html2Markdown {
public class HtmlGraph : IContentGraph
{
private IHtmlDocument _document;

public HtmlGraph(string html)
{
var parser = new HtmlParser();
this._document = parser.Parse(html);

if (HasChildren()) {
ChildNodes = new ContentNodeList(_document.Body.ChildNodes);
}
}

public ContentNodeList ChildNodes { get; private set; }
public bool HasChildren()
{
return _document.Body.HasChildNodes;
}
}
}
8 changes: 8 additions & 0 deletions src/Html2Markdown/IContentGraph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Html2Markdown {
public interface IContentGraph
{
ContentNodeList ChildNodes { get; }

bool HasChildren();
}
}
53 changes: 53 additions & 0 deletions test/Html2Markdown.Test/HtmlContentGraphTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using NUnit.Framework;

namespace Html2Markdown {

[TestFixture]
public class HtmlContentGraphTest
{
[Test]
public void HtmlContentGraph_ImplementsIContentGraph() {
var graph = CreateGraph("");

Assert.That(graph, Is.InstanceOf<IContentGraph>());
}

[Test]
public void HasChildren_WhenHtmlLoadedThatHasChildren_ThenShouldReturnTrue() {
const string html = @"<p>some content</p>";
var graph = CreateGraph(html);

Assert.That(graph.HasChildren(), Is.True);
}

[Test]
public void ChildNodes_WhenHtmlLoadedHasChildren_ThenShouldReturnNodes() {
const string html = @"<p>some content</p>";
var graph = CreateGraph(html);

Assert.That(graph.ChildNodes, Is.InstanceOf<ContentNodeList>());
}

[Test]
public void ChildNodes_WhenHtmlLoadedHasOneChild_ThenChildNodesSizeShouldBeOne() {
const string html = @"<p>some content</p>";
var graph = CreateGraph(html);

Assert.That(graph.ChildNodes.Size(), Is.EqualTo(1));
}

[Test]
public void ChildNodes_WhenHtmlLoadedHasTwoChild_ThenChildNodesSizeShouldBeTwo() {
const string html = @"<p>some content</p><p>some more content</p>";
var graph = CreateGraph(html);

Assert.That(graph.ChildNodes.Size(), Is.EqualTo(2));
}

private IContentGraph CreateGraph(string html)
{
return new HtmlGraph(html);
}
}
}

0 comments on commit 2038024

Please sign in to comment.