<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin"
                 type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
                 type="text/css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=780908

adapted from test_bug607584.xul by Kent James <kent@caspia.com>
-->
<window xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        title="Mozilla Bug 780908" onload="runTest();">
  <script type="application/javascript"
    src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
  <script type="application/javascript"
          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>

  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=780908"
     target="_blank">Mozilla Bug 780908</a>
  <p/>
  <editor xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
          id="editor"
          type="content-primary"
          editortype="html"
          style="width: 400px; height: 100px; border: thin solid black"/>
  <p/>
  <pre id="test">
  </pre>
  </body>
  <script class="testbody" type="application/javascript">
  <![CDATA[

  SimpleTest.waitForExplicitFinish();

  function EditorContentListener(aEditor)
  {
    this.init(aEditor);
  }
  
  EditorContentListener.prototype = {
    init : function(aEditor)
      {
        this.mEditor = aEditor;
      },
  
    QueryInterface : function(aIID)
      {
        if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
            aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
            aIID.equals(Components.interfaces.nsISupports))
          return this;
        throw Components.results.NS_NOINTERFACE;
      },
  
    onStateChange : function(aWebProgress, aRequest, aStateFlags, aStatus)
    {
        if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP)
        {
          var editor = this.mEditor.getEditor(this.mEditor.contentWindow);
          if (editor) {
              this.mEditor.focus();
              editor instanceof Components.interfaces.nsIHTMLEditor;
              editor.returnInParagraphCreatesNewParagraph = true;
              source = "<html><body><table><head></table></body></html>";
              editor.rebuildDocumentFromSource(source);
              ok(true, "Don't crash when head appears after body");
              source = "<html></head><head><body></body></html>";
              editor.rebuildDocumentFromSource(source);
              ok(true, "Don't crash when /head appears before head");
              SimpleTest.finish();
              progress.removeProgressListener(this);
          }
        }
  
    },
  
  
    onProgressChange : function(aWebProgress, aRequest,
                                aCurSelfProgress, aMaxSelfProgress,
                                aCurTotalProgress, aMaxTotalProgress)
      {
      },
  
    onLocationChange : function(aWebProgress, aRequest, aLocation, aFlags)
      {
      },
  
    onStatusChange : function(aWebProgress, aRequest, aStatus, aMessage)
      {
      },
  
    onSecurityChange : function(aWebProgress, aRequest, aState)
      {
      },
  
      mEditor: null
  };

  var progress, progressListener;

  function runTest() {
    var newEditorElement = document.getElementById("editor");
    newEditorElement.makeEditable("html", true);
    var docShell = newEditorElement.boxObject.QueryInterface(Components.interfaces.nsIEditorBoxObject).docShell;
    progress = docShell.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIWebProgress);
    progressListener = new EditorContentListener(newEditorElement);
    progress.addProgressListener(progressListener, Components.interfaces.nsIWebProgress.NOTIFY_ALL);
    newEditorElement.setAttribute("src", "data:text/html,");
  }
]]>
</script>
</window>
