All Projects â†’ fiduswriter â†’ Diffdom

fiduswriter / Diffdom

Licence: lgpl-3.0
A diff for DOM elements, as client-side JavaScript code. Gets all modifications, insertions and removals between two DOM fragments.

Labels

Projects that are alternatives of or similar to Diffdom

Bento
Swift library for building component-based interfaces on top of UITableView and UICollectionView 🍱
Stars: ✭ 371 (-43.79%)
Mutual labels:  diff
Happo
Visual diffing in CI for user interfaces
Stars: ✭ 510 (-22.73%)
Mutual labels:  diff
Diffabledatasources
💾 A library for backporting UITableView/UICollectionViewDiffableDataSource.
Stars: ✭ 601 (-8.94%)
Mutual labels:  diff
Similar
A high level diffing library for rust based on diffs
Stars: ✭ 386 (-41.52%)
Mutual labels:  diff
Diff Match Patch
Diff Match Patch is a high-performance library in multiple languages that manipulates plain text.
Stars: ✭ 4,910 (+643.94%)
Mutual labels:  diff
Patch Package
Fix broken node modules instantly 🏃🏽‍♀️💨
Stars: ✭ 6,062 (+818.48%)
Mutual labels:  diff
Jsondiffpatch
Diff & patch JavaScript objects
Stars: ✭ 3,951 (+498.64%)
Mutual labels:  diff
Xcdiff
A tool which helps you diff xcodeproj files.
Stars: ✭ 641 (-2.88%)
Mutual labels:  diff
Hdiffpatch
a C\C++ library and command-line tools for Diff & Patch between binary files or directories(folder); cross-platform; run fast; create small delta/differential; support large files and limit memory requires when diff & patch.
Stars: ✭ 459 (-30.45%)
Mutual labels:  diff
Daff
align and compare tables
Stars: ✭ 598 (-9.39%)
Mutual labels:  diff
Jsondiff
Diff JSON and JSON-like structures in Python
Stars: ✭ 404 (-38.79%)
Mutual labels:  diff
Pixelmatch
The smallest, simplest and fastest JavaScript pixel-level image comparison library
Stars: ✭ 4,447 (+573.79%)
Mutual labels:  diff
Datasources
💾 🔜📱 Type-safe data-driven CollectionView, TableView Framework. (We can also use ASCollectionNode)
Stars: ✭ 553 (-16.21%)
Mutual labels:  diff
Westore
更好的小程序项目架构
Stars: ✭ 3,897 (+490.45%)
Mutual labels:  diff
Nanomorph
🚅 - Hyper fast diffing algorithm for real DOM nodes
Stars: ✭ 621 (-5.91%)
Mutual labels:  diff
Gojsondiff
Go JSON Diff
Stars: ✭ 371 (-43.79%)
Mutual labels:  diff
Deep Object Diff
Deep diffs two objects, including nested structures of arrays and objects, and returns the difference. ❄️
Stars: ✭ 515 (-21.97%)
Mutual labels:  diff
React Diff Viewer
A simple and beautiful text diff viewer component made with Diff and React.
Stars: ✭ 642 (-2.73%)
Mutual labels:  diff
Sirix
SirixDB is a temporal, evolutionary database system, which uses an accumulate only approach. It keeps the full history of each resource. Every commit stores a space-efficient snapshot through structural sharing. It is log-structured and never overwrites data. SirixDB uses a novel page-level versioning approach called sliding snapshot.
Stars: ✭ 638 (-3.33%)
Mutual labels:  diff
Changedetection
Automatically track websites changes on Android in background.
Stars: ✭ 563 (-14.7%)
Mutual labels:  diff

diffDOM - A JavaScript diffing algorithm for DOM elements

This library allows the abstraction of differences between DOM elements as a "diff" object, representing the sequence of modifications that must be applied to one element in order to turn it into the other element. This diff is non-destructive, meaning that relocations of DOM nodes are preferred over remove-insert operations.

License

This project is licensed under the LGPL v. 3. For details see LICENSE.txt.

Demo and tests

Check http://fiduswriter.github.io/diffDOM for demo and tests.

Usage

Include the diffDOM file in your HTML like this:

<script src="browser/diffDOM.js"></script>

Or like this if you import from npm:

import {DiffDOM} from "diff-dom"

Then create an instance of diffDOM within the javascript code:

dd = new diffDOM.DiffDOM();

