function ensureTemplateOutputHtml(templateResult) {
  // Note we allow everything that isn't an object, because some non-escaping
  // templates end up returning non-strings if their only print statement is a
  // non-escaped argument, plus some unit tests spoof templates.
  // TODO(gboyer): Track down and fix these cases.
  if (!goog.isObject(templateResult)) {
    return SafeHtml.htmlEscape(String(templateResult));
  }

  // Allow SafeHtml from SanitizedContent and the result of
  // javascript.template.soy.makeHtml
  if (templateResult.toSafeHtml) {
    const result = templateResult.toSafeHtml();
    if (result instanceof SafeHtml) {
      return result;
    }
  }

  asserts.fail(
      `Soy template output is unsafe for use as HTML: ${templateResult}`);

  // In production, return a safe string, rather than failing hard.
  return SafeHtml.htmlEscape('zSoyz');
}
Ensures the result is "safe" to insert as HTML.
In the case the argument is a SanitizedContent object, it either must
already be of kind HTML, or if it is kind="text", the output will be HTML
escaped.
@param {*} templateResult The template result.
@return {!SafeHtml} The assumed-safe HTML output string.