Pretty Diff - Documentation

For any questions, comments, requests, or feedback please join the Pretty Diff mailing list. Find Pretty Diff on GitHub.

Since this tool is client-side JavaScript only, meaning that execution occurs on the local computer only and resulting output is not transmitted or stored, it is safe for processing classified information.

Table of Contents

  1. About This Application
    1. Introduction
    2. Some Available Features
  2. Known Issues
  3. Execution
  4. Web Tool URI Parameters
  5. Pretty Diff Function
    1. Overview
    2. Practices of Pretty Diff
    3. Properties of Pretty Diff's One Function Argument
    4. Option String
    5. Input and Output
    6. Executing with Node.js
    7. Executing with Windows Script Host
  6. Beautification
    1. JavaScript
    2. CSS
    3. CSV
    4. Markup
    5. Beautification Options
  7. Minification
    1. JavaScript
    2. CSS
    3. CSV
    4. Markup
    5. Minification Options
  8. Pretty Diff
    1. Diff Code
    2. Diff Process
    3. Diff Options
  9. Component Files

About This Application

Introduction

This tool was originally created to compare minified code by attaching a beautifier and minifier to a file comparison tool. Over the years it has grown into custom language parsers capable of performing a variety of language analysis. This application is 100% vanilla JavaScript and is API independent.

Some Available Features

Known Issues

  1. Webkit browsers, such as Google Chrome and Apple Safari, do not support beautification of white space characters or layout in textarea elements. As a result the web interface offers a slightly degraded experience for these browsers in order to prevent corruption of output. Please see these bugs for more experience: 51168 and 90739.
  2. In the difference report this collection of characters, "$#lt;" and "$#gt;", may appear respectively as, "%#lt;" and "%#gt;", if those collections of characters are interrupted by starting or end of a difference.
  3. Markup beautification will output flawed data if less than characters, "<", are included into sections of content and not escaped. This error occurs regardless if less than characters embedded within content are quoted or not.

Execution

Web Tool URI Parameters

  1. c - This parameter receives the name of a supported color scheme.
  2. d - This parameter receives a URI as a value that points a difference code source. If the value of this URI contains ampersand characters, &, or question mark characters, ?, please escape the ampersands characters to "%26" and the question mark characters to %3F.
  3. jscorrect - This presence of this parameter sets the jscorrect parameter to boolean "true". No value is required.
  4. jsscope - This parameter forcefully applies the jsscope feature of the JSPretty library and does not require a value.
  5. l - This parameter receives a value of markup, html, auto, javascript, js, css, csv, or text. This bypasses all other language settings and determinations thereby forcefully applying the language against the supplied value to this parameter. The value of "html" is identical to the value "markup" except that it forces the option "Presume SGML type HTML" for all modes while "markup" unsets this option. The values "javascript" and "js" are treated equally.
  6. m - This parameter receives a value of beautify, minify, or diff. This parameter sets the mode of the tool.
  7. s - This parameter receives a URI as a value that points a code source. If the value of this URI contains ampersand characters, &, or question mark characters, ?, please escape the ampersands characters to "%26" and the question mark characters to %3F. The tool executes this code automatically on page load for beautify and minify modes, but only for diff mode if a source is provided with the d parameter.
  8. codemirror - If this parameter is present with a value of false the CodeMirror application library will be discarded in favor of HTML textarea elements.

The parameters are optional and are provided soley for portability. The parameters may occur in any order. Examples:

  1. http://prettydiff.com/?l=html&s=http://google.com/&m=beautify
  2. http://prettydiff.com/?s=http://www.amazon.com/Definitive-XML-Schema-Priscilla-Walmsley/dp/0130655678/ref=sr_1_1%3Fie=UTF8%26qid=1312890971%26sr=8-1&html&m=beautify

Pretty Diff Function

Overview

Pretty Diff is an application written entirely in JavaScript and expressed as a single function named 'prettydiff()'. This application was originally written as a means to algorithmically difference between two file similar pieces of code regardless of minification. The result is a fast difference engine offering many options that allows access to the world's most advanced markup beautification algorithm.

