styles.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /**
  6. * @module engine/view/styles
  7. */
  8. import { get, has, isObject, merge, set, unset } from 'lodash-es';
  9. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  10. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  11. class StylesConverter {
  12. constructor() {
  13. /**
  14. * Holds shorthand properties normalizers.
  15. *
  16. * Shorthand properties must be normalized as they can be written in various ways.
  17. * Normalizer must return object describing given shorthand.
  18. *
  19. * Example:
  20. * The `border-color` style is a shorthand property for `border-top-color`, `border-right-color`, `border-bottom-color`
  21. * and `border-left-color`. Similarly there are shorthand for border width (`border-width`) and style (`border-style`).
  22. *
  23. * For `border-color` the given shorthand:
  24. *
  25. * border-color: #f00 #ba7;
  26. *
  27. * might be written as:
  28. *
  29. * border-color-top: #f00;
  30. * border-color-right: #ba7;
  31. * border-color-bottom: #f00;
  32. * border-color-left: #ba7;
  33. *
  34. * Normalizers produces coherent object representation for both shorthand and longhand forms:
  35. *
  36. * stylesConverter.on( 'normalize:border-color', ( evt, data ) => {
  37. * data.path = 'border.color';
  38. * data.value = {
  39. * top: '#f00',
  40. * right: '#ba7',
  41. * bottom: '#f00',
  42. * left: '#ba7'
  43. * }
  44. * } );
  45. *
  46. * @event normalize
  47. */
  48. this.extractors = new Map();
  49. /**
  50. * Holds style normalize object reducers.
  51. *
  52. * An style inliner takes normalized object of style property and outputs array of normalized property-value pairs that can
  53. * be later used to inline a style.
  54. *
  55. * Those work in opposite direction to {@link #normalizers} and always outputs style in the same way.
  56. *
  57. * If normalized style is represented as:
  58. *
  59. * const style = {
  60. * border: {
  61. * color: {
  62. * top: '#f00',
  63. * right: '#ba7',
  64. * bottom: '#f00',
  65. * left: '#ba7'
  66. * }
  67. * }
  68. * }
  69. *
  70. * The border reducer will output:
  71. *
  72. * const reduced = [
  73. * [ 'border-color', '#f00 #ba7' ]
  74. * ];
  75. *
  76. * which can be used to return the inline style string:
  77. *
  78. * style="border-color:#f00 #ba7;"
  79. *
  80. * @type {Map<String, Function>}
  81. */
  82. this.reducers = new Map();
  83. }
  84. /**
  85. * Returns reduced form of style property form normalized object.
  86. *
  87. * @private
  88. * @param {String} styleName
  89. * @param {Object|String} normalizedValue
  90. * @returns {Array.<Array.<String, String>>}
  91. */
  92. _getReduceForm( styleName, normalizedValue ) {
  93. const data = {
  94. value: normalizedValue
  95. };
  96. this.fire( 'reduce:' + styleName, data );
  97. return data.reduced || [ [ styleName, normalizedValue ] ];
  98. }
  99. getNormalized( name, styles ) {
  100. if ( !name ) {
  101. return merge( {}, styles );
  102. }
  103. if ( this.extractors.has( name ) ) {
  104. const extractor = this.extractors.get( name );
  105. if ( typeof extractor === 'string' ) {
  106. return this.getNormalized( extractor, styles );
  107. }
  108. return extractor( styles, this );
  109. }
  110. const path = toPath( name );
  111. if ( has( styles, path ) ) {
  112. return get( styles, path );
  113. } else {
  114. return styles[ name ];
  115. }
  116. }
  117. /**
  118. * Parse style property value to a normalized form.
  119. *
  120. * @param {String} propertyName Name of style property.
  121. * @param {String} value Value of style property.
  122. * @param {Object} styles
  123. * @private
  124. */
  125. _toNormalizedForm( propertyName, value, styles ) {
  126. if ( isObject( value ) ) {
  127. appendStyleValue( styles, toPath( propertyName ), value );
  128. return;
  129. }
  130. const data = {
  131. path: propertyName,
  132. value
  133. };
  134. this.fire( 'normalize:' + propertyName, data );
  135. appendStyleValue( styles, data.path, data.value );
  136. }
  137. }
  138. mix( StylesConverter, EmitterMixin );
  139. const stylesConverter = new StylesConverter();
  140. stylesConverter.on( 'normalize:border', normalizeBorder );
  141. // Border-position shorthands.
  142. stylesConverter.on( 'normalize:border-top', getBorderPositionNormalizer( 'top' ) );
  143. stylesConverter.on( 'normalize:border-right', getBorderPositionNormalizer( 'right' ) );
  144. stylesConverter.on( 'normalize:border-bottom', getBorderPositionNormalizer( 'bottom' ) );
  145. stylesConverter.on( 'normalize:border-left', getBorderPositionNormalizer( 'left' ) );
  146. // Border-property shorthands.
  147. stylesConverter.on( 'normalize:border-color', getBorderPropertyNormalizer( 'color' ) );
  148. stylesConverter.on( 'normalize:border-width', getBorderPropertyNormalizer( 'width' ) );
  149. stylesConverter.on( 'normalize:border-style', getBorderPropertyNormalizer( 'style' ) );
  150. // Border longhands.
  151. stylesConverter.on( 'normalize:border-top-color', getBorderPropertyPositionNormalizer( 'color', 'top' ) );
  152. stylesConverter.on( 'normalize:border-top-style', getBorderPropertyPositionNormalizer( 'style', 'top' ) );
  153. stylesConverter.on( 'normalize:border-top-width', getBorderPropertyPositionNormalizer( 'width', 'top' ) );
  154. stylesConverter.on( 'normalize:border-right-color', getBorderPropertyPositionNormalizer( 'color', 'right' ) );
  155. stylesConverter.on( 'normalize:border-right-style', getBorderPropertyPositionNormalizer( 'style', 'right' ) );
  156. stylesConverter.on( 'normalize:border-right-width', getBorderPropertyPositionNormalizer( 'width', 'right' ) );
  157. stylesConverter.on( 'normalize:border-bottom-color', getBorderPropertyPositionNormalizer( 'color', 'bottom' ) );
  158. stylesConverter.on( 'normalize:border-bottom-style', getBorderPropertyPositionNormalizer( 'style', 'bottom' ) );
  159. stylesConverter.on( 'normalize:border-bottom-width', getBorderPropertyPositionNormalizer( 'width', 'bottom' ) );
  160. stylesConverter.on( 'normalize:border-left-color', getBorderPropertyPositionNormalizer( 'color', 'left' ) );
  161. stylesConverter.on( 'normalize:border-left-style', getBorderPropertyPositionNormalizer( 'style', 'left' ) );
  162. stylesConverter.on( 'normalize:border-left-width', getBorderPropertyPositionNormalizer( 'width', 'left' ) );
  163. stylesConverter.on( 'normalize:margin', getPositionShorthandNormalizer( 'margin' ) );
  164. stylesConverter.on( 'normalize:margin-top', ( evt, data ) => ( data.path = 'margin.top' ) );
  165. stylesConverter.on( 'normalize:margin-right', ( evt, data ) => ( data.path = 'margin.right' ) );
  166. stylesConverter.on( 'normalize:margin-bottom', ( evt, data ) => ( data.path = 'margin.bottom' ) );
  167. stylesConverter.on( 'normalize:margin-left', ( evt, data ) => ( data.path = 'margin.left' ) );
  168. stylesConverter.on( 'normalize:padding', getPositionShorthandNormalizer( 'padding' ) );
  169. stylesConverter.on( 'normalize:padding-top', ( evt, data ) => ( data.path = 'padding.top' ) );
  170. stylesConverter.on( 'normalize:padding-right', ( evt, data ) => ( data.path = 'padding.right' ) );
  171. stylesConverter.on( 'normalize:padding-bottom', ( evt, data ) => ( data.path = 'padding.bottom' ) );
  172. stylesConverter.on( 'normalize:padding-left', ( evt, data ) => ( data.path = 'padding.left' ) );
  173. stylesConverter.on( 'normalize:background', normalizeBackground );
  174. stylesConverter.on( 'normalize:background-color', ( evt, data ) => ( data.path = 'background.color' ) );
  175. stylesConverter.extractors.set( 'border-top', borderPositionExtractor( 'top' ) );
  176. stylesConverter.extractors.set( 'border-right', borderPositionExtractor( 'right' ) );
  177. stylesConverter.extractors.set( 'border-bottom', borderPositionExtractor( 'bottom' ) );
  178. stylesConverter.extractors.set( 'border-left', borderPositionExtractor( 'left' ) );
  179. stylesConverter.extractors.set( 'border-top-color', 'border.color.top' );
  180. stylesConverter.extractors.set( 'border-right-color', 'border.color.right' );
  181. stylesConverter.extractors.set( 'border-bottom-color', 'border.color.bottom' );
  182. stylesConverter.extractors.set( 'border-left-color', 'border.color.left' );
  183. stylesConverter.extractors.set( 'border-top-width', 'border.width.top' );
  184. stylesConverter.extractors.set( 'border-right-width', 'border.width.right' );
  185. stylesConverter.extractors.set( 'border-bottom-width', 'border.width.bottom' );
  186. stylesConverter.extractors.set( 'border-left-width', 'border.width.left' );
  187. stylesConverter.extractors.set( 'border-top-style', 'border.style.top' );
  188. stylesConverter.extractors.set( 'border-right-style', 'border.style.right' );
  189. stylesConverter.extractors.set( 'border-bottom-style', 'border.style.bottom' );
  190. stylesConverter.extractors.set( 'border-left-style', 'border.style.left' );
  191. stylesConverter.on( 'reduce:border-color', getTopRightBottomLeftValueReducer( 'border-color' ) );
  192. stylesConverter.on( 'reduce:border-style', getTopRightBottomLeftValueReducer( 'border-style' ) );
  193. stylesConverter.on( 'reduce:border-width', getTopRightBottomLeftValueReducer( 'border-width' ) );
  194. stylesConverter.on( 'reduce:border-top', ( evt, data ) => ( data.reduced = getBorderPositionReducer( 'top' )( data.value ) ) );
  195. stylesConverter.on( 'reduce:border-right', ( evt, data ) => ( data.reduced = getBorderPositionReducer( 'right' )( data.value ) ) );
  196. stylesConverter.on( 'reduce:border-bottom', ( evt, data ) => ( data.reduced = getBorderPositionReducer( 'bottom' )( data.value ) ) );
  197. stylesConverter.on( 'reduce:border-left', ( evt, data ) => ( data.reduced = getBorderPositionReducer( 'left' )( data.value ) ) );
  198. stylesConverter.on( 'reduce:border', getBorderReducer );
  199. stylesConverter.on( 'reduce:margin', getTopRightBottomLeftValueReducer( 'margin' ) );
  200. stylesConverter.on( 'reduce:padding', getTopRightBottomLeftValueReducer( 'padding' ) );
  201. stylesConverter.on( 'reduce:background', ( evt, data ) => {
  202. const ret = [];
  203. ret.push( [ 'background-color', data.value.color ] );
  204. data.reduced = ret;
  205. } );
  206. /**
  207. * Styles class.
  208. *
  209. * Handles styles normalization.
  210. */
  211. export default class Styles {
  212. /**
  213. * Creates Styles instance.
  214. */
  215. constructor() {
  216. /**
  217. * @type {{}}
  218. * @private
  219. */
  220. this._styles = {};
  221. }
  222. /**
  223. * Number of styles defined.
  224. *
  225. * @type {Number}
  226. */
  227. get size() {
  228. return this.getStyleNames().length;
  229. }
  230. /**
  231. * Re-sets internal styles definition.
  232. *
  233. * @param {String} styleString
  234. */
  235. setStyle( styleString ) {
  236. this.clear();
  237. const map = parseInlineStyles( styleString );
  238. for ( const key of map.keys() ) {
  239. const value = map.get( key );
  240. stylesConverter._toNormalizedForm( key, value, this._styles );
  241. }
  242. }
  243. /**
  244. * Checks if single style rule is set.
  245. *
  246. * Supports shorthands.
  247. *
  248. * @param {String} name
  249. * @returns {Boolean}
  250. */
  251. hasProperty( name ) {
  252. const nameNorm = toPath( name );
  253. return has( this._styles, nameNorm ) || !!this._styles[ name ];
  254. }
  255. /**
  256. * Inserts single style property.
  257. *
  258. * Can insert one by one
  259. *
  260. * styles.insertProperty( 'color', 'blue' );
  261. * styles.insertProperty( 'margin-right', '1em' );
  262. *
  263. * or many styles at once:
  264. *
  265. * styles.insertProperty( {
  266. * color: 'blue',
  267. * 'margin-right': '1em'
  268. * } );
  269. *
  270. * Supports shorthands.
  271. *
  272. * @param {String|Object} nameOrObject
  273. * @param {String|Object} value
  274. * @returns {Boolean}
  275. */
  276. insertProperty( nameOrObject, value ) {
  277. if ( isObject( nameOrObject ) ) {
  278. for ( const key of Object.keys( nameOrObject ) ) {
  279. this.insertProperty( key, nameOrObject[ key ] );
  280. }
  281. } else {
  282. stylesConverter._toNormalizedForm( nameOrObject, value, this._styles );
  283. }
  284. }
  285. /**
  286. * Removes styles property.
  287. *
  288. * @param name
  289. */
  290. removeProperty( name ) {
  291. unset( this._styles, toPath( name ) );
  292. delete this._styles[ name ];
  293. }
  294. /**
  295. * Return normalized style object;
  296. *
  297. * const styles = new Styles();
  298. * styles.setStyle( 'margin:1px 2px 3em;' );
  299. *
  300. * console.log( styles.getNormalized( 'margin' ) );
  301. * // will log:
  302. * // {
  303. * // top: '1px',
  304. * // right: '2px',
  305. * // bottom: '3em',
  306. * // left: '2px'
  307. * // }
  308. *
  309. * @param {String} name
  310. * @returns {Object|undefined}
  311. */
  312. getNormalized( name ) {
  313. return stylesConverter.getNormalized( name, this._styles );
  314. }
  315. /**
  316. * Returns a string containing normalized styles string or undefined if no style properties are set.
  317. *
  318. * @returns {String|undefined}
  319. */
  320. getInlineStyle() {
  321. const entries = this._getStylesEntries();
  322. // Return undefined for empty styles map.
  323. if ( !entries.length ) {
  324. return;
  325. }
  326. return entries.map( arr => arr.join( ':' ) ).join( ';' ) + ';';
  327. }
  328. /**
  329. * Returns property value string.
  330. *
  331. * @param {String} propertyName
  332. * @returns {String|undefined}
  333. */
  334. getInlineProperty( propertyName ) {
  335. const normalized = stylesConverter.getNormalized( propertyName, this._styles );
  336. if ( !normalized ) {
  337. // Try return styles set directly - values that are not parsed.
  338. return this._styles[ propertyName ];
  339. }
  340. if ( isObject( normalized ) ) {
  341. const styles = stylesConverter._getReduceForm( propertyName, normalized );
  342. const propertyDescriptor = styles.find( ( [ property ] ) => property === propertyName );
  343. // Only return a value if it is set;
  344. if ( Array.isArray( propertyDescriptor ) ) {
  345. return propertyDescriptor[ 1 ];
  346. }
  347. } else {
  348. return normalized;
  349. }
  350. }
  351. /**
  352. * Returns style properties names as the would appear when using {@link #getInlineStyle()}
  353. *
  354. * @returns {Array.<String>}
  355. */
  356. getStyleNames() {
  357. const entries = this._getStylesEntries();
  358. return entries.map( ( [ key ] ) => key );
  359. }
  360. /**
  361. * Removes all styles.
  362. */
  363. clear() {
  364. this._styles = {};
  365. }
  366. /**
  367. * Returns normalized styles entries for further processing.
  368. *
  369. * @private
  370. * @returns {Array.<Array.<String, String>> ]}
  371. */
  372. _getStylesEntries() {
  373. const parsed = [];
  374. const keys = Object.keys( this._styles ).sort();
  375. for ( const key of keys ) {
  376. const normalized = stylesConverter.getNormalized( key, this._styles );
  377. parsed.push( ...stylesConverter._getReduceForm( key, normalized ) );
  378. }
  379. return parsed;
  380. }
  381. }
  382. function getTopRightBottomLeftValues( value = '' ) {
  383. if ( value === '' ) {
  384. return { top: undefined, right: undefined, bottom: undefined, left: undefined };
  385. }
  386. const values = value.split( ' ' );
  387. const top = values[ 0 ];
  388. const bottom = values[ 2 ] || top;
  389. const right = values[ 1 ] || top;
  390. const left = values[ 3 ] || right;
  391. return { top, bottom, right, left };
  392. }
  393. function toBorderPropertyShorthand( value, property ) {
  394. return {
  395. [ property ]: getTopRightBottomLeftValues( value )
  396. };
  397. }
  398. function getPositionShorthandNormalizer( longhand ) {
  399. return ( evt, data ) => {
  400. data.path = longhand;
  401. data.value = getTopRightBottomLeftValues( data.value );
  402. };
  403. }
  404. function normalizeBorder( evt, data ) {
  405. const { color, style, width } = normalizeBorderShorthand( data.value );
  406. data.path = 'border';
  407. data.value = {
  408. color: getTopRightBottomLeftValues( color ),
  409. style: getTopRightBottomLeftValues( style ),
  410. width: getTopRightBottomLeftValues( width )
  411. };
  412. }
  413. function getBorderPositionNormalizer( side ) {
  414. return ( evt, data ) => {
  415. const { color, style, width } = normalizeBorderShorthand( data.value );
  416. const border = {};
  417. if ( color !== undefined ) {
  418. border.color = { [ side ]: color };
  419. }
  420. if ( style !== undefined ) {
  421. border.style = { [ side ]: style };
  422. }
  423. if ( width !== undefined ) {
  424. border.width = { [ side ]: width };
  425. }
  426. data.path = 'border';
  427. data.value = border;
  428. };
  429. }
  430. function getBorderPropertyNormalizer( propertyName ) {
  431. return ( evt, data ) => {
  432. data.path = 'border';
  433. data.value = toBorderPropertyShorthand( data.value, propertyName );
  434. };
  435. }
  436. function getBorderPropertyPositionNormalizer( property, side ) {
  437. return ( evt, data ) => {
  438. data.path = 'border';
  439. data.value = {
  440. [ property ]: {
  441. [ side ]: data.value
  442. }
  443. };
  444. };
  445. }
  446. function borderPositionExtractor( which ) {
  447. return ( styles, converter ) => {
  448. const border = converter.getNormalized( 'border', styles );
  449. const value = [];
  450. if ( border.width && border.width[ which ] ) {
  451. value.push( border.width[ which ] );
  452. }
  453. if ( border.style && border.style[ which ] ) {
  454. value.push( border.style[ which ] );
  455. }
  456. if ( border.color && border.color[ which ] ) {
  457. value.push( border.color[ which ] );
  458. }
  459. return value.join( ' ' );
  460. };
  461. }
  462. function normalizeBorderShorthand( string ) {
  463. const result = {};
  464. for ( const part of string.split( ' ' ) ) {
  465. if ( isLength( part ) ) {
  466. result.width = part;
  467. }
  468. if ( isLineStyle( part ) ) {
  469. result.style = part;
  470. }
  471. if ( isColor( part ) ) {
  472. result.color = part;
  473. }
  474. }
  475. return result;
  476. }
  477. function normalizeBackground( evt, data ) {
  478. const background = {};
  479. const parts = data.value.split( ' ' );
  480. for ( const part of parts ) {
  481. if ( isRepeat( part ) ) {
  482. background.repeat = background.repeat || [];
  483. background.repeat.push( part );
  484. } else if ( isPosition( part ) ) {
  485. background.position = background.position || [];
  486. background.position.push( part );
  487. } else if ( isAttachment( part ) ) {
  488. background.attachment = part;
  489. } else if ( isColor( part ) ) {
  490. background.color = part;
  491. } else if ( isURL( part ) ) {
  492. background.image = part;
  493. }
  494. }
  495. data.path = 'background';
  496. data.value = background;
  497. }
  498. function isColor( string ) {
  499. return /^([#0-9A-Fa-f]{3,8}|[a-zA-Z]+)$/.test( string ) && !isLineStyle( string );
  500. }
  501. function isLineStyle( string ) {
  502. return /^(none|hidden|dotted|dashed|solid|double|groove|ridge|inset|outset)$/.test( string );
  503. }
  504. function isLength( string ) {
  505. return /^[+-]?[0-9]?[.]?[0-9]+([a-z]+|%)$/.test( string );
  506. }
  507. function isRepeat( string ) {
  508. return /^(repeat-x|repeat-y|repeat|space|round|no-repeat)$/.test( string );
  509. }
  510. function isPosition( string ) {
  511. return /^(center|top|bottom|left|right)$/.test( string );
  512. }
  513. function isAttachment( string ) {
  514. return /^(fixed|scroll|local)$/.test( string );
  515. }
  516. function isURL( string ) {
  517. return /^url\(/.test( string );
  518. }
  519. function getBorderReducer( evt, data ) {
  520. const ret = [];
  521. ret.push( ...getBorderPositionReducer( 'top' )( data.value ) );
  522. ret.push( ...getBorderPositionReducer( 'right' )( data.value ) );
  523. ret.push( ...getBorderPositionReducer( 'bottom' )( data.value ) );
  524. ret.push( ...getBorderPositionReducer( 'left' )( data.value ) );
  525. data.reduced = ret;
  526. }
  527. function getTopRightBottomLeftValueReducer( styleShorthand ) {
  528. return ( evt, data ) => {
  529. const { top, right, bottom, left } = ( data.value || {} );
  530. const reduced = [];
  531. if ( ![ top, right, left, bottom ].every( value => !!value ) ) {
  532. if ( top ) {
  533. reduced.push( [ styleShorthand + '-top', top ] );
  534. }
  535. if ( right ) {
  536. reduced.push( [ styleShorthand + '-right', right ] );
  537. }
  538. if ( bottom ) {
  539. reduced.push( [ styleShorthand + '-bottom', bottom ] );
  540. }
  541. if ( left ) {
  542. reduced.push( [ styleShorthand + '-left', left ] );
  543. }
  544. } else {
  545. reduced.push( [ styleShorthand, getTopRightBottomLeftShorthandValue( data.value ) ] );
  546. }
  547. data.reduced = reduced;
  548. };
  549. }
  550. function getBorderPositionReducer( which ) {
  551. return value => {
  552. const reduced = [];
  553. if ( value && value.width && value.width[ which ] !== undefined ) {
  554. reduced.push( value.width[ which ] );
  555. }
  556. if ( value && value.style && value.style[ which ] !== undefined ) {
  557. reduced.push( value.style[ which ] );
  558. }
  559. if ( value && value.color && value.color[ which ] !== undefined ) {
  560. reduced.push( value.color[ which ] );
  561. }
  562. if ( reduced.length ) {
  563. return [ [ 'border-' + which, reduced.join( ' ' ) ] ];
  564. }
  565. return [];
  566. };
  567. }
  568. function getTopRightBottomLeftShorthandValue( { left, right, top, bottom } ) {
  569. const out = [];
  570. if ( left !== right ) {
  571. out.push( top, right, bottom, left );
  572. } else if ( bottom !== top ) {
  573. out.push( top, right, bottom );
  574. } else if ( right !== top ) {
  575. out.push( top, right );
  576. } else {
  577. out.push( top );
  578. }
  579. return out.join( ' ' );
  580. }
  581. // Parses inline styles and puts property - value pairs into styles map.
  582. //
  583. // @param {String} stylesString Styles to parse.
  584. // @returns {Map.<String, String>} stylesMap Map of parsed properties and values.
  585. function parseInlineStyles( stylesString ) {
  586. // `null` if no quote was found in input string or last found quote was a closing quote. See below.
  587. let quoteType = null;
  588. let propertyNameStart = 0;
  589. let propertyValueStart = 0;
  590. let propertyName = null;
  591. const stylesMap = new Map();
  592. // Do not set anything if input string is empty.
  593. if ( stylesString === '' ) {
  594. return stylesMap;
  595. }
  596. // Fix inline styles that do not end with `;` so they are compatible with algorithm below.
  597. if ( stylesString.charAt( stylesString.length - 1 ) != ';' ) {
  598. stylesString = stylesString + ';';
  599. }
  600. // Seek the whole string for "special characters".
  601. for ( let i = 0; i < stylesString.length; i++ ) {
  602. const char = stylesString.charAt( i );
  603. if ( quoteType === null ) {
  604. // No quote found yet or last found quote was a closing quote.
  605. switch ( char ) {
  606. case ':':
  607. // Most of time colon means that property name just ended.
  608. // Sometimes however `:` is found inside property value (for example in background image url).
  609. if ( !propertyName ) {
  610. // Treat this as end of property only if property name is not already saved.
  611. // Save property name.
  612. propertyName = stylesString.substr( propertyNameStart, i - propertyNameStart );
  613. // Save this point as the start of property value.
  614. propertyValueStart = i + 1;
  615. }
  616. break;
  617. case '"':
  618. case '\'':
  619. // Opening quote found (this is an opening quote, because `quoteType` is `null`).
  620. quoteType = char;
  621. break;
  622. case ';': {
  623. // Property value just ended.
  624. // Use previously stored property value start to obtain property value.
  625. const propertyValue = stylesString.substr( propertyValueStart, i - propertyValueStart );
  626. if ( propertyName ) {
  627. // Save parsed part.
  628. stylesMap.set( propertyName.trim(), propertyValue.trim() );
  629. }
  630. propertyName = null;
  631. // Save this point as property name start. Property name starts immediately after previous property value ends.
  632. propertyNameStart = i + 1;
  633. break;
  634. }
  635. }
  636. } else if ( char === quoteType ) {
  637. // If a quote char is found and it is a closing quote, mark this fact by `null`-ing `quoteType`.
  638. quoteType = null;
  639. }
  640. }
  641. return stylesMap;
  642. }
  643. function toPath( name ) {
  644. return name.replace( '-', '.' );
  645. }
  646. // Appends style definition to the styles object.
  647. //
  648. // @param {String} nameOrPath
  649. // @param {String|Object} valueOrObject
  650. // @private
  651. function appendStyleValue( stylesObject, nameOrPath, valueOrObject ) {
  652. let valueToSet = valueOrObject;
  653. if ( isObject( valueOrObject ) ) {
  654. valueToSet = merge( {}, get( stylesObject, nameOrPath ), valueOrObject );
  655. }
  656. set( stylesObject, nameOrPath, valueToSet );
  657. }