-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathborder.js
362 lines (305 loc) · 12.6 KB
/
border.js
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
* @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module engine/view/styles/border
*/
import { getShorthandValues, getBoxSidesValueReducer, getBoxSidesValues, isLength, isLineStyle } from './utils';
/**
* Adds a border CSS styles processing rules.
*
* editor.data.addStyleProcessorRules( addBorderRules );
*
* This rules merges all [border](https://developer.mozilla.org/en-US/docs/Web/CSS/border) styles notation shorthands:
*
* - border
* - border-top
* - border-right
* - border-bottom
* - border-left
* - border-color
* - border-style
* - border-width
*
* and all corresponding longhand forms (like `border-top-color`, `border-top-style`, etc).
*
* It does not handle other shorthands (like `border-radius` or `border-image`).
*
* The normalized model stores border values as:
*
* const styles = {
* border: {
* color: { top, right, bottom, left },
* style: { top, right, bottom, left },
* width: { top, right, bottom, left },
* }
* };
*
* @param {module:engine/view/stylesmap~StylesProcessor} stylesProcessor
*/
export function addBorderRules( stylesProcessor ) {
stylesProcessor.setNormalizer( 'border', borderNormalizer );
// Border-position shorthands.
stylesProcessor.setNormalizer( 'border-top', getBorderPositionNormalizer( 'top' ) );
stylesProcessor.setNormalizer( 'border-right', getBorderPositionNormalizer( 'right' ) );
stylesProcessor.setNormalizer( 'border-bottom', getBorderPositionNormalizer( 'bottom' ) );
stylesProcessor.setNormalizer( 'border-left', getBorderPositionNormalizer( 'left' ) );
// Border-property shorthands.
stylesProcessor.setNormalizer( 'border-color', getBorderPropertyNormalizer( 'color' ) );
stylesProcessor.setNormalizer( 'border-width', getBorderPropertyNormalizer( 'width' ) );
stylesProcessor.setNormalizer( 'border-style', getBorderPropertyNormalizer( 'style' ) );
// Border longhands.
stylesProcessor.setNormalizer( 'border-top-color', getBorderPropertyPositionNormalizer( 'color', 'top' ) );
stylesProcessor.setNormalizer( 'border-top-style', getBorderPropertyPositionNormalizer( 'style', 'top' ) );
stylesProcessor.setNormalizer( 'border-top-width', getBorderPropertyPositionNormalizer( 'width', 'top' ) );
stylesProcessor.setNormalizer( 'border-right-color', getBorderPropertyPositionNormalizer( 'color', 'right' ) );
stylesProcessor.setNormalizer( 'border-right-style', getBorderPropertyPositionNormalizer( 'style', 'right' ) );
stylesProcessor.setNormalizer( 'border-right-width', getBorderPropertyPositionNormalizer( 'width', 'right' ) );
stylesProcessor.setNormalizer( 'border-bottom-color', getBorderPropertyPositionNormalizer( 'color', 'bottom' ) );
stylesProcessor.setNormalizer( 'border-bottom-style', getBorderPropertyPositionNormalizer( 'style', 'bottom' ) );
stylesProcessor.setNormalizer( 'border-bottom-width', getBorderPropertyPositionNormalizer( 'width', 'bottom' ) );
stylesProcessor.setNormalizer( 'border-left-color', getBorderPropertyPositionNormalizer( 'color', 'left' ) );
stylesProcessor.setNormalizer( 'border-left-style', getBorderPropertyPositionNormalizer( 'style', 'left' ) );
stylesProcessor.setNormalizer( 'border-left-width', getBorderPropertyPositionNormalizer( 'width', 'left' ) );
stylesProcessor.setExtractor( 'border-top', getBorderPositionExtractor( 'top' ) );
stylesProcessor.setExtractor( 'border-right', getBorderPositionExtractor( 'right' ) );
stylesProcessor.setExtractor( 'border-bottom', getBorderPositionExtractor( 'bottom' ) );
stylesProcessor.setExtractor( 'border-left', getBorderPositionExtractor( 'left' ) );
stylesProcessor.setExtractor( 'border-top-color', 'border.color.top' );
stylesProcessor.setExtractor( 'border-right-color', 'border.color.right' );
stylesProcessor.setExtractor( 'border-bottom-color', 'border.color.bottom' );
stylesProcessor.setExtractor( 'border-left-color', 'border.color.left' );
stylesProcessor.setExtractor( 'border-top-width', 'border.width.top' );
stylesProcessor.setExtractor( 'border-right-width', 'border.width.right' );
stylesProcessor.setExtractor( 'border-bottom-width', 'border.width.bottom' );
stylesProcessor.setExtractor( 'border-left-width', 'border.width.left' );
stylesProcessor.setExtractor( 'border-top-style', 'border.style.top' );
stylesProcessor.setExtractor( 'border-right-style', 'border.style.right' );
stylesProcessor.setExtractor( 'border-bottom-style', 'border.style.bottom' );
stylesProcessor.setExtractor( 'border-left-style', 'border.style.left' );
stylesProcessor.setReducer( 'border-color', getBoxSidesValueReducer( 'border-color' ) );
stylesProcessor.setReducer( 'border-style', getBoxSidesValueReducer( 'border-style' ) );
stylesProcessor.setReducer( 'border-width', getBoxSidesValueReducer( 'border-width' ) );
stylesProcessor.setReducer( 'border-top', getBorderPositionReducer( 'top' ) );
stylesProcessor.setReducer( 'border-right', getBorderPositionReducer( 'right' ) );
stylesProcessor.setReducer( 'border-bottom', getBorderPositionReducer( 'bottom' ) );
stylesProcessor.setReducer( 'border-left', getBorderPositionReducer( 'left' ) );
stylesProcessor.setReducer( 'border', getBorderReducer() );
stylesProcessor.setStyleRelation( 'border', [
'border-color', 'border-style', 'border-width',
'border-top', 'border-right', 'border-bottom', 'border-left',
'border-top-color', 'border-right-color', 'border-bottom-color', 'border-left-color',
'border-top-style', 'border-right-style', 'border-bottom-style', 'border-left-style',
'border-top-width', 'border-right-width', 'border-bottom-width', 'border-left-width'
] );
stylesProcessor.setStyleRelation( 'border-color', [
'border-top-color', 'border-right-color', 'border-bottom-color', 'border-left-color'
] );
stylesProcessor.setStyleRelation( 'border-style', [
'border-top-style', 'border-right-style', 'border-bottom-style', 'border-left-style'
] );
stylesProcessor.setStyleRelation( 'border-width', [
'border-top-width', 'border-right-width', 'border-bottom-width', 'border-left-width'
] );
stylesProcessor.setStyleRelation( 'border-top', [ 'border-top-color', 'border-top-style', 'border-top-width' ] );
stylesProcessor.setStyleRelation( 'border-right', [ 'border-right-color', 'border-right-style', 'border-right-width' ] );
stylesProcessor.setStyleRelation( 'border-bottom', [ 'border-bottom-color', 'border-bottom-style', 'border-bottom-width' ] );
stylesProcessor.setStyleRelation( 'border-left', [ 'border-left-color', 'border-left-style', 'border-left-width' ] );
}
function borderNormalizer( value ) {
const { color, style, width } = normalizeBorderShorthand( value );
return {
path: 'border',
value: {
color: getBoxSidesValues( color ),
style: getBoxSidesValues( style ),
width: getBoxSidesValues( width )
}
};
}
function getBorderPositionNormalizer( side ) {
return value => {
const { color, style, width } = normalizeBorderShorthand( value );
const border = {};
if ( color !== undefined ) {
border.color = { [ side ]: color };
}
if ( style !== undefined ) {
border.style = { [ side ]: style };
}
if ( width !== undefined ) {
border.width = { [ side ]: width };
}
return {
path: 'border',
value: border
};
};
}
function getBorderPropertyNormalizer( propertyName ) {
return value => {
return {
path: 'border',
value: toBorderPropertyShorthand( value, propertyName )
};
};
}
function toBorderPropertyShorthand( value, property ) {
return {
[ property ]: getBoxSidesValues( value )
};
}
function getBorderPropertyPositionNormalizer( property, side ) {
return value => {
return {
path: 'border',
value: {
[ property ]: {
[ side ]: value
}
}
};
};
}
function getBorderPositionExtractor( which ) {
return ( name, styles ) => {
if ( styles.border ) {
return extractBorderPosition( styles.border, which );
}
};
}
function extractBorderPosition( border, which ) {
const value = {};
if ( border.width && border.width[ which ] ) {
value.width = border.width[ which ];
}
if ( border.style && border.style[ which ] ) {
value.style = border.style[ which ];
}
if ( border.color && border.color[ which ] ) {
value.color = border.color[ which ];
}
return value;
}
function normalizeBorderShorthand( string ) {
const result = {};
const parts = getShorthandValues( string );
for ( const part of parts ) {
if ( isLength( part ) || /thin|medium|thick/.test( part ) ) {
result.width = part;
} else if ( isLineStyle( part ) ) {
result.style = part;
} else {
result.color = part;
}
}
return result;
}
// The border reducer factory.
//
// It tries to produce the most optimal output for the specified styles.
//
// For a border style:
//
// style: {top: "solid", bottom: "solid", right: "solid", left: "solid"}
//
// It will produce: `border-style: solid`.
// For a border style and color:
//
// color: {top: "#ff0", bottom: "#ff0", right: "#ff0", left: "#ff0"}
// style: {top: "solid", bottom: "solid", right: "solid", left: "solid"}
//
// It will produce: `border-color: #ff0; border-style: solid`.
// If all border parameters are specified:
//
// color: {top: "#ff0", bottom: "#ff0", right: "#ff0", left: "#ff0"}
// style: {top: "solid", bottom: "solid", right: "solid", left: "solid"}
// width: {top: "2px", bottom: "2px", right: "2px", left: "2px"}
//
// It will combine everything into a single property: `border: 2px solid #ff0`.
//
// The definitions are merged only if all border selectors have the same values.
//
// @returns {Function}
function getBorderReducer() {
return value => {
const topStyles = extractBorderPosition( value, 'top' );
const rightStyles = extractBorderPosition( value, 'right' );
const bottomStyles = extractBorderPosition( value, 'bottom' );
const leftStyles = extractBorderPosition( value, 'left' );
const borderStyles = [ topStyles, rightStyles, bottomStyles, leftStyles ];
const borderStylesByType = {
width: getReducedStyleValueForType( borderStyles, 'width' ),
style: getReducedStyleValueForType( borderStyles, 'style' ),
color: getReducedStyleValueForType( borderStyles, 'color' )
};
// Try reducing to a single `border:` property.
const reducedBorderStyle = reduceBorderPosition( borderStylesByType, 'all' );
if ( reducedBorderStyle.length ) {
return reducedBorderStyle;
}
// Try reducing to `border-style:`, `border-width:`, `border-color:` properties.
const reducedStyleTypes = Object.entries( borderStylesByType ).reduce( ( reducedStyleTypes, [ type, value ] ) => {
if ( value ) {
reducedStyleTypes.push( [ `border-${ type }`, value ] );
// Remove it from the full set to not include it in the most specific properties later.
borderStyles.forEach( style => ( style[ type ] = null ) );
}
return reducedStyleTypes;
}, [] );
// The reduced properties (by type) and all that remains that could not be reduced.
return [
...reducedStyleTypes,
...reduceBorderPosition( topStyles, 'top' ),
...reduceBorderPosition( rightStyles, 'right' ),
...reduceBorderPosition( bottomStyles, 'bottom' ),
...reduceBorderPosition( leftStyles, 'left' )
];
};
// @param {Array.<Object>} styles The array of objects with `style`, `color`, `width` properties.
// @param {'width'|'style'|'color'} type
function getReducedStyleValueForType( styles, type ) {
return styles
.map( style => style[ type ] )
.reduce( ( result, style ) => result == style ? result : null );
}
}
function getBorderPositionReducer( which ) {
return value => reduceBorderPosition( value, which );
}
// Returns an array with reduced border styles depending on the specified values.
//
// If all border properties (width, style, color) are specified, the returned selector will be
// merged into a group: `border-*: [width] [style] [color]`.
//
// Otherwise, the specific definitions will be returned: `border-(width|style|color)-*: [value]`.
//
// @param {Object|null} value Styles if defined.
// @param {'top'|'right'|'bottom'|'left'|'all'} which The border position.
// @returns {Array}
function reduceBorderPosition( value, which ) {
const borderTypes = [];
if ( value && value.width ) {
borderTypes.push( 'width' );
}
if ( value && value.style ) {
borderTypes.push( 'style' );
}
if ( value && value.color ) {
borderTypes.push( 'color' );
}
if ( borderTypes.length == 3 ) {
const borderValue = borderTypes.map( item => value[ item ] ).join( ' ' );
return [
which == 'all' ? [ 'border', borderValue ] : [ `border-${ which }`, borderValue ]
];
}
// We are unable to reduce to a single `border:` property.
if ( which == 'all' ) {
return [];
}
return borderTypes.map( type => {
return [ `border-${ which }-${ type }`, value[ type ] ];
} );
}