Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Сompares two HTML

License

NotificationsYou must be signed in to change notification settings

bem/html-differ

Repository files navigation

Compares two HTML.

The comparison algorithm

html-differ compares HTML using the following criteria:

  • <!DOCTYPE> declarations are case-insensitive, so the following two code samples will be considered to be equivalent:
<!DOCTYPE HTML PUBLIC "_PUBLIC" "_SYSTEM">
<!doctype html public "_PUBLIC" "_SYSTEM">
  • Whitespaces (spaces, tabs, new lines etc.) inside start and end tags are ignored during the comparison.

For example, the following two code samples will be considered to be equivalent:

<spanid="1"></span>
<spanid=    "1"></span>
  • Two respective lists of attributes are considered to be equivalent even if they are specified in different order.

For example, the following two code samples will be considered to be equivalent:

<spanid="blah"class="ololo"tabIndex="1">Text</span>
<spantabIndex="1"id="blah"class="ololo">Text</span>
  • Two respective attributesclass are considered to be equivalent if they refer to the same groups of CSS styles.

For example, the following two code samples will be considered to be equivalent:

<spanclass="ab bc cd">Text</span>
<spanclass=" cd  ab bc bc">Text</span>

CAUTION!
html-differ does not check the validity of HTML, but compares them using the above shown criteria and specified options (see the list of possibleoptions).

Install

$ npm install html-differ

API

HtmlDiffer

varHtmlDiffer=require('html-differ').HtmlDiffer,htmlDiffer=newHtmlDiffer(options);

whereoptions is an object.

Options

ignoreAttributes: [Array]

Sets what kind of respective attributes' content will be ignored during the comparison (default:[]).

Example:['id', 'for']
The following two code samples will be considered to be equivalent:

<labelfor="random">Text</label><inputid="random">
<labelfor="sfsdfksdf">Text</label><inputid="sfsdfksdf">
compareAttributesAsJSON: [Array]

Sets what kind of respective attributes' content will be compared as JSON objects, but not as strings (default:[]).
In cases when the value of the attribute is an invalid JSON or can not be wrapped into a function, it will be compared asundefined.

Example:[{ name: 'data', isFunction: false }, { name: 'onclick', isFunction: true }]
The following two code samples will be considered to be equivalent:

<divdata='{"bla":{"first":"ololo","second":"trololo"}}'></div><spanonclick='return {"aaa":"bbb","bbb":"aaa"}'></span><buttondata='REALLY BAD JSON'></button><buttononclick='REALLY BAD FUNCTION'></button>
<divdata='{"bla":{"second":"trololo","first":"ololo"}}'></div><spanonclick='return {"bbb":"aaa","aaa":"bbb"}'></span><buttondata='undefined'></button><buttononclick='undefined'></button>

REMARK!
The first element of the array could be written in a short form as string:
['data', { name: 'onclick', isFunction: true }].

ignoreWhitespaces: Boolean

Makeshtml-differ ignore whitespaces (spaces, tabs, new lines etc.) during the comparison (default:true).

Example:true
The following two code samples will be considered to be equivalent:

<html>Text Text<headlang="en"><title></title></head><body>Text</body></html>
<html> Text   Text<headlang="en"><title></title></head><body>     Text</body></html>
ignoreComments: Boolean

Makeshtml-differ ignore HTML comments during the comparison (default:true).

REMARK!
Does not ignoreconditional comments.

Example:true
The following two code samples will be considered to be equivalent:

<!DOCTYPE html><!-- comments1 --><html><headlang="en"><metacharset="UTF-8"><!--[if IE]>        <link rel="stylesheet" type="text/css" href="all-ie-only.css" />    <![endif]--><!--[if !IE]><!--><linkhref="non-ie.css"rel="stylesheet"><!--<![endif]--></head><body>Text<!-- comments2 --></body></html>
<!DOCTYPE html><html><headlang="en"><metacharset="UTF-8"><!--[if IE]>        <link href="all-ie-only.css" type="text/css" rel="stylesheet"/>    <![endif]--><!--[if !IE]><!--><linkhref="non-ie.css"rel="stylesheet"><!--<![endif]--></head><body>Text</body></html>
ignoreEndTags: Boolean

Makeshtml-differ ignore end tags during the comparison (default:false).

Example:true
The following two code samples will be considered to be equivalent:

<span>Text</span>
<span>Text</spane>
ignoreDuplicateAttributes: Boolean

