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

Add ParentBlock property to ContainerInline #468

Merged
merged 1 commit into from
Aug 18, 2020
Merged
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
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