Skip to content

Instantly share code, notes, and snippets.

@MSGhero
Created September 24, 2020 01:25
Show Gist options
  • Save MSGhero/848dabee2b1f210abfb252857e7fcc2a to your computer and use it in GitHub Desktop.
Save MSGhero/848dabee2b1f210abfb252857e7fcc2a to your computer and use it in GitHub Desktop.
Unit tests for OpenFL TextField's replaceText() and setTextFormat()
text = new TextField();
text.text = "EXAMPLE";
text.selectable = true;
text.wordWrap = false;
text.type = "input";
text.border = true;
text.width = 300;
text.borderColor = 0x000000;
// cases where beginIndex != endIndex
text.setTextFormat(new openfl.text.TextFormat(null, null, 0xff0000), 0, 2); // completely before, unaffected
text.setTextFormat(new openfl.text.TextFormat(null, null, 0x00ff00), 2, 3); // range end == begin, unaffected
// the M between 3 and 4 => // range exactly equals selection (not the only format range in the TF), delete
text.setTextFormat(new openfl.text.TextFormat(null, null, 0x0000ff), 4, 5); // range start == end, start = begin and end shifted
text.setTextFormat(new openfl.text.TextFormat(null, null, 0xff0000), 5, 7); // completely after, whole range shifted
text.replaceText(3, 4, "ins");
// test colors or sizes [rrgbbbbrr]
// maybe also test format range coherence
// selection encompasses range (range.start == begin), delete
// selection completely encompasses range, delete
// section encompasses range (range.end == end), delete
// no ranges are left, create new one using defaultTextFormat
// THIS DOES NOT REPLICATE ON FLASH! The previous replaceText causes this one to provide a weird result
// Even if the previous replaceText does functionally nothing (`replaceText(3, 3, "")`), weird behavior still occurs
// If the previous replaceText is deleted, this one uses defaultTextFormat as expected
// If setTextFormat is used to create a similar TextField, it also uses defaultTextFormat as expected
// This may be a Flash bug, and it doesn't make sense to try to replicate it
text.replaceText(0, text.length, "EXAMPLE");
// !on flash [dtf all]
text.setTextFormat(new openfl.text.TextFormat(null, null, 0xff0000), 2, 5);
// [bbrrrbb]
text.replaceText(3, 4, "mm"); // selection competely within range, extend range
// [bbrrrrbb]
text.replaceText(1, 3, "xxaa"); // right part of selection within range, extend range left and right
// [brrrrrrrbb]
text.replaceText(7, 9, "ppll"); // left part of selection within range, end range early
// [brrrrrrbbbbb]
text.replaceText(0, text.length, "EXAMPLE");
text.replaceText(0, 2, ""); // test some deletions just in case
// [bbbbb]
text.replaceText(3, 5, "");
// [bbb]
text.replaceText(0, 2, "EXAM"); // range encompasses selection and they both start with the same index, extend range
// [bbbbb]
text.replaceText(3, 5, "MPLE"); // range encompasses selection and they both end with the same index, end range early
// [bbbbbbb]
text.setTextFormat(new openfl.text.TextFormat(null, null, 0xff0000), 0, 4);
text.setTextFormat(new openfl.text.TextFormat(null, null, 0x00ff00), 4, 7);
text.replaceText(3, 7, "MPLE"); // selection encompasses entire final range, use defaultTextFormat
// [rrrbbbb]
text.setTextFormat(new openfl.text.TextFormat(null, null, 0xff0000), 0, 4);
text.setTextFormat(new openfl.text.TextFormat(null, null, 0x00ff00), 4, 7);
text.replaceText(5, 7, "LE"); // selection encompasses end of final range, use defaultTextFormat
// [rrrrgbb]
// cases where beginIndex == endIndex
text.setTextFormat(new openfl.text.TextFormat(null, null, 0xff0000), 0, 7);
text.replaceText(0, 0, "ee"); // insertion is at index 0, use defaultTextFormat
// [bbrrrrrrr]
text.replaceText(5, 5, "aa"); // insertion is completely after a range, do nothing
// and insertion is in the middle of a range, extend the range
// [bbrrrrrrrrr]
// 19 and 21 left
text.replaceText(1, 1, "ee"); // insertion is completely before a range, shift range
// [bbbbrrrrrrrrr]
text.replaceText(4, 4, "ee"); // insertion is at the end of a range, extend the range
// [bbbbbbrrrrrrrrr]
// final: see if replaceText(0, 0, "") or "text" works on empty textfield
text.text = "";
text.replaceText(0, 0, "text"); // replacing empty text, shouldn't be any jank in textFormatRanges
// shouldn't log a warning message
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment