with-style 5.0.189 → 5.0.191

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,41 +1,57 @@
1
1
  "use strict";
2
2
 
3
3
  const entries = [
4
-
5
- { "media": "^@media" },
6
-
7
- { "colour": "^#(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{3})" },
8
-
9
- { "important": "^!important" },
10
-
11
- { "percentage": "^(?:[0-9]+|[0-9]*\\.[0-9]+)%" },
12
-
13
- { "frequency": "^(?:[0-9]+|[0-9]*\\.[0-9]+)(?:hz|khz)" },
14
-
15
- { "fraction": "^[1-9][0-9]*?fr" },
16
-
17
- { "length": "^(?:[0-9]+|[0-9]*\\.[0-9]+)(?:px|cm|mm|in|pt|pc)" },
18
-
19
- { "angle": "^(?:[0-9]+|[0-9]*\\.[0-9]+)(?:deg|rad|grad)" },
20
-
21
- { "time": "^(?:[0-9]+|[0-9]*\\.[0-9]+)(?:s|ms)" },
22
-
23
- { "rems": "^(?:[0-9]+|[0-9]*\\.[0-9]+)rem" },
24
-
25
- { "ems": "^(?:[0-9]+|[0-9]*\\.[0-9]+)em" },
26
-
27
- { "number": "^[0-9]+|[0-9]*\\.[0-9]+" },
28
-
29
- { "special": "^;|::|:|\\.|,|\\|=|~=|=|>|\\{|\\}|\\(|\\)|\\[|\\]" },
30
-
31
- { "identifier": "^[_a-zA-Z0-9-]+" },
32
-
33
- { "logical-operator": "^^(?:and|not)$" },
34
-
35
- { "plus-or-minus": "^\\+|-" },
36
-
37
- { "unassigned": "^[^\\s]+" }
38
-
4
+ {
5
+ "media": "^@media"
6
+ },
7
+ {
8
+ "colour": "^#(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{3})"
9
+ },
10
+ {
11
+ "important": "^!important"
12
+ },
13
+ {
14
+ "percentage": "^(?:[0-9]+|[0-9]*\\.[0-9]+)%"
15
+ },
16
+ {
17
+ "frequency": "^(?:[0-9]+|[0-9]*\\.[0-9]+)(?:hz|khz)"
18
+ },
19
+ {
20
+ "fraction": "^[1-9][0-9]*?fr"
21
+ },
22
+ {
23
+ "length": "^(?:[0-9]+|[0-9]*\\.[0-9]+)(?:px|cm|mm|in|pt|pc)"
24
+ },
25
+ {
26
+ "angle": "^(?:[0-9]+|[0-9]*\\.[0-9]+)(?:deg|rad|grad)"
27
+ },
28
+ {
29
+ "time": "^(?:[0-9]+|[0-9]*\\.[0-9]+)(?:s|ms)"
30
+ },
31
+ {
32
+ "rems": "^(?:[0-9]+|[0-9]*\\.[0-9]+)rem"
33
+ },
34
+ {
35
+ "ems": "^(?:[0-9]+|[0-9]*\\.[0-9]+)em"
36
+ },
37
+ {
38
+ "number": "^[0-9]+|[0-9]*\\.[0-9]+"
39
+ },
40
+ {
41
+ "special": "^;|::|:|\\.|,|\\|=|~=|=|>|\\{|\\}|\\(|\\)|\\[|\\]"
42
+ },
43
+ {
44
+ "identifier": "^[_a-zA-Z0-9-]+"
45
+ },
46
+ {
47
+ "logical-operator": "^^(?:and|not)$"
48
+ },
49
+ {
50
+ "plus-or-minus": "^\\+|-"
51
+ },
52
+ {
53
+ "unassigned": "^[^\\s]+"
54
+ }
39
55
  ];
40
56
 
41
57
  export default entries;
package/src/example.js CHANGED
@@ -31,6 +31,16 @@ generateStyle([[`
31
31
  .active {
32
32
  display: block;
33
33
  }
34
+
35
+ @media (min-width: 640px) {
36
+
37
+ padding-top: 1rem;
38
+
39
+ .menu {
40
+ margin: 0;
41
+ }
42
+
43
+ }
34
44
 
35
45
  `]], "abc");
