TagBalance

io.github.frikit.twirlspec.html.TagBalance
object TagBalance

Whether every element that was opened was closed, read from the source.

By the time a parser hands back a tree the question cannot be asked: an unclosed `

` has already been closed for you. Jsoup does notice, but it reports the symptom — "Unexpected EndTag []" — rather than the cause, which leaves you counting tags by hand. This reads the markup as written and names the element and the line it was opened on.

It reads the source as the tokenizer of the HTML Living Standard, as of November 2026, reads it. Comments, doctypes, processing instructions, CDATA in SVG and MathML, quoted attribute values and the elements whose content is text, such as a script, a style or a textarea, open nothing. It departs from the standard twice. A quoted value that never closes ends its tag at the tag's first >, so the element is still reported rather than the rest of the page vanishing into the value. And a noscript is read as markup, as a browser with scripting off reads it.

It nests elements by their tags, knowing what HTML lets you leave out: void elements, the optional end tags, a paragraph ended by the start of a block (though not left open at the end of a link, a media element, an edit, a map, a noscript, a custom element or an SVG or MathML parent), and a slash closing only an SVG or MathML element. These are HTML's rules, so an SVG or MathML element of the same name keeps its own. It puts each element in the namespace a parser would: SVG and MathML, and HTML again at their integration points.

It does not repeat a parser's error recovery, such as breaking out of SVG, closing by scope, the adoption agency or foster parenting: it also reads template fragments with no page around them, and its job is to name the mistake in the source, not the tree a browser repairs it into.

Attributes

Source
TagBalance.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Self type
TagBalance.type

Members list

Type members

Classlikes

final case class EndedParagraph(line: Int, endedBy: String, endedOn: Int, openedOn: Int) extends Problem

A `` whose paragraph the start of a block had already ended, so it closes nothing, and a browser renders it as an empty paragraph of its own.

A `` whose paragraph the start of a block had already ended, so it closes nothing, and a browser renders it as an empty paragraph of its own.

Attributes

Source
TagBalance.scala
Supertypes
trait Serializable
trait Product
trait Equals
trait Problem
class Object
trait Matchable
class Any
Show all
final case class MisNested(name: String, line: Int, closedInstead: String, openedOn: Int) extends Problem

A closing tag that closed more than it named, so two elements overlap.

A closing tag that closed more than it named, so two elements overlap.

Attributes

Source
TagBalance.scala
Supertypes
trait Serializable
trait Product
trait Equals
trait Problem
class Object
trait Matchable
class Any
Show all
sealed trait Problem

Something wrong with the balance of tags, with the line it was found on.

Something wrong with the balance of tags, with the line it was found on.

Attributes

Source
TagBalance.scala
Supertypes
class Object
trait Matchable
class Any
Known subtypes
class MisNested
class StrayClose
class Unclosed
final case class StrayClose(name: String, line: Int) extends Problem

A closing tag with nothing open to close.

A closing tag with nothing open to close.

Attributes

Source
TagBalance.scala
Supertypes
trait Serializable
trait Product
trait Equals
trait Problem
class Object
trait Matchable
class Any
Show all
final case class Unclosed(name: String, line: Int) extends Problem

An element opened and never closed.

An element opened and never closed.

Attributes

Source
TagBalance.scala
Supertypes
trait Serializable
trait Product
trait Equals
trait Problem
class Object
trait Matchable
class Any
Show all

Value members

Concrete methods

def check(source: String): List[Problem]

The problems in a piece of rendered markup, in line order.

The problems in a piece of rendered markup, in line order.

Attributes

Source
TagBalance.scala
def checkTemplate(template: String): List[Problem]

The same check against a Twirl template rather than a rendered page.

The same check against a Twirl template rather than a rendered page.

Cheap enough to run over a whole estate: no application, no render. It sees only what the template writes literally, so markup arriving from a helper is invisible, and a template that opens an element in one branch and closes it in another looks unbalanced, because on any single render it is.

Attributes

Source
TagBalance.scala
def templatesUnder(root: Path): List[Path]

Every Twirl template under this directory, in a stable order, or none where it is not a directory.

Every Twirl template under this directory, in a stable order, or none where it is not a directory.

Attributes

Source
TagBalance.scala