--- loncom/xml/londefdef.pm	2013/06/04 22:20:20	1.444
+++ loncom/xml/londefdef.pm	2024/04/14 17:12:28	1.475
@@ -1,8 +1,8 @@
 # The LearningOnline Network with CAPA
-# Tags Default Definition Module 
+# Tags Default Definition Module
+#
+# $Id: londefdef.pm,v 1.475 2024/04/14 17:12:28 raeburn Exp $
 #
-# $Id: londefdef.pm,v 1.444 2013/06/04 22:20:20 raeburn Exp $
-# 
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -25,19 +25,19 @@
 # /home/httpd/html/adm/gpl.txt
 #
 # http://www.lon-capa.org/
-## Copyright for TtHfunc and TtMfunc by Ian Hutchinson. 
-# TtHfunc and TtMfunc (the "Code") may be compiled and linked into 
-# binary executable programs or libraries distributed by the 
-# Michigan State University (the "Licensee"), but any binaries so 
+## Copyright for TtHfunc and TtMfunc by Ian Hutchinson.
+# TtHfunc and TtMfunc (the "Code") may be compiled and linked into
+# binary executable programs or libraries distributed by the
+# Michigan State University (the "Licensee"), but any binaries so
 # distributed are hereby licensed only for use in the context
-# of a program or computational system for which the Licensee is the 
-# primary author or distributor, and which performs substantial 
+# of a program or computational system for which the Licensee is the
+# primary author or distributor, and which performs substantial
 # additional tasks beyond the translation of (La)TeX into HTML.
 # The C source of the Code may not be distributed by the Licensee
 # to any other parties under any circumstances.
 #
 
-package Apache::londefdef; 
+package Apache::londefdef;
 
 use Apache::lonnet;
 use strict;
@@ -50,7 +50,7 @@ use Apache::lonlocal;
 use Apache::loncommon();
 use Apache::Constants qw(:common);
 use File::Basename;
-use LONCAPA();
+use LONCAPA;
 # use Data::Dumper;
 
 BEGIN {
@@ -92,6 +92,26 @@ sub start_m {
     my $inside = &Apache::lonxml::get_all_text_unbalanced("/m",$parser);
     if ($target eq 'web' || $target eq 'analyze') {
 	&Apache::lonxml::debug("M is starting with:$inside:");
+        if (!($inside =~ /^\s*\$.*\$\s*$/ || $inside =~ /^\s*\\[([].*\\[)\]]\s*$/)) {
+            # Non-math LaTeX will not be rendered correctly with MathJax
+            # and it should be avoided anyway.
+            # On top of that, MathJax will render math without $, but
+            # it will fail with tth. This is worth a warning.
+            # (even though some people might just use latex for printing)
+            &Apache::lonxml::warning(&mt('Missing $ in [_1].','<m>'));
+        } elsif (($env{'browser.type'} eq 'safari') && ($env{'form.editxmltext'}) &&
+                 (($env{'form.problemmode'} eq 'view') || ($env{'form.problemmode'} eq 'discard'))) {
+            my $delimiter;
+            if ($inside =~ /\$$/) {
+                $delimiter = '$';
+            } elsif ($inside =~ /\\([)\]])$/) {
+                $delimiter = $1;
+            }
+            if ($delimiter) {
+                &Apache::lonxml::warning(&mt('Insert a space between [_1] and [_2].',
+                                             $delimiter,'</m>'));
+            }
+        }
 	my $eval=&Apache::lonxml::get_param('eval',$parstack,$safeeval);
 	if ($eval eq 'on') {
 	    $inside=&Apache::run::evaluate($inside,$safeeval,$$parstack[-1]);
@@ -174,7 +194,7 @@ sub start_html {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'edit' || $target eq 'webgrade' ) {
-	# start_body() takes care of emitting the <html> 
+	# start_body() takes care of emitting the <html>
     } elsif ($target eq 'tex') {
 
 	$currentstring .= &latex_header();
@@ -197,7 +217,7 @@ sub start_head {
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
 	&Apache::lonxml::startredirection();
-    } 
+    }
     return $currentstring;
 }
 
@@ -210,7 +230,7 @@ sub end_head {
 	if ($Apache::lonxml::redirection) {
 	    $Apache::londefdef::head = &Apache::lonxml::endredirection();
 	}
-    } 
+    }
     return $currentstring;
 }
 
@@ -219,8 +239,8 @@ sub start_map {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -228,8 +248,8 @@ sub end_map {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -238,7 +258,7 @@ sub start_select {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     }  elsif ($target eq 'tex') {
 	$Apache::londefdef::select=0;
     }
@@ -249,8 +269,8 @@ sub end_select {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -259,7 +279,7 @@ sub start_option {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     } elsif ($target eq 'tex') {
 	$Apache::londefdef::select++;
 	if ($Apache::londefdef::select == 1) {
@@ -275,7 +295,7 @@ sub end_option {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
+	$currentstring = $token->[2];
     }  elsif ($target eq 'tex') {
 	$currentstring='}';
     }
@@ -287,8 +307,8 @@ sub start_input {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -296,8 +316,8 @@ sub end_input {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -306,8 +326,8 @@ sub start_textarea {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -315,8 +335,8 @@ sub end_textarea {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -325,8 +345,8 @@ sub start_form {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -334,8 +354,8 @@ sub end_form {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -344,10 +364,10 @@ sub start_title {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$Apache::londefdef::title = 
+	$Apache::londefdef::title =
 	    &Apache::lonxml::get_all_text('/title',$parser,$style);
     } elsif ($target eq 'tex') {
-	$currentstring .= '\keephidden{Title of the document:  ' 
+	$currentstring .= '\keephidden{Title of the document:  ';
     }
     if ($target eq 'meta') {
 	$currentstring='<title>';
@@ -363,11 +383,11 @@ sub end_title {
 	# start_title takes care of swallowing the title
     } elsif ($target eq 'tex') {
 	$currentstring .= '}';
-    }  
+    }
     if ($target eq 'meta') {
 	&end_output($target);
 	$currentstring='</title>';
-    } 
+    }
     return $currentstring;
 }
 
@@ -535,7 +555,7 @@ sub end_accessrule {
 	if ($args ne '') {
 	    $currentstring = $token->[4];
 	}
-    } 
+    }
     return $currentstring;
 }
 
@@ -544,7 +564,7 @@ sub generate_css_links {
     my $css_href = &Apache::lonnet::EXT('resource.0.cssfile');
     if ($css_href =~ /\S/) {
 	&Apache::lonxml::extlink($css_href);
-	$links .= 
+	$links .=
 	    '<link rel="stylesheet" type="text/css" href="'.$css_href.'" />';
     }
     return $links;
@@ -571,40 +591,60 @@ sub start_body {
     &Apache::lonhtmlcommon::clear_breadcrumbs();
     if ($env{'request.state'} eq 'construct') {
         my $url=&Apache::lonnet::hreflocation('',$env{'request.filename'});
+        my $text = 'Authoring Space';
+        my $href = &Apache::loncommon::authorspace($url);
+        if ($env{'request.course.id'}) {
+            my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+            my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+            if ($href eq "/priv/$cdom/$cnum/") {
+                $text = 'Course Authoring Space';
+            }
+        }
         &Apache::lonhtmlcommon::add_breadcrumb({
-            'text'  => 'Authoring Space',
-            'href'  => &Apache::loncommon::authorspace($url),
+            'text'  => $text,
+            'href'  => $href,
         });
         &Apache::lonhtmlcommon::add_breadcrumb({
             'text'  => 'HTML Editor',
             'href'  => '',
         });
-        # breadcrumbs (and tools) will be created 
+        # breadcrumbs (and tools) will be created
         # in start_page->bodytag->innerregister
     } else {
         # FIXME Where are we?
     }
 
-	$currentstring = 
+        my $args = {'add_entries'    => $token->[2],
+                    'force_register' => 1,};
+        if ($target eq 'web') {
+            $args->{'print_suppress'} = 1;
+            if ($env{'request.use_absolute'}) {
+                $args->{'use_absolute'} = $env{'request.use_absolute'};
+            }
+            if ($env{'form.only_body'}) {
+                $args->{'only_body'} = 1;
+            }
+        }
+	$currentstring =
 	    &Apache::loncommon::start_page($Apache::londefdef::title,
 					   $Apache::londefdef::head
-					      .$extra_head,
-					   {'add_entries'    => $token->[2],
-#					    'no_title'       => 1,
-					    'force_register' => 1});
-
+					      .$extra_head,$args);
         my $header = '';
         if ($env{'request.state'} ne 'published' &&
             $env{'request.state'} ne 'construct') {
             $header=&Apache::lonmenu::constspaceform();
         }
         if ($env{'request.state'} ne 'published') {
-            $header.=&edit_controls();
+            unless ($env{'form.inhibitmenu'} eq 'yes') {
+                $header.=&edit_controls();
+            }
         }
         if ($env{'request.state'} eq 'construct') {
-            $currentstring.=&Apache::loncommon::head_subbox(
-                                &Apache::loncommon::CSTR_pageheader()
-                               .$header);
+            unless ($env{'form.inhibitmenu'} eq 'yes') {
+                $currentstring.=&Apache::loncommon::head_subbox(
+                                    &Apache::loncommon::CSTR_pageheader()
+                                   .$header);
+            }
         } elsif ($env{'request.state'} eq 'edit') {
             $currentstring.=&Apache::loncommon::head_subbox($header);
         }
@@ -618,9 +658,10 @@ sub start_body {
 
 sub edit_controls {
     my ($nochgview) = @_;
-    my $result .= '
+    my ($is_course_doc,$is_supp,$supp_path,$supp_title);
+    my $result = '
 <form method="post" action="">
-<div class="LC_edit_problem_header">';
+<div class="LC_edit_problem_header">'."\n";
     unless ($nochgview) {
         $result .= '
 <div class="LC_edit_problem_header_row1">'.
@@ -629,7 +670,21 @@ sub edit_controls {
 </div>';
     }
     $result .= '
-<div><input type="submit" name="editmode" accesskey="e" value="'.&mt('Edit').'" />';
+<div>
+<input type="hidden" name="editmode" value="" />
+<input type="button" name="editordefault" accesskey="e" value="'.&mt('Edit').
+'" onclick="seteditmode(this.form,'."'edit'".');" />'."\n";
+    if ($env{'browser.type'} ne 'explorer' || $env{'browser.version'} > 9) {
+        my $uri = $env{'request.uri'};
+        if ($uri =~ /\.(xml|html|htm|xhtml|xhtm)$/) {
+            my %editors = &Apache::loncommon::permitted_editors($uri);
+            if ($editors{'daxe'}) {
+                my $daxeurl = '/daxepage'.$uri;
+                $result .= '<input type="button" name="editordaxe" value="'.&mt('Edit with Daxe').
+                           '" onclick="seteditmode(this.form,'."'daxe'".');" />'."\n";
+            }
+        }
+    }
     if (($env{'request.course.id'}) && ($env{'form.forceedit'})) {
         my $url=&Apache::lonnet::hreflocation('',$env{'request.filename'});
         if ($url =~ /\.html?$/i) {
@@ -637,11 +692,19 @@ sub edit_controls {
             if ($env{'request.course.id'}) {
                 $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
                 $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+                if ($url =~ m{^\Q/uploaded/$cdom/$cnum/\E(supplemental|docs)/}) {
+                    if ($1 eq 'supplemental') {
+                        $is_supp = 1;
+                    }
+                    $is_course_doc = 1;
+                }
                 if ($env{'request.filename'} =~ m{/userfiles/supplemental/default|\d+/}) {
-                    if (&Apache::lonnet::is_course_upload($env{'request.filename'},
-                                                          $cnum,$cdom)) {
+                    my $fn=&Apache::lonnet::declutter($env{'request.filename'});
+                    if (&Apache::lonnet::is_course_upload($fn,$cnum,$cdom)) {
                         &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                                                                 ['folderpath','title']);
+                        $supp_path = &escape(&HTML::Entities::decode($Apache::lonnet::env{'form.folderpath'}));
+                        $supp_title = &escape(&HTML::Entities::decode($Apache::lonnet::env{'form.title'}));
                     }
                 }
             }
@@ -669,7 +732,9 @@ sub edit_controls {
 </div>
 </form>
 ';
-    return $result;
+    my $setedit_js = &Apache::lonxml::seteditor_javascript($is_course_doc,$is_supp,
+                                                           $supp_path,$supp_title);
+    return $setedit_js."\n".$result;
 }
 
 sub end_body {
@@ -678,16 +743,16 @@ sub end_body {
     if ($target eq 'web' || $target eq 'webgrade') {
 	$currentstring .= &Apache::loncommon::end_page({'discussion' => 1});
     } elsif ($target eq 'tex') {
-	$currentstring .= '\strut\newline\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\newline\noindent \end{document}';  
-    } 
+	$currentstring .= '\strut\newline\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}\newline\noindent \end{document}';
+    }
     return $currentstring;
 }
 
-# \begin{center} causes a new paragprah spacing that looks odd inside 
+# \begin{center} causes a new paragprah spacing that looks odd inside
 # of a table cell.  Same at the end of a \center but with a slightly
 # larger space .. hence center_correction and center_end_correction.
 #
-sub center_correction { return '\vspace*{-6 mm}'; } 
+sub center_correction { return '\vspace*{-6 mm}'; }
 sub center_end_correction { return '\vspace*{-7 mm}'; }
 
 #-- <center> tag (end tag required)
@@ -695,12 +760,12 @@ sub start_center {
     my ($target,$token,$tagstack) = @_;
     my $currentstring = &end_p();	# Close off any prior para.
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring .= $token->[4];     
+	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	if (&is_inside_of($tagstack, "table")) {
 	    $currentstring .= &center_correction();
 	}
-	$currentstring .= '\begin{center}';  
+	$currentstring .= '\begin{center}';
     }
     return $currentstring;
 }
@@ -709,9 +774,9 @@ sub end_center {
     my ($target,$token,$tagstack) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];     
+	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
-	$currentstring = '\end{center}';  
+	$currentstring = '\end{center}';
 	if (&is_inside_of($tagstack, "table")) {
 	    $currentstring .= &center_end_correction();
 	}
@@ -725,11 +790,11 @@ sub start_b {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     } elsif ($target eq 'tex') {
 	&disable_para();
-	$currentstring .= '\textbf{';  
-    } 
+	$currentstring .= '\textbf{';
+    }
     return $currentstring;
 }
 
@@ -737,11 +802,11 @@ sub end_b {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];     
+	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
 	&enable_para();
 	$currentstring = '}';
-    } 
+    }
     return $currentstring;
 }
 
@@ -751,11 +816,11 @@ sub start_strong {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     } elsif ($target eq 'tex') {
 	&disable_para();
-	$currentstring = '\textbf{';  
-    } 
+	$currentstring = '\textbf{';
+    }
     return $currentstring;
 }
 
@@ -763,10 +828,10 @@ sub end_strong {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {	
-	$currentstring = $token->[2];     
+	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
 	&enable_para();
-	$currentstring = '}';  
+	$currentstring = '}';
     }
     return $currentstring;
 }
@@ -789,7 +854,7 @@ sub start_h1 {
 	}
 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
 	if (not defined $TeXsize) {$TeXsize="large";}
-	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
+	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{';
     } elsif ($target eq 'meta') {
 	$currentstring.='<subject>';
 	&start_output($target);
@@ -804,7 +869,7 @@ sub end_h1 {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	my $post='\vskip 0 mm ';
-	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
+	my $align=lc(&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1));
 	if ($align eq 'center') {
 	    $post='\end{center}';
 	} elsif ($align eq 'left') {
@@ -816,7 +881,7 @@ sub end_h1 {
     } elsif ($target eq 'meta') {
 	&end_output($target);
 	$currentstring='</subject>';
-    } 
+    }
     return $currentstring;
 }
 
@@ -838,8 +903,8 @@ sub start_h2 {
 	}
 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
 	if (not defined $TeXsize) {$TeXsize="large";}
-	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
-    } 
+	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{';
+    }
     return $currentstring;
 }
 
@@ -859,7 +924,7 @@ sub end_h2 {
 	    $post='}'.'\vskip 0 mm ';
 	}
 	$currentstring .= '}}'.$post;
-    } 
+    }
     return $currentstring;
 }
 
@@ -881,8 +946,8 @@ sub start_h3 {
 	}
 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
 	if (not defined $TeXsize) {$TeXsize="large";}
-	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
-    } 
+	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{';
+    }
     return $currentstring;
 }
 
