Skip to content

Fix: 'a' tag doesn't support sub-tags #7

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

Merged
merged 1 commit into from
Mar 22, 2022
Merged
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
102 changes: 82 additions & 20 deletions FB2Library/Elements/InternalLinkItem.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace FB2Library.Elements
{
public class InternalLinkItem : StyleType
{
private readonly XNamespace lNamespace = @"http://www.w3.org/1999/xlink";

public string Type { get; set; }

public string HRef { get; set; }

public SimpleText LinkText { get; set; }
private readonly List<StyleType> _linkData = new List<StyleType>();
public List<StyleType> LinkData { get { return _linkData; } }

internal const string Fb2InternalLinkElementName = "a";


public override string ToString()
{
return LinkText.ToString();
StringBuilder builder = new StringBuilder();
builder.Append("<a");
if (string.IsNullOrEmpty(Type))
{
builder.Append($" type='{Type}'");
}
if (string.IsNullOrEmpty(HRef))
{
builder.Append($" href='{HRef}'");
}
builder.Append(">");
foreach (var item in _linkData)
{
builder.Append(item.ToString());
builder.Append(" ");
}
builder.Append("</a>");
return builder.ToString();
}

internal void Load(XElement xLink)
Expand All @@ -36,22 +54,51 @@ internal void Load(XElement xLink)
throw new ArgumentException("Element of wrong type passed", "xLink");
}

LinkText = null;
//if (xLink.Value != null)
if (xLink.HasElements)
{
LinkText = new SimpleText();
try
IEnumerable<XNode> childElements = xLink.Nodes();
foreach (var element in childElements)
{
LinkText.Load(xLink);
}
catch (Exception)
{
LinkText = null;
if ((element.NodeType == XmlNodeType.Element) && !IsSimpleText(element))
{
XElement xElement = (XElement)element;
if (xElement.Name.LocalName == InlineImageItem.Fb2InlineImageElementName)
{
InlineImageItem image = new InlineImageItem();
try
{
image.Load(xElement);
_linkData.Add(image);
}
catch (Exception)
{
}
}
}
else
{
SimpleText text = new SimpleText();
try
{
text.Load(element);
_linkData.Add(text);
}
catch (Exception)
{
continue;
}
}
}
}
else if (!string.IsNullOrEmpty(xLink.Value))
{
SimpleText text = new SimpleText();
text.Load(xLink);
_linkData.Add(text);
}

XAttribute xTypeAttr = xLink.Attribute("type");
if ((xTypeAttr != null)&& (xTypeAttr.Value != null))
if ((xTypeAttr != null) && (xTypeAttr.Value != null))
{
Type = xTypeAttr.Value;
}
Expand All @@ -64,20 +111,35 @@ internal void Load(XElement xLink)

}

private bool IsSimpleText(XNode element)
{
// if not element than we assume simple text
if (element.NodeType != XmlNodeType.Element)
{
return true;
}
XElement xElement = (XElement)element;
if (xElement.Name.LocalName == InternalLinkItem.Fb2InternalLinkElementName)
{
throw new ArgumentException("Schema doesn't support nested links");
}
return xElement.Name.LocalName != InlineImageItem.Fb2InlineImageElementName;
}

public XNode ToXML()
{
XElement xLink = new XElement(Fb2Const.fb2DefaultNamespace + Fb2InternalLinkElementName);
if (!string.IsNullOrEmpty(Type))
{
xLink.Add(new XAttribute("type",Type));
{
xLink.Add(new XAttribute("type", Type));
}
if(!string.IsNullOrEmpty(HRef))
if (!string.IsNullOrEmpty(HRef))
{
xLink.Add(new XAttribute(lNamespace + "href",HRef));
xLink.Add(new XAttribute(lNamespace + "href", HRef));
}
if (LinkText != null)
foreach (StyleType childElements in _linkData)
{
xLink.Add(LinkText.ToXML());
xLink.Add(childElements.ToXML());
}
return xLink;
}
Expand Down