Makeshtml-differ ignore tags' duplicate attributes during the comparison.
From the list of the same tag's attributes, the attribute which goes the first will be taken for comparison, others will be ignored (default:false).

Example:true
For example, the following two code samples will be considered to be equivalent:

<spanid="blah"id="ololo">Text</span>
<spanid="blah">Text</span>

Presets

  • bem - sets predefined options forBEM.
Usage

Passing of a preset via the constructor:

varHtmlDiffer=require('html-differ').HtmlDiffer,htmlDiffer=newHtmlDiffer('bem');

Redefinition of a preset via the constructor:

varHtmlDiffer=require('html-differ').HtmlDiffer,htmlDiffer=newHtmlDiffer({preset:'bem',ignoreAttributes:[]});

Methods

htmlDiffer.diffHtml

@param{String} - the 1-st HTML code
@param{String} - the 2-nd HTML code
@returns{Array of objects} -array with diffs between HTML

htmlDiffer.isEqual

@param{String} - the 1-st HTML code
@param{String} - the 2-nd HTML code
@returns{Boolean}

Logger

varlogger=require('html-differ/lib/logger');

Methods

logger.getDiffText

@param{Array of objects} - the result of the work of the methodhtmlDiffer.diffHtml
@param{Object} - options:

  • charsAroundDiff: Number - the number of characters around the diff result between two HTML (default:40).

@returns{String}

logger.logDiffText

@param{Array of objects} - the result of the work of the methodhtmlDiffer.diffHtml
@param{Object} - options:

  • charsAroundDiff: Number - the number of characters around the diff result between two HTML (default:40).

@returns - pretty logging of diffs:

Example

varfs=require('fs'),HtmlDiffer=require('html-differ').HtmlDiffer,logger=require('html-differ/lib/logger');varhtml1=fs.readFileSync('1.html','utf-8'),html2=fs.readFileSync('2.html','utf-8');varoptions={ignoreAttributes:[],compareAttributesAsJSON:[],ignoreWhitespaces:true,ignoreComments:true,ignoreEndTags:false,ignoreDuplicateAttributes:false};varhtmlDiffer=newHtmlDiffer(options);vardiff=htmlDiffer.diffHtml(html1,html2),isEqual=htmlDiffer.isEqual(html1,html2),res=logger.getDiffText(diff,{charsAroundDiff:40});logger.logDiffText(diff,{charsAroundDiff:40});

Usage as a program

$ html-differ --helpCompares two HTMLUsage:  html-differ [OPTIONS] [ARGS]Options:  -h, --help: Help  -v, --version: Shows the version number  --config=CONFIG: Path to a configuration JSON file  --bem: Uses predefined optionsfor BEM (deprecated)  -p PRESET, --preset=PRESET: Name of a preset  --chars-around-diff=CHARSAROUNDDIFF: The number of characters around the diff (default: 40)Arguments:  PATH1: Path to the 1-st HTML file (required)  PATH2: Path to the 2-nd HTML file (required)

Example

$ html-differ path/to/html1 path/to/html2$ html-differ --config=path/to/config --chars-around-diff=40 path/to/html1 path/to/html2$ html-differ --preset=bem path/to/html1 path/to/html2

Configuration file

Study the following fileconfig.json:

{"ignoreAttributes":[],"compareAttributesAsJSON":[],"ignoreWhitespaces":true,"ignoreComments":true,"ignoreEndTags":false,"ignoreDuplicateAttributes":false}

Masks

html-differ supports handling ofmasks in HTML.

For example, the following two code samples will be considered to be equivalent:

<divid="{{[a-z]*\s\d+}}">
<divid="text 12345">

Syntax

Masks inhtml-differ have the following syntax:

{{RegExp}}

where:

  • {{ – opening identifier of themask.

  • RegExp – regular expression for matching with the corresponding value in another HTML. The syntax is similar to regular expressions in JavaScript written in a literal notation.

  • }} – closing identifier of themask.

Screening

The rules of screening of symbols are similar to the rules which are used in regular expressions in JavaScript written in a literal notation.

For example, the following two code samples will be considered to be equivalent:

<divid="{{\d\.\d}}">
<divid="1.1">

If you want to use{{ or}} inside a mask, you should screen both curly braces, i.e.\{\} or\}\}.

For example, the following two code samples will be considered to be equivalent:

<divclass="{{a\{\{b\}\}c}}">
<divclass="a{{b}}c">

[8]ページ先頭

©2009-2025 Movatter.jp