getHtmlDiff method

String getHtmlDiff()

Implementation

String getHtmlDiff() {
  const HtmlEscape elementEscaper = HtmlEscape(HtmlEscapeMode.element);
  const HtmlEscape attrEscaper = HtmlEscape(HtmlEscapeMode.attribute);

  StringBuffer diff = StringBuffer();

  void writeDiff(String source, String className) {
    diff.write('<span class="$className">');
    diff.write(elementEscaper.convert(source));
    diff.write('</span>');
  }

  iterateReplacements(
      onUnmodified: (source) => writeDiff(source, 'diff-unmodified'),
      onRemoval: (source) => writeDiff(source, 'diff-removal'),
      onAddition: (source) => writeDiff(source, 'diff-addition'));

  return '''
      <!DOCTYPE html>
      <html>
      <head>
        <title>Transformer Diff - ${attrEscaper.convert(sourceFile.url!.path)}</title>
        <style>
          .diff-unmodified {
              color: #444;
          }
          .diff-removal {
              color: #a00;
              background-color: #fee;
              text-decoration: line-through;
          }
          .diff-addition {
              color: #0a0;
              background-color: #efe;
          }
        </style>
      </head>
      <body>
        <code>
          <pre>$diff</pre>
        </code>
      </body>
      </html>
  ''';
}