88 The above copyright notice and this permission notice shall be
99 included in all copies or substantial portions of this Source Code Form.
1010*/
11- const isNumber = require ( 'is-number' ) ;
1211
1312const { registerWalker } = require ( '../walker' ) ;
1413
1514const Node = require ( './Node' ) ;
1615
17- const unitRegex = / % | c h | c m | e m | e x | i n | m m | m s | p c | p t | p x | s | q | r e m | v h | v m a x | v m i n | v w $ / i;
16+ /** A Number is:
17+ * 1. None or one plus or minus symbol; then
18+ * 2. Either,
19+ * 2.1. One or more digits; and / or,
20+ * 2.2. One period symbol; followed by,
21+ * 2.2.1. One or more digits;
22+ * then,
23+ * 3. If one "e" letter,
24+ * 3.1. One "e" letter; followed by,
25+ * 3.1.1. None or one plus or minus symbol; followed by,
26+ * 3.1.1.1. One or more digits.
27+ * @see https://drafts.csswg.org/css-syntax/#consume-a-number
28+ */
29+ const numberRegex = / ^ ( [ + - ] ? (?: \d + (?: \. \d * ) ? | \. \d + ) (?: [ E e ] [ + - ] ? \d + ) ? ) $ / ;
30+
31+ /** A Unit is:
32+ * 1. Either,
33+ * 1.1. One dash; followed by,
34+ * 1.1.1. One letter, non-ASCII, underscore, dash; or,
35+ * 1.1.2. One escape slash; followed by,
36+ * 1.1.2.1 One non-newline;
37+ * or,
38+ * 1.2. One letter, non-ASCII, underscore; or,
39+ * 1.3. One escape slash; followed by,
40+ * 1.3.1. One non-newline;
41+ * then,
42+ * 2. Zero or more of;
43+ * 2.1 One letter, non-ASCII, underscore, dash; then / or,
44+ * 2.2 One escape slash; followed by,
45+ * 2.2.1. One non-newline.
46+ * @see https://drafts.csswg.org/css-syntax/#consume-numeric-token
47+ */
48+ const unitRegex = / ^ ( - ? (?: [ - A - Z _ a - z ] | [ ^ \x00 - \x7F ] | \\ [ ^ \n \f \r ] ) (?: [ - \w ] | [ ^ \x00 - \x7F ] | \\ [ ^ \n \f \r ] ) * | % ) $ / ; // eslint-disable-line no-control-regex
49+
50+ /** A Numeric is:
51+ * 1. One Number; followed by,
52+ * 1.1 Zero or one Unit.
53+ */
54+ const numericRegex = new RegExp (
55+ `^${ numberRegex . source . slice ( 1 , - 1 ) + unitRegex . source . slice ( 1 , - 1 ) } ?$`
56+ ) ;
1857
1958class Numeric extends Node {
2059 constructor ( options = { } ) {
@@ -25,33 +64,17 @@ class Numeric extends Node {
2564
2665 static fromTokens ( tokens , parser ) {
2766 parser . fromFirst ( tokens , Numeric ) ;
28- let [ [ , value ] ] = tokens ;
29- const unit = Numeric . parseUnit ( value ) ;
30- value = value . replace ( unit , '' ) ;
67+
68+ const [ [ , rawValue ] ] = tokens ;
69+ const [ , value , unit = '' ] = rawValue . match ( numericRegex ) ;
3170
3271 const { lastNode } = parser ;
33- lastNode . unit = unit || '' ;
72+ lastNode . unit = unit ;
3473 lastNode . value = value ;
3574 }
3675
37- static parseUnit ( what ) {
38- const matches = what . match ( unitRegex ) ;
39- const [ result ] = matches || [ ] ;
40- return result ;
41- }
42-
4376 static test ( what ) {
44- return isNumber ( what ) ;
45- }
46-
47- static testUnit ( what ) {
48- const unit = Numeric . parseUnit ( what ) ;
49-
50- if ( unit ) {
51- const remaining = what . replace ( unit , '' ) ;
52- return isNumber ( remaining ) ;
53- }
54- return false ;
77+ return numericRegex . test ( what ) ;
5578 }
5679}
5780
0 commit comments