While the Pretty Diff application is expressed as a single function it is actually a collection of several components of which many were started by different authors for unrelated projects. Every one of those components that began externally have been heavily modified for either increased processing efficiency, expanded functionality, or JSLint compliance. As a result each of those components must be considered a fork of the originally maintained code that is unique to Pretty Diff.

The Pretty Diff application is completely isolated from DOM methods with only one exception. This means the prettydiff() function receives all data from its one argument and returns output. At this time the charDecoder.js code is entirely reliant upon DOM access to allow an external application transform character entity references into character literals. The only solution to allow similar functionality without DOM access is to include a character map with the charDecoder function. There are no plans, at this time, to write a JavaScript formulation of the Unicode character map.

The Pretty Diff function receives input from a single argument that is an object literal containing named properties as discussed in detail a few sections down. If the input is valid Pretty Diff will return an array of two values. The first value is the beautified source code if in "Beautify" mode, the minified source code if in "Minify" mode, or an HTML formatted diff report if in "Pretty Diff" mode. The second output value is an HTML formatted analysis report of the submitted code.

Practices of Pretty Diff

Pretty Diff hopes to encourage conventions of efficiency, but not at cost to recursion, regression testing, or altered functionality. For example consider the situation of minifying markup. In a typical scenario the practice of code minifcation is to remove all code comments and all white space characters not absolutely necessary for syntax interpretation. If the most impactful form of minification is exercised upon markup the functionality of the code is certainly changed. Consider these two examples:

  1. <p>This is a paragraph with a text field. <input type="text"/></p>
  2. <p>This is a paragraph with a text field.<input type="text"/></p>

The difference between the two examples above is the difference of a single space character between the period and the input tag. In markup white space characters are tokenized when the code is parsed to output by default, and rarely is this default challenged. Tokenized white space means sequential white space characters are converted to a single space character and then sequential space characters are converted to a single space character. This means the presence of some white space characters are completely trivial, while others are not. A single space character separating words of content is not trivial if it is an isolated space. In the above sample the difference of a space separating the input tag from the content is also not trivial since it alters how tokenized content is interpreted.

If markup code were fully minified then all white space characters outside of syntax containers, such as tags, would be removed, thereby making the content illegible. Markup can still be correctly minified, but only when rendering of tokenized white space is fully considered. The opposite of this problem is accidental addition of white space characters from flawed beautification schemes. Consider the following two examples:

  1. <p>This is a statement with a <a href="#">hyperlink</a>.</p>
  2. <p>This is a statement with a<a href="#">hyperlink</a>.</p>

The differences between the two prior examples is that the second example introduces white space tokens where they do not exist in the first example. In the first example there are no characters between the opening <p> tag and the text or the opening <a> and the text while this is not true of the second example. Therefore these two statements are not similar enough for a logical comparison. A well crafted beautifier, or pretty printer, will take these differences into account so far as to alter the entirety of a code base for easier reading but not at the cost of manipulating how the code is parsed by a given interpreter.

Code must never be minified if it cannot be automatically recovered into an easily readable form and must never be beautified if such beautification changes how the code is parsed. This is the importance of regression. The most extreme form of minification is referred to obfuscation. Obfuscation removes all code comments and all white space characters not absolutely required for syntax compliance, but goes one step further and changes all variable and command names to the fewest available character length. Pretty Diff considers the practice of obfuscation to be harmful as its practice eliminates the possibility of regression. Without the possibility for regression recursive practices are improbable.

An instantiation of a pattern where the pattern's presence is available in the given instance without regard for multiplication is said to be idempotent. A recursive practice is the ability replicate an action where the replication does not harm the potential of further replication, or the idempotent nature of a pattern, upon or resulting from that action. In the case of Pretty Diff code that begins unminified should be capable of being minified, beautified, minified again, and so on without harm or difference to the functional integrity of the supplied code. Any process that prevents such recursive practices, such as obfuscation, are harmful and must be avoided.

Comments are the regression exception. There is no way to efficiently reduce code while retaining comments and documentation. Pretty Diff strongly recommends that documentation be separated from production code into either a redundant development version or into a separated documentation archive so that it can be preserved apart from the production code.

