--- loncom/xml/lonxml.pm	2005/09/26 21:39:53	1.383
+++ loncom/xml/lonxml.pm	2005/11/10 21:38:27	1.388
@@ -1,7 +1,7 @@
 # The LearningOnline Network with CAPA
 # XML Parser Module 
 #
-# $Id: lonxml.pm,v 1.383 2005/09/26 21:39:53 albertel Exp $
+# $Id: lonxml.pm,v 1.388 2005/11/10 21:38:27 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -527,7 +527,13 @@ sub inner_xmlparse {
   #   $finaloutput.=&endredirection;
   # }
 
-
+  if ($target eq 'grade') { &endredirection(); }
+  if ( $Apache::lonxml::redirection ) {
+      &error("Unclean exit of parser, text still being redirected. This is likely due to there being missing end tags.");
+      while ($Apache::lonxml::redirection) {
+	  $finaloutput.=&endredirection();
+      }
+  }
   if (($ENV{'QUERY_STRING'}) && ($target eq 'web')) {
     $finaloutput=&afterburn($finaloutput);
   }	    
@@ -636,7 +642,7 @@ sub setup_globals {
     $Apache::lonxml::evaluate = 1;
     $Apache::lonxml::import = 1;
   } elsif ($target eq 'grade') {
-    &startredirection;
+    &startredirection(); #ended in inner_xmlparse on exit
     $Apache::lonxml::metamode = 0;
     $Apache::lonxml::evaluate = 1;
     $Apache::lonxml::import = 1;
@@ -926,7 +932,7 @@ sub get_all_text_unbalanced {
     $tag='<'.$tag.'>';
     while ($token = $$pars[-1]->get_token) {
 	if (($token->[0] eq 'T')||($token->[0] eq 'C')||($token->[0] eq 'D')) {
-	    if ($token->[2]) {
+	    if ($token->[0] eq 'T' && $token->[2]) {
 		$result.='<![CDATA['.$token->[1].']]>';
 	    } else {
 		$result.=$token->[1];
@@ -1109,6 +1115,14 @@ sub parstring {
   return $temp;
 }
 
+sub extlink {
+    my ($res,$exact)=@_;
+    if (!$exact) {
+	$res=&Apache::lonnet::hreflocation($Apache::lonxml::pwd[-1],$res);
+    }
+    push(@Apache::lonxml::extlinks,$res)	 
+}
+
 sub writeallows {
     unless ($#extlinks>=0) { return; }
     my $thisurl = &Apache::lonnet::clutter(shift);
@@ -1385,13 +1399,16 @@ ENDNOTFOUND
 	    }
 #
 # we are in construction space, see if edit mode forced
-            &Apache::loncommon::get_unprocessed_cgi
-                          ($ENV{'QUERY_STRING'},['editmode']);
+            &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
+						    ['editmode']);
 	}
 	if (!$env{'form.editmode'} || $env{'form.viewmode'}) {
 	    $result = &Apache::lonxml::xmlparse($request,$target,$filecontents,
 						'',%mystyle);
 	    undef($Apache::lonhomework::parsing_a_task);
+	    &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
+						    ['rawmode']);
+	    if ($env{'rawmode'}) { $result = $filecontents; }
 	}
     }
     
@@ -1439,7 +1456,12 @@ sub debug {
     if ($Apache::lonxml::debug eq "1") {
 	$|=1;
 	my $request=$Apache::lonxml::request;
-	if (!$request) { $request=Apache->request; }
+	if (!$request) {
+	    eval { $request=Apache->request; };
+	}
+	if (!$request) {
+	    eval { $request=Apache2::RequestUtil->request; };
+	}
 	$request->print('<font size="-2"><pre>DEBUG:'.&HTML::Entities::encode($_[0],'<>&"')."</pre></font>\n");
 	#&Apache::lonnet::logthis($_[0]);
     }