Skip to content

Commit

Permalink
Merge pull request #468 from yufeih/parentblock
Browse files Browse the repository at this point in the history
Add ParentBlock property to ContainerInline
  • Loading branch information
xoofx authored Aug 18, 2020
2 parents 4893e2b + b30b219 commit c59fd5c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
41 changes: 41 additions & 0 deletions src/Markdig.Tests/TestContainerInlines.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using Markdig.Syntax;
using Markdig.Syntax.Inlines;
using NUnit.Framework;

namespace Markdig.Tests
{
public class TestContainerInlines
{
private class MockLeafBlock : LeafBlock
{
public MockLeafBlock()
: base(null)
{

}
}

[Test]
public void CanBeAddedToLeafBlock()
{
var leafBlock1 = new MockLeafBlock();

var one = new ContainerInline();
Assert.Null(one.ParentBlock);

leafBlock1.Inline = one;
Assert.AreSame(leafBlock1, one.ParentBlock);

var two = new ContainerInline();
Assert.Null(two.ParentBlock);

leafBlock1.Inline = two;
Assert.AreSame(leafBlock1, two.ParentBlock);
Assert.Null(one.ParentBlock);

var leafBlock2 = new MockLeafBlock();
Assert.Throws<ArgumentException>(() => leafBlock2.Inline = two);
}
}
}
5 changes: 5 additions & 0 deletions src/Markdig/Syntax/Inlines/ContainerInline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ namespace Markdig.Syntax.Inlines
/// <seealso cref="Inline" />
public class ContainerInline : Inline, IEnumerable<Inline>
{
/// <summary>
/// Gets the parent block of this inline.
/// </summary>
public LeafBlock ParentBlock { get; internal set; }

/// <summary>
/// Gets the first child.
/// </summary>
Expand Down
27 changes: 26 additions & 1 deletion src/Markdig/Syntax/LeafBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace Markdig.Syntax
[DebuggerDisplay("{GetType().Name} Line: {Line}, {Lines}")]
public abstract class LeafBlock : Block
{
private ContainerInline inline;

/// <summary>
/// Initializes a new instance of the <see cref="LeafBlock"/> class.
/// </summary>
Expand All @@ -33,7 +35,30 @@ protected LeafBlock(BlockParser parser) : base(parser)
/// <summary>
/// Gets or sets the inline syntax tree (may be null).
/// </summary>
public ContainerInline Inline { get; set; }
public ContainerInline Inline
{
get => inline;
set
{
if (value != null)
{
if (value.Parent != null)
ThrowHelper.ArgumentException("Cannot add this inline as it as already attached to another container (inline.Parent != null)");

if (value.ParentBlock != null)
ThrowHelper.ArgumentException("Cannot add this inline as it as already attached to another container (inline.ParentBlock != null)");

value.ParentBlock = this;
}

if (inline != null)
{
inline.ParentBlock = null;
}

inline = value;
}
}

/// <summary>
/// Gets or sets a value indicating whether <see cref="Lines"/> must be processed
Expand Down

0 comments on commit c59fd5c

Please sign in to comment.