Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions packages/openslides-motion-diff/src/diff/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { UnifiedChange } from "./definitions";
import { formatDiffWithLineNumbers, insertDanglingSpace, insertInternalLineMarkers, insertLines, recAddOsSplit, removeLines, replaceLinesMergeNodeArrays, serializeDom, serializePartialDomFromChild, serializePartialDomToChild } from "./internal";
import { diffString } from "./internal-diff";
import { diffDetectBrokenDiffHtml, diffParagraphs, fixWrongChangeDetection } from "./internal-diff-transform";
import { getFirstLineNumberNode, getLastLineNumberNode, getLineNumberNode, serializeTagDiff } from "./utils";
import { djb2hash, getFirstLineNumberNode, getLastLineNumberNode, getLineNumberNode, serializeTagDiff } from "./utils";

/**
* Returns the HTML snippet between two given line numbers.
Expand Down Expand Up @@ -230,7 +230,14 @@ export function formatDiff(diff: ExtractedContent): string {
* @param {string} diffHtml
* @returns {LineRange}
*/
const affectedLineRangeCache = new Map<string, LineRange>();
export function detectAffectedLineRange(diffHtml: string): LineRange | null {
const cacheKey = djb2hash(diffHtml);
const cached = affectedLineRangeCache.get(cacheKey);
if (cached) {
return cached;
}

const fragment = htmlToFragment(diffHtml);

insertInternalLineMarkers(fragment);
Expand Down Expand Up @@ -262,10 +269,13 @@ export function detectAffectedLineRange(diffHtml: string): LineRange | null {
}
}

return {
const lineRange = {
from: parseInt(lastLineNumberBefore!.getAttribute(`data-line-number`) as string, 10),
to: parseInt(firstLineNumberAfter!.getAttribute(`data-line-number`) as string, 10) - 1
};

affectedLineRangeCache.set(cacheKey, lineRange);
return lineRange;
}

/**
Expand Down Expand Up @@ -352,6 +362,7 @@ export function replaceLines(oldHtml: string, newHTML: string, fromLine: number,
return serializeDom(mergedFragment, true);
}

const diffCache = new Map<string, string>();
/**
* This function calculates the diff between two strings and tries to fix problems with the resulting HTML.
* If lineLength and firstLineNumber is given, line numbers will be returned es well
Expand All @@ -368,6 +379,12 @@ export function diff(
lineLength: number | null = null,
firstLineNumber: number | null = null
): string {
const cacheKey = lineLength + ` ` + firstLineNumber + ` ` + djb2hash(htmlOld) + djb2hash(htmlNew);
const cached = diffCache.get(cacheKey);
if (cached) {
return cached;
}

// TODO: This is a workaround to make sure the first element of a amendment
// has a line number for correct display of amendments in front of list
// or block elements
Expand Down Expand Up @@ -814,6 +831,7 @@ export function diff(
diff = readdOsSplit(diff, [origHtmlOld, origHtmlNew], true);
}

diffCache.set(cacheKey, diff);
return diff;
}

Expand Down
17 changes: 17 additions & 0 deletions packages/openslides-motion-diff/src/diff/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,20 @@ export function serializeTagDiff(node: Node): string {

return serializeTag(node);
}

/**
* Creates a hash of a given string. This is not meant to be specifically secure, but rather as quick as possible.
*
* @param {string} str
* @returns {string}
*/
export function djb2hash(str: string): string {
let hash = 5381;
let char: number;
for (let i = 0; i < str.length; i++) {
char = str.charCodeAt(i);
hash = (hash << 5) + hash + char;
}
return hash.toString();
}

Loading