[web-devel] BlazeHtml: a question about API design

Johan Tibell johan.tibell at gmail.com
Sun Apr 25 08:54:53 EDT 2010

On Sun, Apr 25, 2010 at 10:54 AM, Jasper Van der Jeugt
<jaspervdj at gmail.com> wrote:
> So, say you have the regular div tag. This would be, in our library,
> Text.Blaze.Html.Strict.div. That makes sense, but it seems possible
> that a user wants to use a div as a leaf node (e.g. `<div />`). So
> there are two possible signatures for `div`:
> div :: Html -- An empty div element.
> div :: Html -> Html -- The argument is the content of the div element.

Note that the far most common use of empty elements like this is when
you later want to populate the element using from JavaScript.

> I'm not sure what we want to do in that case. I so far see two major options:
> Option 1: We can provide leaf and non-leaf combinators for every tag.
> I'm not sure if this is overkill or not, but it is, for example not
> forbidden to have content in an `<img>` tag.

I'm afraid I was wrong when I told you this was the case. The "img"
tag (and several others, like "br") must not have any content. Check
the HTML spec or try it out in w3's HTML validator. Your problem still
remains for tags that may have content, like "div".

> I have my doubts with both options, but I would tend to go for (1),
> because (2) feels more unstable. Anyway, feedback and more ideas would
> be appreciated :-)

Given that this problem is quite rare I'd just have the user provide
some empty contents in the cases where an empty tag is called for

    div (text "")

With OverloadedStrings it's even shorter: div ""

If you care about the distinction between "<tag></tag>" and "<tag />"
you can postpone outputting the start tag until you produced some
content and output the different tag form in case you detect that no
content was added.

This avoid introducing new operators, type classes or doubling the
number of combinators.


More information about the web-devel mailing list