Avoid spurious diff at edit time in "My rules"

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/593

The issue was caused by the lack of empty last line, since
the differ taking into account new line characters.
This commit is contained in:
Raymond Hill 2019-05-25 10:04:31 -04:00
parent 80a8750d35
commit a7bfff03d6
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 22 additions and 19 deletions

View File

@ -68,7 +68,7 @@ let differ;
// observer is necessary because there is no hook for uBO to overwrite // observer is necessary because there is no hook for uBO to overwrite
// reliably the default title attribute assigned by CodeMirror. // reliably the default title attribute assigned by CodeMirror.
(function() { {
const i18nCommitStr = vAPI.i18n('rulesCommit'); const i18nCommitStr = vAPI.i18n('rulesCommit');
const i18nRevertStr = vAPI.i18n('rulesRevert'); const i18nRevertStr = vAPI.i18n('rulesRevert');
const commitArrowSelector = '.CodeMirror-merge-copybuttons-left .CodeMirror-merge-copy-reverse:not([title="' + i18nCommitStr + '"])'; const commitArrowSelector = '.CodeMirror-merge-copybuttons-left .CodeMirror-merge-copy-reverse:not([title="' + i18nCommitStr + '"])';
@ -95,7 +95,7 @@ let differ;
{ attributes: true, attributeFilter: [ 'title' ], subtree: true } { attributes: true, attributeFilter: [ 'title' ], subtree: true }
); );
})(); }
/******************************************************************************/ /******************************************************************************/
@ -137,21 +137,26 @@ const updateOverlay = (function() {
// - Minimum amount of text updated // - Minimum amount of text updated
const rulesToDoc = function(clearHistory) { const rulesToDoc = function(clearHistory) {
for ( let key in unfilteredRules ) { for ( const key in unfilteredRules ) {
if ( unfilteredRules.hasOwnProperty(key) === false ) { continue; } if ( unfilteredRules.hasOwnProperty(key) === false ) { continue; }
let doc = unfilteredRules[key].doc; const doc = unfilteredRules[key].doc;
let rules = filterRules(key); const rules = filterRules(key);
if ( if (
doc.lineCount() === 1 && doc.getValue() === '' || doc.lineCount() === 1 && doc.getValue() === '' ||
rules.length === 0 rules.length === 0
) { ) {
doc.setValue(rules.length !== 0 ? rules.join('\n') : ''); doc.setValue(rules.length !== 0 ? rules.join('\n') + '\n' : '');
continue; continue;
} }
if ( differ === undefined ) { differ = new diff_match_patch(); } if ( differ === undefined ) { differ = new diff_match_patch(); }
let beforeText = doc.getValue(); // https://github.com/uBlockOrigin/uBlock-issues/issues/593
let afterText = rules.join('\n'); // Ensure the text content always ends with an empty line to avoid
let diffs = differ.diff_main(beforeText, afterText); // spurious diff entries.
let beforeText = doc.getValue().trim();
if ( beforeText !== '' ) { beforeText += '\n'; }
let afterText = rules.join('\n').trim();
if ( afterText !== '' ) { afterText += '\n'; }
const diffs = differ.diff_main(beforeText, afterText);
doc.startOperation(); doc.startOperation();
let i = diffs.length, let i = diffs.length,
iedit = beforeText.length; iedit = beforeText.length;
@ -199,13 +204,13 @@ const filterRules = function(key) {
/******************************************************************************/ /******************************************************************************/
const renderRules = (function() { const renderRules = (( ) => {
const reIsSwitchRule = /^[a-z-]+: /;
let firstVisit = true; let firstVisit = true;
let reIsSwitchRule = /^[a-z-]+: /;
// Switches always listed at the top. // Switches always listed at the top.
let customSort = (a, b) => { const customSort = (a, b) => {
let aIsSwitch = reIsSwitchRule.test(a); const aIsSwitch = reIsSwitchRule.test(a);
if ( reIsSwitchRule.test(b) === aIsSwitch ) { if ( reIsSwitchRule.test(b) === aIsSwitch ) {
return a.localeCompare(b); return a.localeCompare(b);
} }
@ -356,10 +361,10 @@ const onFilterChanged = (function() {
/******************************************************************************/ /******************************************************************************/
const onTextChanged = (function() { const onTextChanged = (( ) => {
let timer; let timer;
const process = function(now) { const process = now => {
timer = undefined; timer = undefined;
const diff = document.getElementById('diff'); const diff = document.getElementById('diff');
let isClean = mergeView.editor().isClean(cleanEditToken); let isClean = mergeView.editor().isClean(cleanEditToken);
@ -373,10 +378,8 @@ const onTextChanged = (function() {
} }
diff.classList.toggle('editing', isClean === false); diff.classList.toggle('editing', isClean === false);
diff.classList.toggle('dirty', mergeView.leftChunks().length !== 0); diff.classList.toggle('dirty', mergeView.leftChunks().length !== 0);
document.getElementById('editSaveButton').classList.toggle( document.getElementById('editSaveButton')
'disabled', .classList.toggle('disabled', isClean);
isClean
);
const input = document.querySelector('#ruleFilter input'); const input = document.querySelector('#ruleFilter input');
if ( isClean ) { if ( isClean ) {
input.removeAttribute('disabled'); input.removeAttribute('disabled');