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.
The parameters are optional and are provided soley for portability. The parameters may occur in any order. Examples:
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.
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:
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:
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.
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.
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:
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:
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.
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.
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"
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 beautification uses csspretty function. A minimalist parser that is capable of quick extension and deep analysis.
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 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.
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.
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.
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.
The custom built csspretty library is used to analyze and minify CSS code.
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 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.
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.
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.
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.
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.
Component | Author(s) | Summary | Revised |
---|---|---|---|
charDecoder.js | Austin Cheney | The function that decodes Unicode character entities for csvbeauty.js and csvmin.js. | |
codemirror.css | CodeMirror team | CSS for the CodeMirror editor. | |
codemirror.js | CodeMirror team | JavaScript for the CodeMirror editor. | |
csspretty.js | Austin Cheney | CSS parser. | |
csvbeauty.js | Austin Cheney | The function that beautifies character sequence values. | |
csvmin.js | Austin Cheney | The function that minifies character sequence values. | |
diffview.css | Austin Cheney | The 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.xhtml | Austin Cheney Maria Ramos - Español | This documentation page. | |
dom.js | Austin Cheney | A supplemental JavaScript file providing DOM access and interaction with the web tool. | |
jspretty.js | Austin Cheney Harry Whitfield - QA support | Beautifies JavaScript code. | |
markup_beauty.js | Austin Cheney | Beautifies markup code. | |
markupmin.js | Austin Cheney | Minifies markup code. | |
node-local.js | Austin Cheney | An API for processing the local command line JavaScript with Node.js | |
node-service.js | Austin Cheney | The code for processing the server side API for Node.js. This file is no longer maintained. | |
prettydiff.com.xhtml | Austin Cheney | Actual Pretty Diff tool HTML file. | |
prettydiff.js | Austin Cheney | Actual Pretty Diff application code. | |
prettydiff.wsf | Austin Cheney | Pretty Diff API for Windows Script Host. |
Please send comments, feedback, and requests to austin.cheney@us.army.mil.