eXist-db / templating

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

Fix form control: radio button & checkbox inputs #9

Closed joewiz closed 2 years ago

joewiz commented 3 years ago

Fixes https://github.com/eXist-db/shared-resources/issues/52

Before this PR, saving the following test query to /db/test/templates-form-control.xq...

xquery version "3.1";

declare namespace output="http://www.w3.org/2010/xslt-xquery-serialization";

declare boundary-space preserve;

declare option output:method "html5";
declare option output:media-type "text/html";

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

(: sample HTML taken from https://www.w3schools.com/tags/att_input_type_radio.asp :)

let $content := 
    <html>
        <body>
            <h1>Test</h1>
            <form>
                <input data-template="templates:form-control" type="text" name="foo"/><br/>
                <input data-template="templates:form-control" type="checkbox" id="html" name="fav_language" value="HTML"/>
                <label for="html">HTML</label><br/>
                <input data-template="templates:form-control" type="checkbox" id="css" name="fav_language" value="CSS"/>
                <label for="css">CSS</label><br/>
                <input data-template="templates:form-control" type="checkbox" id="javascript" name="fav_language" value="JavaScript"/>
                <label for="javascript">JavaScript</label><br/>
                <input type="submit"/>
            </form>
        </body>
    </html>
let $resolver := function($functionName as xs:string, $arity as xs:int) {
    try {
        function-lookup(xs:QName($functionName), $arity)
    } catch * {
        ()
    }
}
return 
    templates:apply($content, $resolver, map{})

... and calling http://localhost:8080/exist/rest/db/test/templates-form-control.xq?fav_language=HTML would return all checkboxes checked:

Screen Shot 2021-10-25 at 6 31 51 PM

After this PR, loading the page will return just the "HTML" checkbox checked:

Screen Shot 2021-10-25 at 6 32 40 PM

The fix also works for radio buttons:

Screen Shot 2021-10-25 at 6 33 42 PM

The PR adds a test for the previously untested scenario where a set of checkbox inputs have a @value attribute.

joewiz commented 2 years ago

This PR was included in https://github.com/eXist-db/templating/releases/tag/v1.0.4.