Function to parse raw HTML

I’m trying to write some simple unit tests with Hugo templates itself, one common case is that check if partial returned HTML contain expected elements.

{{ range $cases }}
  {{ $html := partial "foobar" .input | parseHTML }}
  {{/* Check if elements (attributes, text etc...) present in $html. */}}
  {{/* ... */}}
{{ end }}

Some thoughts:

  1. transform.Unmarshal supports parsing XML, however HTML is not equal to XML.
  2. I’m able to implement similar tests via string functions, but not a very robust approach.
  3. I also manage to implement E2E tests with third party tools, like Playwright.

I think this feature will simplify testing without installing extra tools.

I (sort of) understand the high level intent, but this is unlikely to happen. Sure, Go has an HTML parser, but that’s mostly useless without the ability to walk the tree.

1 Like

I see, will go with string functions for now.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.