There is one extremely limited exception to functional interference observed by Pretty Diff. The Cascading Style Sheets language provides a syntax and vocabulary that are limited and fully known. Therefore functional changes can, and are, supplied to CSS code during minification because in this one narrow instance there is no harm to regression. Superior minification can be performed by supplying minor functional changes to the code which be easily and intelligently reversed without error or prior knowledge of the code sample.

Properties of Pretty Diff's One Function Argument

Please note: As of 19 June 2011 Pretty Diff application no longer accepts formal function arguments. It only accepts a single argument, named api, which is an object literal that stores the application options as the following object properties.

  1. alphasort - If you wish to sort properties in CSS by type and then alphabetically this is your option. Accepts a boolean value and defaults to false. This option is perfectly safe in CSS but causes problems in SASS and LESS.
  2. comments - This property determines whether comments should be indented. This property is only used if the source property is set to beautify. Acceptable values are: indent and noindent.
  3. conditional - Use this argument to retain conditional HTML comments used by Internet Explorer in minification of HTML. This property takes boolean types and defaults to false.
  4. content - This property determines if string literals in JavaScript and content in markup should be normalized to a value of text prior to a diff operation. This property is only used if mode property is set to "diff". Acceptable values are boolean false and true.
  5. context - The value of this property stores a number or empty string so as to trigger the Context Size diff option. This property is only used if mode property is set to "diff".
  6. correct - The correct property is a limited attempt to automatically correct certain stylistic code problems that JSLint complains about. This property accepts a boolean type and defaults to false.
  7. csvchar - This property stores the string value used as a data separator for the "csv" language. Any string is accepted, but if value of lang property is not set to "csv" the value of this property is ignored.
  8. diff - The string value of this property stores the data that is to be differenced against value of the source property in a diff operation. Any string is accepted, but this property is only used if the mode property is set to "diff".
  9. diffcli - An option only available from the node-local.js API file for Node.js. This option will output a list of differences in color to the console. If more than one file is compared it will indicate which files are deleted or new. It takes a boolean and defaults to false. If the output option is omitted the diffcli option is assigned a value of true. If true and the context option is omitted it will default to a value of "2".
  10. diffcomments - Use this argument to retain code comments so that code and comments can be compared by the diff process. This property takes boolean types and defaults to false.
  11. difflabel - The value of this property sets a label describing the value of diff property. Any string is accepted.
  12. diffview - This property determines if the diff report should be expressed is a side-by-side comparison or a single inline view. This property is only used if mode property is set to "diff". Acceptable values are: "sidebyside" and "inline".
  13. force_indentation - This property determines if every piece of code and content must be indented or if the source should be beautified according to its semantics so that white space tokens are not added or removed. This property accepts boolean values and defaults to "false".
  14. html - This property determiens if the application must presume markup input to be an SGML form of the HTML language. This property is only used if lang property is set to "markup". Acceptable values are "html-no" and "html-yes".
  15. inchar - This property stores the character literal used for an indentation. Any string is accepted. A single indentation is the result of this value repeated insize times.
  16. indent - This property sets the style of indentation during JavaScript beautification. The default value "knr" sets a JSLint compliant beautification scheme and the other value "allman" puts opening curly braces on their own line.
  17. inlevel - The inlevel property pads JavaScript beautification with additional indentation. This property accepts a number and defaults to 0.
  18. insize - This property stores the character length of an indention. An integer or string containing an integer is accepted. If anything else is supplied this property defaults to a value of "4".
  19. jscorrect - This JavaScript only property determines if sloppiness should corrected for in the beautified output. Semicolons and curly braces are added where missing. Operators "++" and "--" are converted to "+= 1" provided they are not adjacent to arithmetic or arguments of a method. "new Object()" and "new Array()" are converted to "{}" and "[]". This argument accepts a boolean value and defaults to "false".
  20. jsscope - This property, if provided a value of boolean true, will produce HTML output for JavaScript beautification that colors variables based upon their scope of declaration. This feature highlights inheritance, scope depth, and closure.
  21. lang - This property tells the diff program which language it is receiving. Acceptable values are: "auto", "javascript", "css", "csv", "markup", and "text". The value "auto" allows the application to determine between CSS, JavaScript, and Markup without human effort.
  22. mode - This property determiens the operation to be performed. Acceptable values are: "beautify", "minify", and "diff".
  23. quote - This property determines if single quote characters should be converted to double quote characters prior to a diff operation. This property is only used if mode property is set to "diff". Acceptable values are boolean false and true.
  24. preserve - This property, if provided a boolean value of false, will remove all empty lines from JavaScript beautification.
  25. semicolon - This property determines if semicolon characters at the end of a line should be removed prior to a diff operation. This property is only used if mode property is set to "diff". Acceptable values are boolean false and true.
  26. source - The string value of this property is code that is to be beautified, minified, or represents the base text in a diff operation. Any string data is accepted.
  27. sourcelabel - The value of this property sets a label describing the value of source property in the diff report. Any string is accepted.
  28. space - This property, provided a boolean value of false, will remove the space following a function keyword for anonymous functions in JavaScript beautification.
  29. style - This property determines whether CSS and JavaScript code should be indented according to the surrounding markup or if they should be indented starting from 0. This property is only used if lang property is set to "markup" and mode property is set to "beautify" or "diff". Acceptable values are "indent" and "noindent".
  30. topcoms - This property determines whether minification should ignore comments at the top of JavaScript or CSS input before any code. This property is only used if the mode property is set to "minify". It accepts values of boolean "true" or "false".
  31. vertical - If you ever wanted to see your object properties, variable lists, or assignment lists vertically aligned for faster and easier reading this option is for you. It accepts boolean values and defaults to false.
  32. wrap - This argument accepts numeric values only and defaults to 0. The wrap argument determines how many columns text content may be in a markup document before wrapping onto a new line. The value 0 disables text wrapping.

