A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
update differ
ammarahm-ed057113d9b31d
diff --git a/apps/mobile/src/utils/differ.js b/apps/mobile/src/utils/differ.jsindex b30ec7866..5783366b9 100644--- a/apps/mobile/src/utils/differ.js+++ b/apps/mobile/src/utils/differ.js@@ -1,22 +1,43 @@+/*+ * htmldiff.js is a library that compares HTML content. It creates a diff between two+ * HTML documents by combining the two documents and wrapping the differences with+ * <ins> and <del> tags. Here is a high-level overview of how the diff works.+ *+ * 1. Tokenize the before and after HTML with html_to_tokens.+ * 2. Generate a list of operations that convert the before list of tokens to the after+ * list of tokens with calculate_operations, which does the following:+ * a. Find all the matching blocks of tokens between the before and after lists of+ * tokens with find_matching_blocks. This is done by finding the single longest+ * matching block with find_match, then recursively finding the next longest+ * matching block that precede and follow the longest matching block with+ * recursively_find_matching_blocks.+ * b. Determine insertions, deletions, and replacements from the matching blocks.+ * This is done in calculate_operations.+ * 3. Render the list of operations by wrapping tokens with <ins> and <del> tags where+ * appropriate with render_operations.+ *+ * Example usage:+ *+ * var htmldiff = require('htmldiff.js');+ *+ * htmldiff('<p>this is some text</p>', '<p>this is some more text</p>')+ * == '<p>this is some <ins>more </ins>text</p>'+ *+ * htmldiff('<p>this is some text</p>', '<p>this is some more text</p>', 'diff-class')+ * == '<p>this is some <ins class="diff-class">more </ins>text</p>'+ */(function () {- function is_end_of_tag(char) {- return char === ">";- }-function is_start_of_tag(char) {return char === "<";}-+ const IS_CLOSE_TAG_REGEX = /^\s*<\s*\/[^>]+>\s*$/;function is_close_tag(tag) {- return /^\s*<\s*\/[^>]+>\s*$/.test(tag);- }-- function is_whitespace(char) {- return /^\s+$/.test(char);+ return IS_CLOSE_TAG_REGEX.test(tag);}+ const IS_TAG_REGEX = /^\s*<[^>]+>\s*$/;function is_tag(token) {- return /^\s*<[^>]+>\s*$/.test(token);+ return IS_TAG_REGEX.test(token);}function isnt_tag(token) {@@ -33,28 +54,15 @@* @return {string|null} The name of the atomic tag if the word will be an atomic tag,* null otherwise*/+ const IS_START_ATOMIC_TAG = /^<(iframe|object|math|svg|script)/;function is_start_of_atomic_tag(word) {- var result = /^<(iframe|object|math|svg|script)/.exec(word);+ var result = IS_START_ATOMIC_TAG.exec(word);if (result) {result = result[1];}return result;}- /*- * Checks if the current word is the end of an atomic tag (i.e. it has all the characters,- * except for the end bracket of the closing tag, such as '<iframe></iframe').- *- * @param {string} word The characters of the current token read so far.- * @param {string} tag The ending tag to look for.- *- * @return {boolean} True if the word is now a complete token (including the end tag),- * false otherwise.- */- function is_end_of_atomic_tag(word, tag) {- return word.substring(word.length - tag.length - 2) === "</" + tag;- }-/** Checks if a tag is a void tag.*@@ -62,8 +70,9 @@** @return {boolean} True if the token is a void tag, false otherwise.*/+ const IS_VOID_TAG_REGEX = /^\s*<[^>]+\/>\s*$/;function is_void_tag(token) {- return /^\s*<[^>]+\/>\s*$/.test(token);+ return IS_VOID_TAG_REGEX.test(token);}/*@@ -147,96 +156,58 @@** @return {Array.<string>} The list of tokens.*/+function html_to_tokens(html, components_search_rule) {- var mode = "char";- var current_word = "";- var current_atomic_tag = "";- var words = [];- for (var i = 0; i < html.length; i++) {- var char = html[i];- switch (mode) {- case "tag":- var atomic_tag = is_start_of_atomic_tag(current_word);- if (atomic_tag) {- mode = "atomic_tag";- current_atomic_tag = atomic_tag;- current_word += char;- } else if (is_end_of_tag(char)) {- current_word += ">";- words.push(current_word);- current_word = "";- if (is_whitespace(char)) {- mode = "whitespace";- } else {- mode = "char";- }- } else {- current_word += char;- }- break;- case "atomic_tag":- if (- is_end_of_tag(char) &&- is_end_of_atomic_tag(current_word, current_atomic_tag)- ) {- current_word += ">";- words.push(current_word);- current_word = "";- current_atomic_tag = "";- mode = "char";- } else {- current_word += char;- }- break;- case "char":- if (is_start_of_tag(char)) {- if (current_word) {- words.push(current_word);- }- current_word = "<";- mode = "tag";- } else if (/\s/.test(char)) {- if (current_word) {- words.push(current_word);- }- current_word = char;- mode = "whitespace";- } else if (/[\w\d#@]/.test(char)) {- current_word += char;- } else if (/&/.test(char)) {- if (current_word) {- words.push(current_word);- }- current_word = char;- } else {- current_word += char;- words.push(current_word);- current_word = "";- }- break;- case "whitespace":- if (is_start_of_tag(char)) {- if (current_word) {- words.push(current_word);- }- current_word = "<";- mode = "tag";- } else if (is_whitespace(char)) {- current_word += char;- } else {- if (current_word) {- words.push(current_word);- }- current_word = char;- mode = "char";- }- break;- default:- throw new Error("Unknown mode " + mode);+ let words = [];+ const range = {+ start: 0,+ end: 0,+ tagOpen: false,+ whitespaceOpen: false,+ ampersandOpen: false,+ };+ for (var i = 0; i < html.length; ++i) {+ const s = html[i];++ if (s === " " || s === "\n" || s === "\r" || s === "\t") {+ if (range.tagOpen || range.whitespaceOpen) continue;++ //range.end = i;+ if (range.start !== i) words.push(html.slice(range.start, i));++ range.whitespaceOpen = true;+ range.start = i;+ } else if (range.whitespaceOpen) {+ range.whitespaceOpen = false;+ // range.end = i;+ words.push(html.slice(range.start, i));+ range.start = i;+ }++ if (s === "<") {+ range.tagOpen = true;++ if (range.start !== i) {+ words.push(html.slice(range.start, i));+ }++ range.start = i;+ } else if (s === ">") {+ range.tagOpen = false;+ words.push(html.slice(range.start, i + 1));+ range.start = i + 1;+ } else if (!range.ampersandOpen && s === "&") {+ range.ampersandOpen = true;+ if (range.start !== i) words.push(html.slice(range.start, i));+ range.start = i;+ } else if (range.ampersandOpen && s === ";") {+ range.ampersandOpen = false;+ words.push(html.slice(range.start, i + 1));+ range.start = i + 1;}}- if (current_word) {- words.push(current_word);+ if (range.start !== html.length) {+ words.push(html.slice(range.start, html.length));}return components_search_rule@@ -847,6 +818,8 @@}function diff_dual_pane(before, after) {+ if (!before || !after) return { before, after };+var ops;if (before === after) {return return_dual_pane(before, after);