dlang-community / experimental.xml

A replacement of Phobos std.xml
https://rawgit.com/dlang-community/experimental.xml/gh-pages/index.html
Boost Software License 1.0
11 stars 8 forks source link

elementEnd is not parsed when element is empty #11

Open wilzbach opened 6 years ago

wilzbach commented 6 years ago

From @NVolcz on October 18, 2016 21:24

unittest
{
    import std.experimental.xml.lexers;
    import std.experimental.xml.parser;
    import std.string : lineSplitter, strip;
    import std.algorithm : map;
    import std.array : array;

    wstring xml = q{
    <?xml encoding = "utf-8" ?>
    <!DOCTYPE mydoc>
    <aaa>
        <works>
        test
        </works>
        <notWork></notWork>
    </aaa>
    };

    auto cursor = xml.lexer.parser.cursor;

    assert(cursor.atBeginning);

    // <?xml encoding = "utf-8" ?>
    assert(cursor.kind() == XMLKind.document);
    assert(cursor.name() == "xml");
    assert(cursor.prefix() == "");
    assert(cursor.localName() == "xml");
    assert(cursor.attributes().array == [Attribute!wstring("encoding", "utf-8")]);
    assert(cursor.content() == " encoding = \"utf-8\" ");

    assert(cursor.enter());
        assert(!cursor.atBeginning);

        // <!DOCTYPE mydoc
        assert(cursor.kind == XMLKind.dtdEmpty);
        assert(cursor.wholeContent == " mydoc");

        assert(cursor.next);

        // <aaa>
        assert(cursor.kind() == XMLKind.elementStart);
        assert(cursor.name() == "aaa");
        assert(cursor.prefix() == "");
        assert(cursor.localName() == "aaa");

        assert(cursor.enter());
            // <works>
            assert(cursor.kind() == XMLKind.elementStart);
            assert(cursor.name() == "works");

            assert(cursor.enter());
            assert(!cursor.enter());
            assert(!cursor.next());

            //cursor.exit();

            // </works>
            assert(cursor.kind() == XMLKind.elementEnd);
            assert(cursor.name() == "works");

            cursor.exit();

            assert(cursor.next());

             // <notWork>
            assert(cursor.kind() == XMLKind.elementStart);
            assert(cursor.name() == "notWork");

            assert(!cursor.enter());
            assert(!cursor.next());

            // </notWork>
            assert(cursor.kind() == XMLKind.elementEnd);
            assert(cursor.name() == "notWork");

            assert(!cursor.next());
        cursor.exit();

        // </aaa>
        assert(cursor.kind() == XMLKind.elementEnd);
        assert(cursor.name() == "aaa");

        assert(!cursor.next());
    cursor.exit();

    assert(cursor.documentEnd);
    assert(!cursor.atBeginning);
}

Copied from original issue: lodo1995/experimental.xml#34