Option String

The Pretty Diff option string is similar in convention to the JSLint option string. Pretty Diff will only process the first option string it encounters, but it will search the diff code if the option cannot be found in the source code. In order for the option string to be recognized it must start with /*prettydiff.com and end with */. The options are listed in this string separated by commas as a colon separated name value pair. The options match the exact value definition for the Pretty Diff application properties above and options that allow abstract values must have their values enclosed in either single or double quotes. The options can be listed in any order. To prevent possible corruption of the option string it should be separated from other code comments or content. These are examples of appropriate option strings:

Input and Output

The prettydiff function accepts all code input from JavaScript variables supplied to the function's source property and diff property. The code supplied to the diff property is used only during diff operations and is otherwise ignored.

The function outputs an array of two indexes. The first array index is always the processed data and the second array index contains some metadata. In the case of the "beautify" and "minify" operations the first index of the output array is the processed source code as text and the second array index is the code report, as seen generated on the client side tool, formatted as HTML. The output from the diff operation returns an HTML table of the actual diff output as the first array index and a some minor metadata about the number of errors in the second array index with both indexes formatted as HTML and neither comprising a complete HTML document. To form the diff output of the prettydiff function into a single HTML document I typically supply the following extra code:

  1. output = prettydiff(...);
  2. heading = '<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><head><title>Pretty Diff</title><link rel="canonical" href="http://prettydiff.com/" type="application/xhtml+xml"/><meta http-equiv="Content-Type" content="application/xhtml+xml;charset=UTF-8"/><meta name="robots" content="index, follow"/><meta name="DC.title" content="Pretty Diff - The difference tool"/><link rel="icon" type="image/x-icon" href="http://prettydiff.com/images/favicon.ico"/><link rel="meta" href="http://prettydiff.com/labels.rdf" type="application/rdf+xml" title="ICRA labels"/><meta http-equiv="pics-Label" content='(pics-1.1 "http://www.icra.org/pics/vocabularyv03/" l gen true for "http://prettydiff.com" r (n 0 s 0 v 0 l 0 oa 0 ob 0 oc 0 od 0 oe 0 of 0 og 0 oh 0 c 1) gen true for "http://www.prettydiff.com" r (n 0 s 0 v 0 l 0 oa 0 ob 0 oc 0 od 0 oe 0 of 0 og 0 oh 0 c 1))'/><meta name="author" content="Austin Cheney"/><meta name="description" content="Pretty Diff tool can minify, beautify, or diff between minified and beautified code.This tool can even beautify and minify HTML."/><meta name="distribution" content="Global"/><meta http-equiv="Page-Enter" content="blendTrans(Duration=0)"/><meta http-equiv="Page-Exit" content="blendTrans(Duration=0)"/><meta http-equiv="content-style-type" content="text/css"/><meta http-equiv="content-script-type" content="text/javascript"/><meta name="google-site-verification" content="qL8AV9yjL2-ZFGV9ey6wU3t7pTZdpD4lIetUSiNen7E"/><link rel="stylesheet" type="text/css" href="http://prettydiff.com/diffview.css" media="all"/></head><body><h1>Pretty Diff - The difference tool</h1><span class="clear"></span><div id="diffoutput">';
  3. return heading + output[1] + "</div>" + output[0] + "</body></html>";

