From 0719f3eecd1234f6331cab980088239207e93335 Mon Sep 17 00:00:00 2001 From: William Duclot Date: Fri, 3 Jun 2016 14:32:26 +0200 Subject: [PATCH] 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 Signed-off-by: Matthieu Moy Reviewed-by: Johannes Sixt Signed-off-by: Junio C Hamano --- Documentation/gitattributes.txt | 2 ++ t/t4018-diff-funcname.sh | 1 + t/t4018/css-brace-in-col-1 | 5 +++++ t/t4018/css-colon-eol | 4 ++++ t/t4018/css-colon-selector | 5 +++++ t/t4018/css-common | 4 ++++ t/t4018/css-long-selector-list | 6 ++++++ t/t4018/css-prop-sans-indent | 5 +++++ t/t4018/css-short-selector-list | 4 ++++ t/t4018/css-trailing-space | 5 +++++ t/t4034-diff-words.sh | 1 + t/t4034/css/expect | 16 ++++++++++++++++ t/t4034/css/post | 10 ++++++++++ t/t4034/css/pre | 10 ++++++++++ userdiff.c | 12 ++++++++++++ 15 files changed, 90 insertions(+) create mode 100644 t/t4018/css-brace-in-col-1 create mode 100644 t/t4018/css-colon-eol create mode 100644 t/t4018/css-colon-selector create mode 100644 t/t4018/css-common create mode 100644 t/t4018/css-long-selector-list create mode 100644 t/t4018/css-prop-sans-indent create mode 100644 t/t4018/css-short-selector-list create mode 100644 t/t4018/css-trailing-space create mode 100644 t/t4034/css/expect create mode 100644 t/t4034/css/post create mode 100644 t/t4034/css/pre diff --git a/Documentation/gitattributes.txt b/Documentation/gitattributes.txt index e3b1de8033..8882a3e914 100644 --- a/Documentation/gitattributes.txt +++ b/Documentation/gitattributes.txt @@ -525,6 +525,8 @@ patterns are available: - `csharp` suitable for source code in the C# language. +- `css` suitable for cascading style sheets. + - `fortran` suitable for source code in the Fortran language. - `fountain` suitable for Fountain documents. diff --git a/t/t4018-diff-funcname.sh b/t/t4018-diff-funcname.sh index 67373dc44e..1795ffc3aa 100755 --- a/t/t4018-diff-funcname.sh +++ b/t/t4018-diff-funcname.sh @@ -30,6 +30,7 @@ diffpatterns=" bibtex cpp csharp + css fortran fountain html diff --git a/t/t4018/css-brace-in-col-1 b/t/t4018/css-brace-in-col-1 new file mode 100644 index 0000000000..7831577506 --- /dev/null +++ b/t/t4018/css-brace-in-col-1 @@ -0,0 +1,5 @@ +RIGHT label.control-label +{ + margin-top: 10px!important; + border : 10px ChangeMe #C6C6C6; +} diff --git a/t/t4018/css-colon-eol b/t/t4018/css-colon-eol new file mode 100644 index 0000000000..5a30553d29 --- /dev/null +++ b/t/t4018/css-colon-eol @@ -0,0 +1,4 @@ +RIGHT h1 { +color: +ChangeMe; +} diff --git a/t/t4018/css-colon-selector b/t/t4018/css-colon-selector new file mode 100644 index 0000000000..c6d71fb42d --- /dev/null +++ b/t/t4018/css-colon-selector @@ -0,0 +1,5 @@ +RIGHT a:hover { + margin-top: + 10px!important; + border : 10px ChangeMe #C6C6C6; +} diff --git a/t/t4018/css-common b/t/t4018/css-common new file mode 100644 index 0000000000..84ed754b33 --- /dev/null +++ b/t/t4018/css-common @@ -0,0 +1,4 @@ +RIGHT label.control-label { + margin-top: 10px!important; + border : 10px ChangeMe #C6C6C6; +} diff --git a/t/t4018/css-long-selector-list b/t/t4018/css-long-selector-list new file mode 100644 index 0000000000..7ccd25d9ed --- /dev/null +++ b/t/t4018/css-long-selector-list @@ -0,0 +1,6 @@ +p.header, +label.control-label, +div ul#RIGHT { + margin-top: 10px!important; + border : 10px ChangeMe #C6C6C6; +} diff --git a/t/t4018/css-prop-sans-indent b/t/t4018/css-prop-sans-indent new file mode 100644 index 0000000000..a9e3c86b3c --- /dev/null +++ b/t/t4018/css-prop-sans-indent @@ -0,0 +1,5 @@ +RIGHT, label.control-label { +margin-top: 10px!important; +padding: 0; +border : 10px ChangeMe #C6C6C6; +} diff --git a/t/t4018/css-short-selector-list b/t/t4018/css-short-selector-list new file mode 100644 index 0000000000..6a0bdee336 --- /dev/null +++ b/t/t4018/css-short-selector-list @@ -0,0 +1,4 @@ +label.control, div ul#RIGHT { + margin-top: 10px!important; + border : 10px ChangeMe #C6C6C6; +} diff --git a/t/t4018/css-trailing-space b/t/t4018/css-trailing-space new file mode 100644 index 0000000000..32b5606c70 --- /dev/null +++ b/t/t4018/css-trailing-space @@ -0,0 +1,5 @@ +RIGHT label.control-label { + margin:10px; + padding:10px; + border : 10px ChangeMe #C6C6C6; +} diff --git a/t/t4034-diff-words.sh b/t/t4034-diff-words.sh index f2f55fc51c..912df91226 100755 --- a/t/t4034-diff-words.sh +++ b/t/t4034-diff-words.sh @@ -302,6 +302,7 @@ test_language_driver ada test_language_driver bibtex test_language_driver cpp test_language_driver csharp +test_language_driver css test_language_driver fortran test_language_driver html test_language_driver java diff --git a/t/t4034/css/expect b/t/t4034/css/expect new file mode 100644 index 0000000000..ed10393bda --- /dev/null +++ b/t/t4034/css/expect @@ -0,0 +1,16 @@ +diff --git a/pre b/post +index b8ae0bb..fe500b7 100644 +--- a/pre ++++ b/post +@@ -1,10 +1,10 @@ +.class-formother-form label.control-label { + margin-top: 1015px!important; + border : 10px dasheddotted #C6C6C6; +} +#CCCCCC#CCCCCB +10em +padding-bottommargin-left +150pxem +10px +!important +divli.class#id diff --git a/t/t4034/css/post b/t/t4034/css/post new file mode 100644 index 0000000000..fe500b7a4f --- /dev/null +++ b/t/t4034/css/post @@ -0,0 +1,10 @@ +.other-form label.control-label { + margin-top: 15px!important; + border : 10px dotted #C6C6C6; +} +#CCCCCB +10em +margin-left +150em +10px +li.class#id diff --git a/t/t4034/css/pre b/t/t4034/css/pre new file mode 100644 index 0000000000..b8ae0bb48f --- /dev/null +++ b/t/t4034/css/pre @@ -0,0 +1,10 @@ +.class-form label.control-label { + margin-top: 10px!important; + border : 10px dashed #C6C6C6; +} +#CCCCCC +10em +padding-bottom +150px +10px!important +div.class#id diff --git a/userdiff.c b/userdiff.c index 6bf2505994..2125d6da26 100644 --- a/userdiff.c +++ b/userdiff.c @@ -148,6 +148,18 @@ PATTERNS("csharp", "[a-zA-Z_][a-zA-Z0-9_]*" "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), +IPATTERN("css", + "![:;][[:space:]]*$\n" + "^[_a-z0-9].*$", + /* -- */ + /* + * This regex comes from W3C CSS specs. Should theoretically also + * allow ISO 10646 characters U+00A0 and higher, + * but they are not handled in this regex. + */ + "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */ + "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */ +), { "default", NULL, -1, { NULL, 0 } }, }; #undef PATTERNS -- 2.39.2