@@ -902,7 +967,7 @@ sub end_h3 {
 	    $post='}'.'\vskip 0 mm ';
 	}
 	$currentstring .= '}}'.$post;
-    } 
+    }
     return $currentstring;
 }
 
@@ -924,8 +989,8 @@ sub start_h4 {
 	}
 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
 	if (not defined $TeXsize) {$TeXsize="large";}
-	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
-    } 
+	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{';
+    }
     return $currentstring;
 }
 
@@ -945,7 +1010,7 @@ sub end_h4 {
 	    $post='}'.'\vskip 0 mm ';
 	}
 	$currentstring .= '}}'.$post;
-    } 
+    }
     return $currentstring;
 }
 
@@ -967,8 +1032,8 @@ sub start_h5 {
 	}
 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
 	if (not defined $TeXsize) {$TeXsize="large";}
-	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
-    } 
+	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{';
+    }
     return $currentstring;
 }
 
@@ -988,7 +1053,7 @@ sub end_h5 {
 	    $post='}'.'\vskip 0 mm ';
 	}
 	$currentstring .= '}}'.$post;
-    } 
+    }
     return $currentstring;
 }
 
@@ -1010,8 +1075,8 @@ sub start_h6 {
 	}
 	my $TeXsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval,undef,0);
 	if (not defined $TeXsize) {$TeXsize="large";}
-	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{'; 
-    } 
+	$currentstring .= '\strut\newline '.$pre.'{\\'.$TeXsize.' \textbf{';
+    }
     return $currentstring;
 }
 
@@ -1031,7 +1096,7 @@ sub end_h6 {
 	    $post='}'.'\vskip 0 mm ';
 	}
 	$currentstring .= '}}'.$post;
-    } 
+    }
     return $currentstring;
 }
 
@@ -1077,7 +1142,7 @@ sub end_i {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring .= '}';
-    } 
+    }
     return $currentstring;
 }
 
@@ -1112,7 +1177,7 @@ sub start_dfn {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= '\textit{';
-    } 
+    }
     return $currentstring;
 }
 
@@ -1181,7 +1246,7 @@ sub start_code {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= '\texttt{';
-    } 
+    }
     return $currentstring;
 }
 
@@ -1192,7 +1257,7 @@ sub end_code {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring .= '}';
-    } 
+    }
     return $currentstring;
 }
 
@@ -1215,7 +1280,7 @@ sub end_em {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring .= '}';
-    } 
+    }
     return $currentstring;
 }
 
@@ -1238,7 +1303,7 @@ sub end_q {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring .= '}';
-    } 
+    }
     return $currentstring;
 }
 