Executing with Node.js

Node.js is a command line run time. Node.js execution could be the result of a one-time execution or part of a scripted automation process. To execute the prettydiff function with Node.js the following code needs to be added after the prettydiff function. Nothing else needs to occur for compatibility or integration with Node.js.

if(typeof exports!=="string"){exports.api=function(x){"use strict";return prettydiff(x);};}

Some Node.js API files are supplied as part of the Pretty Diff project. An actively maintained API for use on the local file system is supplied as api/node-local.js. An API for executing Pretty Diff as a service is also supplied, but is no longer maintained at api/node-service.js.

Executing with Windows Script Host (WSH)

Windows Script Host allows for a JavaScript run time in Windows environments from a command line with output directly returned to the command line or in a debugger window. To execute JavaScript with WSH a file is needed to supply the JavaScript function call, pass arguments from command line into a JavaScript compatible format, and to request dependencies.

An actively maintained API is provided at api/prettydiff.wsf. A WSF file follows basic XML syntax and may allow multiple operations of different languages to execute in tandem so long as each operation is confined to a job tag. The named elements in the example file are used to intercept arguments supplied via command line.

The example file would be operated for HTML compatibility using the following command: cscript prettydiff.wsf /source:"my_source_file.js" /html:true /mode:"beautify"

Writing the output of a WSH task into a file would require an additional ActiveX instruction in the wsh.wsf file or would require the automation of script execution in the context of the PowerShell language.

Beautification

JavaScript

JavaScript beautification uses jspretty function. This code is used in accordance with licensing provided in documentation embedded within the original code.

JavaScript is beautified in accordance with conventions expressed by JSLint beautification scheme. This essentially means that spaces are added after commas, object contents are indented, and a new line is added after each statement termination and object closing.

jspretty now warns on unnecessary use of the new keyword, which is any use except immediately preceeding one of these global objects:

"Date", "RegExp", "Error", "XMLHttpRequest", "FileReader", "ActiveXObject", "DataView", "ArrayBuffer", "Proxy", "DOMParser", "ParallelArray", "Int8Array", "Uint8Array", "Int16Array", "Uint16Array", "Int32Array", "Uint32Array", "Float32Array", "Float64Array", "Canvas", "CustomAnimation", "FadeAnimation", "Flash", "FormField", "Frame", "HotKey", "Image", "MenuItem", "MoveAnimation", "Point", "Rectangle", "ResizeAnimation", "RotateAnimation", "ScrollBar", "Shadow", "SQLite", "Text", "TextArea", "Timer", "URL", "Web", "Window"

Summary

An unassigned anonymous function creates the summary report and assigns its output to a variable named summary. Variable summary is not provided a scope by jspretty.js, so it must be provided a scope by a consuming function or it will become an implied global, or an undeclared variable error in strict mode. It is intended to be provided to jspretty as closure so that it can access the interior of jspretty and be accessed outside of jspretty.

CSS

CSS beautification uses csspretty function. A minimalist parser that is capable of quick extension and deep analysis.

CSV

