[whatwg] JavaScript function for closing tags

Michael A. Peters mpeters at domblogger.net
Sat Oct 14 01:13:23 PDT 2017

I use TextTrack API but it's documention does not specify that it closes 
open tags within a cue, in fact I'm fairly certain it doesn't because 
some people use it for json and other related none tag related content.

Some errors using the tracks in XML were solved by the innerHTML trick 
where I create a separate html document, append the cue, and then grab 
the innerHTML but that doesn't always work to close tags when html 
entities are part of the cue string.

I use JS regex to turn <c.foo> into <span class="foo"> and <v whatever> 
into <span data-voice="whatwever"> and just jQuery .append() to place 
the cue in a div for captions/subtitles since browser standard html5 
audio players have zero support for captions by themselves, and the 
jQuery .append() is what needs closing tags in XML or it understandably 
completely fails.

Also using it for chapters which don't appear to be supported in either 
browser standard audio or video players.

On 10/14/2017 12:46 AM, Silvia Pfeiffer wrote:
> Hi Michael,
> It seems to me that the TextTrack API is made for this use case.
> Why does it not work for you?
> Cheers,
> Silvia.
> On Sat, Oct 14, 2017 at 4:36 PM, Michael A. Peters
> <mpeters at domblogger.net> wrote:
>> There does not seem to be a JavaScript API for closing open tags.
>> This is problematic when dealing with WebVTT which does not require tags be
>> closed.
>> Where it is the biggest problem is when the document is being served as
>> I tried the following hack which seemed to be working:
>> cleandoc = document.implementation.createHTMLDocument("FuBar");
>> cleanbody = document.createElementNS("http://www.w3.org/1999/xhtml",
>> "body");
>> cleandoc.documentElement.appendChild(cleanbody);
>> Then I could do the following when with a WebVTT cue:
>> cleanbody.innerHTML = string;
>> return (cleanbody.innerHTML);
>> That *mostly* works but seems to sometimes fail when string contains
>> entities, such as  
>> What happens is it returns an empty string.
>> Given that WebVTT is part of HTML5 and browser native html5 audio players
>> don't support caption tracks forcing us to write our own implementations if
>> we want captions with audio, it sure would be nice if there was a pure
>> JavaScript way to just add closing tags to a string because there is never a
>> guarantee valid WebVTT cue has closed tags which are required for XHTML sent
>> as XML.
>> Seems to me that a JS native function to add missing closing tags would have
>> more application than just WebVTT cues.
>> I looked for a jQuery filter that does it, but could not find one.
>> It also could be of benefit in emulating document.write() as many of
>> Google's tools *still* require document.write() despite the issues with
>> document.write() and XML having been known for 15+ years now.
>> Any chance of getting a parser into JavaScript that at least would be
>> capable of closing open tags in a string passed to it?

More information about the whatwg mailing list