@@ -1254,7 +1319,7 @@ sub end_q {
     my $closing_string = '';		# String required to close <p>
 
 #   Some tags are <p> fragile meaning that <p> inside of them
-#   does not work within TeX mode.  This is managed via the 
+#   does not work within TeX mode.  This is managed via the
 #   counter below:
 #
 
@@ -1283,6 +1348,10 @@ sub start_p {
 
 	$currentstring .= &end_p();	# close off prior para if in progress.
 	my $align=&Apache::lonxml::get_param('align',$parstack,$safeeval,undef,1);
+	if (!defined $align) {
+          # check inline CSS
+          $align = &get_css_property('text-align',$parstack,$safeeval);
+        }
 	if ($align eq 'center') {
 	    $currentstring .='\begin{center}\par ';
 	    $closing_string = '\end{center}';
@@ -1290,13 +1359,13 @@ sub start_p {
 		$currentstring = &center_correction().$currentstring;
 	    }
 	} elsif ($align eq 'right') {
-	    $currentstring.="\n".'{\flushright ';
+	    $currentstring.="\n".'\begin{flushright}';
 #	    $currentstring.='\makebox['.$env{'form.textwidth'}.']{\hfill\llap{';
-	    $closing_string= "}\n";
+	    $closing_string= '\end{flushright}'."\n";
 	} elsif ($align eq 'left') {
-	    $currentstring.= "\n".'{\flushleft ';
+	    $currentstring.= "\n".'\begin{flushleft}';
 #	    $currentstring.='\noindent\makebox['.$env{'form.textwidth'}.']{{';
-	    $closing_string = "}\n";
+	    $closing_string = '\end{flushleft}'."\n";
 	} else {
             $currentstring.='\par ';
 	    if (&is_inside_of($tagstack, 'table')) {
@@ -1350,7 +1419,7 @@ sub start_br {
 	if ($signal != 1) {
 	    $currentstring .= '\strut \\\\ \strut ';
 	}
-    
+
     }
     return $currentstring;
 }
@@ -1372,7 +1441,7 @@ sub start_big {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= '{\large ';
-    } 
+    }
     return $currentstring;
 }
 
@@ -1415,7 +1484,7 @@ sub start_basefont {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     } elsif ($target eq 'tex') {
 	my $basesize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 	if (defined $basesize) {
@@ -1429,7 +1498,7 @@ sub end_basefont {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     } elsif ($target eq 'tex') {
 	my $basesize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 	if (defined $basesize) {
@@ -1445,7 +1514,7 @@ sub start_font {
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
 	my $face=&Apache::lonxml::get_param('face',$parstack,$safeeval);
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     }  elsif ($target eq 'tex') {
 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 	if (defined $fontsize) {
@@ -1459,7 +1528,7 @@ sub end_font {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
+	$currentstring = $token->[2];
     }  elsif ($target eq 'tex') {
 	my $fontsize=&Apache::lonxml::get_param('TeXsize',$parstack,$safeeval);
 	if (defined $fontsize) {
@@ -1468,7 +1537,7 @@ sub end_font {
     }
     return $currentstring;
 }
- 
+
 #-- <strike> tag (end tag required)
 sub start_strike {
     my ($target,$token) = @_;
@@ -1477,7 +1546,7 @@ sub start_strike {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	&Apache::lonxml::startredirection();
-    } 
+    }
     return $currentstring;
 }
 
@@ -1488,8 +1557,8 @@ sub end_strike {
 	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring=&Apache::lonxml::endredirection();
-	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g; 
-	$currentstring=~s/^\s*(\S)/\\underline\{$1/; 
+	$currentstring=~s/(\S)(\s+)(\S)/$1\}$2\\underline\{$3/g;
+	$currentstring=~s/^\s*(\S)/\\underline\{$1/;
 	$currentstring=~s/(\S)\s*$/$1\}/;
     }
     return $currentstring;
@@ -1503,7 +1572,7 @@ sub start_s {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	&Apache::lonxml::startredirection();
-    } 
+    }
     return $currentstring;
 }
 
@@ -1529,7 +1598,7 @@ sub start_sub {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= '\raisebox{-\smallskipamount}{\scriptsize{';
-    } 
+    }
     return $currentstring;
 }
 
@@ -1552,7 +1621,7 @@ sub start_sup {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= '\raisebox{\smallskipamount}{\scriptsize{';
-    } 
+    }
     return $currentstring;
 }
 
@@ -1576,7 +1645,7 @@ sub start_hr {
     } elsif ($target eq 'tex') {
 
 	# <hr /> can't be inside of <sup><sub> thank you LaTeX.
-	# 
+	#
 	my $restart_sub = 0;
 	my $restart_sup = 0;
 
@@ -1586,7 +1655,7 @@ sub start_hr {
 
 	if (&is_inside_of($tagstack, "sub")) {
 	    $restart_sub = 1;
-	    $currentstring .= &end_sub($target, $token, $tagstack, 
+	    $currentstring .= &end_sub($target, $token, $tagstack,
 				       $parstack, $parser, $safeeval);
 	}
 	if (&is_inside_of($tagstack, "sup")) {
@@ -1625,7 +1694,7 @@ sub start_hr {
 	    $currentstring .= &start_sup($target, $token, $tagstack,
 					 $parstack, $parser, $safeeval);
 	}	
-    } 
+    }
     return $currentstring;
 }
 
@@ -1652,12 +1721,12 @@ sub start_div {
     my $currentstring = &end_p();	# Close enclosing para.
     if ($target eq 'web' || $target eq 'webgrade') {
 	$currentstring .= $token->[4];
-    } 
+    }
     if ($target eq 'tex') {
 	# 4 possible alignments: left, right, center, and -missing-.
         # If inside a table row, we must let the table logic
 	# do the alignment, however.
-	# 
+	#
 
 	my $endstring = '';
 
@@ -1668,7 +1737,7 @@ sub start_div {
 	    $endstring      = '\end{center}';
 	    if (&is_inside_of($tagstack, "table")) {
 		$currentstring = &center_correction().$currentstring;
-		$endstring    .= &center_end_correction(); 
+		$endstring    .= &center_end_correction();
 	    }
 	}
 	elsif ($align eq 'right') {
@@ -1724,6 +1793,15 @@ sub start_a {
                     if (!&Apache::lonnet::allowed('bre',$linkurl)) {
                         if (&Apache::lonnet::is_on_map($url)) {
                             &Apache::lonxml::extlink($linkurl);
+                        } elsif ($env{'request.course.id'}) {
+                            my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+                            my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+                            if ($linkurl =~ m{^([^/]|/uploaded/$cdom/$cnum/(docs|supplemental)/)}) {
+                                my $cleanhref = &clean_docs_httpref($linkurl,$url,$cdom,$cnum);
+                                if ($cleanhref) {
+                                    &Apache::lonxml::extlink($cleanhref);
+                                }
+                            }
                         }
                     }
                 }
@@ -1769,11 +1847,11 @@ sub start_li {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     } elsif ($target eq 'tex') {
 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 	my $value=&Apache::lonxml::get_param('value',$parstack,$safeeval,undef,0);
-	#FIXME need to support types i and I 
+	#FIXME need to support types i and I
 	if ($type=~/disc/) {
 	    $currentstring .= ' \item[$\bullet$] ';
 	} elsif ($type=~/circle/) {
@@ -1790,7 +1868,7 @@ sub start_li {
 	    $currentstring .= ' \item['.$value.'] ';
 	} else {
 	    $currentstring .= ' \item ';
-	}  
+	}
 	$Apache::londefdef::list_index++;
     }
     return $currentstring;
@@ -1800,8 +1878,8 @@ sub end_li {
     my ($target,$token) = @_;
     my $currentstring = &end_p();	# In case there's a <p> in the <li>
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring .= $token->[2];     
-    } 
+	$currentstring .= $token->[2];
+    }
     return $currentstring;
 }
 
@@ -1813,7 +1891,7 @@ sub start_u {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	&Apache::lonxml::startredirection();
-    } 
+    }
     return $currentstring;
 }
 
@@ -1836,28 +1914,28 @@ sub start_ul {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = &end_p();	# Close off enclosing list.
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring .= $token->[4];     
+	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	my $TeXtype=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 	$Apache::londefdef::list_index=0;
 	if ($TeXtype eq 'disc') {
 	    $currentstring .= '\renewcommand{\labelitemi}{$\bullet$}'.
-                              '\renewcommand{\labelitemii}{$\bullet$}'. 
+                              '\renewcommand{\labelitemii}{$\bullet$}'.
                               '\renewcommand{\labelitemiii}{$\bullet$}'.
                               '\renewcommand{\labelitemiv}{$\bullet$}';
 	} elsif ($TeXtype eq 'circle') {
 	    $currentstring .= '\renewcommand{\labelitemi}{$\circ$}'.
-                              '\renewcommand{\labelitemii}{$\circ$}'. 
+                              '\renewcommand{\labelitemii}{$\circ$}'.
                               '\renewcommand{\labelitemiii}{$\circ$}'.
                               '\renewcommand{\labelitemiv}{$\circ$}';
 	} elsif ($TeXtype eq 'square') {
 	    $currentstring .= '\renewcommand{\labelitemi}{$\diamond$}'.
-                              '\renewcommand{\labelitemii}{$\diamond$}'. 
+                              '\renewcommand{\labelitemii}{$\diamond$}'.
                               '\renewcommand{\labelitemiii}{$\diamond$}'.
                               '\renewcommand{\labelitemiv}{$\diamond$}';
 	}
-	$currentstring .= '\strut \begin{itemize}';  
-    } 
+	$currentstring .= '\strut \begin{itemize}';
+    }
     return $currentstring;
 }
 
@@ -1865,13 +1943,13 @@ sub end_ul {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];     
+	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring = '\end{itemize} \renewcommand{\labelitemi}{$\bullet$}'.
-                               '\renewcommand{\labelitemii}{$\bullet$}'. 
+                               '\renewcommand{\labelitemii}{$\bullet$}'.
                                '\renewcommand{\labelitemiii}{$\bullet$}'.
-                               '\renewcommand{\labelitemiv}{$\bullet$}\strut ';  
-    } 
+                               '\renewcommand{\labelitemiv}{$\bullet$}\strut ';
+    }
     return $currentstring;
 }
 
@@ -1880,10 +1958,10 @@ sub start_menu {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     } elsif ($target eq 'tex') {
-	$currentstring = " \\begin{itemize} ";  
-    } 
+	$currentstring = " \\begin{itemize} ";
+    }
     return $currentstring;
 }
 
@@ -1891,10 +1969,10 @@ sub end_menu {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];     
+	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
-	$currentstring = " \\end{itemize}";  
-    } 
+	$currentstring = " \\end{itemize}";
+    }
     return $currentstring;
 }
 
@@ -1903,10 +1981,10 @@ sub start_dir {
     my ($target,$token) = @_;
     my $currentstring = &end_p();	# In case there's a <p> prior to the list.
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring .= $token->[4];     
+	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
-	$currentstring .= " \\begin{itemize} ";  
-    } 
+	$currentstring .= " \\begin{itemize} ";
+    }
     return $currentstring;
 }
 
@@ -1914,10 +1992,10 @@ sub end_dir {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];     
+	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
-	$currentstring = " \\end{itemize}";  
-    } 
+	$currentstring = " \\end{itemize}";
+    }
     return $currentstring;
 }
 
@@ -1926,18 +2004,18 @@ sub start_ol {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = &end_p();	# In case there's a <p> prior to the list.
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring .= $token->[4];     
+	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$Apache::londefdef::list_index=0;
 	my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval,undef,0);
 	if ($type eq '1') {
 	    $currentstring .= '\renewcommand{\labelenumi}{\arabic{enumi}.}'.
-                              '\renewcommand{\labelenumii}{\arabic{enumii}.}'. 
+                              '\renewcommand{\labelenumii}{\arabic{enumii}.}'.
                               '\renewcommand{\labelenumiii}{\arabic{enumiii}.}'.
                               '\renewcommand{\labelenumiv}{\arabic{enumiv}.}';
 	} elsif ($type eq 'A') {
 	    $currentstring .= '\renewcommand{\labelenumi}{\Alph{enumi}.}'.
-                              '\renewcommand{\labelenumii}{\Alph{enumii}.}'. 
+                              '\renewcommand{\labelenumii}{\Alph{enumii}.}'.
                               '\renewcommand{\labelenumiii}{\Alph{enumiii}.}'.
                               '\renewcommand{\labelenumiv}{\Alph{enumiv}.}';
 	} elsif ($type eq 'a') {
@@ -1956,8 +2034,8 @@ sub start_ol {
                               '\renewcommand{\labelenumiii}{\Roman{enumiii}.}'.
                               '\renewcommand{\labelenumiv}{\Roman{enumiv}.}';
 	}
-	$currentstring .= '\strut \begin{enumerate}';  
-    } 
+	$currentstring .= '\strut \begin{enumerate}';
+    }
     return $currentstring;
 }
 
@@ -1965,13 +2043,13 @@ sub end_ol {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];     
+	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring = '\end{enumerate}\renewcommand{\labelenumi}{\arabic{enumi}.}'.
                                         '\renewcommand{\labelenumii}{\arabic{enumii}.}'.
                                         '\renewcommand{\labelenumiii}{\arabic{enumiii}.}'.
-                                        '\renewcommand{\labelenumiv}{\arabic{enumiv}.}\strut ';  
-    } 
+                                        '\renewcommand{\labelenumiv}{\arabic{enumiv}.}\strut ';
+    }
     return $currentstring;
 }
 
@@ -1980,7 +2058,7 @@ sub start_dl {
     my ($target,$token) = @_;
     my $currentstring = &end_p();	# In case there's a <p> unclosed prior to the list.
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring .= $token->[4];     
+	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= '\begin{description}';
 	$Apache::londefdef::DL++;
@@ -1988,7 +2066,7 @@ sub start_dl {
 	$Apache::londefdef::DD[$Apache::londefdef::DL]=0;
 	$Apache::londefdef::DT[$Apache::londefdef::DL]=0;
 	$Apache::londefdef::seenDT[$Apache::londefdef::DL]=0;
-    } 
+    }
     return $currentstring;
 }
 
@@ -1996,7 +2074,7 @@ sub end_dl {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];     
+	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_); }
@@ -2004,12 +2082,12 @@ sub end_dl {
 	    $currentstring.=' '.$element.' ';
 	}
 	pop(@Apache::londefdef::description);
-	$currentstring.='\end{description}';  
+	$currentstring.='\end{description}';
 	delete($Apache::londefdef::DD[$Apache::londefdef::DL]);
 	delete($Apache::londefdef::DT[$Apache::londefdef::DL]);
 	delete($Apache::londefdef::seenDT[$Apache::londefdef::DL]);
 	$Apache::londefdef::DL--;
-    } 
+    }
     return $currentstring;
 }
 
@@ -2018,14 +2096,14 @@ sub start_dt {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring='';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     } elsif ($target eq 'tex') {
 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_); }
 	&Apache::lonxml::startredirection();
 	$Apache::londefdef::DT[-1]++;
 	$Apache::londefdef::seenDT[-1]=1;
-    } 
+    }
     return $currentstring;
 }
 
