mirror of
https://github.com/xoofx/markdig.git
synced 2026-02-07 21:42:25 +00:00
Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
94e07d11ce | ||
|
|
263041e899 | ||
|
|
e8f9274b64 | ||
|
|
b32e71aaeb | ||
|
|
f991b2123b |
@@ -88,5 +88,102 @@ namespace Markdig.Tests
|
||||
|
||||
Assert.Throws<ArgumentException>(() => container[0] = two); // two already has a parent
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Contains()
|
||||
{
|
||||
var container = new MockContainerBlock();
|
||||
var block = new ParagraphBlock();
|
||||
|
||||
Assert.False(container.Contains(block));
|
||||
|
||||
container.Add(block);
|
||||
Assert.True(container.Contains(block));
|
||||
|
||||
container.Add(new ParagraphBlock());
|
||||
Assert.True(container.Contains(block));
|
||||
|
||||
container.Insert(0, new ParagraphBlock());
|
||||
Assert.True(container.Contains(block));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Remove()
|
||||
{
|
||||
var container = new MockContainerBlock();
|
||||
var block = new ParagraphBlock();
|
||||
|
||||
Assert.False(container.Remove(block));
|
||||
Assert.AreEqual(0, container.Count);
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => container.RemoveAt(0));
|
||||
Assert.AreEqual(0, container.Count);
|
||||
|
||||
container.Add(block);
|
||||
Assert.AreEqual(1, container.Count);
|
||||
Assert.True(container.Remove(block));
|
||||
Assert.AreEqual(0, container.Count);
|
||||
Assert.False(container.Remove(block));
|
||||
Assert.AreEqual(0, container.Count);
|
||||
|
||||
container.Add(block);
|
||||
Assert.AreEqual(1, container.Count);
|
||||
container.RemoveAt(0);
|
||||
Assert.AreEqual(0, container.Count);
|
||||
Assert.Throws<ArgumentOutOfRangeException>(() => container.RemoveAt(0));
|
||||
Assert.AreEqual(0, container.Count);
|
||||
|
||||
container.Add(new ParagraphBlock { Column = 1 });
|
||||
container.Add(new ParagraphBlock { Column = 2 });
|
||||
container.Add(new ParagraphBlock { Column = 3 });
|
||||
container.Add(new ParagraphBlock { Column = 4 });
|
||||
Assert.AreEqual(4, container.Count);
|
||||
|
||||
container.RemoveAt(2);
|
||||
Assert.AreEqual(3, container.Count);
|
||||
Assert.AreEqual(4, container[2].Column);
|
||||
|
||||
Assert.True(container.Remove(container[1]));
|
||||
Assert.AreEqual(2, container.Count);
|
||||
Assert.AreEqual(1, container[0].Column);
|
||||
Assert.AreEqual(4, container[1].Column);
|
||||
Assert.Throws<IndexOutOfRangeException>(() => _ = container[2]);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CopyTo()
|
||||
{
|
||||
var container = new MockContainerBlock();
|
||||
|
||||
var destination = new Block[4];
|
||||
container.CopyTo(destination, 0);
|
||||
container.CopyTo(destination, 1);
|
||||
container.CopyTo(destination, -1);
|
||||
container.CopyTo(destination, 5);
|
||||
Assert.Null(destination[0]);
|
||||
|
||||
container.Add(new ParagraphBlock());
|
||||
container.CopyTo(destination, 0);
|
||||
Assert.NotNull(destination[0]);
|
||||
Assert.Null(destination[1]);
|
||||
Assert.Null(destination[2]);
|
||||
Assert.Null(destination[3]);
|
||||
|
||||
container.CopyTo(destination, 2);
|
||||
Assert.NotNull(destination[0]);
|
||||
Assert.Null(destination[1]);
|
||||
Assert.NotNull(destination[2]);
|
||||
Assert.Null(destination[3]);
|
||||
|
||||
Array.Clear(destination);
|
||||
|
||||
container.Add(new ParagraphBlock());
|
||||
container.CopyTo(destination, 1);
|
||||
Assert.Null(destination[0]);
|
||||
Assert.NotNull(destination[1]);
|
||||
Assert.NotNull(destination[2]);
|
||||
Assert.Null(destination[3]);
|
||||
|
||||
Assert.Throws<IndexOutOfRangeException>(() => container.CopyTo(destination, 3));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using Markdig.Helpers;
|
||||
using Markdig.Syntax;
|
||||
using Markdig.Syntax.Inlines;
|
||||
@@ -17,20 +19,7 @@ namespace Markdig.Renderers
|
||||
/// <seealso cref="IMarkdownRenderer" />
|
||||
public abstract class RendererBase : IMarkdownRenderer
|
||||
{
|
||||
private readonly struct KeyWrapper : IEquatable<KeyWrapper>
|
||||
{
|
||||
public readonly IntPtr Key;
|
||||
|
||||
public KeyWrapper(IntPtr key) => Key = key;
|
||||
|
||||
public bool Equals(KeyWrapper other) => Key == other.Key;
|
||||
|
||||
public override int GetHashCode() => Key.GetHashCode();
|
||||
|
||||
public override bool Equals(object? obj) => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
private readonly Dictionary<KeyWrapper, IMarkdownObjectRenderer?> _renderersPerType = new();
|
||||
private readonly Dictionary<Type, IMarkdownObjectRenderer?> _renderersPerType = new();
|
||||
internal int _childrenDepth = 0;
|
||||
|
||||
/// <summary>
|
||||
@@ -40,13 +29,11 @@ namespace Markdig.Renderers
|
||||
|
||||
private IMarkdownObjectRenderer? GetRendererInstance(MarkdownObject obj)
|
||||
{
|
||||
KeyWrapper key = GetKeyForType(obj);
|
||||
Type objectType = obj.GetType();
|
||||
|
||||
var key = obj.GetType();
|
||||
for (int i = 0; i < ObjectRenderers.Count; i++)
|
||||
{
|
||||
var renderer = ObjectRenderers[i];
|
||||
if (renderer.Accept(this, objectType))
|
||||
if (renderer.Accept(this, key))
|
||||
{
|
||||
_renderersPerType[key] = renderer;
|
||||
return renderer;
|
||||
@@ -154,7 +141,7 @@ namespace Markdig.Renderers
|
||||
// Calls before writing an object
|
||||
ObjectWriteBefore?.Invoke(this, obj);
|
||||
|
||||
if (!_renderersPerType.TryGetValue(GetKeyForType(obj), out IMarkdownObjectRenderer? renderer))
|
||||
if (!_renderersPerType.TryGetValue(obj.GetType(), out IMarkdownObjectRenderer? renderer))
|
||||
{
|
||||
renderer = GetRendererInstance(obj);
|
||||
}
|
||||
@@ -175,22 +162,5 @@ namespace Markdig.Renderers
|
||||
// Calls after writing an object
|
||||
ObjectWriteAfter?.Invoke(this, obj);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static KeyWrapper GetKeyForType(MarkdownObject obj)
|
||||
{
|
||||
#if NET
|
||||
IntPtr methodTablePtr = Unsafe.Add(ref Unsafe.As<RawData>(obj).Data, -1);
|
||||
return new KeyWrapper(methodTablePtr);
|
||||
#else
|
||||
IntPtr typeHandle = Type.GetTypeHandle(obj).Value;
|
||||
return new KeyWrapper(typeHandle);
|
||||
#endif
|
||||
}
|
||||
|
||||
private sealed class RawData
|
||||
{
|
||||
public IntPtr Data;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -130,7 +130,7 @@ namespace Markdig.Syntax
|
||||
BlockWrapper[] children = _children;
|
||||
for (int i = 0; i < Count && i < children.Length; i++)
|
||||
{
|
||||
array[arrayIndex + 1] = children[i].Block;
|
||||
array[arrayIndex + i] = children[i].Block;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,7 +193,7 @@ namespace Markdig.Syntax
|
||||
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
if ((uint)index > (uint)Count)
|
||||
if ((uint)index >= (uint)Count)
|
||||
ThrowHelper.ArgumentOutOfRangeException_index();
|
||||
|
||||
Count--;
|
||||
|
||||
Reference in New Issue
Block a user