CSV typically stands for comma separated values, but in this tool it stands for character separated values. The csvbeauty function takes a sequence of characters and splits the input upon that supplied sequence onto new lines. Prior existing line breaks, if they were quoted, are converted to a space contained by braces: { }. Unquote line breaks are converted into two simultaneous line breaks. If the final character(s) match the user supplied character sequence, after charDecoder processing, then those characters are converted into {|} so that csvmin will know a character sequence must exist at the extreme end of input. Escaped double quote characters, escaped using the formal CSV method by immediately preceeding the characters with an extra double quote character, are converted in a single double quote character to improve ledgibility.

CSV beautification uses charDecoder to decode Unicode character entities. The charDecode function accepts any combination of HTML decimal Unicode entities and Unicode hexidecimal entities. HTML decimal entities must begin with an ampersand and pound character '&#', be immediately followed with between one and six decimals, and be immediately terminated by a semicolon ';'. Examples of accepted HTML entities are:

The Unicode hexidecimal entities must begin with a lowercase u and plus character 'u+', be immediately followed by a four or five digit hexidecimal value, and be immediately terminated by a plus character. Hexidecimal values smaller than four digits must be padded with 0 characters necessary to achieve four digits. Examples of accepted Unicode entities are:

Please be aware that charDecode is reliant upon the interpreting application's HTML character rendering engine to map entity values to character maps, which means if the browser does not support the entity supplied the browser will return a generic character marker instead of the intended character. The content will then be separated in accordance to the rendered sequence value, which means a generic character marker will be used in the separation instead of the character referrenced by the supplied entity. In summary, if your browser has limited support for Unicode characters you must expect equally limited results when using entity references.

Markup

Markup beautification uses the markup_beauty function of the application. This function operates upon a pattern based logic of referrential integrity. This means decisions are made through exposure to the pattern as established so far. Unfortunately, this requires defined logic to consider all possible combinations of patterns. At this point the beautification appears to work for more than 99.9% of pattern combinations, but undefined combinations are continually being discovered. If an error in my logic is discovered please contact me so that I am supply you with a corrected application.

The markup beautification is based upon syntax conventions only and absolutely not upon vocabulary. The two exceptions are that the contents of a script tag are presumed to be JavaScript if the tag does not contain a type attribute or the type attribute contains one of these values: text/javascript, text/ecmascript, application/javascript, application/x-javascript, application/ecmascript. When the contents of a script tag are presumed as JavaScript they are beautified accordingly. The contents of a style tag are presumed to be CSS if the tag contains a type attribute with a value of text/css or if the type attribute is not present, and those contents are beautified as CSS. The presumed CSS and JavaScript do not inherit indentation from the markup. Since the beautification is not based upon vocabulary any language that uses angle brackets for delimiters should work assuming the conditions of the next paragraph are met. The supplied markup does not have to be valid or well formed by any means.

Content in the markup is represented by whether or not it begins or ends with any whitespace. If content does begin and/or end with whitespace then new line characters are added and the content is indented. This means tags that butt up directly to content are then treated as an extension of that content and are not indented. Singleton tags are expected to be terminated as XML singleton tags, which means a forward slash character prior to its closing angle bracket. If a singleton tag is not properly closed the beautifier believes the tag to be a start tag, which expects an end tag. Singleton tags may represent an indication of content in the form of media or form controls, and so they are indented in the same manner as content.

PHP tags are expected to open with "<?php" and XML parsing declarations are expected to open with "<?xml". Tags that begin with only "<?" are not supported, and so they are believed to be start tags missing a closing tag. This unsupported convention is no longer supported by PHP, even if tolerated, and will generate errors to an XML parser. I don't support this and neither should you.

Start tags expect to receive an end tag. End tags will be indented exactly like their starting pair unless they are directly next to content and the same is true for start tags. The beautification logic is smart enough to compensate and correct itself in adjustment for start tags or end tags that are not indented due to content.

The markup_beauty function also supports nested tags. Some server side processing languages use an XML base tag syntax for application processing, such as JSTL, and allow the direct embedding of HTML and XML tags directly. This following tag is example of something that can be beautified: <c:out value="<strong>variable text output</strong>"/>. The only limitation is that the nested tags must be quoted in either double or single quotes.