@@ -2033,21 +2111,21 @@ sub end_dt {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
+	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
 	if ($Apache::londefdef::DT[-1]) {
 	    my $data=&item_cleanup();
 	    push(@{$Apache::londefdef::description[-1]},'\item['.$data.'] \strut \vskip 0mm');
 	    $Apache::londefdef::DT[-1]--;
 	}
-    } 
+    }
     return $currentstring;
 }
 
 sub item_cleanup {
     my $item=&Apache::lonxml::endredirection();
-    $item=~s/\\begin{center}//g;
-    $item=~s/\\end{center}//g;
+    $item=~s/\\begin\{center}//g;
+    $item=~s/\\end\{center}//g;
     return $item;
 }
 
@@ -2056,7 +2134,7 @@ sub start_dd {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     } elsif ($target eq 'tex') {
 	if ($Apache::londefdef::DT[-1]) { &end_dt(@_); }
 	if ($Apache::londefdef::DD[-1]) { &end_dd(@_);}
@@ -2067,7 +2145,7 @@ sub start_dd {
 	$Apache::londefdef::description[-1]->[-1].=' \strut ';
 	$Apache::londefdef::DD[-1]++;
 	&Apache::lonxml::startredirection();
-    } 
+    }
     return $currentstring;
 }
 
@@ -2075,7 +2153,7 @@ sub end_dd {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
+	$currentstring = $token->[2];
     }  elsif ($target eq 'tex') {
 	$Apache::londefdef::description[-1]->[-1].=
 	    &Apache::lonxml::endredirection().' \vskip 0mm ';
@@ -2087,7 +2165,7 @@ sub end_dd {
 #-- <table> tag (end tag required)
 #       <table> also ends any prior <p> that is not closed.
 #               but, unless I allow <p>'s to nest, that's the
-#               only way I could think of to allow <p> in 
+#               only way I could think of to allow <p> in
 #               <tr> <th> bodies
 #
 #list of supported attributes: border,width,TeXwidth,TeXtheme
@@ -2097,7 +2175,7 @@ sub start_table {
     my $textwidth = '';
     my $currentstring = &end_p();
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring .= $token->[4];     
+	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	&disable_para();	# Can't have paras in a table.
 
@@ -2165,12 +2243,12 @@ sub start_table {
     }
     return $currentstring;
 }
- 
+
 sub end_table {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];     
+	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
 	
 	
@@ -2190,7 +2268,7 @@ sub start_tr {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     } elsif ($target eq 'tex') {
 
 	my $align = &Apache::lonxml::get_param('align', $parstack, $safeeval, undef, 1);
@@ -2224,21 +2302,21 @@ sub start_tr {
 	push @ {$Apache::londefdef::table[-1]{'maxlen'}}, [];
 	push @ {$Apache::londefdef::table[-1]{'content'}}, [];
     }
-    } 
+    }
     return $currentstring;
 }
