From 783c876c011ba7bd9161d2c5fedb647f425e6aa5 Mon Sep 17 00:00:00 2001 From: Andrey Alexandrov Date: Thu, 24 Oct 2019 15:49:40 +0300 Subject: [PATCH] Add support for `font-weight` ranges in `@font-face` (#4372) * Fix #4327 * Replace regexp with list.space + update tests * Add reject cases * Fix lint error --- .../font-weight-notation/__tests__/index.js | 63 +++++++++++++++++++ lib/rules/font-weight-notation/index.js | 10 +++ 2 files changed, 73 insertions(+) diff --git a/lib/rules/font-weight-notation/__tests__/index.js b/lib/rules/font-weight-notation/__tests__/index.js index f6be4478c8..22f5c295ef 100644 --- a/lib/rules/font-weight-notation/__tests__/index.js +++ b/lib/rules/font-weight-notation/__tests__/index.js @@ -84,6 +84,33 @@ testRule(rule, { code: 'a { font-weight: INITIAL; }', description: 'ignore initial value', }, + { + code: '@font-face { font-weight: 400; }', + }, + { + code: '@font-face { font-weight: 400.5; }', + }, + { + code: '@font-face { font-weight: 400 700; }', + }, + { + code: '@font-face { font-weight: 100.5 400; }', + }, + { + code: '@font-face { font-weight: 400.5 700.5; }', + }, + { + code: '@font-face { font-weight: 400.5 /* 700 */ 700.5; }', + }, + { + code: '@font-face { font-weight: 400.5 /* 700 */700.5; }', + }, + { + code: '@font-face { font-weight: 400 /*700*/700; }', + }, + { + code: '@font-face { font-weight: 400.5/*700*/ 700.5; }', + }, ], reject: [ @@ -144,6 +171,42 @@ testRule(rule, { line: 1, column: 11, }, + { + code: '@font-face { font-weight: normal bold; }', + message: messages.expected('numeric'), + line: 1, + column: 27, + }, + { + code: '@font-face { font-weight: 400 bold; }', + message: messages.expected('numeric'), + line: 1, + column: 27, + }, + { + code: '@font-face { font-weight: normal 700; }', + message: messages.expected('numeric'), + line: 1, + column: 27, + }, + { + code: '@font-face { font-weight: /* 400 */ normal 700; }', + message: messages.expected('numeric'), + line: 1, + column: 37, + }, + { + code: '@font-face { font-weight: normal /* 400 */700; }', + message: messages.expected('numeric'), + line: 1, + column: 27, + }, + { + code: '@font-face { font-weight: normal/* 400 */ 700; }', + message: messages.expected('numeric'), + line: 1, + column: 27, + }, ], }); diff --git a/lib/rules/font-weight-notation/index.js b/lib/rules/font-weight-notation/index.js index c6181affd7..bc21df7d2d 100644 --- a/lib/rules/font-weight-notation/index.js +++ b/lib/rules/font-weight-notation/index.js @@ -103,6 +103,16 @@ const rule = function(expectation, options) { const weightValueOffset = decl.value.indexOf(weightValue); if (expectation === 'numeric') { + if (decl.parent.type === 'atrule' && decl.parent.name.toLowerCase() === 'font-face') { + const weightValueNumbers = postcss.list.space(weightValue); + + if (!weightValueNumbers.every(isNumbery)) { + return complain(messages.expected('numeric')); + } + + return; + } + if (!isNumbery(weightValue)) { return complain(messages.expected('numeric')); }