<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bugfree.dk - Ronnie Holm&#039;s blog &#187; Html</title>
	<atom:link href="http://www.bugfree.dk/blog/tag/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bugfree.dk/blog</link>
	<description>Not anti-anything, just pro-quality</description>
	<lastBuildDate>Tue, 07 Sep 2010 06:28:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Printing iframe content using JavaScript</title>
		<link>http://www.bugfree.dk/blog/2009/12/17/printing-iframe-content-using-javascript/</link>
		<comments>http://www.bugfree.dk/blog/2009/12/17/printing-iframe-content-using-javascript/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 21:50:41 +0000</pubDate>
		<dc:creator>Ronnie Holm</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Html]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.bugfree.dk/blog/?p=1005</guid>
		<description><![CDATA[JavaScript is on top of the list of things I’d like to become better at. So I was quite happy when assigned a JavaScript-related defect in an application at work. Apparently, there was a bug in the JavaScript routine used for printing only the content of an iframe on a page. Rather than printing only [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript is on top of the list of things I’d like to become better at. So I was quite happy when assigned a JavaScript-related defect in an application at work. Apparently, there was a bug in the JavaScript routine used for printing only the content of an iframe on a page. Rather than printing only the iframe’s content, it printed the page hosting the iframe, cropping parts of the iframe’s content.</p>
<p>I started out confident that printing would be easy to fix. But after Googling and playing around with JavaScript for a while, frustration, fueled by the number of non-working hits I’d stumbled on, started to grow on me. Suppose you start out with an HTML document hosting an iframe and a few lines of JavaScript.</p>
<pre class="prettyprint lang-html">  &lt;html&gt;
    &lt;head&gt;
      &lt;title&gt;IFrame printing example&lt;/title&gt;
      &lt;script type=&quot;text/javascript&quot;&gt;
        function myPrint() {
          var browser = navigator.appName;
          if (browser == &quot;Microsoft Internet Explorer&quot;) {
            window.frames[&quot;frame&quot;].focus();
            window.frames.print();
          }
          else if (browser == &quot;Netscape&quot;)
            alert(&quot;Printing not supported in Firefox&quot;);
        }
      &lt;/script&gt;
    &lt;/head&gt;
    &lt;body&gt;
      &lt;iframe id=&quot;frame&quot; name=&quot;frame&quot; width=&quot;100&quot;
              height=&quot;100&quot; src=&quot;http://google.com&quot;&gt;
      &lt;/iframe&gt;
      &lt;a href=&quot;#&quot; onclick=&quot;javascript:myPrint();&quot;&gt;Print&lt;/a&gt;
    &lt;/body&gt;
  &lt;/html&gt;</pre>
<p>A number of issues arise when printing the iframe. First you need to consider if you’re always hosting the main document on the same domain as the iframe’s source. If so, printing only the iframe with Internet Explorer works like a charm. If, on the other hand, the main document and the iframe’s source reside on different domains, it triggers Internet Explorer to displays a bar across the top of the window.</p>
<p><img src="http://www.bugfree.dk/blog/wp-content/uploads/2009/12/allow-blocked-content-browser-bar.png" /></p>
<p>Now, to print the iframe’s content, you must allow blocked content. Only then can JavaScript access the iframe’s object model. But at the same time, you make yourself vulnerable to <a href="http://en.wikipedia.org/wiki/Cross-site_scripting">cross-site scripting attacks</a>.</p>
<p>As far as Firefox support goes, you can issue the same API calls as for Internet Explorer. But Firefox will always print the main page, including only the visible parts of the iframe. To my knowledge there’s no way to make Firefox print the iframe’s content as seamlessly as Internet Explorer. One workaround, though, would be to open a new window, grab a reference to the iframe element, and move its contents to the new window before printing it. For cross-domain content, however, Firefox responds with a “permission denied” error when accessing the iframe element. And there’s no option to allow blocked content. Another workaround may therefore be to use Ajax to retrieve the source of the iframe for any domain and insert it into the new window. That, however, wouldn’t work in my case since the iframe may contain a form that the user may have started filling out before hitting print.</p>
<p>As a last resort, I considered writing a proxy to retrieve cross-domain hosted content. This would make Internet Explorer never display the bar and allow Firefox to always open a new window with the iframe’s content loaded, even if the user started filling out a form first. But this approach sort of defies the purpose of the browser bar and the permission denied error. Unless you absolutely trust your iframe’s source, you’re exposing your users without their consent.</p>
<p>In the end I went with the simplest and most secure solution of not supporting printing at all.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.bugfree.dk/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.bugfree.dk/blog/2009/12/17/printing-iframe-content-using-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to modify the enclosing tag of a web control</title>
		<link>http://www.bugfree.dk/blog/2008/07/30/how-to-modify-the-enclosing-tag-of-a-web-control/</link>
		<comments>http://www.bugfree.dk/blog/2008/07/30/how-to-modify-the-enclosing-tag-of-a-web-control/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 20:45:47 +0000</pubDate>
		<dc:creator>Ronnie Holm</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[Asp.Net]]></category>
		<category><![CDATA[Html]]></category>

		<guid isPermaLink="false">http://www.bugfree.dk/blog/2008/07/30/how-to-modify-a-web-control%e2%80%99s-enclosing-tag/</guid>
		<description><![CDATA[In this post I&#8217;ll rework a piece of code that changes the enclosing tag of a web control from a span to a div. A substitution of particular value with SharePoint, where adding web controls is a common way to extend the out of box experience. With SharePoint, and Asp.Net in general, a page is [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I&#8217;ll rework a piece of code that changes the enclosing tag of a web control from a <a href="http://webdesign.about.com/od/htmltags/a/aa011000a.htm">span to a div</a>. A substitution of particular value with SharePoint, where adding web controls is a common way to extend the out of box experience. With SharePoint, and Asp.Net in general, a page is typically composed of reusable controls, each responsible for rendering its part of the page.
</p>
<p>To showcase anti-patterns of API usage, intermediate code is included, i.e., code that may render the correct tags, although not necessarily in the true spirit of the .Net framework.
</p>
<p>To set the stage, imagine writing a web control that inherits from ASP.Net <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.aspx">WebControl</a>. The goal is then to modify the behavior of the control in such a way that it (1) encloses its content in a div tag and (2) adds a CSS class for styling, and (3) can be added to a page like so:
</p>
<pre class="prettyprint lang-html">
   &lt;MyControls:MyControl CssClass="myCssClass" runat="server" /&gt;
</pre>
<p>After making its way through the page rendering pipeline, the control tag gets transformed to:
</p>
<pre class="prettyprint lang-html">
   &lt;div class="myCssClass"&gt;
      Hello World
   &lt;/div&gt;
</pre>
<p>Within the control, a straight-forward way of achieving the intended output may be to hand craft the html and render it in <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.rendercontents.aspx">RenderContents</a> of WebControl. It almost goes without saying that rendering html by hand is but only a starting point when working with .Net. The next progression from there may be to override <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.renderbegintag.aspx">RenderBeginTag</a> and <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.renderendtag.aspx">RenderEndTag</a> of WebControl:
</p>
<pre class="prettyprint lang-cs">
   // version 1
   public class MyControl : WebControl {
      public override void RenderBeginTag(HtmlTextWriter w) {
         w.RenderBeginTag(string.Format("div class=\"{0}\"", CssClass));
      }

      public override void RenderEndTag(HtmlTextWriter w) {
         w.RenderEndTag();
      }

      protected override void RenderContents(HtmlTextWriter w) {
         w.Write("Hello World");
      }
   }
</pre>
<p>Resulting in this piece of html:</p>
<pre class="prettyprint lang-html">
   &lt;div class="myCssClass"&gt;
      Hello World
   &lt;/div class="myCssClass"&gt;
</pre>
<p>Clearly, <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.renderbegintag.aspx">RenderBeginTag</a> and <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.renderendtag.aspx">RenderEndTag</a> of <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.htmltextwriter.aspx">HtmlTextWriter</a> don&#8217;t know how to separate tag from attribute. Nonetheless, the code reveals the stack based approach taken by the Render-pair of methods.</p>
<p>Gleaning over the documentation of HtmlTextWriter, however, I encountered the <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.htmltextwriter.addattribute.aspx">AddAttribute</a> method for setting attributes on a tag. Inside HtmlTextWriter, what AddAttribute does is add attributes and associated values to an array that gets rendered during the next call to RenderBeginTag. So besides introducing strongly typed attributes and tags, the next version contains a modified RenderBeginTag:</p>
<pre class="prettyprint lang-cs">
   // version 2
   public override void RenderBeginTag(HtmlTextWriter w) {
      w.AddAttribute(HtmlTextWriterAttribute.Class, CssClass);
      w.RenderBeginTag(HtmlTextWriterTag.Div);
   }
</pre>
<p>Now the control renders as expected, although the code still has a certain bad smell to it. WebControl ought to know how to properly render its begin and end tag. After all it&#8217;s WebControl that exposes, among others, the <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.cssclass.aspx">CssClass</a> property. So taking another dive into the documentation, I found the virtual, read-only <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webcontrol.tagkey.aspx">TagKey</a> property of WebControl. Strangely named as it may be, overriding TagKey makes WebControl call our override as it renders the control, adding in optional attributes, such as CssClass. Implementing the property also turns RenderBeginTag and RenderEndTag obsolete:</p>
<pre class="prettyprint lang-cs">
   // version 3
   protected override HtmlTextWriterTag TagKey {
      get {
         return HtmlTextWriterTag.Div;
      }
   }
</pre>
<p>As an added bonus, changing the enclosing tag can also be done calling the WebControl&#8217;s constructor, passing in the desired tag:
</p>
<pre class="prettyprint lang-cs">
   // version  4
   public class MyControl : WebControl {
      public MyControl() : base(HtmlTextWriterTag.Div) { }

      protected override void RenderContents(HtmlTextWriter w) {
         w.Write("Hello World");
      }
   }
</pre>
<p>As it turns out, calling the constructor is how WebControl derived classes get to use span in the first place. The default WebControl constructor passes control onto another constructor that sets the value of what gets returned by the base class TagKey implementation. In essence, overriding TagKey makes it a player in the <a href="http://en.wikipedia.org/wiki/Template_method_pattern">template design pattern</a>:
</p>
<pre class="prettyprint lang-cs">
   // .Net framework code
   protected WebControl() : this(HtmlTextWriterTag.Span) { }

   public WebControl(HtmlTextWriterTag tag) {
      tagKey = tag;
   }
</pre>
<p>Also worth noting is that for controls deriving from WebControl derived classes, such as <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.compositecontrol.aspx">CompositeControl</a>, the constructor cannot be used to set the enclosing tag. The constructor exposed by CompositeControl and friends doesn&#8217;t call up the constructor chain passing in the tag, making TagKey the choice to go.
</p>
<p>Finally, deep down the inhertitance chain below WebControl lives <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.webparts.webpart.aspx">WebPart</a>, a type of control very popular with SharePoint. Interestingly enough, WebPart defaults to using div as its enclosing tag, because walking up the chain, we come across the constructor of <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.panel.aspx">Panel</a>:
</p>
<pre class="prettyprint lang-cs">
   // .Net framework code
   public Panel() : base(HtmlTextWriterTag.Div) { }
</pre>
<p>So, using Panel provides another way to implement a web control that owns a distinct part of a page.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://www.bugfree.dk/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.bugfree.dk/blog/2008/07/30/how-to-modify-the-enclosing-tag-of-a-web-control/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