36
46
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  import { Query } from "occam-query";
4
4
 
5
- import { EMPTY_STRING } from "../constants";
5
+ import { EMPTY_STRING, TWO_SPACES } from "../constants";
6
6
  import { contentFromQueryNodeAndTokens } from "../utilities/content";
7
7
 
8
8
  const importantQuery = Query.fromExpression("/*/important"),
@@ -20,7 +20,7 @@ export default class Declaration {
20
20
  return this.propertyValues;
21
21
  }
22
22
 
23
- getNameProperty() {
23
+ getPropertyName() {
24
24
  return this.propertyName;
25
25
  }
26
26
 
@@ -53,10 +53,10 @@ export default class Declaration {
53
53
  return matches;
54
54
  }
55
55
 
56
- asCSS(indent, last) {
57
- const css = last ?
58
- `${indent}${this.propertyName}: ${this.propertyValues}${this.important};` :
59
- `${indent}${this.propertyName}: ${this.propertyValues}${this.important};\n`;
56
+ asCSS(indent) {
57
+ indent = indent + TWO_SPACES;
58
+
59
+ const css = `${indent}${this.propertyName}: ${this.propertyValues}${this.important};\n`;
60
60
 
61
61
  return css;
62
62
  }
@@ -33,37 +33,19 @@ export default class Declarations {
33
33
  }
34
34
 
35
35
  asCSS(className, indent) {
36
- if (indent === undefined) {
37
- indent = className; ///
36
+ const declarationsCSS = this.array.reduce((declarationsCSS, declaration) => {
37
+ const declarationCSS = declaration.asCSS(indent);
38
38
 
39
- className = null; ///
40
- }
39
+ declarationsCSS += declarationCSS;
41
40
 
42
- let css = EMPTY_STRING;
43
-
44
- const length = this.array.length;
45
-
46
- if (length > 0) {
47
- const lastIndex = length - 1,
48
- declarationsCSS = this.array.reduce((declarationsCSS, declaration, index) => {
49
- const last = (index === lastIndex),
50
- declarationCSS = declaration.asCSS(indent, last);
51
-
52
- declarationsCSS += declarationCSS;
53
-
54
- return declarationsCSS;
55
- }, EMPTY_STRING);
56
-
57
- if (className === null) {
58
- css = declarationsCSS; ///
59
- } else {
60
- css = `.${className} {
61
- ${declarationsCSS}
62
- }
41
+ return declarationsCSS;
42
+ }, EMPTY_STRING),
43
+ css = (className === null) ?
44
+ declarationsCSS : ///
45
+ `${indent}.${className} {
46
+ ${declarationsCSS}${indent}}
63
47
 
64
48
  `;
65
- }
66
- }
67
49
 
68
50
  return css;
69
51
  }
@@ -5,7 +5,8 @@ import { Query } from "occam-query";
5
5
  import RuleSets from "./ruleSets";
6
6
  import Declarations from "./declarations";
7
7
 
8
- import { TWO_SPACES, FOUR_SPACES } from "../constants";
8
+ import { trim } from "../utilities/string";
9
+ import { TWO_SPACES, EMPTY_STRING } from "../constants";
9
10
  import { contentFromQueryNodeAndTokens } from "../utilities/content";
10
11
 
11
12
  const queriesQuery = Query.fromExpression("/media/mediaQueries"); ///
@@ -29,16 +30,21 @@ export default class Media {
29
30
  return this.queries;
30
31
  }
31
32
 
32
- asCSS(className) {
33
- let css = "";
33
+ asCSS(className, indent) {
34
+ indent = indent + TWO_SPACES;
34
35
 
35
- const ruleSetsCSS = this.ruleSets.asCSS(className, TWO_SPACES),
36
- declarationsCSS = this.declarations.asCSS(className, FOUR_SPACES);
36
+ let css = EMPTY_STRING;
37
+
38
+ const ruleSetsCSS = this.ruleSets.asCSS(className, indent),
39
+ declarationsCSS = this.declarations.asCSS(className, indent);
40
+
41
+ let ruleSetsDeclarationsCSS = `${declarationsCSS}${ruleSetsCSS}`;
42
+
43
+ if (ruleSetsDeclarationsCSS !== EMPTY_STRING) {
44
+ ruleSetsDeclarationsCSS = trim(ruleSetsDeclarationsCSS);
37
45
 
38
- if ((ruleSetsCSS !== null) || (declarationsCSS !== null)) {
39
46
  css = `@media ${this.queries} {
40
- ${declarationsCSS}${ruleSetsCSS}
41
- }
47
+ ${ruleSetsDeclarationsCSS}}
42
48
 
43
49
  `;
44
50
  }
@@ -23,9 +23,9 @@ export default class Medias {
23
23
  this.array.forEach(callback);
24
24
  }
25
25
 
26
- asCSS(className) {
26
+ asCSS(className, indent) {
27
27
  const css = this.array.reduce((css, media) => {
28
- const mediaCSS = media.asCSS(className);
28
+ const mediaCSS = media.asCSS(className, indent);
29
29
 
30
30
  css += mediaCSS;
31
31
 
@@ -46,12 +46,11 @@ export default class RuleSet {
46
46
  asCSS(className, indent) {
47
47
  let css = EMPTY_STRING;
48
48
 
49
- const declarationsCSS = this.declarations.asCSS(` ${indent}`);
49
+ const declarationsCSS = this.declarations.asCSS(null, indent);
50
50
 
51
51
  if (declarationsCSS !== EMPTY_STRING) {
52
52
  css = `${indent}.${className}${this.selectors} {
53
- ${declarationsCSS}
54
- ${indent}}
53
+ ${declarationsCSS}${indent}}
55
54
 
56
55
  `;
57
56
  }
package/src/style.js CHANGED
@@ -4,6 +4,8 @@ import Medias from "./style/medias";
4
4
  import RuleSets from "./style/ruleSets";
5
5
  import Declarations from "./style/declarations";
6
6
 
7
+ import { EMPTY_STRING } from "./constants";
8
+
7
9
  export default class Style {
8
10
  constructor(declarations, ruleSets, medias) {
9
11
  this.declarations = declarations;
@@ -38,9 +40,10 @@ export default class Style {
38
40
  }
39
41
 
40
42
  asCSS(className) {
41
- const declarationsCSS = this.declarations.asCSS(className, " "),
42
- ruleSetsCSS = this.ruleSets.asCSS(className, ""),
43
- mediasCSS = this.medias.asCSS(className),
43
+ const indent = EMPTY_STRING,
44
+ declarationsCSS = this.declarations.asCSS(className, indent),
45
+ ruleSetsCSS = this.ruleSets.asCSS(className, indent),
46
+ mediasCSS = this.medias.asCSS(className, indent),
44
47
  css = `${declarationsCSS}${ruleSetsCSS}${mediasCSS}`;
45
48
 
46
49
  return css;
@@ -0,0 +1,9 @@
1
+ "use strict";
2
+
3
+ import { EMPTY_STRING } from "../constants";
4
+
5
+ export function trim(string) {
6
+ string = string.replace(/[\n\r]$/g, EMPTY_STRING);
7
+
8
+ return string;
9
+ }
@@ -18,9 +18,9 @@ const { styleMap } = globalThis;
18
18
  function renderStyle(style) {
19
19
  const headDOMElement = document.querySelector(HEAD),
20
20
  styleDOMElement = document.createElement(STYLE),
21
- innerHTML = `
22
-
23
- ${style}`;
21
+ innerHTML = `
22
+ ${style}
23
+ `;
24
24
 
25
25
  Object.assign(styleDOMElement, {
26
26
  innerHTML
@@ -41,11 +41,11 @@ function renderStyles() {
41
41
  function generateStyle(args, className, superStyle = null) {
42
42
  const strings = args.shift(), ///
43
43
  content = strings.reduce((content, string, index) => {
44
- const arg = args[index];
44
+ const arg = args[index] || null;
45
45
 
46
- content = (arg !== undefined) ?
47
- `${content}${string}${arg}` :
48
- `${content}${string}`;
46
+ content = (arg === null) ?
47
+ `${content}${string}` :
48
+ `${content}${string}${arg}`;
49
49
 
50
50
  return content;
51
51
  }, EMPTY_STRING),