<?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>DaveOnCode &#187; javascript</title>
	<atom:link href="http://www.daveoncode.com/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveoncode.com</link>
	<description>Objective C, iOS and more programming stuff</description>
	<lastBuildDate>Mon, 19 Dec 2011 12:11:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Image background fallback using &lt;img/&gt; tag error handlers</title>
		<link>http://www.daveoncode.com/2010/08/20/image-background-fallback-img-tag-error-handlers/</link>
		<comments>http://www.daveoncode.com/2010/08/20/image-background-fallback-img-tag-error-handlers/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 22:49:12 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=644</guid>
		<description><![CDATA[In the project I&#8217;m working on, we make large use of external assets like images, coming from several providers. Unfortunately these links are often broken, resulting in &#8220;empty squares&#8221; which are unpleasant for the user. The best thing to do, since we don&#8217;t have the full control over these resources, is to display the classic [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2010/08/20/image-background-fallback-img-tag-error-handlers/' addthis:title='Image background fallback using &#60;img/&#62; tag error handlers ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>In the project I&#8217;m working on, we make large use of external assets like images, coming from several providers. Unfortunately these links are often broken, resulting in &#8220;empty squares&#8221; which are unpleasant for the user. The best thing to do, since we don&#8217;t have the full control over these resources, is to display the classic &#8220;image not available&#8221; pic, but this is not simple as writing an if statement such:</p>
<div class="codecolorer-container text mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">if (record.image) {<br />
&nbsp; &nbsp; &lt;img src=&quot;record.image&quot; /&gt;<br />
} else {<br />
&nbsp; &nbsp; &lt;img src=&quot;imageNotAvailableLink&quot; /&gt;<br />
}</div></td></tr></tbody></table></div>
<p>because in our case &#8220;record.image&#8221; is defined although its path is unreachable. So, we have to try to load the image and replace it at runtime when we know that the src is broken. How can we catch that exception? We just need to listen to <strong>onerror</strong> and <strong>onabort</strong> img events, so we can simply write this smart img tag:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;providerSrc&quot;</span> onerror<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;this.src=fallbackSrc&quot;</span> onabort<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;this.src=fallbackSrc&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span></div></td></tr></tbody></table></div>
<p>The <strong>this</strong> key inside an event attribute, refers to the dom node itself, so we can switch an attribute like src with a little piece of inline JavaScript.<br />
Anyway in my scenario the problem is bigger than this example, because thumbs images are rendered as divs background in order to make a sort of polaroid effect and, at the same time, to solve a potential issue related to exceeding image dimensions or a stretched one.<br />
Fortunately the solution I found, is not so far from the above, the only difference is that I used an invisible img tag located inside the div and when it catch the error rather than change its src it changes its parent background:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background:url(providerSrc)&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;display:none&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;providerSrc&quot;</span> onerror<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;this.parentNode.style.backgroundImage='url(fallbackSrc)'&quot;</span> &nbsp;<span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></div></td></tr></tbody></table></div>
<p>Of course in my final implementation I replaced the piece of inline js assignment with an utility function call that does the job and also trace a debug message to the console, displaying the path of the broken image&#8230; I&#8217;m pretty satisfied about my work :^)</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2010/08/20/image-background-fallback-img-tag-error-handlers/' addthis:title='Image background fallback using &lt;img/&gt; tag error handlers ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2010/08/20/image-background-fallback-img-tag-error-handlers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Extending Eclipse using JavaScript and Monkey Script engine</title>
		<link>http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/</link>
		<comments>http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/#comments</comments>
		<pubDate>Wed, 16 Dec 2009 16:20:50 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[extensions]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=555</guid>
		<description><![CDATA[I was wondering how to wrap a string with quotes in Eclipse by using a shortcut, then I realized that there is not such command, so I started thinking for a solution and initially I created an Aptana&#8217;s snippet, but I was not satisfied, because I want to have an handy shortcut to invoke my [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/' addthis:title='Extending Eclipse using JavaScript and Monkey Script engine ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I was wondering how to wrap a string with quotes in Eclipse by using a shortcut, then I realized that there is not such command, so I started thinking for a solution and initially I created an Aptana&#8217;s snippet, but I was not satisfied, because I want to have an handy shortcut to invoke my snippet. By googling, I discovered Eclipse Monkey Script engine, that is an extremely powerful tool for every JavaScript developer who wants to extend Eclipse features by writing few lines of JavaScript code. In order to use the js engine, you must have the package org.eclipse.eclipsemonkey (which is installed by default by Aptana) installed. Unfortunately I can&#8217;t find a complete and exhaustive reference for this project, which seems forgotten by authors, so I learnt what I know reading different posts.<br />
Basically, Monkey Script allow developers to access editor&#8217;s instance, get selected test, get document content, edit it and update it. Moreover is possible to print text to consol, read and create files on the filesystem&#8230; and finally the scripts created will be accessible from Eclipse menu (under &#8220;Scripts&#8221;) and invokable through user defined shortcut&#8230; really nice!<br />
So, backing to my experiment, I created this monkey script:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #006600; font-style: italic;">/* <br />
* Key: M1+M2+C<br />
* Menu: Custom Scripts &gt; Wrap with double quotes<br />
* DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript<br />
* Kudos: Davide Zanotti<br />
*/</span><br />
<span style="color: #003366; font-weight: bold;">function</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// no editor... exit</span><br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> editors.<span style="color: #660066;">activeEditor</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">&quot;undefined&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;No active editor&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// get a reference to the editor in use</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> editor <span style="color: #339933;">=</span> editors.<span style="color: #660066;">activeEditor</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// beginning of text selection</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> startOffset <span style="color: #339933;">=</span> editor.<span style="color: #660066;">selectionRange</span>.<span style="color: #660066;">startingOffset</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// end of text selection</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> endOffset <span style="color: #339933;">=</span> editor.<span style="color: #660066;">selectionRange</span>.<span style="color: #660066;">endingOffset</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// selected text</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> selection <span style="color: #339933;">=</span> editor.<span style="color: #660066;">source</span>.<span style="color: #660066;">substring</span><span style="color: #009900;">&#40;</span>startOffset<span style="color: #339933;">,</span> endOffset<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// surround selection with quotes</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;editor.<span style="color: #660066;">applyEdit</span><span style="color: #009900;">&#40;</span>startOffset<span style="color: #339933;">,</span> endOffset <span style="color: #339933;">-</span> startOffset<span style="color: #339933;">,</span> <span style="color: #3366CC;">'&quot;'</span> <span style="color: #339933;">+</span> selection <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Error &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">code</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;: &quot;</span> <span style="color: #339933;">+</span> e.<span style="color: #660066;">message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
&nbsp; &nbsp; <br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>It&#8217;s important to notice some points, first you MUST write an &#8220;header&#8221; using the comment syntax &#8220;/* */&#8221; into which you MUST declare at least 2 parameter: the &#8220;<strong>Menu</strong>&#8221; path (you can nest several menu items by writing several &#8220;&gt;&#8221;) and the &#8220;<strong>DOM</strong>&#8221; package (which allows you to access specific objects like &#8220;<strong>editors</strong>&#8220;). You can write several js functions, but you must provide a <strong>main()</strong> function, which is called automatically by Eclipse once you launch your script, otherwise you can call the main function as you like but you must then provide the &#8220;OnLoad&#8221; param in the header, specifying which function will be called.<br />
To define a shortcut you can use the &#8220;<strong>Key</strong>&#8221; parameter and define your own keys combination by choosing among the four modifiers: M1, M2, M3, M4, that are a platform-independent way of representing keys (these stand for ALT, COMMAND, CTRL, and SHIFT). The strange (at least to me) parameter &#8220;<strong>Kudos</strong>&#8221; is used to declare the author of the monkey script.<br />
To test my little script or create your own, you have simply to create a new project into Eclipse by calling it as you like (ie: &#8220;custom-extensions&#8221;), then create a &#8220;scripts&#8221; folder into which you will save your .js files&#8230; that&#8217;s all, try yourself and enjoy :)</p>
<p>This is how it looks:</p>
<div id="attachment_558" class="wp-caption alignnone" style="width: 519px"><img src="http://www.daveoncode.com/wp-content/uploads/2009/12/Screen-shot-2009-12-16-at-17.08.56.jpg" alt="eclipse monkey script menu" title="eclipse-monkey-script-menu" width="509" height="254" class="size-full wp-image-558" /><p class="wp-caption-text">eclipse monkey script menu</p></div>
<p>ps: I&#8217;m going to explore the Monkey Script API in order to write more complex and useful extensions :P</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/' addthis:title='Extending Eclipse using JavaScript and Monkey Script engine ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/12/16/extending-eclipse-using-javascript-monkey-script-engine/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Find outermost top level XML/HTML tags with regular expressions</title>
		<link>http://www.daveoncode.com/2009/09/10/top-level-html-xml-tags-regex-regular-expressions/</link>
		<comments>http://www.daveoncode.com/2009/09/10/top-level-html-xml-tags-regex-regular-expressions/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 14:58:31 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[regex]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=484</guid>
		<description><![CDATA[I&#8217;m working on a personal big project (which I&#8217;m going to release soon) and in this project I need to parse strings containing XHTML tags with the goal of extract the top level of a given tag name, ie. from: 123456789&#60;onetag id=&#34;t1&#34;&#62; &#160; &#160; &#60;onetag id=&#34;t1-1&#34;&#62;&#60;/onetag&#62; &#160; &#160; &#60;onetag id=&#34;t1-2&#34;&#62;&#60;/onetag&#62; &#60;/onetag&#62; &#60;onetag id=&#34;t2&#34;&#62;&#60;/onetag&#62; &#60;onetag id=&#34;t3&#34;&#62;&#60;/onetag&#62; [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/09/10/top-level-html-xml-tags-regex-regular-expressions/' addthis:title='Find outermost top level XML/HTML tags with regular expressions ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working on a personal big project (which I&#8217;m going to release soon) and in this project I need to parse strings containing XHTML tags with the goal of extract the top level of a given tag name, ie. from:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t1&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t1-1&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t1-2&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t2&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t3&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t4&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t4-1&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span></div></td></tr></tbody></table></div>
<p>I have to get 4 tags (t1, t2, t3, t4 with t1 and t4 containing their child nodes).<br />
My regex knowledge is unfortunately very basic, so I googled for a ready to use regex, but none satisfied my need&#8230; all the examples I found didn&#8217;t handle properly nested tags&#8230; so, after some hours of testing I realized my own regex (my first real one), the result is the following:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> pattern <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/&lt;(onetag)[^&lt;&gt;]*&gt;(&lt;\1[^&lt;&gt;]*&gt;&lt;\/\1&gt;)*&lt;\/\1&gt;/gi</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>In my case I&#8217;m using that pattern in Javascript, but I think it can be used with any language, because it doesn&#8217;t make use of advanced features like &#8220;atomic grouping&#8221; and these kind of &#8220;black magics&#8221;. To match the desired tag you can use it by replacing &#8220;onetag&#8221; with the tag you are looking for (even a tag with a namespace like &#8220;&lt;foo:mytag&gt;&#8221;).</p>
<p>EDIT:</p>
<p>The pattern above will work only if applied to a single line string (ie: var myString = &#8220;&lt;onetag id=&#8217;t1&#8242;&gt;&#8230;&#8221;), if you use that pattern on a &#8220;complex string&#8221; (a string containing spaces and new lines) it won&#8217;t works properly. Fortunately you can remove &#8220;bad characters&#8221; before by using a simple replace:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> parsedString <span style="color: #339933;">=</span> originalString.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\s(?!\w)/gi</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>pattern<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p><strong>\s(?!\w)</strong> will match any space and new line not followed by an alphanumeric characters (in this way spaces between tag attributes won&#8217;t be removed)</p>
<p>EDIT 2:</p>
<p>The pattern <strong>/<(onetag)[^<>]*>(<\1[^<>]*><\/\1>)*<\/\1>/gi</strong> won&#8217;t works properly in presence of several type of nested tags, ie:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t1&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;anothertag&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t1-1&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t1-2&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>anothertag&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t2&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t3&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
<span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t4&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;anothertag&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;onetag <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;t4-1&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>anothertag&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span>onetag&gt;</span></div></td></tr></tbody></table></div>
<p>The updated pattern is the following:</p>
<div class="codecolorer-container javascript mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">var</span> newP <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/&lt;(onetag)[^&lt;&gt;]*&gt;.*?(&lt;\1[^&lt;&gt;]*&gt;.*?&lt;\/\1&gt;)*.*?&lt;\/\1&gt;/gi</span><span style="color: #339933;">;</span></div></td></tr></tbody></table></div>
<p>I hope this will works without further modifications :P</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/09/10/top-level-html-xml-tags-regex-regular-expressions/' addthis:title='Find outermost top level XML/HTML tags with regular expressions ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/09/10/top-level-html-xml-tags-regex-regular-expressions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Adapting iframe height to its content with 2 lines of javascript</title>
		<link>http://www.daveoncode.com/2009/06/12/adapting-iframe-height-to-its-content-with-2-lines-of-javascript/</link>
		<comments>http://www.daveoncode.com/2009/06/12/adapting-iframe-height-to-its-content-with-2-lines-of-javascript/#comments</comments>
		<pubDate>Fri, 12 Jun 2009 09:38:06 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[html]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=435</guid>
		<description><![CDATA[In a project, I have to use an iframe and I need that its height is automatically calculated based on its content. However is not possible to accomplish this by simply setting the attribute height to 100%, instead we must smartly use javascript to dynamically assign that value after a computation. The solution I&#8217;m going [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/06/12/adapting-iframe-height-to-its-content-with-2-lines-of-javascript/' addthis:title='Adapting iframe height to its content with 2 lines of javascript ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>In a project, I have to use an iframe and I need that its height is automatically calculated based on its content. However is not possible to accomplish this by simply setting the attribute height to 100%, instead we must smartly use javascript to dynamically assign that value after a computation. The solution I&#8217;m going to show you is not 100% my work but instead an optimization and a better implementation of clever intuitions I found on the web.<br />
The &#8220;trick&#8221; works in this way:</p>
<p>1. The iframe should has an ID assigned:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">iframe</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;slideshow_frame&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;frame.html&quot;</span> <span style="color: #000066;">frameborder</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;100%&quot;</span> <span style="color: #000066;">marginheight</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">marginwidth</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;0&quot;</span> <span style="color: #000066;">scrolling</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;no&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">iframe</span>&gt;</span></div></td></tr></tbody></table></div>
<p>2. the page loaded into the iframe should contains all the html into an &#8220;easy retrievable&#8221; container (a div with an ID is perfect!):</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;content&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background: red; width: 400px; height: 120px&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background: green; width: 400px; height: 300px&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;background: yellow; width: 400px; height: 60px&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span></div></td></tr></tbody></table></div>
<p>3. on the iframe&#8217;s page &#8220;onload&#8221; event, a function will be executed to adjust its height:</p>
<div class="codecolorer-container html4strict mac-classic" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br /></div></td><td><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">function resizeIframe(iframeID) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; var iframe = window.parent.document.getElementById(iframeID);<br />
&nbsp; &nbsp; var container = document.getElementById(&quot;content&quot;);<br />
<br />
&nbsp; &nbsp; iframe.style.height = container.offsetHeight + &quot;px&quot;;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
}</div></td></tr></tbody></table></div>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/06/12/adapting-iframe-height-to-its-content-with-2-lines-of-javascript/' addthis:title='Adapting iframe height to its content with 2 lines of javascript ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/06/12/adapting-iframe-height-to-its-content-with-2-lines-of-javascript/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Jetpack: building Firefox plugin with Javascript (and jQuery)</title>
		<link>http://www.daveoncode.com/2009/05/29/jetpack-firefox-plugin-with-javascript-jquery/</link>
		<comments>http://www.daveoncode.com/2009/05/29/jetpack-firefox-plugin-with-javascript-jquery/#comments</comments>
		<pubDate>Fri, 29 May 2009 15:08:00 +0000</pubDate>
		<dc:creator>Davide Zanotti</dc:creator>
				<category><![CDATA[browsers]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[mozilla]]></category>
		<category><![CDATA[plugin]]></category>

		<guid isPermaLink="false">http://www.daveoncode.com/?p=427</guid>
		<description><![CDATA[I discovered a couple of days ago a new open source project from Mozilla labs: Jetpack. It&#8217;s an &#8220;API set&#8221; which allows to build Firefox&#8217;s plugins by using Javascript, HTML (including new HTML 5 tags: &#8220;video&#8221; and &#8220;audio&#8220;) and CSS (oh yes, in a single word Ajax). Jetpack is still in an early development phase [...]<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/05/29/jetpack-firefox-plugin-with-javascript-jquery/' addthis:title='Jetpack: building Firefox plugin with Javascript (and jQuery) ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></description>
			<content:encoded><![CDATA[<p>I discovered a couple of days ago a new open source project from Mozilla labs: <a href="https://jetpack.mozillalabs.com">Jetpack</a>.<br />
It&#8217;s an &#8220;API set&#8221; which allows to build Firefox&#8217;s plugins by using Javascript, HTML (including new HTML 5 tags: &#8220;<strong>video</strong>&#8221; and &#8220;<strong>audio</strong>&#8220;) and CSS (oh yes, in a single word Ajax).<br />
Jetpack is still in an early development phase and released as 0.1 version.<br />
<a href="https://jetpack.mozillalabs.com/api.html">Here</a> you can find the (really brief) api documentation and <a href="https://wiki.mozilla.org/Labs/Jetpack/In_The_Wild">here</a> some examples.<br />
I would like to try it in the future</p>
<div class="addthis_toolbox addthis_default_style addthis_32x32_style" addthis:url='http://www.daveoncode.com/2009/05/29/jetpack-firefox-plugin-with-javascript-jquery/' addthis:title='Jetpack: building Firefox plugin with Javascript (and jQuery) ' ><a class="addthis_button_preferred_1"></a><a class="addthis_button_preferred_2"></a><a class="addthis_button_preferred_3"></a><a class="addthis_button_preferred_4"></a><a class="addthis_button_compact"></a></div>]]></content:encoded>
			<wfw:commentRss>http://www.daveoncode.com/2009/05/29/jetpack-firefox-plugin-with-javascript-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

