Skip to content

Commit

Permalink
[FIX] Improve rule diffing algorithm (#104)
Browse files Browse the repository at this point in the history
Diffing algorithm now handles comments between rules correctly
  • Loading branch information
tobiasso85 committed Nov 26, 2019
1 parent 2548c94 commit 2527189
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ Diffing.prototype.diffRules = function(oBaseRules, oCompareRules) {

if (oBaseNode.type === "rule") {
// Add all rules with different selector to stack and check for next one
while (!selectorEquals(oBaseNode.selectors, oCompareNode.selectors)) {
while (oCompareNode && (oCompareNode.type !== "rule" || !selectorEquals(oBaseNode.selectors, oCompareNode.selectors))) {
this.oStack.stylesheet.rules.push(oCompareNode);
iCompareNode++;
oCompareNode = oCompareRules[iCompareNode];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
body {
color: black;
}
a {
color: black;
}
b {
text-align: center;
}
.sapContrastPlus {
color: orange;
}

body.sapContrastPlus,
body .sapContrastPlus {
color: blue;
}

.sapContrastPlus a {
color: blue;
}
/* ============================= */

/* Shared CSS 1 theme */

/* ============================= */

/* ============================= */

/* Shared CSS 2 theme */

/* ============================= */
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
body {
color: black;
}
a {
color: black;
}
b {
text-align: center;
}
.sapContrastPlus {
color: orange;
}

body.sapContrastPlus,
body .sapContrastPlus {
color: blue;
}

.sapContrastPlus a {
color: blue;
}
/* ============================= */

/* Shared CSS 1 theme */

/* ============================= */

/* ============================= */

/* Shared CSS 2 theme */

/* ============================= */
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
body {
color: black;
}

a {
color: black;
}

b {
text-align: center;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import "base/library.source.less";
@import "plus/library.source.less";
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
body {
color: blue;
}
/* ============================= */
/* Shared CSS 1 theme */
/* ============================= */
#CSS_SCOPE_ROOT {
color: orange;
}
/* ============================= */
/* Shared CSS 2 theme */
/* ============================= */
a {
color: blue;
}

b {
text-align: center;
}
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1309,5 +1309,25 @@ describe("CSS Scoping (via option) of", function() {
}
return Promise.all(aPromises);
});

it("diff testing with comments in between", function() {
const expectedLibraryCssContent = readFile("test/expected/libraries/lib5/my/ui/lib/themes/cascading/library.css");
const expectedLibraryCssRtlContent = readFile("test/expected/libraries/lib5/my/ui/lib/themes/cascading/library-RTL.css");
return new Builder().build({
lessInputPath: "my/ui/lib/themes/cascading/library.source.less",
rootPaths: [
"test/fixtures/libraries/lib5"
],
scope: {
selector: "sapContrastPlus",
embeddedFilePath: "my/ui/lib/themes/cascading/plus/library.source.less",
embeddedCompareFilePath: "my/ui/lib/themes/cascading/base/library.source.less",
baseFilePath: "."
}
}).then(function(result) {
assert.equal(result.css, expectedLibraryCssContent, "CSS scoping should be correctly generated");
assert.equal(result.cssRtl, expectedLibraryCssRtlContent, "RTL CSS scoping should be correctly generated");
});
});
});
});

0 comments on commit 2527189

Please sign in to comment.