On 12 April 2011 support was added for complex nested SGML tags. This support is limited to the points of conflict with the terse syntax requirements of XML. In the case of such conflicts markup_beauty chooses to comply with XML syntax in opposition to SGML flexibility. This is of particular concern to tags that use the "<?" syntax.

Markup Summary

The markup_beauty function contains, at its end, an unassigned anonymous function that creates the summary report and assigns its output to a variable named summary. Variable summary is not provided a scope by markup_beauty, because it is meant to be supplied as a closure to markup_beauty. This summary variable must be provided a scope by the consuming application or it will become an implied global, or an undeclared variable error in strict mode.

The markup_summary creates a report of the number of parts comprising the markup, the weight of each of those parts, and a score using a math formula to compute a performance rating that reenforces reliance upon structure and elaboration of content. This function also displays each HTML element making a HTTP request.

Beautification Options

The Indentation Size is merely a multiplier of the specified indentation character. Each component of the total application computes indentation from multiplying the indentation size by the indendation character independently so that the components may be used without the others and with minimal or no modification.

The Indentation Characters option allows a choice of space, tab, new line, or a string of text characters from the HTML tool. The code will literally allow anything that can be expressed as a character or series of characters.

The Indent Comments option determines if comments should be indented in accordance to the neighboring code or if comments should not be indented at all.

The Indent Style/Script option is only available for markup beautification. This option determines if the contents of style or script should be indented to match the indentation of their parent tag or if their indentation should begin from the left.

The Presume SGML type HTML option is only available for the markup type of code. When this option is enabled all tag names that are "presumed" as singleton tags in HTML 4.01 are treated as singletons regardless of their syntax.

Minification

JavaScript

The custom built csspretty library is used to analyze and minify JavaScript code. It is also capable of providing some minor auto-correction, such as inserting missing semicolons and curly braces.

CSS

The custom built csspretty library is used to analyze and minify CSS code.

CSV

CSV typically stands for comma separated values, but in this tool it stands for character separated values. The csvmin function reverts all changes inflicted by the csvbeauty function.

CSV beautification uses charDecoder to decode Unicode character entities. The charDecode function accepts any combination of HTML decimal Unicode entities and Unicode hexidecimal entities. HTML decimal entities must begin with an ampersand and pound character '&#', be immediately followed with between one and six decimals, and be immediately terminated by a semicolon ';'. Examples of accepted HTML entities are:

The Unicode hexidecimal entities must begin with a lowercase u and plus character 'u+', be immediately followed by a four or five digit hexidecimal value, and be immediately terminated by a plus character. Hexidecimal values smaller than four digits must be padded with 0 characters necessary to achieve four digits. Examples of accepted Unicode entities are:

Please be aware that charDecoder is reliant upon the interpreting application's HTML character rendering engine to map entity values to character maps, which means if the browser does not support the entity supplied the browser will return a generic character marker instead of the intended character. The content will then be separated in accordance to the rendered sequence value, which means a generic character marker will be used in the separation instead of the character referrenced by the supplied entity. In summary, if your browser has limited support for Unicode characters you must expect equally limited results when using entity references. csvmin does not revert any changes supplied by the charDecoder function.

Markup

Markup is minified using markupmin. This function does little more than tokenize a run of whitespace characters into a single space character and scrubbing of comments. It does, however, preserve whitespace inside ASP and PHP tags and preserve SSI tags. It will also assume the contents of a script tag are JavaScript and minify them according, and also assumes the contents of style tags are CSS and minifies them as such.

Minification Options

The Presume SGML type HTML option is only available for the markup type of code. When this option is enabled all tag names that are "presumed" as singleton tags in HTML 4.01 are treated as singletons regardless of their syntax. The conditional argument accepts boolean types and defaults to a value of false. The conditional argument allows the output to retain HTML conditional comments supported by Internet Explorer if the value is true.

Pretty Diff

Diff Code

The diff engine uses diffview. Originally by Snowtide Informatics Systems. diffview is almost entirely rewritten from scratch so that JavaScript arrays are used to store the dynamic output instead of DOM objects. This change has result in a 3.5x faster response rate. charcomp is the function used to highlight per character differences.

