Commit 082e82db by Russell Belfer

Update Javascript userdiff driver and tests

Writing a sample Javascript driver pointed out some extra
whitespace handling that needed to be done in the diff driver.
This adds some tests with some sample javascript code that I
pulled off of GitHub just to see what would happen.  Also, to
clean up the userdiff test data, I did a "git gc" and packed
up the test objects.
parent 41159877
......@@ -437,6 +437,7 @@ static int diff_context_line__pattern_match(
i = (pmatch[1].rm_so >= 0) ? 1 : 0;
git_buf_consume(line, git_buf_cstr(line) + pmatch[i].rm_so);
git_buf_truncate(line, pmatch[i].rm_eo - pmatch[i].rm_so);
git_buf_rtrim(line);
return true;
}
......
......@@ -193,9 +193,9 @@ PATTERNS("php",
"|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
PATTERNS("javascript",
"^[ \t]*(\(?function[ \t].*)$\n"
"^[ \t]*(var[ \t]+[a-zA-Z_][a-zA-Z0-9_]*[ \t]*=[ \t]*function[ \t\(].*)$\n"
"^[ \t]*([a-zA-Z_][a-zA-Z0-9_]*[ \t]*:[ \t]*function[ \t\(].*)$",
"^[ \t]*(function[ \t][a-zA-Z_][^\{]*)\n"
"^[ \t]*(var[ \t]+[a-zA-Z_][a-zA-Z0-9_]*[ \t]*=[ \t]*function[ \t\(][^\{]*)\n"
"^[ \t]*([a-zA-Z_][a-zA-Z0-9_]*[ \t]*:[ \t]*function[ \t\(][^\{]*)",
/* -- */
"[a-zA-Z_][a-zA-Z0-9_]*"
"|[-+0-9.e]+[fFlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
......
......@@ -180,22 +180,23 @@ void test_diff_drivers__builtins(void)
git_patch *patch;
git_buf file = GIT_BUF_INIT, actual = GIT_BUF_INIT, expected = GIT_BUF_INIT;
git_diff_options opts = GIT_DIFF_OPTIONS_INIT;
int i;
static const char *files[] = {
"html",
NULL
};
git_vector files = GIT_VECTOR_INIT;
size_t i;
char *path, *extension;
g_repo = cl_git_sandbox_init("userdiff");
cl_git_pass(git_path_dirload("userdiff/files", 9, 0, 0, &files));
opts.interhunk_lines = 1;
opts.context_lines = 1;
opts.pathspec.count = 1;
for (i = 0; files[i]; ++i) {
git_buf_sets(&file, "files/file.");
git_buf_puts(&file, files[i]);
opts.pathspec.strings = &file.ptr;
git_vector_foreach(&files, i, path) {
if (git__prefixcmp(path, "files/file."))
continue;
extension = path + strlen("files/file.");
opts.pathspec.strings = &path;
/* do diff with no special driver */
......@@ -205,7 +206,7 @@ void test_diff_drivers__builtins(void)
cl_git_pass(git_patch_to_buf(&actual, patch));
git_buf_sets(&expected, "userdiff/expected/nodriver/diff.");
git_buf_puts(&expected, files[i]);
git_buf_puts(&expected, extension);
cl_git_pass(git_futils_readbuffer(&expected, expected.ptr));
overwrite_filemode(expected.ptr, &actual);
......@@ -220,7 +221,7 @@ void test_diff_drivers__builtins(void)
{
FILE *fp = fopen("userdiff/.gitattributes", "w");
fprintf(fp, "*.%s diff=%s\n", files[i], files[i]);
fprintf(fp, "*.%s diff=%s\n", extension, extension);
fclose(fp);
}
......@@ -230,7 +231,7 @@ void test_diff_drivers__builtins(void)
cl_git_pass(git_patch_to_buf(&actual, patch));
git_buf_sets(&expected, "userdiff/expected/driver/diff.");
git_buf_puts(&expected, files[i]);
git_buf_puts(&expected, extension);
cl_git_pass(git_futils_readbuffer(&expected, expected.ptr));
overwrite_filemode(expected.ptr, &actual);
......@@ -240,9 +241,12 @@ void test_diff_drivers__builtins(void)
git_buf_clear(&actual);
git_patch_free(patch);
git_diff_free(diff);
git__free(path);
}
git_buf_free(&file);
git_buf_free(&actual);
git_buf_free(&expected);
git_vector_free(&files);
}
802734c801bffc466d69876bc4814f747aaac4fe refs/heads/master
P pack-03f78c35e3ca74fffd9d6c2b6dcd60d6ab6a614a.pack
# pack-refs with: peeled fully-peeled
802734c801bffc466d69876bc4814f747aaac4fe refs/heads/master
9db1d09ff9ad5190bcf12d72ea3c818ffca344c5
/*
Some code extracted from https://github.com/julianlloyd/scrollReveal.js
which happens to be a trending Javascript repo with an MIT license at
the time I was working on Javascript userdiff support in libgit2
I extracted just some of the code, so I suspect this is no longer valid
Javascript code, but it contains enough example patterns to work.
*/
;(function (window) {
'use strict';
var docElem = window.document.documentElement;
function getViewportH () {
var client = docElem['clientHeight'],
inner = window['innerHeight'],
sample = window['otherProperty'];
return (client < inner) ? inner : client;
}
function getOffset (el) {
var offsetTop = 0,
offsetLeft = 0;
do {
if (!isNaN(el.offsetTop)) {
offsetTop += el.offsetTop + 1;
}
if (!isNaN(el.offsetLeft)) {
offsetLeft += el.offsetLeft;
}
} while (el = el.offsetParent)
return {
top: offsetTop,
left: offsetLeft
}
}
function isElementInViewport (el, h) {
var scrolled = window.pageYOffset,
viewed = scrolled + getViewportH(),
elTop = getOffset(el).top,
elBottom = elTop + el.offsetHeight,
h = h || 0;
return (elTop + el.offsetHeight * h) <= viewed && (elBottom) >= scrolled;
}
scrollReveal.prototype = {
_init: function () {
var self = this;
this.elems = Array.prototype.slice.call(docElem.querySelectorAll('[data-scrollReveal]'));
this.scrolled = false;
this.elems.forEach(function (el, i) {
self.animate(el);
});
var scrollHandler = function () {
if (!self.scrolled) {
self.scrolled = true;
setTimeout(function () {
self._scrollPage();
}, 61);
}
};
var resizeHandler = function () {
function delayed() {
self._scrollPage();
self.resizeTimeout = null;
}
if (self.resizeTimeout) {
clearTimeout(self.resizeTimeout);
}
self.resizeTimeout = setTimeout(delayed, 200);
};
window.addEventListener('scroll', scrollHandler, false);
window.addEventListener('resize', resizeHandler, false);
},
/*=============================================================================*/
_scrollPage: function () {
var self = this;
this.elems.forEach(function (el, i) {
if (isElementInViewport(el, self.options.viewportFactor)) {
self.animate(el);
}
});
this.scrolled = false;
this.tested = true;
},
}; // end scrollReveal.prototype
document.addEventListener("DOMContentLoaded", function (evt) {
window.scrollReveal = new scrollReveal();
});
})(window);
/*
Some code extracted from https://github.com/julianlloyd/scrollReveal.js
which happens to be a trending Javascript repo with an MIT license at
the time I was working on Javascript userdiff support in libgit2
I extracted just some of the code, so I suspect this is no longer valid
Javascript code, but it contains enough example patterns to work.
*/
;(function (window) {
'use strict';
var docElem = window.document.documentElement;
function getViewportH () {
var client = docElem['clientHeight'],
inner = window['innerHeight'];
return (client < inner) ? inner : client;
}
function getOffset (el) {
var offsetTop = 0,
offsetLeft = 0;
do {
if (!isNaN(el.offsetTop)) {
offsetTop += el.offsetTop;
}
if (!isNaN(el.offsetLeft)) {
offsetLeft += el.offsetLeft;
}
} while (el = el.offsetParent)
return {
top: offsetTop,
left: offsetLeft
}
}
function isElementInViewport (el, h) {
var scrolled = window.pageYOffset,
viewed = scrolled + getViewportH(),
elH = el.offsetHeight,
elTop = getOffset(el).top,
elBottom = elTop + elH,
h = h || 0;
return (elTop + elH * h) <= viewed && (elBottom) >= scrolled;
}
scrollReveal.prototype = {
_init: function () {
var self = this;
this.elems = Array.prototype.slice.call(docElem.querySelectorAll('[data-scrollReveal]'));
this.scrolled = false;
// Initialize all scrollreveals, triggering all
// reveals on visible elements.
this.elems.forEach(function (el, i) {
self.animate(el);
});
var scrollHandler = function () {
if (!self.scrolled) {
self.scrolled = true;
setTimeout(function () {
self._scrollPage();
}, 60);
}
};
var resizeHandler = function () {
function delayed() {
self._scrollPage();
self.resizeTimeout = null;
}
if (self.resizeTimeout) {
clearTimeout(self.resizeTimeout);
}
self.resizeTimeout = setTimeout(delayed, 200);
};
window.addEventListener('scroll', scrollHandler, false);
window.addEventListener('resize', resizeHandler, false);
},
/*=============================================================================*/
_scrollPage: function () {
var self = this;
this.elems.forEach(function (el, i) {
if (isElementInViewport(el, self.options.viewportFactor)) {
self.animate(el);
}
});
this.scrolled = false;
},
}; // end scrollReveal.prototype
document.addEventListener("DOMContentLoaded", function (evt) {
window.scrollReveal = new scrollReveal();
});
})(window);
diff --git a/files/file.javascript b/files/file.javascript
index b9f1286..7cd3c5a 100644
--- a/files/file.javascript
+++ b/files/file.javascript
@@ -16,3 +16,4 @@ function getViewportH ()
var client = docElem['clientHeight'],
- inner = window['innerHeight'];
+ inner = window['innerHeight'],
+ sample = window['otherProperty'];
@@ -27,3 +28,3 @@ function getOffset (el)
if (!isNaN(el.offsetTop)) {
- offsetTop += el.offsetTop;
+ offsetTop += el.offsetTop + 1;
}
@@ -43,8 +44,7 @@ function isElementInViewport (el, h)
viewed = scrolled + getViewportH(),
- elH = el.offsetHeight,
elTop = getOffset(el).top,
- elBottom = elTop + elH,
+ elBottom = elTop + el.offsetHeight,
h = h || 0;
- return (elTop + elH * h) <= viewed && (elBottom) >= scrolled;
+ return (elTop + el.offsetHeight * h) <= viewed && (elBottom) >= scrolled;
}
@@ -60,4 +60,2 @@ _init: function ()
- // Initialize all scrollreveals, triggering all
- // reveals on visible elements.
this.elems.forEach(function (el, i) {
@@ -71,3 +69,3 @@ var scrollHandler = function ()
self._scrollPage();
- }, 60);
+ }, 61);
}
@@ -101,2 +99,3 @@ _scrollPage: function ()
this.scrolled = false;
+ this.tested = true;
},
diff --git a/files/file.javascript b/files/file.javascript
index b9f1286..7cd3c5a 100644
--- a/files/file.javascript
+++ b/files/file.javascript
@@ -16,3 +16,4 @@
var client = docElem['clientHeight'],
- inner = window['innerHeight'];
+ inner = window['innerHeight'],
+ sample = window['otherProperty'];
@@ -27,3 +28,3 @@
if (!isNaN(el.offsetTop)) {
- offsetTop += el.offsetTop;
+ offsetTop += el.offsetTop + 1;
}
@@ -43,8 +44,7 @@
viewed = scrolled + getViewportH(),
- elH = el.offsetHeight,
elTop = getOffset(el).top,
- elBottom = elTop + elH,
+ elBottom = elTop + el.offsetHeight,
h = h || 0;
- return (elTop + elH * h) <= viewed && (elBottom) >= scrolled;
+ return (elTop + el.offsetHeight * h) <= viewed && (elBottom) >= scrolled;
}
@@ -60,4 +60,2 @@
- // Initialize all scrollreveals, triggering all
- // reveals on visible elements.
this.elems.forEach(function (el, i) {
@@ -71,3 +69,3 @@
self._scrollPage();
- }, 60);
+ }, 61);
}
@@ -101,2 +99,3 @@
this.scrolled = false;
+ this.tested = true;
},
/*
Some code extracted from https://github.com/julianlloyd/scrollReveal.js
which happens to be a trending Javascript repo with an MIT license at
the time I was working on Javascript userdiff support in libgit2
I extracted just some of the code, so I suspect this is no longer valid
Javascript code, but it contains enough example patterns to work.
*/
;(function (window) {
'use strict';
var docElem = window.document.documentElement;
function getViewportH () {
var client = docElem['clientHeight'],
inner = window['innerHeight'],
sample = window['otherProperty'];
return (client < inner) ? inner : client;
}
function getOffset (el) {
var offsetTop = 0,
offsetLeft = 0;
do {
if (!isNaN(el.offsetTop)) {
offsetTop += el.offsetTop + 1;
}
if (!isNaN(el.offsetLeft)) {
offsetLeft += el.offsetLeft;
}
} while (el = el.offsetParent)
return {
top: offsetTop,
left: offsetLeft
}
}
function isElementInViewport (el, h) {
var scrolled = window.pageYOffset,
viewed = scrolled + getViewportH(),
elTop = getOffset(el).top,
elBottom = elTop + el.offsetHeight,
h = h || 0;
return (elTop + el.offsetHeight * h) <= viewed && (elBottom) >= scrolled;
}
scrollReveal.prototype = {
_init: function () {
var self = this;
this.elems = Array.prototype.slice.call(docElem.querySelectorAll('[data-scrollReveal]'));
this.scrolled = false;
this.elems.forEach(function (el, i) {
self.animate(el);
});
var scrollHandler = function () {
if (!self.scrolled) {
self.scrolled = true;
setTimeout(function () {
self._scrollPage();
}, 61);
}
};
var resizeHandler = function () {
function delayed() {
self._scrollPage();
self.resizeTimeout = null;
}
if (self.resizeTimeout) {
clearTimeout(self.resizeTimeout);
}
self.resizeTimeout = setTimeout(delayed, 200);
};
window.addEventListener('scroll', scrollHandler, false);
window.addEventListener('resize', resizeHandler, false);
},
/*=============================================================================*/
_scrollPage: function () {
var self = this;
this.elems.forEach(function (el, i) {
if (isElementInViewport(el, self.options.viewportFactor)) {
self.animate(el);
}
});
this.scrolled = false;
this.tested = true;
},
}; // end scrollReveal.prototype
document.addEventListener("DOMContentLoaded", function (evt) {
window.scrollReveal = new scrollReveal();
});
})(window);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment