Skip to content

Inserting child in self-closing element instead inserts element as sibling #27

Open
@heinrich-ulbricht

Description

@heinrich-ulbricht

Bug Report

Prerequisites

  • Can you reproduce the problem in a MWE?
  • Are you running the latest version of AngleSharp?
  • Did you check the FAQs to see if that helps you?
  • Are you reporting to the correct repository? (there are multiple AngleSharp libraries, e.g., AngleSharp.Css for CSS support)
  • Did you perform a search in the issues?

For more information, see the CONTRIBUTING guide.

Description

I have a self-closing element and now want to add a child element.

Steps to Reproduce

Here's a complete repo:

[TestMethod]
public void TestInsertChildToSelfClosingElementSimple()
{
    var xml =
    """
    <ac:structured-macro ac:name="children"/>
    """;

    var config = Configuration.Default.With(HtmlEntityProvider.Resolver).WithDefaultLoader(new LoaderOptions { IsResourceLoadingEnabled = true }).WithCss().WithXml();
    var context = BrowsingContext.New(config);
    var parser = new XmlParser(new XmlParserOptions(), context);

    var doc = parser.ParseDocument(xml);
    var parentElement = doc.DocumentElement;
    Assert.IsNotNull(parentElement);
    Assert.AreEqual(NodeFlags.SelfClosing, parentElement.Flags);

    var child = doc.CreateElement("ac:parameter");
    child.SetAttribute("ac:name", "dummy");
    parentElement.AppendChild(child);

    var parentElementXml = parentElement.ToXml();
    // it seems to be impossible to insert something into a self-closing element!? this test is here to document this fact
    Assert.AreEqual("""<ac:structured-macro ac:name="children" /><ac:parameter ac:name="dummy"></ac:parameter>""", parentElementXml);
}

Expected behavior: I expect the ac:parameter element to become a child element of the parent element. The outcome should look like this: <ac:structured-macro ac:name="children"><ac:parameter ac:name="dummy"></ac:parameter></ac:structured-macro>.

Actual behavior: The ac:parameter element is inserted as sibling instead: <ac:structured-macro ac:name="children" /><ac:parameter ac:name="dummy"></ac:parameter>.

Environment details: .NET 8, Windows 11 && Debian

Possible Solution

Cloning

I'd be fine with cloning the element if I could specify if the clone should be self-closing, or not. Basically optionally introduce the behavior that was once fixed here: #17 :D

I tried finding some specs that describe if it is valid to "transform" a self-closing element back to having children, but couldn't find anything. Cloning might introduce a compliant workaround if any specs prohibit changing the self-closing flag.

Changing the Flag

Maybe it is as easy as allowing to change the Flags property. Not sure about the consequences, though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions