userdiff: add built-in pattern for CSS

CSS is widely used, motivating it being included as a built-in pattern. It must be noted that the word_regex for CSS (i.e. the regex defining what is a word in the language) does not consider '.' and '#' characters (in CSS selectors) to be part of the word. This behavior is documented by the test t/t4018/css-rule. The logic behind this behavior is the following: identifiers in CSS selectors are identifiers in a HTML/XML document. Therefore, the '.'/'#' character are not part of the identifier, but an indicator of the nature of the identifier in HTML/XML (class or id). Diffing ".class1" and ".class2" must show that the class name is changed, but we still are selecting a class. Logic behind the "pattern" regex is: 1. reject lines ending with a colon/semicolon (properties) 2. if a line begins with a name in column 1, pick the whole line Credits to Johannes Sixt (j6t@kdbg.org) for the pattern regex and most of the tests. Signed-off-by: William Duclot <william.duclot@ensimag.grenoble-inp.fr> Signed-off-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Reviewed-by: Johannes Sixt <j6t@kdbg.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

William Duclot committed Jun 3, 2016 at 14:32 UTC 0719f3eecd1234f6331cab980088239207e93335
15 files changed +90
Documentation/gitattributes.txt
+2
@@ -525,6 +525,8 @@ patterns are available:
525
526 - `csharp` suitable for source code in the C# language.
527
528 +- `css` suitable for cascading style sheets.
529 +
530 - `fortran` suitable for source code in the Fortran language.
531
532 - `fountain` suitable for Fountain documents.
t/t4018-diff-funcname.sh
+1
@@ -30,6 +30,7 @@ diffpatterns="
30 bibtex
31 cpp
32 csharp
33 + css
34 fortran
35 fountain
36 html
t/t4018/css-brace-in-col-1 new
+5
@@ -0,0 +1,5 @@
1 +RIGHT label.control-label
2 +{
3 + margin-top: 10px!important;
4 + border : 10px ChangeMe #C6C6C6;
5 +}
t/t4018/css-colon-eol new
+4
@@ -0,0 +1,4 @@
1 +RIGHT h1 {
2 +color:
3 +ChangeMe;
4 +}
t/t4018/css-colon-selector new
+5
@@ -0,0 +1,5 @@
1 +RIGHT a:hover {
2 + margin-top:
3 + 10px!important;
4 + border : 10px ChangeMe #C6C6C6;
5 +}
t/t4018/css-common new
+4
@@ -0,0 +1,4 @@
1 +RIGHT label.control-label {
2 + margin-top: 10px!important;
3 + border : 10px ChangeMe #C6C6C6;
4 +}
t/t4018/css-long-selector-list new
+6
@@ -0,0 +1,6 @@
1 +p.header,
2 +label.control-label,
3 +div ul#RIGHT {
4 + margin-top: 10px!important;
5 + border : 10px ChangeMe #C6C6C6;
6 +}
t/t4018/css-prop-sans-indent new
+5
@@ -0,0 +1,5 @@
1 +RIGHT, label.control-label {
2 +margin-top: 10px!important;
3 +padding: 0;
4 +border : 10px ChangeMe #C6C6C6;
5 +}
t/t4018/css-short-selector-list new
+4
@@ -0,0 +1,4 @@
1 +label.control, div ul#RIGHT {
2 + margin-top: 10px!important;
3 + border : 10px ChangeMe #C6C6C6;
4 +}
t/t4018/css-trailing-space new
+5
@@ -0,0 +1,5 @@
1 +RIGHT label.control-label {
2 + margin:10px;
3 + padding:10px;
4 + border : 10px ChangeMe #C6C6C6;
5 +}
t/t4034-diff-words.sh
+1
@@ -302,6 +302,7 @@ test_language_driver ada
302 test_language_driver bibtex
303 test_language_driver cpp
304 test_language_driver csharp
305 +test_language_driver css
306 test_language_driver fortran
307 test_language_driver html
308 test_language_driver java
t/t4034/css/expect new
+16
@@ -0,0 +1,16 @@
1 +<BOLD>diff --git a/pre b/post<RESET>
2 +<BOLD>index b8ae0bb..fe500b7 100644<RESET>
3 +<BOLD>--- a/pre<RESET>
4 +<BOLD>+++ b/post<RESET>
5 +<CYAN>@@ -1,10 +1,10 @@<RESET>
6 +.<RED>class-form<RESET><GREEN>other-form<RESET> label.control-label {
7 + margin-top: <RED>10<RESET><GREEN>15<RESET>px!important;
8 + border : 10px <RED>dashed<RESET><GREEN>dotted<RESET> #C6C6C6;
9 +}<RESET>
10 +<RED>#CCCCCC<RESET><GREEN>#CCCCCB<RESET>
11 +10em<RESET>
12 +<RED>padding-bottom<RESET><GREEN>margin-left<RESET>
13 +150<RED>px<RESET><GREEN>em<RESET>
14 +10px
15 +<RED>!important<RESET>
16 +<RED>div<RESET><GREEN>li<RESET>.class#id
t/t4034/css/post new
+10
@@ -0,0 +1,10 @@
1 +.other-form label.control-label {
2 + margin-top: 15px!important;
3 + border : 10px dotted #C6C6C6;
4 +}
5 +#CCCCCB
6 +10em
7 +margin-left
8 +150em
9 +10px
10 +li.class#id
t/t4034/css/pre new
+10
@@ -0,0 +1,10 @@
1 +.class-form label.control-label {
2 + margin-top: 10px!important;
3 + border : 10px dashed #C6C6C6;
4 +}
5 +#CCCCCC
6 +10em
7 +padding-bottom
8 +150px
9 +10px!important
10 +div.class#id
userdiff.c
+12
@@ -148,6 +148,18 @@ PATTERNS("csharp",
148 "[a-zA-Z_][a-zA-Z0-9_]*"
149 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
150 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
151 +IPATTERN("css",
152 + "![:;][[:space:]]*$\n"
153 + "^[_a-z0-9].*$",
154 + /* -- */
155 + /*
156 + * This regex comes from W3C CSS specs. Should theoretically also
157 + * allow ISO 10646 characters U+00A0 and higher,
158 + * but they are not handled in this regex.
159 + */
160 + "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */
161 + "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */
162 +),
163 { "default", NULL, -1, { NULL, 0 } },
164 };
165 #undef PATTERNS