eXist-db / templating

HTML Templating Library for eXist-db
GNU Lesser General Public License v2.1
5 stars 8 forks source link

[BUG] templates:each produces superfluous empty element #14

Closed yamahito closed 2 years ago

yamahito commented 2 years ago

Expected behaviour

@data-template="templates:each" on an e.g. li element produces one li for each member of a sequence given by @data-template-from.

Actual behaviour

An extra, superfluous, empty li element is inserted before (or sometimes around, see below) the expected results. Attributes are preserved.

Reproduction steps

This is an old, established bug: see for example https://github.com/HistoryAtState/hsg-shell/issues/37.

In trying to replicate the behaviour I am seeing on our live server, I have instead managed to produce the superfluous li around the expected result, instead of before it; perhaps something to do with html vs xml outputs?:

xquery version "3.1";

import module namespace templates="http://exist-db.org/xquery/templates";

declare namespace test="example.com/test";

declare function test:li($node, $model) {
    $model?li
};

let $config := map{
    $templates:CONFIG_FN_RESOLVER : function($functionName as xs:string, $arity as xs:int) {
        try {
            function-lookup(xs:QName($functionName), $arity)
        } catch * {
            ()
        }
    },
    $templates:CONFIG_PARAM_RESOLVER : map{}
}

let $model := map {
    $templates:CONFIGURATION : $config,
    "ul": ("one", "two", "five")
}

let $node :=
    <ul>
        <li class="ArthurianCount" data-template="templates:each" data-template-from="ul" data-template-to="li">
            <span data-template="test:li"/>
        </li>
    </ul>

return templates:process($node, $model)

Please provide the following

open-collective-bot[bot] commented 2 years ago

Hey @yamahito :wave:,

Thank you for opening an issue. We will get back to you as soon as we can. Have you seen our Open Collective page? Please consider contributing financially to our project. This will help us involve more contributors and get to issues like yours faster.

https://opencollective.com/existdb

We offer priority support for all financial contributors. Don't forget to add priority label once you become one! :smile:

yamahito commented 2 years ago

So if I choose 'direct output' in exide and inspect the result window, I see the list item as a first sibling rather than a surrounding li:

Screenshot 2022-09-30 at 12 44 47

So, I suspect the exact nature of the bug was being obfusticated by browsers' HTML parsing, and the bug is actually that the element is duplicated as both parent and children.

yamahito commented 2 years ago

🤦‍♂️ never mind, just spotted that http://exist-db.org/xquery/templates should be http://exist-db.org/xquery/html-templating