title: How to manually execute WYMeditor function w/o errors?
body: <p>For debug, watch <a href="https://github.com/wymeditor/wymeditor/blob/master/dist/wymeditor/jquery.wymeditor.js" rel="nofollow noreferrer"><code>jquery.wymeditor.js</code></a>.</p>
  
  <p>I need to execute commands like "Strong", "Indent", etc. from my custom toolbar (to be precise, ribbon) via JavaScript and without errors. 80% are done, I can execute commands, but have a strange error.</p>
  
  <p>So, what I do, when document is ready (with contained textarea "#doc"):</p>
  
  <pre><code>myDoc = $("#doc").wymeditor()[0];
  myDoc_wym = $.getWymeditorByTextarea(myDoc);
  
  setTimeout(function() {
      console.log(myDoc_wym._iframe.contentWindow); // contentWindow returns proper Window object of editor IFrame
      $(myDoc_wym._doc).keydown(function(e) {
          if (e.which == 112) {
              console.log(myDoc_wym); // must return extended jQuery editor object
              myDoc_wym.__proto__._exec("Strong"); // BREAKPOINT
              console.log("It might be done!"); // no luck today
              return false;
          }
      });
  }, 1000); // to be sure, that myDoc_wym._doc exists, I just enforced timeout
  </code></pre>
  
  <ol>
  <li>WYMeditor on half way to be fully prepared.</li>
  <li>Get its extended <code>editor</code> object with <code>getWymeditorByTextarea</code>.</li>
  <li>Wait a second.</li>
  <li>Log <code>Window</code> object of editor's IFrame.</li>
  <li><p>When we press <code>F1</code>:</p>
  
  <ol>
  <li>Log <code>myDoc_wym</code>.</li>
  <li><code>myDoc_wym.__proto__._exec("Strong");</code> - execute prototyped function <code>_exec</code> with command <code>"Strong"</code>...</li>
  </ol></li>
  </ol>
  
  <p>Here goes a crash of <code>keydown</code>. What I get in Chromium (above last released NW.JS) console:</p>
  
  <p><a href="https://i.stack.imgur.com/fgVbb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fgVbb.png" alt="Whole log"></a></p>
  
  <p>Error is caused in prototyped function <code>hasSelection</code>. So, it's saying that <code>_iframe</code> is <code>undefined</code> in that code:</p>
  
  <pre><code>WYMeditor.editor.prototype.hasSelection = function () {
      var wym = this;
  
      if (
          // `isSelectionValid` is undocumented in current Rangy (`1.2.2`).
          // It seems to be required because the `rangeCount` in `IE &lt;= 8` seems
          // to be misleading.
          rangy.isSelectionValid(wym._iframe.contentWindow) !== true
      ) {
          return false;
      }
  
      if (wym.selection().rangeCount === 0) {
          return false;
      }
  
      return true;
  };
  </code></pre>
  
  <p>After this tried to enable my own call for this function by pasting in console input similar code, but with debug needs (<code>console.log(wym);</code>):</p>
  
  <pre><code>WYMeditor.editor.prototype.hasSelection = function () {
      var wym = this;
      console.log(wym);
      //console.log(wym._iframe);
      //console.log(wym._iframe.contentWindow);
      if (rangy.isSelectionValid(wym._iframe.contentWindow) !== true) return false;
      if (wym.selection().rangeCount === 0) return false;
      return true;
  };
  </code></pre>
  
  <p>Pressing <code>F1</code>:
  <a href="https://i.stack.imgur.com/hU4eC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hU4eC.png" alt="Playing with custom function"></a></p>
  
  <ol>
  <li>First object is what I get <strong>before</strong> executing WYMeditor command.</li>
  <li>Second has no <code>_iframe</code>, so it's returning undefined.</li>
  <li>Accordingly, cannot read from undefined.</li>
  <li>It seems like <code>hasSelection</code> is called twice (for example, <code>selectedContainer</code>, that calls <code>hasSelection</code>, may be called 3 times by <code>_exec</code>). At this time we see <code>_iframe</code>, but it isn't longer needed.</li>
  </ol>
  
  <p>It turns out that:</p>
  
  <ol>
  <li>at <em>first time</em> <code>hasSelection</code> called, <strong><code>wym</code> returned as prototyped object</strong>, but...</li>
  <li>at <em>second time</em> <code>wym</code> returned as full "editor object" with needed properties (such as <code>_iframe</code>).</li>
  </ol>
  
  <p><a href="https://i.stack.imgur.com/4s8Rh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4s8Rh.png" alt="Weird object"></a></p>
  
  <p>Something weird going up there and I don't know what. When pressing on default toolbar buttons all works perfectly.</p>