(leave out the diffdom. if you use the npm-version)

Now you can create a diff to get from dom elementA to dom elementB like this:

diff = dd.diff(elementA, elementB);

You can now apply this diff like this:

dd.apply(elementA, diff);

Now elementA will have been changed to be structurally equal to elementB.

Virtual DOM and HTML strings

You can also use HTML strings or the virtual DOM objects diffDOM uses internally to create diffs.

diff = dd.diff(elementA, '<div>hello</div>')

You can create the Virtual DOM objects diffDOM uses, create them like this:

import {nodeToObj, stringToObj} from "diff-dom"

obj1 = nodeToObj(elementA)
obj2 = stringToObj('<div>hello</div>')

Diffing between these objects will be faster than diffing DOM nodes and can be useful in environments without access to the DOM.

Advanced uses

Undo

Continuing on from the previous example, you can also undo a diff, like this:

dd.undo(elementA, diff);

Now elementA will be what it was like before applying the diff.

Remote changes

If you need to move diffs from one machine to another one, you will likely want to send the diffs through a websocket connection or as part of a form submit. In both cases you need to convert the diff to a json string.

To convert a diff to a json string which you can send over the network, do:

diffJson = JSON.stringify(diff);

On the receiving end you then need to unpack it like this:

diff = JSON.parse(diffJson);

Error handling when patching/applying

Sometimes one may try to patch an elment without knowing whether the patch actually will apply cleanly. This should not be a problem. If diffDOM determines that a patch cannot be executed, it will simple return false. Else it will return true:

result = dd.apply(element, diff);

if (result) {
    console.log('no problem!');
} else {
    console.log('diff could not be applied');
}

Advanced merging of text node changes

diffDOM does not include merging for changes to text nodes. However, it includes hooks so that you can add more advanced handling. Simple overwrite the textDiff function of the diffDOM instance. The functions TEXTDIFF and TEXTPATCH need to be defined in the code:

dd = new diffDOM.DiffDOM({
    textDiff: function (node, currentValue, expectedValue, newValue) {
        if (currentValue===expectedValue) {
            // The text node contains the text we expect it to contain, so we simple change the text of it to the new value.
            node.data = newValue;
        } else {
            // The text node currently does not contain what we expected it to contain, so we need to merge.
            difference = TEXTDIFF(expectedValue, currentValue);
            node.data = TEXTPATCH(newValue, difference);
        }
        return true;
    }
  });

Pre and post diff hooks

diffDOM provides extension points before and after virtual and actual diffs, exposing some of the internals of the diff algorithm, and allowing you to make additional decisions based on that information.

dd = new diffDOM.DiffDOM({
    preVirtualDiffApply: function (info) {
        console.log(info);
    },
    postVirtualDiffApply: function (info) {
        console.log(info);
    },
    preDiffApply: function (info) {
        console.log(info);
    },
    postDiffApply: function (info) {
        console.log(info);
    }
  });

Additionally, the pre hooks allow you to shortcircuit the standard behaviour of the diff by returning true from this callback. This will cause the diffApply functions to return prematurely, skipping their standard behaviour.

dd = new diffDOM.DiffDOM({
    // prevent removal of attributes
    preDiffApply: function (info) {
        if (info.diff.action === 'removeAttribute') {
            console.log("preventing attribute removal");
            return true;
        }
    }
  });

Outer and Inner diff hooks

diffDOM also provides a way to filter outer diff

dd = new diffDOM.DiffDOM({
    filterOuterDiff: function(t1, t2, diffs) {
        // can change current outer diffs by returning a new array,
        // or by mutating outerDiffs.
        if (!diffs.length && t1.nodeName == "my-component" && t2.nodeName == t1.nodeName) {
            // will not diff childNodes
            t1.innerDone = true;
        }
    }
});

Debugging

For debugging you might want to set a max number of diff changes between two elements before diffDOM gives up. To allow for a maximum of 500 differences between elements when diffing, initialize diffDOM like this:

dd = new diffDOM.DiffDOM({
    debug: true,
    diffcap: 500
  });

Disable value diff detection

For forms that have been filled out by a user in ways that have changed which value is associated with an input field or which options are checked/selected without the DOM having been updated, the values are diffed. For use cases in which no changes have been made to any of the form values, one may choose to skip diffing the values. To do this, hand false as a third configuration option to diffDOM:

dd = new diffDOM.DiffDOM({
    valueDiffing: false
  });
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].