{"id":515,"date":"2009-07-22T18:19:37","date_gmt":"2009-07-22T22:19:37","guid":{"rendered":"http:\/\/www.outofwhatbox.com\/blog\/?p=515"},"modified":"2009-07-27T16:21:13","modified_gmt":"2009-07-27T20:21:13","slug":"filling-the-cracks","status":"publish","type":"post","link":"https:\/\/www.outofwhatbox.com\/blog\/2009\/07\/filling-the-cracks\/","title":{"rendered":"Filling The Cracks"},"content":{"rendered":"<div class=\"oowbbtw\">The <strong>latest<\/strong> posted version of SyntaxHighlighter is <a href=\"http:\/\/static.outofwhatbox.com\/downloads\/SyntaxHighlighter\/syntaxhighlighter.zip\">here<\/a>. For the change history, or to download older versions from this site, see the <a href=\"http:\/\/www.outofwhatbox.com\/blog\/syntaxhighlighter-downloads\/\">downloads page<\/a>.<\/div>\n<div class=\"alignleft\" style=\"width: 190px\"><img loading=\"lazy\" alt=\"Mind The Gap!\" src=\"http:\/\/img.outofwhatbox.com\/shGutter\/gap.jpg\" title=\"Photo from http:\/\/www.flickr.com\/photos\/jvk\/387951549\/ )\" width=\"180\" height=\"135\" \/><\/div>\n<p><a href=\"http:\/\/alexgorbatchev.com\/wiki\/SyntaxHighlighter\">SyntaxHighlighter<\/a> can display line numbers on the left side of the code block, in an area called the <em>gutter<\/em>. As a result of other changes that I&#8217;d made, the gutter sometimes showed gaps between the line number elements. I added some JavaScript to resize the number elements so that they&#8217;d be contiguous, closing any gaps. However, that code needs the <code>offsetHeight<\/code> for each line in the code block. As I then discovered, when JavaScript so much as <strong>reads<\/strong> the <code>offsetHeight<\/code> of an element, it&#8217;s likely that the browser will need to <a href=\"http:\/\/www.stubbornella.org\/content\/2009\/03\/27\/reflows-repaints-css-performance-making-your-javascript-slow\/\">reflow the page<\/a> to calculate the value. Repeat that for enough lines, and you&#8217;ve got a bit of a performance issue (or I do, depending on how you look at it.)<\/p>\n<p>At one point, I&#8217;d thought that I could bypass the height adjustment for the most common cases (e.g., text lines that are short enough not to wrap.) But life and HTML come with few guarantees; testing showed that some browsers, in some environments, would still produce some 1-pixel gaps like so:<\/p>\n<div id=\"attachment_179\" style=\"width: 483px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-179\" loading=\"lazy\" class=\"size-full wp-image-179\" title=\"BrokenGutter\" src=\"http:\/\/img.outofwhatbox.com\/shGutter\/BrokenGutter.png\" alt=\"Gutter, on the left, showing unwanted white lines.\" width=\"473\" height=\"107\" \/><p id=\"caption-attachment-179\" class=\"wp-caption-text\">Gutter, left, showing unwanted white lines.  <em>(The text on the right is blurred; do not adjust your eyes.)<\/em><\/p><\/div>\n<p>(The gaps appeared below lines that had keywords highlighted in boldface fonts.  Depending on the fonts that the browser is using, the boldface can sometimes add a pixel to the height of the text line.)<\/p>\n<p>Sometimes the best way to solve a problem is to make it no longer <em>be<\/em> a problem. I&#8217;d caused the gaps to appear by changing how backgrounds are rendered. In the original SyntaxHighlighter, the number elements are transparent; the gutter&#8217;s background is the background of the top-level <code>&lt;div><\/code> for the entire code block. There may be gaps between the number elements, but those gaps <em>can&#8217;t be seen<\/em>. I couldn&#8217;t easily go back to that approach, but thinking about it inspired a new one: By stretching the first gutter element vertically, it could be used to provide the background for the gutter. The remaining gutter elements would then be laid out over that first one. Here&#8217;s an illustration:<\/p>\n<div id=\"attachment_179\" style=\"width: 483px\" class=\"wp-caption alignnone\"><img aria-describedby=\"caption-attachment-179\" loading=\"lazy\" class=\"size-full wp-image-179\" title=\"BrokenGutter\" src=\"http:\/\/img.outofwhatbox.com\/shGutter\/GutterHow.png\" alt=\"The first number element fills the gutter vertically; the remaining elements are placed on top of it, leaving no visible gaps.\" width=\"473\" height=\"107\" \/><p id=\"caption-attachment-179\" class=\"wp-caption-text\">The first number element fills the gutter vertically; the remaining elements are placed on top of it, leaving no <em>visible<\/em> gaps.<\/p><\/div>\n<p>The gaps are invisible once more.  The associated JavaScript still causes some reflow, but no more than five times per code block, a much smaller overhead in most cases.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes the best way to solve a problem is to make it no longer <em>be<\/em> a problem. This was the case with a problem that I&#8217;d created &#8212; and have now fixed &#8212; in my version of SyntaxHighlighter.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20],"tags":[22,21,23,24],"_links":{"self":[{"href":"https:\/\/www.outofwhatbox.com\/blog\/wp-json\/wp\/v2\/posts\/515"}],"collection":[{"href":"https:\/\/www.outofwhatbox.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.outofwhatbox.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.outofwhatbox.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.outofwhatbox.com\/blog\/wp-json\/wp\/v2\/comments?post=515"}],"version-history":[{"count":14,"href":"https:\/\/www.outofwhatbox.com\/blog\/wp-json\/wp\/v2\/posts\/515\/revisions"}],"predecessor-version":[{"id":548,"href":"https:\/\/www.outofwhatbox.com\/blog\/wp-json\/wp\/v2\/posts\/515\/revisions\/548"}],"wp:attachment":[{"href":"https:\/\/www.outofwhatbox.com\/blog\/wp-json\/wp\/v2\/media?parent=515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.outofwhatbox.com\/blog\/wp-json\/wp\/v2\/categories?post=515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.outofwhatbox.com\/blog\/wp-json\/wp\/v2\/tags?post=515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}