hterm: move invisible text processing to the end

There shouldn't be functional changes here.  Simplify processing of
the invisible attribute by delaying it to the end.  It's not common
to use this attribute, so we don't really need to optimize for it.

Change-Id: I345db3fedbee3b09cc330dfb91468b025b66f23b
Reviewed-on: https://chromium-review.googlesource.com/765591
Reviewed-by: Brandon Gilmore <varz@google.com>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/hterm/js/hterm_text_attributes.js b/hterm/js/hterm_text_attributes.js
index 0ae594d..c945220 100644
--- a/hterm/js/hterm_text_attributes.js
+++ b/hterm/js/hterm_text_attributes.js
@@ -337,11 +337,6 @@
     }
   }
 
-  if (this.invisible) {
-    foregroundSource = backgroundSource;
-    defaultForeground = this.defaultBackground;
-  }
-
   if (foregroundSource == this.SRC_DEFAULT)
     this.foreground = defaultForeground;
   else if (Number.isInteger(foregroundSource))
@@ -349,7 +344,7 @@
   else
     this.foreground = foregroundSource;
 
-  if (this.faint && !this.invisible) {
+  if (this.faint) {
     var colorToMakeFaint = ((this.foreground == this.DEFAULT_COLOR) ?
                             this.defaultForeground : this.foreground);
     this.foreground = lib.colors.mix(colorToMakeFaint, 'rgb(0, 0, 0)', 0.3333);
@@ -361,6 +356,10 @@
     this.background = this.colorPalette[backgroundSource];
   else
     this.background = backgroundSource;
+
+  // Process invisible settings last to keep it simple.
+  if (this.invisible)
+    this.foreground = this.background;
 };
 
 /**
diff --git a/hterm/js/hterm_text_attributes_tests.js b/hterm/js/hterm_text_attributes_tests.js
index 971d707..861257b 100644
--- a/hterm/js/hterm_text_attributes_tests.js
+++ b/hterm/js/hterm_text_attributes_tests.js
@@ -208,6 +208,32 @@
   result.pass();
 });
 
+/**
+ * Handling of invisible tags.
+ */
+hterm.TextAttributes.Tests.addTest('invisible', function(result, cx) {
+  const tattrs = new hterm.TextAttributes(cx.window.document);
+  let node;
+
+  // Set an attribute to force a container (rather than a text node),
+  // but doesn't affect the color behavior in syncColors.
+  tattrs.underline = true;
+  tattrs.setDefaults('rgb(1, 2, 3)', 'rgb(3, 2, 1)');
+
+  // Set colors to something other than the default.
+  tattrs.foregroundSource = 'rgb(1, 1, 1)';
+  tattrs.backgroundSource = 'rgb(2, 2, 2)';
+
+  // Invisible settings should have same colors.
+  tattrs.invisible = true;
+  tattrs.syncColors();
+  node = tattrs.createContainer('asdf');
+  result.assertEQ(tattrs.backgroundSource, node.style.color);
+  result.assertEQ(tattrs.backgroundSource, node.style.backgroundColor);
+
+  result.pass();
+});
+
 hterm.TextAttributes.Tests.addTest('splitWidecharString-ascii', function(result, cx) {
   var text = 'abcdefghijklmn';