• Stars
    star
    211
  • Rank 186,867 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 10 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Сompares two HTML

html-differ Build Status Coverage Status Dependency Status devDependency Status

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:

<span id="1"></span>
<span id=
    "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:

<span id="blah" class="ololo" tabIndex="1">Text</span>
<span tabIndex="1" id="blah" class="ololo">Text</span>
  • Two respective attributes class 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:

<span class="ab bc cd">Text</span>
<span class=" 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 possible options).

Install

$ npm install html-differ

API

HtmlDiffer

var HtmlDiffer = require('html-differ').HtmlDiffer,
    htmlDiffer = new HtmlDiffer(options);

where options 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:

<label for="random">Text</label>
<input id="random">
<label for="sfsdfksdf">Text</label>
<input id="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 as undefined.

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

<div data='{"bla":{"first":"ololo","second":"trololo"}}'></div>
<span onclick='return {"aaa":"bbb","bbb":"aaa"}'></span>

<button data='REALLY BAD JSON'></button>
<button onclick='REALLY BAD FUNCTION'></button>
<div data='{"bla":{"second":"trololo","first":"ololo"}}'></div>
<span onclick='return {"bbb":"aaa","aaa":"bbb"}'></span>

<button data='undefined'></button>
<button onclick='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

Makes html-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<head lang="en"><title></title></head><body>Text</body></html>
 <html>
 Text   Text
<head lang="en">
    <title>               </title>


            </head>

<body>
     Text

    </body>




</html>
ignoreComments: Boolean

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

REMARK!
Does not ignore conditional comments.

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

<!DOCTYPE html>
<!-- comments1 -->
<html>
<head lang="en">
    <meta charset="UTF-8">
    <!--[if IE]>
        <link rel="stylesheet" type="text/css" href="all-ie-only.css" />
    <![endif]-->
    <!--[if !IE]><!-->
        <link href="non-ie.css" rel="stylesheet">
    <!--<![endif]-->
</head>
<body>
Text<!-- comments2 -->
</body>
</html>
<!DOCTYPE html>

<html>
<head lang="en">
    <meta charset="UTF-8">
    <!--[if IE]>
        <link href="all-ie-only.css" type="text/css" rel="stylesheet"/>
    <![endif]-->
    <!--[if !IE]><!-->
        <link href="non-ie.css" rel="stylesheet">
    <!--<![endif]-->
</head>
<body>
Text
</body>
</html>
ignoreEndTags: Boolean

Makes html-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

Makes html-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:

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

Presets

  • bem - sets predefined options for BEM.
Usage

Passing of a preset via the constructor:

var HtmlDiffer = require('html-differ').HtmlDiffer,
    htmlDiffer = new HtmlDiffer('bem');

Redefinition of a preset via the constructor:

