Commit fedda084 authored by sanketh's avatar sanketh
Browse files

Bug 1635224 - Preserve mLastValueChangeWasInteractive between SaveState and...

Bug 1635224 - Preserve mLastValueChangeWasInteractive between SaveState and RestoreState r=emilio,masayuki, a=pascalc

Modify PresState's string variant to also store whether the last change was
interactive, and preserve that property when saving and restoring state.

Differential Revision: https://phabricator.services.mozilla.com/D73920
parent 44c9ce7c
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -5777,7 +5777,8 @@ HTMLInputElement::SaveState() {
        }
      }

      state->contentData() = std::move(value);
      state->contentData() =
          TextContentData(value, mLastValueChangeWasInteractive);
      break;
  }

@@ -5994,12 +5995,16 @@ bool HTMLInputElement::RestoreState(PresState* aState) {
        break;
      }

      if (inputState.type() == PresContentData::TnsString) {
      if (inputState.type() == PresContentData::TTextContentData) {
        // TODO: What should we do if SetValueInternal fails?  (The allocation
        // may potentially be big, but most likely we've failed to allocate
        // before the type change.)
        SetValueInternal(inputState.get_nsString(),
        SetValueInternal(inputState.get_TextContentData().value(),
                         TextControlState::eSetValue_Notify);
        if (inputState.get_TextContentData().lastValueChangeWasInteractive()) {
          mLastValueChangeWasInteractive = true;
          UpdateState(true);
        }
      }
      break;
  }
+8 −4
Original line number Diff line number Diff line
@@ -735,7 +735,8 @@ HTMLTextAreaElement::SaveState() {
        return rv;
      }

      state->contentData() = std::move(value);
      state->contentData() =
          TextContentData(value, mLastValueChangeWasInteractive);
    }
  }

@@ -757,12 +758,15 @@ HTMLTextAreaElement::SaveState() {
bool HTMLTextAreaElement::RestoreState(PresState* aState) {
  const PresContentData& state = aState->contentData();

  if (state.type() == PresContentData::TnsString) {
  if (state.type() == PresContentData::TTextContentData) {
    ErrorResult rv;
    SetValue(state.get_nsString(), rv);
    SetValue(state.get_TextContentData().value(), rv);
    ENSURE_SUCCESS(rv, false);
    if (state.get_TextContentData().lastValueChangeWasInteractive()) {
      mLastValueChangeWasInteractive = true;
      UpdateState(true);
    }
  }

  if (aState->disabledSet() && !aState->disabled()) {
    SetDisabled(false, IgnoreErrors());
  }
+1 −0
Original line number Diff line number Diff line
@@ -89,6 +89,7 @@ skip-if = os == "android" || (os == "mac" && debug) # Bug 1484442
[test_option_text.html]
[test_output_element.html]
[test_pattern_attribute.html]
[test_preserving_metadata_between_reloads.html]
[test_progress_element.html]
[test_radio_in_label.html]
[test_radio_radionodelist.html]
+83 −0
Original line number Diff line number Diff line
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test preserving metadata between page reloads</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css" />
  </head>
<body>
<p id="display"></p>
<div id="content">
  <iframe id="test-frame" width="800px" height="600px" srcdoc='
        <html>
          <body>
            <h3>Bug 1635224: Preserve mLastValueChangeWasInteractive between reloads</h3>
            <div>
              <form>
                <textarea id="maxlen-textarea" maxlength="2" rows="2" cols="10"></textarea><br/>
                <input id="maxlen-inputtext" type="text" maxlength="2"><br/>
                <textarea id="minlen-textarea" minlength="8" rows="2" cols="10"></textarea><br/>
                <input id="minlen-inputtext" type="text" minlength="8"><br/>
              </form>
            </div>
          </body>
        </html>
'></iframe>
</div>

<pre id="test">
<script>
  SimpleTest.waitForExplicitFinish()
  const Ci = SpecialPowers.Ci;
  const str = "aaaaa";

  function afterLoad() {
    SimpleTest.waitForFocus(function () {
      var iframeDoc = $("test-frame").contentDocument;
      var src = iframeDoc.getElementById("src");

      function test(fieldId, callback) {
        var field = iframeDoc.getElementById(fieldId);
        field.focus();
        SimpleTest.waitForClipboard(str,
          function () {
            SpecialPowers.Cc["@mozilla.org/widget/clipboardhelper;1"]
              .getService(Ci.nsIClipboardHelper)
              .copyString(str);
          },
          function () {
            synthesizeKey("v", { accelKey: true });
            is(field.value, "aaaaa", "the value of " + fieldId + " was entered correctly");
            is(field.checkValidity(), false, "the validity of " + fieldId + " should be false");
            $("test-frame").contentWindow.location.reload();
            is(field.value, "aaaaa", "the value of " + fieldId + " persisted correctly");
            is(field.checkValidity(), false, "the validity of " + fieldId + " should be false after reload");
            callback();
          },
          function () {
            ok(false, "Failed to copy the string");
            SimpleTest.finish();
          }
        );
      }

      function runNextTest() {
        if (fieldIds.length) {
          var currentFieldId = fieldIds.shift();
          test(currentFieldId, runNextTest);
        } else {
          SimpleTest.finish();
        }
      }

      var fieldIds = ["maxlen-textarea", "maxlen-inputtext", "minlen-textarea", "minlen-inputtext"];
      runNextTest();
    });
  }
  addLoadEvent(afterLoad);
</script>
</pre>
</body>
</html>
 No newline at end of file
+7 −2
Original line number Diff line number Diff line
@@ -24,9 +24,14 @@ union FileContentData {
  nsString;
};

struct TextContentData {
  nsString value;
  bool lastValueChangeWasInteractive;
};

union PresContentData {
  void_t;
  nsString;
  TextContentData;
  SelectContentData;
  CheckedContentData;
  // We can need to serialize blobs in order to transmit this type, so we need