<?xml version="1.0" encoding="UTF-8" ?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
	<channel>
			<title>Tagged with "xhtml"</title>
			<link>http://www.addedbytes.com/feeds/tag-feed/</link>
			<description></description>
			<language>en</language>
			<copyright>Web Development in Brighton - Added Bytes 2006</copyright>
			<ttl>120</ttl>
			<item>
				<title>View Page Structure</title>
				<link>http://www.addedbytes.com/blog/code/view-page-structure/</link>
				<description><![CDATA[ A tool that outputs the structure of a page. Makes working with CSS (especially resolving inheritance issues) much easier. <p>A couple of days ago, I was having a little CSS trouble. In the end, it turned out that I had set a property of an element "above" in the document tree, and the problematic element was inheriting that property.</p>

<p>It struck me that it would be easier to work through this kind of CSS problem with some kind of simple tool to show how a page was put together. If I could see all the tags on the page in a nested format, with parent and child relationships obvious, and without all the text getting in the way, my life would be easier.</p>

<p>So, I put together this tool. In simple terms, it will fetch a page from a web server and output the tags within the page in a nested list. The JavaScript side of it will also highlight children of an element when you hover over it.</p>

<p>Classes and ids attributes are highlighted, as are tag names. Class and ID names, though, must be enclosed in quotation marks to be highlighted. Text, closing tags and line breaks are not shown. Though I can understand some people may find it useful to see text, I found it made the tree too large to be usable.</p>

<p>I've used it a few times, and am quickly finding it saves quite a lot of time solving simple CSS problems or conflicts. Which is exactly what it was supposed to do. Enjoy!</p>

<h3>Highlighting Issues</h3>

<p>When writing the tool, I came across a fairly unusual problem. I wanted, when the mouse was over an element, to highlight its children. However, this cannot be done with CSS (at least, I couldn't think of a way to make it work).</p>

<p>The problem with the CSS was that whenever you hover over an element, you are also hovering over its parents. So they, and their children, are highlighted - meaning everything is highlighted. For this reason, the highlighting of elements uses JavaScript.</p>

<h3>How to Use</h3>

<p>The page structure tool is written to accept a URL either by GET or POST. You can therefore use it one of two ways.</p>

<p>First, you can use the tool by visiting the URL below, replacing "##url##" with the address of the page you want to view:</p>

<p>http://www.addedbytes.com/view_structure.php?url=##url##</p>

<p>Alternatively, you can use the following form to submit an address to the page:</p>

<form action="http://www.addedbytes.com/view_structure.php" method="post"><label for="viewstrucinput">Enter URL</label> <input id="viewstrucinput" name="url" type="text" /> <input type="submit" value="View" /></form>

<h3>Bookmarklet</h3>

<p>To make life a little easier, I've coded a quick JavaScript bookmarklet for you to use, that, when activated, will automatically submit the URL of the page you are viewing to the tool. Simply copy or drag the link below to your links bar, your favourites folder or anywhere else you wish:</p>

<ul><li><a href="javascript:void(location.href='http://www.addedbytes.com/view_structure.php?url='+location.href);">View Page Structure</a></li></ul>

<h3>Notes</h3>

<ul><li>This tool works best with valid code, especially XHTML.</li><li>A certain amount of basic code improvement is done before processing (for example all empty tags are automatically closed).</li><li>Sites with non-empty tags that aren't closed properly may not show up correctly.</li><li>Sites with large amounts of nested code should still show up, but it may be difficult to view the output.</li></ul>

<h3>Example</h3>

<p>If you want to see an example of the output of this tool, you can view the  <a href="http://www.addedbytes.com/view_structure.php?url=http://www.addedbytes.com">structure for AddedBytes.com</a>.</p> <br><br>]]></description>
				<pubDate>Tue, 12 Oct 2004 16:24:00 +0100</pubDate>
				<guid isPermaLink="false">http://www.addedbytes.com/blog/code/view-page-structure/</guid>
				<dc:creator>Dave Child</dc:creator>
				<a href="/feeds/tag-feed/?tags=cheatsheet&amp;start=0" class="ditto_tag" rel="tag">cheatsheet</a>,<a href="/feeds/tag-feed/?tags=code&amp;start=0" class="ditto_tag" rel="tag">code</a>,<a href="/feeds/tag-feed/?tags=css&amp;start=0" class="ditto_tag" rel="tag">css</a>,<a href="/feeds/tag-feed/?tags=design&amp;start=0" class="ditto_tag" rel="tag">design</a>,<a href="/feeds/tag-feed/?tags=imported&amp;start=0" class="ditto_tag" rel="tag">imported</a>,<a href="/feeds/tag-feed/?tags=resources&amp;start=0" class="ditto_tag" rel="tag">resources</a>,<a href="/feeds/tag-feed/?tags=tool&amp;start=0" class="ditto_tag" rel="tag">tool</a>,<a href="/feeds/tag-feed/?tags=tools&amp;start=0" class="ditto_tag" rel="tag">tools</a>,<a href="/feeds/tag-feed/?tags=useful&amp;start=0" class="ditto_tag" rel="tag">useful</a>,<a href="/feeds/tag-feed/?tags=webdesign&amp;start=0" class="ditto_tag" rel="tag">webdesign</a>,<a href="/feeds/tag-feed/?tags=xhtml&amp;start=0" class="ditto_tag" rel="tag">xhtml</a>
			</item>

			<item>
				<title>On-The-Fly Validation</title>
				<link>http://www.addedbytes.com/blog/code/on-the-fly-validation/</link>
				<description><![CDATA[ A tool to help automatically fix most common (X)HTML errors before outputting a page to the user. <p>Of all coding errors in websites (usually highlighted by code validators), there are a few that crop up time and time again. These common coding bugs account for more 90% of the mistakes in web sites. Despite being so prevalent, most designers still allow these basic errors to creep into their code.</p>

<p><em>Note: Many people may consider "validation errors" unimportant. However, if you are going to write a web page in a specific language it makes sense to actually use that language properly, rather than making up your own random dialect. After all, can you be sure that that dialect will be interpreted the same way every single time? Any while many people find errors like this easy to ignore, they should remember that while they might not stop a page being usable, what validators bring up are <strong>"coding errors"</strong> - mistakes in the markup of the page.</em></p>

<p>These scripts are intended to make life a touch easier for busy developers. Of course, these scripts will slow down your site, and are no substitute for actually writing valid code in the first place. They are intended to catch the occasional bug that you may have missed, or that may be introduced through a comments system, for example.</p>

<p>In order to make use of the following code, you will need to be using PHP 4 or higher on Apache. The following scripts make use of output buffering and work best with a <a href="http://www.addedbytes.com/php/caching-output-in-php">caching system</a> in place as well.</p>

<p>This script will not remove all of your validation errors. It cannot remove them all without running very slowly - there is a lot to check in each document. However, it can catch a few of the more common bugs that most designers miss at least once in a website.</p>

<p>To begin, the scripts we use start output buffering. This means that rather than send the page to the user as it is created, the page is saved on the server until the server is told to output the page (or the script ends). This will allow us to modify page output without needing to worry about editing the PHP behind it. To start output buffering, you need to include the following code at the top of each page. You can include it using the "include()" or "require()" functions, or using htaccess's superb auto_prepend_file function (which you can see in use in this <a href="http://www.addedbytes.com/php/caching-output-in-php">caching tutorial</a>).</p>

<pre class="php">ob_start();</pre>

<p>After the script has run, we need to include another script at the end to process the page and output it to the user. You can, again, use "include()" or "require()", or auto_append_file in htaccess to include this script.</p>

<p>The script itself runs in three steps. The first step, below, grabs the contents of the output buffer. This will create a variable called "$output" that contains the page we were about to send to the user. $output contains the HTML after all PHP has run as normal, so the variable literally only contains what the user would normally see. The second line empties the output buffer (but does not send its contents to the user).</p>

<pre class="php">$output = ob_get_contents();
$output = trim($output);
ob_end_clean();</pre>

<p>Now $output contains the page we are about to send the user, it is time to run the various checks we want, to make sure there are no validation errors in place.</p>

<pre class="php">if ((strpos($output, "&lt;!DOCTYPE") &gt; strpos($output, "&lt;html")) or (strpos($output, "&lt;!DOCTYPE") === false)) {
    $output = str_replace('$lt;html', "$lt;!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n$lt;html", $output);
}</pre>

<p>First, we check for a DTD. These are important, as they tell the user agent (e.g. the browser) what language a page is written in. The above checks for the presence of a DTD before the &lt;html&gt; tag, and if it is missing it adds in the DTD for HTML 4.01 Transitional - probably the most common one in use today.</p>

<pre class="php">function encode_chars($text) {
    $text = str_replace("&lt;", "&amp;lt;", $text);

    $tag_list = '((\/?)(!DOCTYPE|!--|a(bbr|cronym|ddress|pplet|rea)?|b(ase(font)?|do|ig|lockquote|ody|r|utton)?|c(aption|enter|ite|(o(de|l(group)?)))|d(d|el|fn|i(r|v)|l|t)|em|f(ieldset|o(nt|rm)|rame(set)?)|h(1|2|3|4|5|6|ead|r|tml)|i(frame|mg|n(put|s)|sindex)?|kbd|l(abel|egend|i(nk)?)|m(ap|e(nu|ta))|no(frames|script)|o(bject|l|pt(group|ion))|p(aram|re)?|q|s(amp|cript|elect|mall|pan|t(r(ike|ong)|yle)|u(b|p))|t(able|body|d|extarea|foot|h|itle|r|t)|u(l)?|var)([^&gt;]*))';

    $text = preg_replace("/(&amp;lt;)" . $tag_list . "(&gt;)/mi", "&lt;$2&gt;", $text);
    $text = preg_replace("/(&gt;[^&lt;]*)&gt;/mi", "$1&amp;gt;", $text);
    $text = str_replace("/&gt;", "&gt;", $text);

    return $text;
}

$output = encode_chars($output);</pre>

<p>Next, we run a function on the script to check the tags on the page. Any tags that don't belong there are encoded so they are displayed rather than processed. We use the HTML 4.01 tag list, which means we will catch the worst of the invalid tags.</p>

<pre class="php">$output = preg_replace("/&lt;img([^&gt;]*)alt=([^&gt;]*)>/im", "&lt;img$1`alt=$2&gt;", $output);
$output = preg_replace("/&lt;img([^`|&gt;]*)&gt;/im", "&lt;img alt=\" \"$1&gt;", $output);
$output = preg_replace("/&lt;img([^&gt;]*)`alt=([^&gt;]*)&gt;/im", "&lt;img$1alt=$2&gt;", $output);</pre>

<p>This small snippet of code checks for alt attributes on images. If they are missing, it adds a single space as an alt attribute. This is by no means optimal (and the regex and technique is ugly - if anyone can improve on this, please give me a shout!), however does mean that if an alt attribute is missed, screen readers will not simply give the name of the image file. You should <em>always</em> take the greatest care to ensure that all images have appropriate alt attributes.</p>

<p>Next, we do a little language-specific work. In the above code, we removed all closing slashes (e.g. in a &lt;br /&gt; tag). Now, if we are using XHTML, we add them back in for the appropriate elements. We also check the case of elements if using XHTML, as tags and attributes must be lower case in XHTML. This will only affect attributes whose values are quoted.</p>

<pre class="php">function process_attributes($text) {
    return preg_replace("/ ([a-z]+)=\"([^( |\")]*)\"/mie", "' ' . strtolower('$1') . '=\"' . stripslashes('$2') . '\"'", $text);
}

if (strpos($output, "//W3C//DTD XHTML") !== false) {
    $output = encode_chars($output, "XHTML");
    $output = preg_replace("/&lt;(img|hr|meta|link|br|base|frame|input)([^&gt;]*)&gt;/mi", "&lt;$1$2 /&gt;", $output);
    $output = preg_replace("/&lt;(\/?)([a-z]+)( |&gt;)/mie", "'&lt;$1' . strtolower('$2') . '$3'", $output);
    $output = preg_replace("/&lt;([^&gt;]+)&gt;/mie", "'&lt;'.process_attributes(stripslashes('$1')).'&gt;'", $output);
    $output = preg_replace("/&amp;(?!#?[xX]?(?:[0-9a-fA-F]+|\w{1,8});)/i", "&amp;", $output);
}</pre>

<p>We also, at the end, encode any ampersands that should be encoded. Many thanks to [url=http://www.shauninman.com]Shaun Inman[/url] for the last line.</p>

<p>Finally, we need to send the processed output to the user.</p>

<pre class="php">$output = str_replace("&lt;b&gt;", "&lt;strong&gt;", $output);
$output = str_replace("&lt;i&gt;", "&lt;em&gt;", $output);
$output = str_replace("&lt;/b&gt;", "&lt;/strong&gt;", $output);
$output = str_replace("&lt;/i&gt;", "&lt;/em&gt;", $output);
echo $output;</pre>

<p>At this stage, the code sent to the user will have a valid Document Type Definition. All tags will be correctly closed whether using HTML or XHTML. All images will have alt attributes. If we're using XHTML, all tags and attributes will be lower case (as long as the attributes are quoted). All invalid opening and closing tags will have been encoded. All ampersands should be properly encoded. And for good measure, we've replaced all bold (&lt;b&gt;) and italic (&lt;i&gt;) tags with the proper &lt;strong&gt; and &lt;em&gt; tags.</p>

<p>If you put it all together, you get the following code to be included at the end of each script:</p>

<pre class="php">&lt;?php

$output = ob_get_contents();
$output = trim($output);
ob_end_clean();
    
function process_attributes($text) {
    return preg_replace("/ ([a-z]+)=\"([^( |\")]*)\"/mie", "' ' . strtolower('$1') . '=\"' . stripslashes('$2') . '\"'", $text);
}

function encode_chars($text) {
    $text = str_replace("&lt;", "&amp;lt;", $text);

    $tag_list = '((\/?)(!DOCTYPE|!--|a(bbr|cronym|ddress|pplet|rea)?|b(ase(font)?|do|ig|lockquote|ody|r|utton)?|c(aption|enter|ite|(o(de|l(group)?)))|d(d|el|fn|i(r|v)|l|t)|em|f(ieldset|o(nt|rm)|rame(set)?)|h(1|2|3|4|5|6|ead|r|tml)|i(frame|mg|n(put|s)|sindex)?|kbd|l(abel|egend|i(nk)?)|m(ap|e(nu|ta))|no(frames|script)|o(bject|l|pt(group|ion))|p(aram|re)?|q|s(amp|cript|elect|mall|pan|t(r(ike|ong)|yle)|u(b|p))|t(able|body|d|extarea|foot|h|itle|r|t)|u(l)?|var)([^&gt;]*))';

    $text = preg_replace("/(&amp;lt;)" . $tag_list . "(&gt;)/mi", "&lt;$2&gt;", $text);
    $text = preg_replace("/(&gt;[^&lt;]*)&gt;/mi", "$1&amp;gt;", $text);
    $text = str_replace("/&gt;", "&gt;", $text);

    return $text;
}

if ((strpos($output, "&lt;!DOCTYPE") &gt; strpos($output, "&lt;html")) or (strpos($output, "&lt;!DOCTYPE") === false)) {
    $output = str_replace('&lt;html', "&lt;!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\"&gt;\n&lt;html", $output);
}

$output = encode_chars($output);
$output = preg_replace("/&lt;img([^&gt;]*)alt=([^&gt;]*)>/im", "&lt;img$1`alt=$2&gt;", $output);
$output = preg_replace("/&lt;img([^`|&gt;]*)&gt;/im", "&lt;img alt=\" \"$1&gt;", $output);
$output = preg_replace("/&lt;img([^&gt;]*)`alt=([^&gt;]*)&gt;/im", "&lt;img$1alt=$2&gt;", $output);

if (strpos($output, "//W3C//DTD XHTML") !== false) {
    $output = preg_replace("/&lt;(img|hr|meta|link|br|base|frame|input)([^&gt;]*)&gt;/mi", "&lt;$1$2 /&gt;", $output);
    $output = preg_replace("/&lt;(\/?)([a-z]+)( |&gt;)/mie", "'&lt;$1' . strtolower('$2') . '$3'", $output);
    $output = preg_replace("/&lt;([^&gt;]+)&gt;/mie", "'&lt;'.process_attributes(stripslashes('$1')).'&gt;'", $output);
    $output = preg_replace("/&amp;(?!#?[xX]?(?:[0-9a-fA-F]+|\w{1,8});)/i", "&amp;", $output);
}

$output = str_replace("&lt;b&gt;", "&lt;strong&gt;", $output);
$output = str_replace("&lt;i&gt;", "&lt;em&gt;", $output);
$output = str_replace("&lt;/b&gt;", "&lt;/strong&gt;", $output);
$output = str_replace("&lt;/i&gt;", "&lt;/em&gt;", $output);
echo $output;

?&gt;</pre> <br><br>]]></description>
				<pubDate>Thu, 01 Jul 2004 13:40:00 +0100</pubDate>
				<guid isPermaLink="false">http://www.addedbytes.com/blog/code/on-the-fly-validation/</guid>
				<dc:creator>Dave Child</dc:creator>
				<a href="/feeds/tag-feed/?tags=html&amp;start=0" class="ditto_tag" rel="tag">html</a>,<a href="/feeds/tag-feed/?tags=php&amp;start=0" class="ditto_tag" rel="tag">php</a>,<a href="/feeds/tag-feed/?tags=regexp&amp;start=0" class="ditto_tag" rel="tag">regexp</a>,<a href="/feeds/tag-feed/?tags=validation&amp;start=0" class="ditto_tag" rel="tag">validation</a>,<a href="/feeds/tag-feed/?tags=xhtml&amp;start=0" class="ditto_tag" rel="tag">xhtml</a>
			</item>

			<item>
				<title>Printer Friendly Pages for Beginners</title>
				<link>http://www.addedbytes.com/articles/for-beginners/printer-friendly-pages/</link>
				<description><![CDATA[ Most designers create printer friendly pages by creating two seperate pages for each single page of information. This article will teach you how to do the same with CSS and no duplication. <p>CSS is a great new tool for web designers. Now it is more widely supported, and browser-makers are sticking to the W3C standards, we can begin to explore the possibilities it has offered us since the 90s but that we could not make use of.</p>

<p>CSS allows you to seperate your design from your markup, making maintenance infinitely easier, and your site more likely to work in different devices, both of which are definite pluses. It also, however, allows you to tell a page to display a different way depending upon what device is being used to render it.</p>

<p>This is achieved using the "media" attribute of the "&lt;link&gt;" or "&lt;style&gt;" tags, like so:</p>

<code>&lt;link media="screen" href="style.css" type="text/css"&gt;</code>

<code>&lt;style media="screen" type="text/css"&gt;</code>

<p>There are several media types available to you - [i]all, aural, braille, handheld, print, projection, screen, tty, tv[/i] - and a [url=http://www.w3.org/TR/1998/PR-CSS2-19980324/media.html#media-types]list of available media types with more detail[/url] is available from W3C (where you can find out more about media groups, as well).</p>

<p>With such a range of options, you can, if you like, create one HTML document that will display exactly as you like depending entirely upon which device is being used to view it. You can hide large images from PDAs, you can specify seperate font styling for overhead  projectors - you can write your site so that it works well with the technology at the user's disposal. And one very useful side effect of this is that you can specify an entire print style without the need for copies of every page of your content, like the following, that will use the "normal.css" style sheet for screens, and the "printer.css" style sheet when printing.</p>

<code>&lt;link media="screen" href="normal.css" type="text/css"&gt;
&lt;link media="print" href="printer.css" type="text/css"&gt;</code>

<p>For print styling, there are a lot of factors to consider. Are many users going to want to print out your header? No. But do you want your logo on the page? Yes. Is the navigation worth printing? No. Each item on a web page serves a purpose, and on paper, that may not be worth much.</p>

<p>It is best, then, to start at the beginning. With an unstyled HTML document. Pick a page to start working on, and remove all styles from it. With an unformatted page in front of you, it should be pretty easy to see what needs styling and how, but don't rush in. There's a lot to think about.</p>

<h3>Fonts</h3>

<p>Fonts are very different on the web to print. Sans-serif fonts are, allegedly, the best to use on the web. Serif fonts are, apparently, better on paper. So first things first, set your font to a serif font you like (personally, I like Georgia for Windows). As with picking all fonts, make sure you provide alternatives for those without the first-choice font.</p>

<p>Font sizing is a bit different on paper too. It's usually best just to leave this to the default, but if you want to specify a font, make sure you run plenty of tests to make sure it's easily legible.</p>

<code>* {
    font-family: Georgia, "Times New Roman", Times, serif;
    font-size: 12pt;
}</code>

<h3>Links</h3>

<p>Links are not going to be a lot of use on paper, by their very nature. But that does not make them useless.</p>

<code>a {
    color:#000000;
    text-decoration: underline;
}

a:after {
    content: " ( "attr(href)" )";
}</code>

<p>The above, if added to a printer style sheet, will make all links black and underlined, and add the address of the link in brackets afterwards, meaning that if a user did want to visit any site linked to from that page at a later date, they can.</p>

<h3>Spacing</h3>

<p>In print, margins and spacings are as important as with the web. There is no harm in adding in a little space between words and lines, and a decent gap around the edges, if it will make a print-version easier to read.</p>

<code>body {
    padding: 10%;
    line-height: 1.2;
    word-spacing: 1px;
}</code>

<h3>Hide Useless Stuff</h3>

<p>You can hide elements in this style sheet easily enough, using display none. This will save some space on the paper, and reduce useless junk on the page, something the user will be very grateful for. Elements worth hiding are usually form elements, some images, marquees, and flash content. If you are unsure, print the page and then work through that page deciding what is useful and what is not.</p>

<code>.navigation {
    display: none;
}</code>

<h3>Colours</h3>

<p>Print colours will show up very differently to screen colours, especially as a high proportion still have black and white printers. Even those who don't will be grateful to you if you keep the proportion of colour to a minimum, simply because of the expense.</p>

<p>Highlight and emphasize things using italics, underlining, boldness and size in your print style sheet. Remove all references to colours where you can, save to set colours to black.</p>

<h3>Page Address</h3>

<p>You may want to add the address of the page itself to the page, and hide it, except when the page is printed, so that the user can return to your page later if they wish.</p>

<h3>Tell the User</h3>

<p>A user may see a page without a "printer friendly" link and print anyway, but they may be put off. Try and add a note to your page explaining that the page they are now viewing is printer friendly, and encouraging them to print it.</p>

<h3>Finally</h3>

<p>Do plenty of testing and reading. The print industry has been around a very very long time, and there are plenty of resources around for you to research what makes a document easy to read on paper as opposed to screen - make use of them. The more you test, too, the more likely you will be to notice small flaws in the print version of your site, in the same way that looking at your site during the design process highlights tiny imperfections that make all the difference once fixed.</p>

<p>Wish these simple measures, you should now be able to create an alternative version of your entire site, that will be invisible to the user until they actually print a page. They do not need to do anything bar click the print button.</p> <br><br>]]></description>
				<pubDate>Mon, 15 Mar 2004 18:30:00 +0000</pubDate>
				<guid isPermaLink="false">http://www.addedbytes.com/articles/for-beginners/printer-friendly-pages/</guid>
				<dc:creator>Dave Child</dc:creator>
				<a href="/feeds/tag-feed/?tags=css&amp;start=0" class="ditto_tag" rel="tag">css</a>,<a href="/feeds/tag-feed/?tags=design&amp;start=0" class="ditto_tag" rel="tag">design</a>,<a href="/feeds/tag-feed/?tags=printing&amp;start=0" class="ditto_tag" rel="tag">printing</a>,<a href="/feeds/tag-feed/?tags=web&amp;start=0" class="ditto_tag" rel="tag">web</a>,<a href="/feeds/tag-feed/?tags=webdesign&amp;start=0" class="ditto_tag" rel="tag">webdesign</a>,<a href="/feeds/tag-feed/?tags=xhtml&amp;start=0" class="ditto_tag" rel="tag">xhtml</a>
			</item>
	</channel>
</rss>