var HtmlDiffer = require('html-differ').HtmlDiffer,
    htmlDiffer = new HtmlDiffer({ 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

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

Methods

logger.getDiffText

@param {Array of objects} - the result of the work of the method htmlDiffer.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 method htmlDiffer.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

var fs = require('fs'),
    HtmlDiffer = require('html-differ').HtmlDiffer,
    logger = require('html-differ/lib/logger');

var html1 = fs.readFileSync('1.html', 'utf-8'),
    html2 = fs.readFileSync('2.html', 'utf-8');

var options = {
        ignoreAttributes: [],
        compareAttributesAsJSON: [],
        ignoreWhitespaces: true,
        ignoreComments: true,
        ignoreEndTags: false,
        ignoreDuplicateAttributes: false
    };

var htmlDiffer = new HtmlDiffer(options);

var diff = 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 --help
Compares two HTML

Usage:
  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 options for 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 file config.json:

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

Masks

html-differ supports handling of masks in HTML.

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

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

Syntax

Masks in html-differ have the following syntax:

{{RegExp}}

where:

  • {{ – opening identifier of the mask.

  • 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 the mask.

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:

<div id="{{\d\.\d}}">
<div id="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:

<div class="{{a\{\{b\}\}c}}">
<div class="a{{b}}c">

More Repositories

1

bem-react

A set of tools for developing user interfaces using the BEM methodology in React
TypeScript
440
star
2

yandex-ui

Yandex UI Kit build on React and bem-react
TypeScript
352
star
3

bem-components

Set of components for sites development
JavaScript
332
star
4

project-stub

deps
JavaScript
313
star
5

bem-core

BEM Core Library
JavaScript
275
star
6

bem-bl

Base BEM library
JavaScript
197
star
7

bem-xjst

bem-xjst (eXtensible JavaScript Templates): declarative template engine for the browser and server
JavaScript
115
star
8

bem-sdk

BEM SDK packages
JavaScript
88
star
9

next-global-css

A preset for nextjs allowing using 3d party libraries with global css
JavaScript
86
star
10

themekit

Build system of design-tokens for any platforms
TypeScript
73
star
11

bh

BH template engine
JavaScript
68
star
12

bem-express

BEM project-stub with BEMTREE and express
JavaScript
43
star
13

bh-php

PHP port of https://github.com/bem/bh. It's cool thing but better use this:
PHP
33
star
14

bem-react-boilerplate

DEPRECATED! A bare minimum frontend boilerplate based on create-react-app and bem-react-core.
TypeScript
31
star
15

sssr

Master class for BEMup
JavaScript
30
star
16

bem-react-components

Components library for develop with React and BEM methodology
JavaScript
29
star
17

bem-mvc

Yet another MVC for i-bem
JavaScript
29
star
18

web-platform

React SDK for building modern, accessible and cross-platforms interfaces
TypeScript
28
star
19

gulp-bem

Usefull gulp modules and plugins to work with BEM methodology
JavaScript
25
star
20

webpack-bem-loader

Webpack BEM loader
JavaScript
24
star
21

bem-history

BEM wrap for History API
JavaScript
22
star
22

bem-identity

BEM brand identity
20
star
23

bem-calendar

calendar based on bem-components
JavaScript
16
star
24

storybook-to-figma

The tool for importing Storybook components to Figma
TypeScript
15
star
25

yandex-ui-icons

TypeScript
13
star
26

yandex-ui-themer

A simple way to create your theme for @yandex/ui with Themekit
TypeScript
12
star
27

babel-plugin-bem-import

BEM import resolver
JavaScript
9
star
28

eslint-plugin-bem-xjst

ESLint environments for bem-xjst
JavaScript
9
star
29

bem-components-php

Set of bh.php templates for bem-components library
PHP
9
star
30

figma-extractor

TypeScript
8
star
31

bem-components-dist

Prebuilt version of bem-components library
JavaScript
8
star
32

jscs-bem

jscs bem preset and some tasty rules
JavaScript
6
star
33

bem-forum

BEM forum on top of github issues
JavaScript
6
star
34

bem-core-dist

Prebuilt version of bem-core library
JavaScript
6
star
35

bem-core-php

bh.php templates for bem-core
PHP
5
star
36

babel-plugin-bem

Babel plugin BEM
JavaScript
5
star
37

ts-docgen-next

Webpack loader for generating documentation from typescript files.
TypeScript
4
star
38

jsd

JSDoc parser
JavaScript
4
star
39

cra-template-yandex-ui

TypeScript
2
star
40

bem-jsdoc3-plugin

Plugin for http://usejsdoc.org/ for documenting bem enteties
JavaScript
2
star
41

es5-shims

es5 shims moved from bem-core
JavaScript
2
star
42

webpack-bem-plugin

JavaScript
1
star
43

design-system

TypeScript
1
star
44

webpack-bem-i18n-loader

JavaScript
1
star
45

bem-jsd

Wrapper for use JSD with BEM plugins
JavaScript
1
star