Diff Process

JavaScript code is beautified with jspretty and then compared. CSV is first minified with csvmin and then beautified with csvbeauty. CSS is beautified with csspretty and then compared. Markup is beautified using markup-beautify. Plain text is diffed without any minification or beautification. If code that needs to be compared that is not compatible with the other processes then use the plain text mode.

Only after the automated beautification does the diff process begin. The difflib finds differences per line and sends its results, as an array of numeric values where to look in the code, over the diffview. Diffview takes the opcodes supplied by difflib and then builds an array where the code is pumped into HTML table cell code. Once the view is completely built it is immediately inserted into the page using innerHTML. You cannot see the output at this point because it is set to display none until charcomp finishes.

charcomp finds the table cells with a class of "replace" and only works on those cells. Before performing any comparison it converts non-breaking space references into actual spaces to reduce processing requirements, and converts angle brackets into entity references, and converts entity references for quotes into actual quotes. In JavaScript a single quote compares to true against a double quote even if both are string literals, so I invented character references that I could convert back to quotes within the context of the comparison function so that they actually do become string literals that comparable. Once a difference is located it is wrapped in an em tag.

Diff Options

Print or Save Output option dumps the HTML output as raw text into a textarea. Please copy the text out of this new textarea at the bottom of the page and paste it into a new text file to be saved with the file extension of 'html'. If the output is to be printed please use landscape instead of portrait orientation for the paper in order to achieve the best results.

The Context Size option provides padding to the lines of code differences with lines of code that are not different. This option expects to receive a number or empty value only. If anything else is entered an empty value will be processed. An empty value negates this option by returning all supplied code with differences highlighted. If there is no differences are discovered this option is negated.

JavaScript Toleration is exactly the same as described in Minification Options above. Indentation Size are exactly the same as described in Beautification Options above. Indentation Characters, Indent Style/Script, and Presume SGML type HTML are exactly the same as described in Beautification Options above.

The Diff View Type option provides two choices. The first choice, Side by Side View, reports the output into two columns that display a side by side comparison of the differences. The second choice, Inline View, displays the output into a single column so that the differences can be seen in a vertical comparison.

Diff Quotes is an option to normalize all single quote characters, ', to double quote characters, ". This option is off by default and it functions after input code is beautified and before diff operation execution.

Trailing Semicolons is an option to remove semicolon characters just before a new line character. This option is off by default and it functions after input code is beautified and before diff operation execution.

Components Files

A list of code components, author information, and dates of revision.
ComponentAuthor(s)SummaryRevised
charDecoder.jsAustin CheneyThe function that decodes Unicode character entities for csvbeauty.js and csvmin.js.
codemirror.cssCodeMirror teamCSS for the CodeMirror editor.
codemirror.jsCodeMirror teamJavaScript for the CodeMirror editor.
csspretty.jsAustin CheneyCSS parser.
csvbeauty.jsAustin CheneyThe function that beautifies character sequence values.
csvmin.jsAustin CheneyThe function that minifies character sequence values.
diffview.cssAustin CheneyThe CSS that powers everything to do with the form, diff output, and this documentation.
diffview.js Chas Emerick - Original Austin Cheney - Major Revision Builds the HTML diff output.
documentation.xhtmlAustin CheneyMaria Ramos - EspañolThis documentation page.
dom.jsAustin CheneyA supplemental JavaScript file providing DOM access and interaction with the web tool.
jspretty.jsAustin CheneyHarry Whitfield - QA supportBeautifies JavaScript code.
markup_beauty.jsAustin CheneyBeautifies markup code.
markupmin.jsAustin CheneyMinifies markup code.
node-local.jsAustin CheneyAn API for processing the local command line JavaScript with Node.js
prettydiff.com.xhtmlAustin CheneyActual Pretty Diff tool HTML file.
prettydiff.jsAustin CheneyActual Pretty Diff application code.
prettydiff.wsfAustin CheneyPretty Diff API for Windows Script Host.

Please send comments, feedback, and requests to edward.a.cheney.mil@mail.mil.