editor/libeditor/tests/test_bug676401.html
author Michael Layzell <michael@thelayzells.com>
Mon, 25 May 2015 10:09:00 +0200
changeset 245650 0431f570160df1497943b8d7039b5f70c4b45e74
parent 200240 a357afff88300587c1905f01819c777f461af7eb
child 245663 fdc2aa9e612f108f85cee85f6d13c47753fd1db9
permissions -rw-r--r--
Bug 1162952 - Return true from document.queryCommandEnabled('cut'/'copy') when in privileged or user-initiated code. r=ehsan

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=676401
-->
<head>
  <title>Test for Bug 676401</title>
  <script type="application/javascript" src="/MochiKit/packed.js"></script>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=676401">Mozilla Bug 676401</a>
<p id="display"></p>
<div id="content">
  <!-- we need a blockquote to test the "outdent" command -->
  <section>
    <blockquote> not editable </blockquote>
  </section>
  <section contenteditable>
    <blockquote> editable </blockquote>
  </section>
</div>

<pre id="test">
<script type="application/javascript">

/** Test for Bug 676401 **/
SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(runTests);

var gBlock1, gBlock2;

var alwaysEnabledCommands = [
  "contentReadOnly",
  "copy",
  "cut",
  "enableInlineTableEditing",
  "enableObjectResizing",
  "insertBrOnReturn",
  "selectAll",
  "styleWithCSS",
];

function IsCommandEnabled(command) {
  var enabled;

  // non-editable div: should return false unless alwaysEnabled
  window.getSelection().selectAllChildren(gBlock1);
  enabled = document.queryCommandEnabled(command);
  is(enabled, alwaysEnabledCommands.indexOf(command) != -1,
     "'" + command + "' should not be enabled on a non-editable block.");

  // editable div: should return true
  window.getSelection().selectAllChildren(gBlock2);
  enabled = document.queryCommandEnabled(command);
  is(enabled, true, "'" + command + "' should be enabled on an editable block.");
}

function runTests() {
  var i, commands;
  gBlock1 = document.querySelector("#content section blockquote");
  gBlock2 = document.querySelector("#content [contenteditable] blockquote");

  // common commands: test with and without "styleWithCSS"
  commands = [
    "bold", "italic", "underline", "strikeThrough", 
    "subscript", "superscript", "foreColor", "backColor", "hiliteColor",
    "fontName", "fontSize",
    "justifyLeft", "justifyCenter", "justifyRight", "justifyFull",
    "indent", "outdent",
    "insertOrderedList", "insertUnorderedList", "insertParagraph",
    "heading", "formatBlock",
    "contentReadOnly", "createLink",
    "decreaseFontSize", "increaseFontSize",
    "insertHTML", "insertHorizontalRule", "insertImage",
    "removeFormat", "selectAll", "styleWithCSS"
  ];
  document.execCommand("styleWithCSS", false, false);
  for (i = 0; i < commands.length; i++)
    IsCommandEnabled(commands[i]);
  document.execCommand("styleWithCSS", false, true);
  for (i = 0; i < commands.length; i++)
    IsCommandEnabled(commands[i]);

  // Mozilla-specific stuff
  commands = ["enableInlineTableEditing", "enableObjectResizing", "insertBrOnReturn"];
  for (i = 0; i < commands.length; i++)
    IsCommandEnabled(commands[i]);

  // These are privileged, and available only to chrome.
  commands = ["paste"];
  for (i = 0; i < commands.length; i++) {
    is(document.queryCommandEnabled(commands[i]), false,
       "Command should not be enabled for non-privileged code");
    is(SpecialPowers.wrap(document).queryCommandEnabled(commands[i]), true,
       "Command should be enabled for privileged code");
    try {
      document.execCommand(commands[i], false, false);
      ok(false, "Thould have thrown: " + commands[i]);
    } catch (e) {
      ok(/insecure|denied/.test(e), "Threw correctly: " + commands[i] + " - " + e);
    }
    SpecialPowers.wrap(document).execCommand(commands[i], false, false);
  }

  // delete/undo/redo -- we have to execute this commands because:
  //  * there's nothing to undo if we haven't modified the selection first
  //  * there's nothing to redo if we haven't undone something first
  commands = ["delete", "undo", "redo"];
  for (i = 0; i < commands.length; i++) {
    IsCommandEnabled(commands[i]);
    document.execCommand(commands[i], false, false);
  }

  // done
  SimpleTest.finish();
}

</script>
</pre>
</body>
</html>