diff --git a/lib/WebGUI/Asset.pm b/lib/WebGUI/Asset.pm index 97887aced..42f4579ca 100644 --- a/lib/WebGUI/Asset.pm +++ b/lib/WebGUI/Asset.pm @@ -1948,13 +1948,12 @@ sub outputWidgetMarkup { # we'll be serializing the content of the asset which is being widgetized. my $storage = WebGUI::Storage->get($session, $assetId); my $content = $self->view; - if($styleTemplateId eq '' or $styleTemplateId eq 'none'){ - $content = $self->session->style->userStyle($content); - }else{ + if($styleTemplateId ne '' && $styleTemplateId ne 'none'){ $content = $self->session->style->process($content,$styleTemplateId); } WebGUI::Macro::process($session, \$content); - my $jsonContent = to_json( { "asset$assetId" => { content => $content } } ); + my ($headTags, $body) = WebGUI::HTML::splitHeadBody($content); + my $jsonContent = to_json( { "asset$assetId" => { content => $body } } ); $storage->addFileFromScalar("$assetId.js", "data = $jsonContent"); my $jsonUrl = $storage->getUrl("$assetId.js"); @@ -1985,6 +1984,7 @@ sub outputWidgetMarkup { } YAHOO.util.Event.addListener(window, 'load', setupPage); + $headTags \${asset$assetId.content} diff --git a/lib/WebGUI/HTML.pm b/lib/WebGUI/HTML.pm index 73d82f0f2..9b15e56c8 100644 --- a/lib/WebGUI/HTML.pm +++ b/lib/WebGUI/HTML.pm @@ -443,5 +443,44 @@ sub splitTag { return $result[0]; } +sub splitHeadBody { + my $html = shift; + + my $parser = HTML::Parser->new(api_version => 3); + + my $head = ''; + my $body = ''; + my $accum; + $parser->handler(start => sub { + my ($tag, $text) = @_; + if ($tag eq 'head') { + $accum = \$head; + } + elsif ($tag eq 'body') { + $accum = \$body; + } + elsif ($accum) { + $$accum .= $text; + } + }, 'tagname, text'); + $parser->handler(end => sub { + my ($tag, $text) = @_; + if ($tag eq 'head' || $tag eq 'body') { + $accum = undef; + } + elsif ($accum) { + $$accum .= $text; + } + }, 'tagname, text'); + $parser->handler(default => sub { + my ($tag, $text) = @_; + if ($accum) { + $$accum .= $text; + } + }, 'tagname, text'); + $parser->parse($html); + return ($head, $body); +} + 1; diff --git a/lib/WebGUI/Macro/Widget.pm b/lib/WebGUI/Macro/Widget.pm index 29d976ced..8119ac749 100644 --- a/lib/WebGUI/Macro/Widget.pm +++ b/lib/WebGUI/Macro/Widget.pm @@ -72,6 +72,11 @@ sub process { $wgWidgetPath = $exportUrl . $extras . '/wgwidget.js'; $scratch->delete('exportUrl'); my $viewContent = $asset->view; + if ($styleTemplateId ne '' && $styleTemplateId ne 'none') { + $viewContent = $session->style->process($viewContent,$styleTemplateId); + } + my ($headTags, $bodyContent) = WebGUI::HTML::splitHeadBody($viewContent); + WebGUI::Macro::process($session, \$viewContent); my $containerCss = $extras . '/yui/build/container/assets/container.css'; my $containerJs = $extras . '/yui/build/container/container-min.js'; @@ -95,9 +100,10 @@ sub process { } YAHOO.util.Event.addListener(window, 'load', setupPage); + $headTags - $viewContent + $bodyContent OUTPUT