-        
+
 sub end_tr {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = &end_p();	# Close any pending <p> in the row.
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring .= $token->[2];     
+	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
 
 	# In case the user is missing a </td> or </th> tag:
 
 	if ($Apache::londefdef::TD_redirection) {
-	    &end_td_tex($parstack,$parser,$safeeval);    
+	    &end_td_tex($parstack,$parser,$safeeval);
 	}
 	$Apache::londefdef::table[-1]->end_row();
 
@@ -2248,7 +2326,7 @@ sub end_tr {
 
 	if (0) {
 	if ($Apache::londefdef::TD_redirection) {
-	    &end_td_tex($parstack,$parser,$safeeval);    
+	    &end_td_tex($parstack,$parser,$safeeval);
 	}
 	# Counter columns must be the maximum number of columns seen
 	# in the table so far so:
@@ -2267,17 +2345,17 @@ sub start_td {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     } elsif ($target eq 'tex') {
 	$Apache::londefdef::TD_redirection = 1;
 	&tag_check('tr','td',$tagstack,$parstack,$parser,$safeeval);
-    } 
+    }
     return $currentstring;
-}   
-    
+}
+
 sub tag_check {
     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
-    my @ar=@$parstack; 
+    my @ar=@$parstack;
     for (my $i=$#ar-1;$i>=0;$i--) {
 	if (lc($$tagstack[$i]) eq $good_tag) {
 	    &start_td_tex($parstack,$parser,$safeeval);
@@ -2299,7 +2377,7 @@ sub tag_check {
 sub cell_config_hash {
     my ($align, $rowspan, $colspan, $width) = @_;
     if ($rowspan ne '') {
-        $rowspan =~ s/^\s+|\s+$//g; 
+        $rowspan =~ s/^\s+|\s+$//g;
     }
     if ($colspan ne '') {
         $colspan =~ s/^\s+|\s+$//g;
@@ -2319,7 +2397,7 @@ sub cell_config_hash {
     }
     return \%config;
 }
- 
+
 sub start_td_tex {
     my ($parstack,$parser,$safeeval) = @_;
 
@@ -2336,7 +2414,7 @@ sub start_td_tex {
 
     my $table = $Apache::londefdef::table[-1];
     $table->add_cell('', $config);
-    
+
 
     #------------------------------------------------
     #  Old table code.
@@ -2374,7 +2452,7 @@ sub end_td_tex {
     if (0) {
     my ($parstack,$parser,$safeeval) = @_;
     my $current_row    = $Apache::londefdef::table[-1]{'row_number'};
-    my $current_column = $Apache::londefdef::table[-1]{'counter_columns'}; 
+    my $current_column = $Apache::londefdef::table[-1]{'counter_columns'};
     my $data = &Apache::lonxml::endredirection();
 
     #  The rowspan array of the table indicates which cells are part of a span.
@@ -2384,14 +2462,14 @@ sub end_td_tex {
     #  If this and subsequent cells are part of a rowspan, we must
     #  push along the row until we find one that is not.
 
-    while ((defined $Apache::londefdef::table[-1]{'rowspan'}[$current_row] [$current_column]) 
+    while ((defined $Apache::londefdef::table[-1]{'rowspan'}[$current_row] [$current_column])
 	   && ($Apache::londefdef::table[-1]{'rowspan'}[$current_row][$current_column] =~ /[\^\_]/)) {
 	# Part of a span.
 	push @ {$Apache::londefdef::table[-1]{'content'}[-1]}, '';
 	$current_column++;
     }
     $Apache::londefdef::table[-1]{'counter_columns'} = $current_column;
-   
+
 
     # Get the column and row spans.
     # Colspan can be done via \multicolumn if I can figure out the data structs.
@@ -2467,9 +2545,9 @@ sub end_td_tex {
 		push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 		push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 	    }
-	    $data=~s/\\\\\s*$//; 
-	} else {  
-	    $data=~s/^\s+(\S.*)/$1/; 
+	    $data=~s/\\\\\s*$//;
+	} else {
+	    $data=~s/^\s+(\S.*)/$1/;
 	    $data=~s/(.*\S)\s+$/$1/;
 	    $data=~s/(\s)+/$1/;
 	    my ($current_length,$min_length)=(0,0);
@@ -2500,13 +2578,13 @@ sub end_td_tex {
 		push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
 		push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
 	    }
-	}        
+	}
     }
     # Substitute all of the tables nested in this cell in their appropriate places.
 
 
     my $nested_count = $#{$Apache::londefdef::table[-1]{'include'}}; # This one is constant...
-    for (my $in=0; $in<=$nested_count; $in++) {    
+    for (my $in=0; $in<=$nested_count; $in++) {
 	my $nested = shift @{$Apache::londefdef::table[-1]{'include'}};
 	$nested =~ s/\\end\{tabular\}\\strut\\\\/\\end\{tabular\}/;
 	# $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
@@ -2545,7 +2623,7 @@ sub end_td {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];     
+	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
         $Apache::londefdef::TD_redirection =0;
 	&end_td_tex($parstack,$parser,$safeeval);
@@ -2558,17 +2636,17 @@ sub start_th {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     } elsif ($target eq 'tex') {
 	$Apache::londefdef::TD_redirection = 1;
 	&tagg_check('tr','th',$tagstack,$parstack,$parser,$safeeval);
-    } 
+    }
     return $currentstring;
-}   
-    
+}
+
 sub tagg_check {
     my ($good_tag,$bad_tag,$tagstack,$parstack,$parser,$safeeval) = @_;
-    my @ar=@$parstack; 
+    my @ar=@$parstack;
     for (my $i=$#ar-1;$i>=0;$i--) {
 	if (lc($$tagstack[$i]) eq $good_tag) {
 	    &start_th_tex($parstack,$parser,$safeeval);
@@ -2582,7 +2660,7 @@ sub tagg_check {
     }
     return '';
 }
- 
+
 sub start_th_tex {
     my ($parstack,$parser,$safeeval) = @_;
 
@@ -2661,8 +2739,8 @@ sub end_th_tex {
 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$fwidth;
 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
-	} else {  
-	    $data=~s/^\s+(\S.*)/$1/; 
+	} else {
+	    $data=~s/^\s+(\S.*)/$1/;
 	    $data=~s/(.*\S)\s+$/$1/;
 	    $data=~s/(\s)+/$1/;
 	    my ($current_length,$min_length)=(0,0);
@@ -2691,9 +2769,9 @@ sub end_th_tex {
 	    push @ {$Apache::londefdef::table[-1]{'objectlen'}[$Apache::londefdef::table[-1]{'row_number'}] },'0';
 	    push @ {$Apache::londefdef::table[-1]{'maxlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$current_length;
 	    push @ {$Apache::londefdef::table[-1]{'minlen'}[$Apache::londefdef::table[-1]{'row_number'}] },$min_length;
-	}        
+	}
     }
-	for (my $in=0; $in<=$#{$Apache::londefdef::table[-1]{'include'}};$in++) {         
+	for (my $in=0; $in<=$#{$Apache::londefdef::table[-1]{'include'}};$in++) {
 	    $data=~s/\\keephidden\{NEW TABLE ENTRY\}/$Apache::londefdef::table[-1]{'include'}[$in]/;
 	}
     #make data bold
@@ -2707,47 +2785,51 @@ sub end_th {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = &end_p();	# Close any open <p> in the row.
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring .= $token->[2];     
+	$currentstring .= $token->[2];
     } elsif ($target eq 'tex') {
         $Apache::londefdef::TD_redirection =0;
 	&end_th_tex($parstack,$parser,$safeeval);
     }
     return $currentstring;
 }
-     
+
 #-- <img> tag (end tag forbidden)
 #
 #  Render the <IMG> tag.
-#     <IMG> has the following attributes (in addition to the 
+#     <IMG> has the following attributes (in addition to the
 #     standard HTML ones:
 #      TeXwrap   - Governs how the tex target will try to wrap text around
 #                  horizontally aligned images.
 #      TeXwidth  - The width of the image when rendered for print (mm).
 #      TeXheight - The height of the image when rendered for print (mm)
 #         (Note there seems to also be support for this as a % of page size)
-#      
+#
 sub start_img {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style) = @_;
     my $src = &Apache::lonxml::get_param('src',$parstack,$safeeval,
 					 undef,1);
-    if (! $src && 
+    if (! $src &&
 	($target eq 'web' || $target eq 'webgrade' || $target eq 'tex')
-	) { 
+	) {
 	my $inside = &Apache::lonxml::get_all_text("/img",$parser,$style);
 	return '';
     }
-    &Apache::lonxml::extlink($src);
+    unless ($src =~ m{^data\:image/gif;base64,}) {
+        &Apache::lonxml::extlink($src);
+    }
     my $currentstring = '';
     my $scaling = .3;
 
    # Render unto browsers that which are the browser's...
 
     if ($target eq 'web' || $target eq 'webgrade') {
-        my $enc = ('yes' eq 
+        my $enc = ('yes' eq
                    lc(&Apache::lonxml::get_param('encrypturl',$parstack,
                       $safeeval)));
-        $currentstring.=&Apache::lonenc::encrypt_ref($token,{'src'=>$src},
-                         $enc);
+        unless ($src =~ m{^data\:image/gif;base64,}) {
+            $currentstring.=&Apache::lonenc::encrypt_ref($token,{'src'=>$src},
+                            $enc);
+        }
 
     # and render unto TeX that which is LaTeX
     } elsif ($target eq 'tex') {
@@ -2757,7 +2839,7 @@ sub start_img {
 	#  simulate the alignments offered by html.
 	#
 	#
-	my $align = lc(&Apache::lonxml::get_param('align', 
+	my $align = lc(&Apache::lonxml::get_param('align',
 						  $parstack,
 						  $safeeval,
 						  undef,1));
@@ -2767,7 +2849,7 @@ sub start_img {
 	#
 	&Apache::lonxml::debug("Alignemnt = $align");
 	#  LaTeX's image/text wrapping is really bad since it wants to
-	#  make figures float.  
+	#  make figures float.
         #   The user has the optional parameter (applicable only to l/r
 	# alignment to use the picins/parpic directive to get wrapped text
 	# this is also imperfect.. that's why we give them a choice...
@@ -2790,7 +2872,7 @@ sub start_img {
 
 	# &Apache::lonxml::debug("LaTeX rendering = $latex_rendering image file: $src");
 
-	#if original bmp/gif/jpg/png file exist do following:
+	#if original bmp/gif/jpg/png/svg file exist do following:
 	my $origsrc=$src;
 	my ($path,$file) = &get_eps_image($src);
 	# &Apache::lonnet::logthis("Image source: $src result: $path $file");
@@ -2804,25 +2886,25 @@ sub start_img {
 	    if ($width_param)  { $size.='width='.$width_param.' mm,'; }
 	    if ($height_param) { $size.='height='.$height_param.' mm]'; }
 	    # Default size if not able to extract that (e.g. eps image).
-	    
+
 	    # &Apache::lonnet::logthis("Size = $size");
-	    
+
 	    $size='['.$size;
-	    $size=~s/,$/]/; 
+	    $size=~s/,$/]/;
 	    $currentstring .= '\graphicspath{{'.$path.'}}'
 		.'\includegraphics'.$size.'{'.$file.'} ';
 	    my $closure;
-	    ($currentstring, $closure) = &align_latex_image($align, 
-							    $latex_rendering, 
-							    $currentstring, 
-							    $width_param, 
+	    ($currentstring, $closure) = &align_latex_image($align,
+							    $latex_rendering,
+							    $currentstring,
+							    $width_param,
 							    $height_param);
 	    $currentstring .= $closure;
-						
+
 	} else {
 	    &Apache::lonxml::debug("$src does not exist");
 	    #original image file doesn't exist so check the alt attribute
-	    my $alt = 
+	    my $alt =
 		&Apache::lonxml::get_param('alt',$parstack,$safeeval,undef,1);
 	    unless ($alt) {
 		$alt=&Apache::lonmeta::alttag($Apache::lonxml::pwd[-1],$src);
@@ -2837,8 +2919,8 @@ sub start_img {
         my $only = join(',',&Apache::loncommon::filecategorytypes('Pictures'));
 	$currentstring .=&Apache::edit::tag_start($target,$token);
 	$currentstring .=&Apache::edit::text_arg('Image Url:','src',$token,70).
-	    &Apache::edit::browse('src',undef,'alt',$only).' '.
-	    &Apache::edit::search('src',undef,'alt').'<br />';
+                         &Apache::edit::browse_or_search('src',undef,'alt',$only,undef,1).
+                         '<br />';
 	$currentstring .=&Apache::edit::text_arg('Description:','alt',$token,70).'<br />';
 	$currentstring .=&Apache::edit::text_arg('width (pixel):','width',$token,5);
 	$currentstring .=&Apache::edit::text_arg('height (pixel):','height',$token,5).'<br />';
@@ -2850,7 +2932,7 @@ sub start_img {
 						   ['', 'none','parbox', 'parpic', 'wrapfigure'], $token, 2);
         my $alt=    &Apache::lonxml::get_param('alt',$parstack,$safeeval);
         my $enc=    &Apache::lonxml::get_param('encrypturl',$parstack,$safeeval);
- 
+
 	$currentstring .=&Apache::edit::select_arg('Encrypt URL:','encrypturl',
 						   ['no','yes'], $token, 2);
         if (($alt=~/\S/) && (lc($enc) eq 'yes')) {
@@ -2860,17 +2942,19 @@ sub start_img {
 	my $src=    &Apache::lonxml::get_param('src',$parstack,$safeeval);
 	my $width=  &Apache::lonxml::get_param('width',$parstack,$safeeval);
 	my $height= &Apache::lonxml::get_param('height',$parstack,$safeeval);
-
+        my $element = &Apache::edit::get_element('src');
+        my $text;
         if ($token->[2]{'src'}=~/\$/) {
-           $currentstring.=&mt('Variable image source');
+           $text = &mt('Variable image source');
         } elsif ($token->[2]{'src'}=~/\S/) {
 	   $currentstring .= '<img src="'.$src.'" alt="'.$alt.'" ';
 	   if ($width) { $currentstring.=' width="'.$width.'" '; }
 	   if ($height) { $currentstring.=' height="'.$height.'" '; }
-	   $currentstring .= ' />';
+	   $currentstring .= ' id="previewimg_'.$element.'" />';
         } else {
-           $currentstring.=&mt("No image source specified");
+           $text = &mt("No image source specified");
         }
+        $currentstring .= ' <span id="showimg_'.$element.'">'.$text.'</span>';
     } elsif ($target eq 'modified') {
 	my ($osrc,$owidth,$oheight)=
 	    ($token->[2]{'src'},$token->[2]{'width'},$token->[2]{'height'});
@@ -2891,7 +2975,7 @@ sub start_img {
 	}
 	if ($osrc ne $nsrc || (!$nwidth && !$nheight)) {
 	    # changed image or no size specified,
-            # if they didn't explicitly change the 
+            # if they didn't explicitly change the
             # width or height use the ones from the image
 	    if ($iwidth && $iheight) {
 		if ($owidth == $nwidth || (!$nwidth && !$nheight)) {
@@ -2930,7 +3014,7 @@ sub end_img {
 #-- <applet> tag (end tag required)
 sub start_applet {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
-    
+
     my $code=&Apache::lonxml::get_param('code',$parstack,$safeeval,undef,1);
     &Apache::lonxml::extlink($code);
     my $archive=&Apache::lonxml::get_param('archive',$parstack,$safeeval,
@@ -2951,7 +3035,7 @@ sub start_applet {
 
 	if (&is_inside_of($tagstack, "sub")) {
 	    $restart_sub = 1;
-	    $currentstring .= &end_sub($target, $token, $tagstack, 
+	    $currentstring .= &end_sub($target, $token, $tagstack,
 				       $parstack, $parser, $safeeval);
 	}
 	if (&is_inside_of($tagstack, "sup")) {
@@ -2983,7 +3067,7 @@ sub start_applet {
 	    $currentstring .= &start_sup($target, $token, $tagstack,
 					 $parstack, $parser, $safeeval);
 	}
-    } 
+    }
     return $currentstring;
 }
 
@@ -2993,20 +3077,20 @@ sub end_applet {
     if ($target eq 'web' || $target eq 'webgrade') {
 	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
-    } 
+    }
     return $currentstring;
 }
 
 #-- <embed> tag (end tag optional/required)
-sub start_embed {    
+sub start_embed {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $src=&Apache::lonxml::get_param('src',$parstack,$safeeval,undef,1);
     &Apache::lonxml::extlink($src);
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-    $currentstring=&Apache::lonenc::encrypt_ref($token,{'src'=>$src}); 
+    $currentstring=&Apache::lonenc::encrypt_ref($token,{'src'=>$src});
     } elsif ($target eq 'tex') {
-    } 
+    }
     return $currentstring;
 }
 
@@ -3071,7 +3155,7 @@ sub start_param {
 	}
 	$currentstring = &Apache::lonenc::encrypt_ref($token,\%toconvert);
     } elsif ($target eq 'tex') {
-    } 
+    }
     return $currentstring;
 }
 
@@ -3079,9 +3163,9 @@ sub end_param {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];     
+	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
-    } 
+    }
     return $currentstring;
 }
 
@@ -3116,8 +3200,8 @@ sub end_allow {
 sub start_frameset {
     my ($target,$token) = @_;
     my $currentstring = '';	# Close any pending para.
-    if ($target eq 'web' || $target eq 'webgrade') { 
-	$currentstring = 
+    if ($target eq 'web' || $target eq 'webgrade') {
+	$currentstring =
 	    &Apache::loncommon::start_page($Apache::londefdef::title,
 					   $Apache::londefdef::head,
 					   {'add_entries'    => $token->[2],
@@ -3146,7 +3230,7 @@ sub start_xmp {
 	$currentstring .= $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring .= '\begin{verbatim}';
-    } 
+    }
     return $currentstring;
 }
 
@@ -3170,7 +3254,7 @@ sub start_pre {
     } elsif ($target eq 'tex') {
 	$currentstring .= '\begin{verbatim}';
 	&Apache::lonxml::disable_LaTeX_substitutions();
-    } 
+    }
     return $currentstring;
 }
 
@@ -3226,12 +3310,12 @@ sub end_externallink {
     return $currentstring;
 }
 
-#-- <blankspace heigth="">
+#-- <blankspace height="">
 sub start_blankspace {
     my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = &end_p();	# closes off any unclosed <p>
     if ($target eq 'tex') {
-	my $howmuch = &Apache::lonxml::get_param('heigth',$parstack,$safeeval,undef,1);
+	my $howmuch = &Apache::lonxml::get_param('height',$parstack,$safeeval,undef,1);
 	$currentstring .= '\vskip '.$howmuch.' ';
     }
     return $currentstring;
@@ -3251,8 +3335,8 @@ sub start_abbr {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3260,8 +3344,8 @@ sub end_abbr {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3270,8 +3354,8 @@ sub start_acronym {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3279,8 +3363,8 @@ sub end_acronym {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3289,8 +3373,8 @@ sub start_area {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3298,8 +3382,8 @@ sub end_area {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3308,7 +3392,7 @@ sub start_base {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     }
     return $currentstring;
 }
@@ -3317,8 +3401,8 @@ sub end_base {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3327,8 +3411,8 @@ sub start_bdo {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3336,8 +3420,8 @@ sub end_bdo {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3346,8 +3430,8 @@ sub start_bgsound {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3355,8 +3439,8 @@ sub end_bgsound {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3365,8 +3449,8 @@ sub start_blink {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3374,8 +3458,8 @@ sub end_blink {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3384,8 +3468,8 @@ sub start_blockquote {
     my ($target,$token) = @_;
     my $currentstring = &end_p();	# Close any unclosed <p>
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring .= $token->[4];     
-    } 
+	$currentstring .= $token->[4];
+    }
     if ($target eq 'tex') {
 	$currentstring .= '\begin{quote}';
     }
@@ -3396,8 +3480,8 @@ sub end_blockquote {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     if ($target eq 'tex') {
 	$currentstring = '\end{quote}';
     }
@@ -3409,8 +3493,8 @@ sub start_button {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3418,8 +3502,8 @@ sub end_button {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3428,8 +3512,8 @@ sub start_caption {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3437,8 +3521,8 @@ sub end_caption {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3447,8 +3531,8 @@ sub start_col {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3456,8 +3540,8 @@ sub end_col {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3466,8 +3550,8 @@ sub start_colgroup {
     my ($target,$token,$tagstack, $parstack, $parser, $safeeval, $style) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     if ($target eq 'tex') {
 	# TODO: Ensure this tag is in a table:
 
@@ -3496,8 +3580,8 @@ sub end_colgroup {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3507,11 +3591,11 @@ sub start_del {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     } elsif ($target eq 'tex') {
 	&disable_para();
-	$currentstring .= '\st{';  
-    } 
+	$currentstring .= '\st{';
+    }
     return $currentstring;
 }
 
@@ -3519,11 +3603,11 @@ sub end_del {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];     
+	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
 	&enable_para();
 	$currentstring = '}';
-    } 
+    }
     return $currentstring;
 }
 
@@ -3532,8 +3616,8 @@ sub start_fieldset {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3541,8 +3625,8 @@ sub end_fieldset {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3551,8 +3635,8 @@ sub start_frame {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3560,8 +3644,8 @@ sub end_frame {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3570,8 +3654,68 @@ sub start_iframe {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+        my ($src,$url,$query);
+        if ($token->[2]->{'src'}) {
+            $src = $token->[2]->{'src'};
+        } elsif ($token->[2]->{'SRC'}) {
+            $src = $token->[2]->{'SRC'};
+        }
+        if ($src) {
+            ($url,$query) = ($src =~ /^([^?]+)\??([^?]*)$/);
+            if ($query =~ /inhibitmenu=yes/) {
+                $currentstring = $token->[4];
+            } else {
+                my $inhibit;
+                if ($url =~ m{^[^/.].*\.x?html?$}) {
+                    $inhibit = 1;
+                } elsif ($url =~ m{^/(uploaded|res)/.*\.x?html?$}) {
+                    $inhibit = 1;
+                }
+                if ($inhibit) {
+                    $currentstring = '<iframe ';
+                    foreach my $attrib (@{$token->[3]}) {
+                        if (lc($attrib) eq 'src') {
+                            if ($query) {
+                                $query.='&amp;inhibitmenu=yes';
+                            } else {
+                                $query = 'inhibitmenu=yes';
+                            }
+                            $currentstring .= 'src="'.$url.'?'.$query.'" ';
+                        } else {
+                            $currentstring .= lc($attrib).'="'.$token->[2]->{$attrib}.'" ';
+                        }
+                    }
+                    $currentstring =~ s/\s+$//;
+                    if ($token->[4] =~ m{/>$}) {
+                        $currentstring .= ' />';
+                    } else {
+                        $currentstring .= '>';
+                    }
+                } else {
+                    $currentstring = $token->[4];
+                }
+            }
+            if (($url !~ m{^https?://}) && ($env{'request.course.id'})) {
+                my $docuri = &Apache::lonnet::hreflocation('',$env{'request.filename'});
+                my $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+                my $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+                if ($url =~ m{^([^/]|/uploaded/)}) {
+                    my $cleanhref = &clean_docs_httpref($url,$docuri,$cdom,$cnum);
+                    if ($cleanhref) {
+                        &Apache::lonxml::extlink($cleanhref);
+                    }
+                } elsif (($url =~ m{/res/$LONCAPA::domain_re/}) && ($docuri =~ m{^\Q/uploaded/$cdom/$cnum/docs/\E})) {
+                    if (!&Apache::lonnet::allowed('bre',$url)) {
+                        if (&Apache::lonnet::is_on_map($url)) {
+                            &Apache::lonxml::extlink($url);
+                        }
+                    }
+                }
+            }
+        } else {
+            $currentstring = $token->[4];
+        }
+    }
     return $currentstring;
 }
 
@@ -3579,8 +3723,8 @@ sub end_iframe {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3589,8 +3733,8 @@ sub start_ins {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3598,8 +3742,8 @@ sub end_ins {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3608,8 +3752,8 @@ sub start_isindex {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3617,8 +3761,8 @@ sub end_isindex {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3627,8 +3771,8 @@ sub start_keygen {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3636,8 +3780,8 @@ sub end_keygen {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3646,8 +3790,8 @@ sub start_label {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3655,8 +3799,8 @@ sub end_label {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3665,8 +3809,8 @@ sub start_layer {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3674,8 +3818,8 @@ sub end_layer {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3684,8 +3828,8 @@ sub start_legend {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3693,8 +3837,8 @@ sub end_legend {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3706,8 +3850,8 @@ sub start_link {
 	my $href=&Apache::lonxml::get_param('href',$parstack,$safeeval,
 					    undef,1);
 	&Apache::lonxml::extlink($href);
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3715,8 +3859,8 @@ sub end_link {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3725,8 +3869,8 @@ sub start_marquee {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3734,8 +3878,8 @@ sub end_marquee {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3744,8 +3888,8 @@ sub start_multicol {
     my ($target,$token) = @_;
     my $currentstring = &end_p();	# Close any pending <p>
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring .= $token->[4];     
-    } 
+	$currentstring .= $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3753,8 +3897,8 @@ sub end_multicol {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3763,7 +3907,7 @@ sub start_nobr {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     }  elsif ($target eq 'tex') {
 	$currentstring='\mbox{';
     }
@@ -3774,7 +3918,7 @@ sub end_nobr {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
+	$currentstring = $token->[2];
     }   elsif ($target eq 'tex') {
 	$currentstring='}';
     }
@@ -3786,8 +3930,8 @@ sub start_noembed {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3795,8 +3939,8 @@ sub end_noembed {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3805,8 +3949,8 @@ sub start_noframes {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3814,8 +3958,8 @@ sub end_noframes {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3824,8 +3968,8 @@ sub start_nolayer {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3833,8 +3977,8 @@ sub end_nolayer {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3843,8 +3987,8 @@ sub start_noscript {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3852,8 +3996,8 @@ sub end_noscript {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3862,8 +4006,8 @@ sub start_object {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3871,8 +4015,8 @@ sub end_object {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3881,8 +4025,8 @@ sub start_optgroup {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3890,8 +4034,8 @@ sub end_optgroup {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3900,7 +4044,7 @@ sub start_samp {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     } elsif ($target eq 'tex') {
 	$currentstring='\texttt{';
     }
@@ -3911,7 +4055,7 @@ sub end_samp {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
+	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
 	$currentstring='}';
     }
@@ -3923,8 +4067,8 @@ sub start_server {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3932,8 +4076,8 @@ sub end_server {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -3942,8 +4086,8 @@ sub start_spacer {
     my ($target,$token) = @_;
     my $currentstring = &end_p();	# Close off any open <p> tag.
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring .= $token->[4];     
-    } 
+	$currentstring .= $token->[4];
+    }
     return $currentstring;
 }
 
@@ -3951,18 +4095,38 @@ sub end_spacer {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
+my @span_end_stack; # for span tex target
+
 #-- <span> tag (end tag required)
 sub start_span {
-    my ($target,$token) = @_;
+    my ($target,$token,$tagstack,$parstack,$parser,$safeeval) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    } elsif ($target eq 'tex') {
+        my $endstring = '';
+        my $family = &get_css_property('font-family',$parstack,$safeeval);
+        if ($family eq 'monospace') {
+            $currentstring .= '\texttt{';
+            $endstring .= '}';
+        }
+        my $weight = &get_css_property('font-weight',$parstack,$safeeval);
+        if ($weight eq 'bold') {
+            $currentstring .= '\textbf{';
+            $endstring .= '}';
+        }
+        my $style = &get_css_property('font-style',$parstack,$safeeval);
+        if ($style eq 'italic') {
+            $currentstring .= '\textit{';
+            $endstring .= '}';
+        }
+        push(@span_end_stack, $endstring);
+    }
     return $currentstring;
 }
 
@@ -3970,8 +4134,11 @@ sub end_span {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    } elsif ($target eq 'tex') {
+        my $endstring = pop @span_end_stack;
+        $currentstring .= $endstring;
+    }
     return $currentstring;
 }
 
@@ -3980,8 +4147,8 @@ sub start_tbody {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     if ($target eq 'tex') {
 	# TODO: Ensure this tag is within a table:
 
@@ -3995,8 +4162,8 @@ sub end_tbody {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     if($target eq 'tex') {
 	# TODO: Ensure this tag is within a table:
 
@@ -4011,8 +4178,8 @@ sub start_tfoot {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     if ($target eq 'tex') {
         # TODO: ensure this is within a table tag.
 	my $table = $Apache::londefdef::table[-1];
@@ -4025,10 +4192,10 @@ sub end_tfoot {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     if ($target eq 'tex') {
-	#  TODO: Ensure this is in side a table 
+	#  TODO: Ensure this is in side a table
 	my $table = $Apache::londefdef::table[-1];
 	$table->end_foot();
     }
@@ -4040,8 +4207,8 @@ sub start_thead {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     if ($target eq 'tex') {
 	# Assume we're in a table... TODO: Verify that and ignore tag if not.
 	my $table = $Apache::londefdef::table[-1];
@@ -4054,8 +4221,8 @@ sub end_thead {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     if ($target eq 'tex') {
      	# TODO: Verify we are in a table and ignore tag if not.
 
@@ -4070,9 +4237,9 @@ sub start_var {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
+	$currentstring = $token->[4];
     } elsif ($target eq 'tex') {
-	$currentstring = '\textit{'; 
+	$currentstring = '\textit{';
     }
     return $currentstring;
 }
@@ -4083,8 +4250,8 @@ sub end_var {
     if ($target eq 'web' || $target eq 'webgrade') {
 	$currentstring = $token->[2];
     } elsif ($target eq 'tex') {
-	$currentstring = '}'; 
-    } 
+	$currentstring = '}';
+    }
     return $currentstring;
 }
 
@@ -4093,8 +4260,8 @@ sub start_wbr {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[4];     
-    } 
+	$currentstring = $token->[4];
+    }
     return $currentstring;
 }
 
@@ -4102,8 +4269,8 @@ sub end_wbr {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = $token->[2];    
-    } 
+	$currentstring = $token->[2];
+    }
     return $currentstring;
 }
 
@@ -4111,8 +4278,8 @@ sub end_wbr {
 sub start_hideweboutput {
     my ($target,$token) = @_;
     if ($target eq 'web' || $target eq 'webgrade') {
-	&Apache::lonxml::startredirection();     
-    } 
+	&Apache::lonxml::startredirection();
+    }
     return '';
 }
 
@@ -4120,8 +4287,8 @@ sub end_hideweboutput {
     my ($target,$token) = @_;
     my $currentstring = '';
     if ($target eq 'web' || $target eq 'webgrade') {
-	$currentstring = &Apache::lonxml::endredirection();    
-    } 
+	$currentstring = &Apache::lonxml::endredirection();
+    }
     return '';
 }
 
@@ -4129,7 +4296,7 @@ sub end_hideweboutput {
 sub image_replication {
     my $src = shift;
     if (not -e $src) { &Apache::lonnet::repcopy($src); }
-    #replicates eps or ps 
+    #replicates eps or ps
     my $epssrc = my $pssrc = $src;
     $epssrc =~ s/\.(gif|jpg|jpeg|png)$/.eps/i;
     $pssrc  =~ s/\.(gif|jpg|jpeg|png)$/.ps/i;
@@ -4152,9 +4319,9 @@ sub resize_image {
     $width_param  = $width_param  * $scaling;
 
     #do we have any specified LaTeX size of the picture?
-    my $toget='TeXwidth'; 
-    if ($cis) { 
-	$toget=lc($toget); 
+    my $toget='TeXwidth';
+    if ($cis) {
+	$toget=lc($toget);
     }
     my $TeXwidth = &Apache::lonxml::get_param($toget,$parstack,
 					      $safeeval,$depth,$cis);
@@ -4164,11 +4331,11 @@ sub resize_image {
     #do we have any specified web size of the picture?
     my $width = &Apache::lonxml::get_param('width',$parstack,$safeeval,
 					   $depth,1);
-    if ($TeXwidth) { 
+    if ($TeXwidth) {
 	my $old_width_param=$width_param;
 	if ($TeXwidth=~/(\d+)\s*\%/) {
 	    $width_param = $1*$env{'form.textwidth'}/100;
-	} else { 
+	} else {
 	    $width_param = $TeXwidth;
 	}
 	if ($TeXheight) {
@@ -4177,10 +4344,10 @@ sub resize_image {
 	    $height_param=$TeXwidth/$old_width_param*$height_param;
 	}
     } elsif ($TeXheight) {
-	$height_param = $TeXheight;
 	if ($height_param) {
 	    $width_param  = $TeXheight/$height_param*$width_param;
 	}
+	$height_param = $TeXheight;
     } elsif ($width) {
 	my $old_width_param=$width_param;
 	$width_param = $width*$scaling;
@@ -4202,7 +4369,7 @@ sub resize_image {
 sub image_size {
     my ($src,$scaling,$parstack,$safeeval,$depth,$cis)=@_;
 
-    #size of image from gif/jpg/jpeg/png 
+    #size of image from gif/jpg/jpeg/png
     my $ressrc=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
     if (-e $ressrc) {
 	$src = $ressrc;
@@ -4215,7 +4382,7 @@ sub image_size {
     undef($image);
 
     ($height_param, $width_param) = &resize_image($height_param, $width_param,
-						  $scaling, $parstack, $safeeval, 
+						  $scaling, $parstack, $safeeval,
 						  $depth, $cis);
 
     return ($height_param, $width_param);
@@ -4244,8 +4411,8 @@ sub get_eps_image {
 	&Apache::lonnet::repcopy($orig_src); # Failure is not completely fatal.
     }
     &Apache::lonxml::debug("get_eps_image: Original image: $orig_src");
-    my ($spath, $sname, $sext) = &fileparse($src, qr/\.(bmp|gif|png|jpg|jpeg)/i);
-    $src=~s/\.(bmp|gif|png|jpg|jpeg)$/\.eps/i;
+    my ($spath, $sname, $sext) = &fileparse($src, qr/\.(bmp|gif|png|jpg|jpeg|svg)/i);
+    $src=~s/\.(bmp|gif|png|jpg|jpeg|svg)$/\.eps/i;
     $src=&Apache::lonnet::filelocation($Apache::lonxml::pwd[-1],$src);
     &Apache::lonxml::debug("Filelocation gives: $src");
     if (! -e $src) {
@@ -4301,9 +4468,9 @@ sub get_eps_image {
 }
 
 sub eps_generation {
-    my ($src,$file,$width_param) = @_;	     
+    my ($src,$file,$width_param) = @_;
     my $filename = "/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.dat";
-    if (open(my $tmpfile,">>$filename")) { 
+    if (open(my $tmpfile,">>$filename")) {
         print $tmpfile "$src\n";
         close($tmpfile);
     }
@@ -4324,13 +4491,13 @@ sub eps_generation {
     }
 }
 
-sub file_path {     
+sub file_path {
     my $src=shift;
-    my ($file,$path); 
+    my ($file,$path);
     if ($src =~ m!(.*)/([^/]*)$!) {
-	$file = $2; 
-	$path = $1.'/'; 
-    } 
+	$file = $2;
+	$path = $1.'/';
+    }
     return $file,$path;
 }
 
@@ -4356,23 +4523,23 @@ sub recalc {
 sub LATEX_length {
     my $garbage=shift;
     $garbage=~s/^\s+$//;
-    $garbage=~s/^\s+(\S.*)/$1/;#space before 
-    $garbage=~s/(.*\S)\s+$/$1/;#space after 
+    $garbage=~s/^\s+(\S.*)/$1/;#space before
+    $garbage=~s/(.*\S)\s+$/$1/;#space after
     $garbage=~s/(\s)+/$1/;#only one space
-    $garbage=~s/(\\begin{([^\}]+)}|\\end{([^\}]+)})//g;#remove LaTeX \begin{...} and \end{...}
-    $garbage=~s/(\$\_\{|\$\_|\$\^{|\$\^|\}\$)//g;#remove $_{,$_,$^{,$^,}$
+    $garbage=~s/(\\begin\{([^\}]+)}|\\end\{([^\}]+)})//g;#remove LaTeX \begin{...} and \end{...}
+    $garbage=~s/(\$\_\{|\$\_|\$\^\{|\$\^|\}\$)//g;#remove $_{,$_,$^{,$^,}$
     $garbage=~s/([^\\])\$/$1/g;#$
-    $garbage=~s/(\\ensuremath\{\_\{|\\ensuremath\{\_|\\ensuremath\{\^{|\\ensuremath\{\^|\})//g;#remove \ensuremath{...}
+    $garbage=~s/(\\ensuremath\{\_\{|\\ensuremath\{\_|\\ensuremath\{\^\{|\\ensuremath\{\^|\})//g;#remove \ensuremath{...}
    $garbage=~s/(\\alpha|\\beta|\\gamma|\\delta|\\epsilon|\\verepsilon|\\zeta|\\eta|\\theta|\\vartheta|\\iota|\\kappa|\\lambda|\\mu|\\nu|\\xi|\\pi|\\varpi|\\rho|\\varrho|\\sigma|\\varsigma|\\tau|\\upsilon|\\phi|\\varphi|\\chi|\\psi|\\omega|\\Gamma|\\Delta|\\Theta|\\Lambda|\\Xi|\\Pi|\\Sigma|\\Upsilon|\\Phi|\\Psi|\\Omega)/1/g;
     $garbage=~s/(\\pm|\\mp|\\times|\\div|\\cdot|\\ast|\\star|\\dagger|\\ddagger|\\amalg|\\cap|\\cup|\\uplus|\\sqcap|\\sqcup|\\vee|\\wedge|\\oplus|\\ominus|\\otimes|\\circ|\\bullet|\\diamond|\\lhd|\\rhd|\\unlhd|\\unrhd|\\oslash|\\odot|\\bigcirc|\\Box|\\Diamond|\\bigtriangleup|\\bigtriangledown|\\triangleleft|\\triangleright|\\setminus|\\wr)/1/g;
     $garbage=~s/(\\le|\\ll|\\leq|\\ge|\\geq|\\gg|\\neq|\\doreq|\\sim|\\simeq|\\subset|\\subseteq|\\sqsubset|\\sqsubseteq|\\in|\\vdash|\\models|\\supset|\\supseteq|\\sqsupset|\\sqsupseteq|\\ni|\\dash|\\perp|\\approx|\\cong|\\equiv|\\propto|\\prec|\\preceq|\\parallel|\\asymp|\\smile|\\frown|\\bowtie|\\succ|\\succeq|\\mid)/1/g;
     $garbage=~s/(\\not<|\\\\not\\le|\\not\\prec|\\not\\preceq|\\not\\subset|\\not\\subseteq|\\not\\sqsubseteq|\\not\\in|\\not>|\\not\\ge|\\not\\succ|\\notsucceq|\\not\\supset|\\notsupseteq|\\not\\sqsupseteq|\\notin|\\not=|\\not\\equiv|\\not\\sim|\\not\\simeq|\\not\\approx|\\not\\cong|\\not\\asymp)/1/g;
     $garbage=~s/(\\leftarrow|\\gets|\\Leftarrow|\\rightarrow|\\to|\\Rightarrow|\\leftrightarrow|\\Leftrightarrow|\\mapsto|\\hookleftarrow|\\leftharpoonup|\\leftkarpoondown|\\rightleftharpoons|\\longleftarrow|\\Longleftarrow|\\longrightarrow|\\Longrightarrow|\\longleftrightarrow|\\Longleftrightarrow|\\longmapsto|\\hookrightarrow|\\rightharpoonup|\\rightharpoondown|\\uparrow|\\Uparrow|\\downarrow|\\Downarrow|\\updownarrow|\\Updownarrow|\\nearrow|\\searrow|\\swarrow|\\nwarrow)/11/g;
     $garbage=~s/(\\aleph|\\hbar|\\imath|\\jmath|\\ell|\\wp|\\Re|\\Im|\\mho|\\prime|\\emptyset|\\nabla|\\surd|\\partial|\\top|\\bot|\\vdash|\\dashv|\\forall|\\exists|\\neg|\\flat|\\natural|\\sharp|\\\||\\angle|\\backslash|\\Box|\\Diamond|\\triangle|\\clubsuit|\\diamondsuit|\\heartsuit|\\spadesuit|\\Join|\\infty)/11/g;
-    $garbage=~s/(\\hat{([^}]+)}|\\check{([^}]+)}|\\dot{([^}]+)}|\\breve{([^}]+)}|\\acute{([^}]+)}|\\ddot{([^}]+)}|\\grave{([^}]+)}|\\tilde{([^}]+)}|\\mathring{([^}]+)}|\\bar{([^}]+)}|\\vec{([^}]+)})/$1/g;
+    $garbage=~s/(\\hat\{([^}]+)}|\\check\{([^}]+)}|\\dot\{([^}]+)}|\\breve\{([^}]+)}|\\acute\{([^}]+)}|\\ddot\{([^}]+)}|\\grave\{([^}]+)}|\\tilde\{([^}]+)}|\\mathring\{([^}]+)}|\\bar\{([^}]+)}|\\vec\{([^}]+)})/$1/g;
     #remove some other LaTeX command
-    $garbage=~s|\\(\w+)\\|\\|g;	 
-    $garbage=~s|\\(\w+)(\s*)|$2|g;	 	 
+    $garbage=~s|\\(\w+)\\|\\|g;
+    $garbage=~s|\\(\w+)(\s*)|$2|g;
     $garbage=~s|\+|11|g;
     my  $value=length($garbage);
     return $value;
@@ -4392,8 +4559,8 @@ sub align_latex_image {
     #    If there's an alignment specification we need to honor it here.
     #    For the horizontal alignments, we will also honor the
     #    value of the latex specfication.  The default is parbox,
-    #    and that's used for illegal values too.  
-    #    
+    #    and that's used for illegal values too.
+    #
     #    Even though we set a default alignment value, the user
     #    could have given us an illegal value.  In that case we
     #    just use the default alignment of bottom..
@@ -4405,8 +4572,8 @@ sub align_latex_image {
 	my $offset = $height/2;
 	$currentstring .= '\raisebox{-'.$offset.'mm}{'.$image;
 	$closure       = '}';
-    } elsif ($align eq "left")   { 
-	if ($latex_rendering eq "parpic") { 
+    } elsif ($align eq "left")   {
+	if ($latex_rendering eq "parpic") {
 	    $currentstring .= '\parpic[l]{'.$image;
 	    $closure       = '}';
 	} elsif ($latex_rendering eq "parbox") {
@@ -4415,12 +4582,12 @@ sub align_latex_image {
 	    $closure = '\end{minipage}';
 	} elsif ($latex_rendering eq "wrapfigure"
 		 || $latex_rendering ne 'none') {  # wrapfig render
-	    $currentstring .= 
+	    $currentstring .=
 		'\begin{wrapfigure}{l}{'.$width.'mm}'
 		.'\scalebox{1.0}{'.$image;
 	    $closure = '}\end{wrapfigure}';
 	}
-    } elsif ($align eq "right")  {   
+    } elsif ($align eq "right")  {
 	if ($latex_rendering eq "parpic") {
 	    $currentstring .= '\parpic[r]{'.$image;
 	    $closure = '}';
@@ -4430,7 +4597,7 @@ sub align_latex_image {
 	    $closure = '\end{minipage}';
 	} elsif ($latex_rendering eq "wrapfigure"
 		 || $latex_rendering ne 'none') {  # wrapfig render
-	    $currentstring .= 
+	    $currentstring .=
 		'\begin{wrapfigure}{r}{'.$width.'mm}'
 		.'\scalebox{1.0}{'.$image;
 	    $closure = '}\end{wrapfigure}';
@@ -4463,62 +4630,129 @@ sub latex_header {
     my ($mode) = @_;
     my $currentstring = '';
 
-    $currentstring .= 
+    $currentstring .=
 	"\n% &Apache::lonxml::londefdef \n" .
 	'\documentclass[letterpaper,twoside]{article}\raggedbottom';
     if (($env{'form.latex_type'}=~'batchmode') ||
-	(!$env{'request.role.adv'}) || 
-	($mode eq 'batchmode')) {$currentstring .='\batchmode';} 
+	(!$env{'request.role.adv'}) ||
+	($mode eq 'batchmode')) {$currentstring .='\batchmode';}
     $currentstring .= '\newcommand{\keephidden}[1]{}'.
 	'\renewcommand{\deg}{$^{\circ}$}'.
-	'\usepackage{multirow}'.
-	'\usepackage{longtable}'.
-	'\usepackage{textcomp}'.
-	'\usepackage{makeidx}'.
-	'\usepackage[dvips]{graphicx}'.
-	'\usepackage{wrapfig}'.
-	'\usepackage{picins}'.
+	'\usepackage{multirow}'."\n".
+	'\usepackage{longtable}'."\n".
+	'\usepackage{textcomp}'."\n".
+	'\usepackage{makeidx}'."\n".
+	'\usepackage[dvips]{graphicx}'."\n".
+	'\usepackage{wrapfig}'."\n".
+	'\usepackage{picins}'."\n".
 	'\usepackage[T1]{fontenc}'."\n".
 	'\usepackage{lmodern}'."\n".
 	'\usepackage[postscript]{ucs}'."\n".
 	'\usepackage[utf8x]{inputenc}'."\n".
-	'\usepackage{pifont}' ."\n".
+	'\usepackage{pifont}'."\n".
 	'\usepackage{latexsym}'."\n".
-	'\usepackage{epsfig}'.
-	"\\usepackage{xtab}\n".
-	"\\usepackage{tabularx}\n".
-	"\\usepackage{booktabs}\n".
-	"\\usepackage{array}\n".
-	"\\usepackage{colortbl}\n".
-	"\\usepackage{xcolor}\n".
-	'\usepackage{calc}'.
-	'\usepackage{amsmath}'.
-	'\usepackage{soul}'.
-	'\usepackage{amssymb}'.
-	'\usepackage{amsfonts}'.
-	'\usepackage{amsthm}'.
-	'\usepackage{amscd}'
-        .'\usepackage{picins}\usepackage{calc}'."\n". # From lonprintout.pm
-	'\usepackage[T1]{fontenc}'."\n".
-	'\usepackage{lmodern}'."\n".
-	'\usepackage[postscript]{ucs}'."\n".
-	'\usepackage[utf8x]{inputenc}'."\n".
-	'\usepackage{pifont}'  . "\n";
-	
+	'\usepackage{epsfig}'."\n".
+	'\usepackage{xtab}'."\n".
+	'\usepackage{tabularx}'."\n".
+	'\usepackage{booktabs}'."\n".
+	'\usepackage{array}'."\n".
+	'\usepackage{colortbl}'."\n".
+	'\usepackage{xcolor}'."\n".
+	'\usepackage{calc}'."\n".
+	'\usepackage{amsmath}'."\n".
+	'\usepackage{soul}'."\n".
+	'\usepackage{amssymb}'."\n".
+	'\usepackage{amsfonts}'."\n".
+	'\usepackage{amsthm}'."\n".
+	'\usepackage{amscd}'."\n".
+	'\usepackage{actuarialangle}'."\n";
     if($env{'form.pdfFormFields'} eq 'yes') {
 	$currentstring .= '\usepackage{hyperref}'.
 	    '\usepackage{eforms}'.
 	    '\usepackage{tabularx}';
-    } 
-    
+    }
+
         $currentstring .= '\newenvironment{choicelist}{\begin{list}{}{\setlength{\rightmargin}{0in}\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.05in}\setlength{\itemsep}{0.022in}\setlength{\parsep}{0in}\setlength{\belowdisplayskip}{0.04in}\setlength{\abovedisplayskip}{0.05in}\setlength{\abovedisplayshortskip}{-0.04in}\setlength{\belowdisplayshortskip}{0.04in}}}{\end{list}}'.
                           '\renewenvironment{theindex}{\begin{list}{}{{\vskip 1mm \noindent \large\textbf{Index}} \newline \setlength{\rightmargin}{0in}\setlength{\leftmargin}{0.13in}\setlength{\topsep}{0.01in}\setlength{\itemsep}{0.1in}\setlength{\parsep}{-0.02in}\setlength{\belowdisplayskip}{0.01in}\setlength{\abovedisplayskip}{0.01in}\setlength{\abovedisplayshortskip}{-0.04in}\setlength{\belowdisplayshortskip}{0.01in}}}{\end{list}}';
     $currentstring .= '\begin{document}';
-    
+
     return $currentstring;
 
 }
 
+sub clean_docs_httpref {
+    my ($href,$docuri,$cdom,$cnum) = @_;
+    if ($docuri eq '') {
+        $docuri = &Apache::lonnet::hreflocation('',$env{'request.filename'});
+    }
+    if ($cdom eq '') {
+        $cdom = $env{'course.'.$env{'request.course.id'}.'.domain'};
+    }
+    if ($cnum eq '') {
+        $cnum = $env{'course.'.$env{'request.course.id'}.'.num'};
+    }
+    my $cleanhref;
+    if ($docuri =~ m{^(\Q/uploaded/$cdom/$cnum/\E(?:docs|supplemental)/)(.+/)[^/]+$}) {
+        my $prefix = $1;
+        my $relpath = $2;
+        my ($hrefpath,$fname);
+        if ($href =~ m{^/}) {
+            if ($href =~ m{^\Q$prefix\E(.+/)([^/]+)$}) {
+                $hrefpath = $1;
+                $fname = $2;
+            } else {
+                return $cleanhref;
+            }
+        } else {
+            if ($href =~ m{/}) {
+                (my $path,$fname) = ($href =~ m{^(.*)/([^/]*)$});
+                $hrefpath = $relpath.$path;
+                if ($path eq '') {
+                    $hrefpath =~ s{/$}{};
+                }
+            } else {
+                $fname = $href;
+                $hrefpath = $relpath;
+                $hrefpath =~ s{/$}{};
+            }
+        }
+        if ($fname ne '') {
+            my $cleanrelpath;
+            foreach my $dir (split(/\//,$hrefpath)) {
+                next if ($dir eq '.');
+                if ($dir eq '..') {
+                    $cleanrelpath =~ s{([^/]+/)$}{};
+                } else {
+                    $cleanrelpath .= $dir.'/';
+                }
+            }
+            if ($cleanrelpath ne '') {
+                $cleanhref = $prefix.$cleanrelpath.$fname;
+            } else {
+                $cleanhref = $prefix.$fname;
+            }
+        }
+    }
+    return $cleanhref;
+}
+
+# This is retrieving a CSS property from the style attribute of the current element.
+# It is not checking <style> elements or linked stylesheets yet.
+sub get_css_property {
+    my ($property,$parstack,$safeeval) = @_;
+    my $style=&Apache::lonxml::get_param('style',$parstack,$safeeval,undef,1);
+    my @style_components=split(/;/,$style);
+    foreach my $css_pair (@style_components) {
+        my ($name, $value) = split(/:/, $css_pair);
+        $name =~ s/^\s+|\s+$//g;
+        $value =~ s/^\s+|\s+$//g;
+        if ($name eq $property) {
+            return $value;
+        }
+    }
+    return undef;
+}
+
 =pod
 
 =head1 NAME
@@ -4549,12 +4783,12 @@ described at http://www.lon-capa.org.
 	it's initial ht. and wid.  This allows sizing of
 	images that are generated on-the-fly (e.g. gnuplot)
 	as well as serving as a utility for image_size.
- 
+
 	Parameter:
         height_param
         width_param    - Initial picture dimensions.
         scaling        - A scale factor.
-        parstack,      - the current stack of tag attributes 
+        parstack,      - the current stack of tag attributes
                          from the xml parser
         safeeval,      - pointer to the safespace
         depth,         - from what level in the stack to look for attributes
@@ -4579,9 +4813,9 @@ described at http://www.lon-capa.org.
 
 =item recalc()
 
-	Converts a measurement in to mm from any of 
+	Converts a measurement in to mm from any of
 	the other valid LaTeX units of measure.
-	If the units of measure are missing from the 
+	If the units of measure are missing from the
 	parameter, it is assumed to be in and returned
 	with mm units of measure
 
@@ -4589,7 +4823,7 @@ described at http://www.lon-capa.org.
 
 =item align_latex_image()
 
-  	Wrap image 'stuff' inside of the LaTeX required to implement 
+  	Wrap image 'stuff' inside of the LaTeX required to implement
    	alignment:
      	align_tex_image(align, latex_rendering, image)
    	Where:
@@ -4604,7 +4838,7 @@ described at http://www.lon-capa.org.
 
 
 =item is_inside_of($tagstack, $tag)
-   	This sub returns true if the current state of Xml processing is inside of the tag.   
+   	This sub returns true if the current state of Xml processing is inside of the tag.
 	Parameters:
     	tagstack   - The tagstack from the parser.
     	tag        - The tag (without the <>'s.).
@@ -4613,7 +4847,23 @@ described at http://www.lon-capa.org.
      	   I'm in a table....
      	}
 
-
+=item clean_docs_httpref($href,$docuri,$cdom,$cnum)
+        HTML pages uploaded to a course which contain dependencies either from iframes,
+        javascript files or objects (FlashPlayerSwf, MediaSrc, XMPSrc, ConfigurationSrc,
+        and PosterImageSrc) for which dependency is another file uploaded to the same
+        course.
+
+        Required input:
+        href - dependency (either a relative URL, or an absolute URL)
+        Optional inputs:
+        docuri - URL of HTML page containing the dependency
+        cdom - Course domain
+        cnum - CourseID
+
+        Output:
+        returns an absolute URL constructed from the href provided, and the calling context.
+        (this will be null, if the URL does not begin: /uploaded/$cdom/$cnum/docs/ or
+        /uploaded/$cdom/$cnum/supplemental/).
 
 =back