1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
////
/// Validators are custom functions that will be called before a BEM entity is created.
/// They check if the current mixin usage is valid or not and thus they are a flexible way to
/// let you implement your own rules.
///
/// Validator functions receive the following information:
/// - BEM entity type
/// - Arguments passed to the mixin
/// - The generated selector
/// - The generated context, if any
///
/// Additionally, the context stack used by the BEM system can be examined.
///
/// @group BEM
///
/// @access public
////
@use './vars';
@use '../functions';
@use '../contexts';
///
/// A list of validator functions.
///
/// @type list
///
/// @access private
///
$validators: ();
///
/// Register one or multiple validator functions.
///
/// A validator function is a function that accepts 4 arguments:
/// 1. BEM entity type (string)
/// 2. Arguments passed to the mixin (map)
/// 3. The generated selector (selector)
/// 4. The generated context (list, may be null)
///
/// The function must return a list with two items:
/// 1. `true` if the mixin usage is valid, otherwise `false`,
/// 2. a string with a rejection reason (empty if the usage is valid).
///
/// @param {string} $func-name - First function name.
/// @param {string} $func-names - Other function names.
///
@mixin add($func-name, $func-names...) {
$noop: add($func-name, $func-names...);
}
///
/// Register one or multiple validator functions. Check the respective mixin documentation for more information.
///
/// @see {mixin} add
///
@function add($func-name, $func-names...) {
@each $fn-name in join($func-name, $func-names) {
$fn: get-function($fn-name);
$validators: map-merge($validators, ($fn-name: $fn));
}
@return null;
}
///
/// Unregister one or multiple validator functions.
///
/// @param {string} $func-name - First function name.
/// @param {string} $func-names - Other function names.
///
@mixin remove($func-name, $func-names...) {
$noop: remove($func-name, $func-names...);
}
///
/// Unregister one or multiple validator functions. Check the respective mixin documentation for more information.
///
/// @see {mixin} remove
///
@function remove($func-name, $func-names...) {
$validators: map-remove($validators, $func-name, $func-names...);
@return null;
}
///
/// @access private
///
@mixin validate($type, $args, $selector, $context) {
@each $id, $fn in $validators {
$result: call($fn, $type, $args, $selector, $context);
@if not nth($result, 1) {
@error 'A BEM validator rejected this mixin usage due to the following reason: #{nth($result, 2)}';
}
}
}
//
// ---------------------------------------------------------------------------------------------------------
// Built-in validators
// ---------------------------------------------------------------------------------------------------------
//
///
/// A validator that makes sure blocks are declared in the right order, determined by the
/// namespace used.
///
@function enforce-namespace-order($type, $args, $selector, $context) {
@if not global-variable-exists(namespace-order, vars) {
vars.$namespace-order: map-keys(vars.$namespaces);
}
@if not global-variable-exists(cur-namespace-index, vars) {
vars.$cur-namespace-index: 1;
}
@if $type == 'block' {
$block-type: map-get($args, 'type');
@if $block-type != null {
$ns-index: index(vars.$namespace-order, $block-type);
@if $ns-index != null {
@if $ns-index < vars.$cur-namespace-index {
@return false 'Namespace "#{$block-type}" comes before current namespace #{nth(vars.$namespace-order, vars.$cur-namespace-index)}';
}
vars.$cur-namespace-index: $ns-index;
}
}
}
@return true '';
}
///
/// A validator that makes all BEM entities immutable.
///
@function immutable-entities($type, $args, $selector, $context) {
@if not global-variable-exists(generated-selectors, vars) {
vars.$generated-selectors: ();
}
$block-name: null;
$block-type: null;
$block-id: null;
@if $type == 'block' {
$block-name: map-get($args, 'name');
$block-type: map-get($args, 'type');
} @else {
$block-context: contexts.get(vars.$context-id, 'block');
$block-name: map-get(nth($block-context, 2), 'name');
$block-type: map-get(nth($block-context, 2), 'type');
}
@if $block-type != null {
$block-id: $block-name + '_' + $block-type;
} @else {
$block-id: $block-name;
}
@if $type == 'block' {
@if map-has-key(vars.$generated-selectors, $block-id) {
@return false 'Entity "#{$type}" with arguments [ #{functions.map-print($args)} ] was already defined.';
}
vars.$generated-selectors: map-merge(vars.$generated-selectors, ($block-id: ()));
} @else {
$selectors: map-get(vars.$generated-selectors, $block-id);
@if index($selectors, $selector) {
@return false 'Entity "#{$type}" with arguments [ #{functions.map-print($args)} ] was already defined.';
}
$selectors: append($selectors, $selector);
vars.$generated-selectors: map-merge(vars.$generated-selectors, ($block-id: $selectors));
}
@return true '';
}
|