StylesMap
Styles map. Allows handling (adding, removing, retrieving) a set of style rules (usually, of an element).
Properties
-
isEmpty : booleanreadonlymodule:engine/view/stylesmap~StylesMap#isEmptyReturns true if style map has no styles set.
-
size : numberreadonlymodule:engine/view/stylesmap~StylesMap#sizeNumber of styles defined.
-
_cachedExpandedStyleNames : null | Array<string>privatemodule:engine/view/stylesmap~StylesMap#_cachedExpandedStyleNamesCached list of expanded style names for faster access.
-
_cachedStyleNames : null | Array<string>privatemodule:engine/view/stylesmap~StylesMap#_cachedStyleNamesCached list of style names for faster access.
-
_styleProcessor : StylesProcessorprivatereadonlymodule:engine/view/stylesmap~StylesMap#_styleProcessorAn instance of the
StylesProcessor. -
module:engine/view/stylesmap~StylesMap#_stylesKeeps an internal representation of styles map. Normalized styles are kept as object tree to allow unified modification and value access model using lodash's get, set, unset, etc methods.
When no style processor rules are defined it acts as simple key-value storage.
Methods
-
constructor( styleProcessor )module:engine/view/stylesmap~StylesMap#constructor -
clear() → voidmodule:engine/view/stylesmap~StylesMap#clear -
getAsString( propertyName ) → undefined | stringmodule:engine/view/stylesmap~StylesMap#getAsStringReturns property as a value string or undefined if property is not set.
// Enable 'margin' shorthand processing: editor.data.addStyleProcessorRules( addMarginStylesRules ); const styles = new Styles(); styles.setTo( 'margin:1px;' ); styles.set( 'margin-bottom', '3em' ); styles.getAsString( 'margin' ); // -> 'margin: 1px 1px 3em;'Copy codeNote, however, that all sub-values must be set for the longhand property name to return a value:
const styles = new Styles(); styles.setTo( 'margin:1px;' ); styles.remove( 'margin-bottom' ); styles.getAsString( 'margin' ); // -> undefinedCopy codeIn the above scenario, it is not possible to return a
marginvalue, soundefinedis returned. Instead, you should use:const styles = new Styles(); styles.setTo( 'margin:1px;' ); styles.remove( 'margin-bottom' ); for ( const styleName of styles.getStyleNames() ) { console.log( styleName, styles.getAsString( styleName ) ); } // 'margin-top', '1px' // 'margin-right', '1px' // 'margin-left', '1px'Copy codeIn general, it is recommend to iterate over style names like in the example above. This way, you will always get all the currently set style values. So, if all the 4 margin values would be set the for-of loop above would yield only
'margin','1px':const styles = new Styles(); styles.setTo( 'margin:1px;' ); for ( const styleName of styles.getStyleNames() ) { console.log( styleName, styles.getAsString( styleName ) ); } // 'margin', '1px'Copy codeNote: To get a normalized version of a longhand property use the
#getNormalized()method.Parameters
propertyName : string
Returns
undefined | string
-
getNormalized( [ name ] ) → undefined | StyleValuemodule:engine/view/stylesmap~StylesMap#getNormalizedReturns a normalized style object or a single value.
// Enable 'margin' shorthand processing: editor.data.addStyleProcessorRules( addMarginStylesRules ); const styles = new Styles(); styles.setTo( 'margin:1px 2px 3em;' ); styles.getNormalized( 'margin' ); // will log: // { // top: '1px', // right: '2px', // bottom: '3em', // left: '2px' // normalized value from margin shorthand // } styles.getNormalized( 'margin-left' ); // -> '2px'Copy codeNote: This method will only return normalized styles if a style processor was defined.
Parameters
[ name ] : stringStyle name.
Returns
undefined | StyleValue
-
getStyleNames( expand ) → Array<string>module:engine/view/stylesmap~StylesMap#getStyleNamesReturns all style properties names as they would appear when using
#toString().When
expandis set to true and there's a shorthand style property set, it will also return all equivalent styles:stylesMap.setTo( 'margin: 1em' )Copy codewill be expanded to:
[ 'margin', 'margin-top', 'margin-right', 'margin-bottom', 'margin-left' ]Copy codeParameters
expand : booleanExpand shorthand style properties and all return equivalent style representations.
Defaults to
false
Returns
Array<string>
-
getStylesEntries() → Array<StylePropertyDescriptor>module:engine/view/stylesmap~StylesMap#getStylesEntries -
has( name ) → booleanmodule:engine/view/stylesmap~StylesMap#hasChecks if a given style is set.
styles.setTo( 'margin-left:1px;' ); styles.has( 'margin-left' ); // -> true styles.has( 'padding' ); // -> falseCopy codeNote: This check supports normalized style names.
// Enable 'margin' shorthand processing: editor.data.addStyleProcessorRules( addMarginStylesRules ); styles.setTo( 'margin:2px;' ); styles.has( 'margin' ); // -> true styles.has( 'margin-top' ); // -> true styles.has( 'margin-left' ); // -> true styles.remove( 'margin-top' ); styles.has( 'margin' ); // -> false styles.has( 'margin-top' ); // -> false styles.has( 'margin-left' ); // -> trueCopy codeParameters
name : stringStyle name.
Returns
boolean
-
isSimilar( other ) → booleanmodule:engine/view/stylesmap~StylesMap#isSimilar -
keys() → Array<string>module:engine/view/stylesmap~StylesMap#keys -
remove( names ) → voidmodule:engine/view/stylesmap~StylesMap#removeRemoves given style.
styles.setTo( 'background:#f00;margin-right:2px;' ); styles.remove( 'background' ); styles.toString(); // -> 'margin-right:2px;'Copy codeNote: This method uses enabled style processor rules to normalize passed values.
// Enable 'margin' shorthand processing: editor.data.addStyleProcessorRules( addMarginStylesRules ); styles.setTo( 'margin:1px' ); styles.remove( 'margin-top' ); styles.remove( 'margin-right' ); styles.toString(); // -> 'margin-bottom:1px;margin-left:1px;'Copy codeParameters
names : ArrayOrItem<string>Style name or an array of names.
Returns
void
-
set( styles ) → voidmodule:engine/view/stylesmap~StylesMap#set:CONFIG_OBJECT -
set( name, value ) → voidmodule:engine/view/stylesmap~StylesMap#set:KEY_VALUESets a given style.
Can insert one by one:
styles.set( 'color', 'blue' ); styles.set( 'margin-right', '1em' );Copy codeNote: This method uses enabled style processor rules to normalize passed values.
// Enable 'margin' shorthand processing: editor.data.addStyleProcessorRules( addMarginStylesRules ); styles.set( 'margin', '2px' );Copy codeThe above code will set margin to:
styles.getNormalized( 'margin' ); // -> { top: '2px', right: '2px', bottom: '2px', left: '2px' }Copy codeWhich makes it possible to retrieve a "sub-value":
styles.get( 'margin-left' ); // -> '2px'Copy codeOr modify it:
styles.remove( 'margin-left' ); styles.getNormalized( 'margin' ); // -> { top: '1px', bottom: '1px', right: '1px' } styles.toString(); // -> 'margin-bottom:1px;margin-right:1px;margin-top:1px;'Copy codeThis method also allows to set normalized values directly (if a particular styles processor rule was enabled):
styles.set( 'border-color', { top: 'blue' } ); styles.set( 'margin', { right: '2em' } ); styles.toString(); // -> 'border-color-top:blue;margin-right:2em;'Copy codeParameters
name : stringStyle property name.
value : StyleValueValue to set.
Returns
void
-
setTo( inlineStyle ) → thismodule:engine/view/stylesmap~StylesMap#setToSet styles map to a new value.
styles.setTo( 'border:1px solid blue;margin-top:1px;' );Copy codeParameters
inlineStyle : string
Returns
this
-
toString() → stringmodule:engine/view/stylesmap~StylesMap#toStringReturns a normalized style string. Styles are sorted by name.
styles.set( 'margin' , '1px' ); styles.set( 'background', '#f00' ); styles.toString(); // -> 'background:#f00;margin:1px;'Copy codeNote: This method supports normalized styles if defined.
// Enable 'margin' shorthand processing: editor.data.addStyleProcessorRules( addMarginStylesRules ); styles.set( 'margin' , '1px' ); styles.set( 'background', '#f00' ); styles.remove( 'margin-top' ); styles.remove( 'margin-right' ); styles.toString(); // -> 'background:#f00;margin-bottom:1px;margin-left:1px;'Copy codeReturns
string
-
_canMergeFrom( other ) → booleaninternalmodule:engine/view/stylesmap~StylesMap#_canMergeFromUsed by
_canMergeAttributesFromto verify if the given attribute can be merged without conflicts into the attribute.This method is indirectly used by the
ViewDowncastWriterwhile down-casting anViewAttributeElementto merge it with other ViewAttributeElement.Parameters
other : StylesMap
Returns
boolean
-
_clone() → thisinternalmodule:engine/view/stylesmap~StylesMap#_clone -
_getConsumables( [ name ] ) → Array<string>internalmodule:engine/view/stylesmap~StylesMap#_getConsumablesReturns a list of consumables for the attribute. This includes related styles.
Could be filtered by the given style name.
Parameters
[ name ] : string
Returns
Array<string>
-
_getTokensMatch( tokenPattern, valuePattern ) → undefined | Array<string>internalmodule:engine/view/stylesmap~StylesMap#_getTokensMatchUsed by the Matcher to collect matching styles.
Parameters
tokenPattern : string | true | RegExpThe matched style name pattern.
valuePattern : string | true | RegExpThe matched style value pattern.
Returns
undefined | Array<string>An array of matching tokens (style names).
-
_isMatching( other ) → booleaninternalmodule:engine/view/stylesmap~StylesMap#_isMatchingUsed by
_canSubtractAttributesOfto verify if the given attribute can be fully subtracted from the attribute.This method is indirectly used by the
ViewDowncastWriterwhile down-casting anViewAttributeElementto unwrap the ViewAttributeElement.Parameters
other : StylesMap
Returns
boolean
-
_mergeFrom( other ) → voidinternalmodule:engine/view/stylesmap~StylesMap#_mergeFromUsed by
_mergeAttributesFromto merge a given attribute into the attribute.This method is indirectly used by the
ViewDowncastWriterwhile down-casting anViewAttributeElementto merge it with other ViewAttributeElement.Parameters
other : StylesMap
Returns
void