element.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/view/element
  7. */
  8. import Node from './node';
  9. import Text from './text';
  10. import TextProxy from './textproxy';
  11. import objectToMap from '@ckeditor/ckeditor5-utils/src/objecttomap';
  12. import isIterable from '@ckeditor/ckeditor5-utils/src/isiterable';
  13. import isPlainObject from '@ckeditor/ckeditor5-utils/src/lib/lodash/isPlainObject';
  14. import Matcher from './matcher';
  15. /**
  16. * View element.
  17. *
  18. * Editing engine does not define fixed HTML DTD. This is why the type of the {@link module:engine/view/element~Element} need to
  19. * be defined by the feature developer. Creating an element you should use {@link module:engine/view/containerelement~ContainerElement}
  20. * class, {@link module:engine/view/attributeelement~AttributeElement} class or {@link module:engine/view/emptyelement~EmptyElement} class.
  21. *
  22. * Note that for view elements which are not created from model, like elements from mutations, paste or
  23. * {@link module:engine/controller/datacontroller~DataController#set data.set} it is not possible to define the type of the element, so
  24. * these will be instances of the {@link module:engine/view/element~Element}.
  25. *
  26. * @extends module:engine/view/node~Node
  27. */
  28. export default class Element extends Node {
  29. /**
  30. * Creates a view element.
  31. *
  32. * Attributes can be passed in various formats:
  33. *
  34. * new Element( 'div', { 'class': 'editor', 'contentEditable': 'true' } ); // object
  35. * new Element( 'div', [ [ 'class', 'editor' ], [ 'contentEditable', 'true' ] ] ); // map-like iterator
  36. * new Element( 'div', mapOfAttributes ); // map
  37. *
  38. * **Note:** Constructor of this class shouldn't be used directly in the code. Use the
  39. * {@link module:engine/view/writer~Writer#createAttributeElement} for inline element,
  40. * {@link module:engine/view/writer~Writer#createContainerElement} for block element,
  41. * {@link module:engine/view/writer~Writer#createEditableElement} for editable element,
  42. * {@link module:engine/view/writer~Writer#createEmptyElement} for empty element or
  43. * {@link module:engine/view/writer~Writer#createUIElement} for UI element instead.
  44. *
  45. * @protected
  46. * @param {String} name Node name.
  47. * @param {Object|Iterable} [attrs] Collection of attributes.
  48. * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children]
  49. * List of nodes to be inserted into created element.
  50. */
  51. constructor( name, attrs, children ) {
  52. super();
  53. /**
  54. * Name of the element.
  55. *
  56. * @readonly
  57. * @member {String}
  58. */
  59. this.name = name;
  60. /**
  61. * Map of attributes, where attributes names are keys and attributes values are values.
  62. *
  63. * @protected
  64. * @member {Map} #_attrs
  65. */
  66. this._attrs = parseAttributes( attrs );
  67. /**
  68. * Array of child nodes.
  69. *
  70. * @protected
  71. * @member {Array.<module:engine/view/node~Node>}
  72. */
  73. this._children = [];
  74. if ( children ) {
  75. this._insertChildren( 0, children );
  76. }
  77. /**
  78. * Set of classes associated with element instance.
  79. *
  80. * @protected
  81. * @member {Set}
  82. */
  83. this._classes = new Set();
  84. if ( this._attrs.has( 'class' ) ) {
  85. // Remove class attribute and handle it by class set.
  86. const classString = this._attrs.get( 'class' );
  87. parseClasses( this._classes, classString );
  88. this._attrs.delete( 'class' );
  89. }
  90. /**
  91. * Map of styles.
  92. *
  93. * @protected
  94. * @member {Set} module:engine/view/element~Element#_styles
  95. */
  96. this._styles = new Map();
  97. if ( this._attrs.has( 'style' ) ) {
  98. // Remove style attribute and handle it by styles map.
  99. parseInlineStyles( this._styles, this._attrs.get( 'style' ) );
  100. this._attrs.delete( 'style' );
  101. }
  102. /**
  103. * Map of custom properties.
  104. * Custom properties can be added to element instance, will be cloned but not rendered into DOM.
  105. *
  106. * @protected
  107. * @memeber {Map}
  108. */
  109. this._customProperties = new Map();
  110. }
  111. /**
  112. * Number of element's children.
  113. *
  114. * @readonly
  115. * @type {Number}
  116. */
  117. get childCount() {
  118. return this._children.length;
  119. }
  120. /**
  121. * Is `true` if there are no nodes inside this element, `false` otherwise.
  122. *
  123. * @readonly
  124. * @type {Boolean}
  125. */
  126. get isEmpty() {
  127. return this._children.length === 0;
  128. }
  129. /**
  130. * Checks whether given view tree object is of given type.
  131. *
  132. * Read more in {@link module:engine/view/node~Node#is}.
  133. *
  134. * @param {String} type
  135. * @param {String} [name] Element name.
  136. * @returns {Boolean}
  137. */
  138. is( type, name = null ) {
  139. if ( !name ) {
  140. return type == 'element' || type == this.name;
  141. } else {
  142. return type == 'element' && name == this.name;
  143. }
  144. }
  145. /**
  146. * Clones provided element.
  147. *
  148. * @protected
  149. * @param {Boolean} [deep=false] If set to `true` clones element and all its children recursively. When set to `false`,
  150. * element will be cloned without any children.
  151. * @returns {module:engine/view/element~Element} Clone of this element.
  152. */
  153. _clone( deep = false ) {
  154. const childrenClone = [];
  155. if ( deep ) {
  156. for ( const child of this.getChildren() ) {
  157. childrenClone.push( child._clone( deep ) );
  158. }
  159. }
  160. // ContainerElement and AttributeElement should be also cloned properly.
  161. const cloned = new this.constructor( this.name, this._attrs, childrenClone );
  162. // Classes and styles are cloned separately - this solution is faster than adding them back to attributes and
  163. // parse once again in constructor.
  164. cloned._classes = new Set( this._classes );
  165. cloned._styles = new Map( this._styles );
  166. // Clone custom properties.
  167. cloned._customProperties = new Map( this._customProperties );
  168. // Clone filler offset method.
  169. // We can't define this method in a prototype because it's behavior which
  170. // is changed by e.g. toWidget() function from ckeditor5-widget. Perhaps this should be one of custom props.
  171. cloned.getFillerOffset = this.getFillerOffset;
  172. return cloned;
  173. }
  174. /**
  175. * Gets child at the given index.
  176. *
  177. * @param {Number} index Index of child.
  178. * @returns {module:engine/view/node~Node} Child node.
  179. */
  180. getChild( index ) {
  181. return this._children[ index ];
  182. }
  183. /**
  184. * Gets index of the given child node. Returns `-1` if child node is not found.
  185. *
  186. * @param {module:engine/view/node~Node} node Child node.
  187. * @returns {Number} Index of the child node.
  188. */
  189. getChildIndex( node ) {
  190. return this._children.indexOf( node );
  191. }
  192. /**
  193. * Gets child nodes iterator.
  194. *
  195. * @returns {Iterable.<module:engine/view/node~Node>} Child nodes iterator.
  196. */
  197. getChildren() {
  198. return this._children[ Symbol.iterator ]();
  199. }
  200. /**
  201. * Returns an iterator that contains the keys for attributes. Order of inserting attributes is not preserved.
  202. *
  203. * @returns {Iterable.<String>} Keys for attributes.
  204. */
  205. * getAttributeKeys() {
  206. if ( this._classes.size > 0 ) {
  207. yield 'class';
  208. }
  209. if ( this._styles.size > 0 ) {
  210. yield 'style';
  211. }
  212. // This is not an optimal solution because of https://github.com/ckeditor/ckeditor5-engine/issues/454.
  213. // It can be simplified to `yield* this._attrs.keys();`.
  214. for ( const key of this._attrs.keys() ) {
  215. yield key;
  216. }
  217. }
  218. /**
  219. * Returns iterator that iterates over this element's attributes.
  220. *
  221. * Attributes are returned as arrays containing two items. First one is attribute key and second is attribute value.
  222. * This format is accepted by native `Map` object and also can be passed in `Node` constructor.
  223. *
  224. * @returns {Iterable.<*>}
  225. */
  226. * getAttributes() {
  227. yield* this._attrs.entries();
  228. if ( this._classes.size > 0 ) {
  229. yield [ 'class', this.getAttribute( 'class' ) ];
  230. }
  231. if ( this._styles.size > 0 ) {
  232. yield [ 'style', this.getAttribute( 'style' ) ];
  233. }
  234. }
  235. /**
  236. * Gets attribute by key. If attribute is not present - returns undefined.
  237. *
  238. * @param {String} key Attribute key.
  239. * @returns {String|undefined} Attribute value.
  240. */
  241. getAttribute( key ) {
  242. if ( key == 'class' ) {
  243. if ( this._classes.size > 0 ) {
  244. return [ ...this._classes ].join( ' ' );
  245. }
  246. return undefined;
  247. }
  248. if ( key == 'style' ) {
  249. if ( this._styles.size > 0 ) {
  250. let styleString = '';
  251. for ( const [ property, value ] of this._styles ) {
  252. styleString += `${ property }:${ value };`;
  253. }
  254. return styleString;
  255. }
  256. return undefined;
  257. }
  258. return this._attrs.get( key );
  259. }
  260. /**
  261. * Returns a boolean indicating whether an attribute with the specified key exists in the element.
  262. *
  263. * @param {String} key Attribute key.
  264. * @returns {Boolean} `true` if attribute with the specified key exists in the element, false otherwise.
  265. */
  266. hasAttribute( key ) {
  267. if ( key == 'class' ) {
  268. return this._classes.size > 0;
  269. }
  270. if ( key == 'style' ) {
  271. return this._styles.size > 0;
  272. }
  273. return this._attrs.has( key );
  274. }
  275. /**
  276. * Checks if this element is similar to other element.
  277. * Both elements should have the same name and attributes to be considered as similar. Two similar elements
  278. * can contain different set of children nodes.
  279. *
  280. * @param {module:engine/view/element~Element} otherElement
  281. * @returns {Boolean}
  282. */
  283. isSimilar( otherElement ) {
  284. if ( !( otherElement instanceof Element ) ) {
  285. return false;
  286. }
  287. // If exactly the same Element is provided - return true immediately.
  288. if ( this === otherElement ) {
  289. return true;
  290. }
  291. // Check element name.
  292. if ( this.name != otherElement.name ) {
  293. return false;
  294. }
  295. // Check number of attributes, classes and styles.
  296. if ( this._attrs.size !== otherElement._attrs.size || this._classes.size !== otherElement._classes.size ||
  297. this._styles.size !== otherElement._styles.size ) {
  298. return false;
  299. }
  300. // Check if attributes are the same.
  301. for ( const [ key, value ] of this._attrs ) {
  302. if ( !otherElement._attrs.has( key ) || otherElement._attrs.get( key ) !== value ) {
  303. return false;
  304. }
  305. }
  306. // Check if classes are the same.
  307. for ( const className of this._classes ) {
  308. if ( !otherElement._classes.has( className ) ) {
  309. return false;
  310. }
  311. }
  312. // Check if styles are the same.
  313. for ( const [ property, value ] of this._styles ) {
  314. if ( !otherElement._styles.has( property ) || otherElement._styles.get( property ) !== value ) {
  315. return false;
  316. }
  317. }
  318. return true;
  319. }
  320. /**
  321. * Returns true if class is present.
  322. * If more then one class is provided - returns true only when all classes are present.
  323. *
  324. * element.hasClass( 'foo' ); // Returns true if 'foo' class is present.
  325. * element.hasClass( 'foo', 'bar' ); // Returns true if 'foo' and 'bar' classes are both present.
  326. *
  327. * @param {...String} className
  328. */
  329. hasClass( ...className ) {
  330. for ( const name of className ) {
  331. if ( !this._classes.has( name ) ) {
  332. return false;
  333. }
  334. }
  335. return true;
  336. }
  337. /**
  338. * Returns iterator that contains all class names.
  339. *
  340. * @returns {Iterable.<String>}
  341. */
  342. getClassNames() {
  343. return this._classes.keys();
  344. }
  345. /**
  346. * Returns style value for given property.
  347. * Undefined is returned if style does not exist.
  348. *
  349. * @param {String} property
  350. * @returns {String|undefined}
  351. */
  352. getStyle( property ) {
  353. return this._styles.get( property );
  354. }
  355. /**
  356. * Returns iterator that contains all style names.
  357. *
  358. * @returns {Iterable.<String>}
  359. */
  360. getStyleNames() {
  361. return this._styles.keys();
  362. }
  363. /**
  364. * Returns true if style keys are present.
  365. * If more then one style property is provided - returns true only when all properties are present.
  366. *
  367. * element.hasStyle( 'color' ); // Returns true if 'border-top' style is present.
  368. * element.hasStyle( 'color', 'border-top' ); // Returns true if 'color' and 'border-top' styles are both present.
  369. *
  370. * @param {...String} property
  371. */
  372. hasStyle( ...property ) {
  373. for ( const name of property ) {
  374. if ( !this._styles.has( name ) ) {
  375. return false;
  376. }
  377. }
  378. return true;
  379. }
  380. /**
  381. * Returns ancestor element that match specified pattern.
  382. * Provided patterns should be compatible with {@link module:engine/view/matcher~Matcher Matcher} as it is used internally.
  383. *
  384. * @see module:engine/view/matcher~Matcher
  385. * @param {Object|String|RegExp|Function} patterns Patterns used to match correct ancestor.
  386. * See {@link module:engine/view/matcher~Matcher}.
  387. * @returns {module:engine/view/element~Element|null} Found element or `null` if no matching ancestor was found.
  388. */
  389. findAncestor( ...patterns ) {
  390. const matcher = new Matcher( ...patterns );
  391. let parent = this.parent;
  392. while ( parent ) {
  393. if ( matcher.match( parent ) ) {
  394. return parent;
  395. }
  396. parent = parent.parent;
  397. }
  398. return null;
  399. }
  400. /**
  401. * Returns the custom property value for the given key.
  402. *
  403. * @param {String|Symbol} key
  404. * @returns {*}
  405. */
  406. getCustomProperty( key ) {
  407. return this._customProperties.get( key );
  408. }
  409. /**
  410. * Returns an iterator which iterates over this element's custom properties.
  411. * Iterator provides `[ key, value ]` pairs for each stored property.
  412. *
  413. * @returns {Iterable.<*>}
  414. */
  415. * getCustomProperties() {
  416. yield* this._customProperties.entries();
  417. }
  418. /**
  419. * Returns identity string based on element's name, styles, classes and other attributes.
  420. * Two elements that {@link #isSimilar are similar} will have same identity string.
  421. * It has the following format:
  422. *
  423. * 'name class="class1,class2" style="style1:value1;style2:value2" attr1="val1" attr2="val2"'
  424. *
  425. * For example:
  426. *
  427. * const element = writer.createContainerElement( 'foo', {
  428. * banana: '10',
  429. * apple: '20',
  430. * style: 'color: red; border-color: white;',
  431. * class: 'baz'
  432. * } );
  433. *
  434. * // returns 'foo class="baz" style="border-color:white;color:red" apple="20" banana="10"'
  435. * element.getIdentity();
  436. *
  437. * NOTE: Classes, styles and other attributes are sorted alphabetically.
  438. *
  439. * @returns {String}
  440. */
  441. getIdentity() {
  442. const classes = Array.from( this._classes ).sort().join( ',' );
  443. const styles = Array.from( this._styles ).map( i => `${ i[ 0 ] }:${ i[ 1 ] }` ).sort().join( ';' );
  444. const attributes = Array.from( this._attrs ).map( i => `${ i[ 0 ] }="${ i[ 1 ] }"` ).sort().join( ' ' );
  445. return this.name +
  446. ( classes == '' ? '' : ` class="${ classes }"` ) +
  447. ( styles == '' ? '' : ` style="${ styles }"` ) +
  448. ( attributes == '' ? '' : ` ${ attributes }` );
  449. }
  450. /**
  451. * {@link module:engine/view/element~Element#_insertChildren Insert} a child node or a list of child nodes at the end of this node
  452. * and sets the parent of these nodes to this element.
  453. *
  454. * @fires module:engine/view/node~Node#change
  455. * @param {module:engine/view/item~Item|Iterable.<module:engine/view/item~Item>} items Items to be inserted.
  456. * @returns {Number} Number of appended nodes.
  457. */
  458. _appendChildren( items ) {
  459. return this._insertChildren( this.childCount, items );
  460. }
  461. /**
  462. * Inserts a child node or a list of child nodes on the given index and sets the parent of these nodes to
  463. * this element.
  464. *
  465. * @protected
  466. * @param {Number} index Position where nodes should be inserted.
  467. * @param {module:engine/view/item~Item|Iterable.<module:engine/view/item~Item>} items Items to be inserted.
  468. * @fires module:engine/view/node~Node#change
  469. * @returns {Number} Number of inserted nodes.
  470. */
  471. _insertChildren( index, items ) {
  472. this._fireChange( 'children', this );
  473. let count = 0;
  474. const nodes = normalize( items );
  475. for ( const node of nodes ) {
  476. // If node that is being added to this element is already inside another element, first remove it from the old parent.
  477. if ( node.parent !== null ) {
  478. node._remove();
  479. }
  480. node.parent = this;
  481. this._children.splice( index, 0, node );
  482. index++;
  483. count++;
  484. }
  485. return count;
  486. }
  487. /**
  488. * Removes number of child nodes starting at the given index and set the parent of these nodes to `null`.
  489. *
  490. * @param {Number} index Number of the first node to remove.
  491. * @param {Number} [howMany=1] Number of nodes to remove.
  492. * @returns {Array.<module:engine/view/node~Node>} The array of removed nodes.
  493. * @fires module:engine/view/node~Node#change
  494. */
  495. _removeChildren( index, howMany = 1 ) {
  496. this._fireChange( 'children', this );
  497. for ( let i = index; i < index + howMany; i++ ) {
  498. this._children[ i ].parent = null;
  499. }
  500. return this._children.splice( index, howMany );
  501. }
  502. /**
  503. * Adds or overwrite attribute with a specified key and value.
  504. *
  505. * @protected
  506. * @param {String} key Attribute key.
  507. * @param {String} value Attribute value.
  508. * @fires module:engine/view/node~Node#change
  509. */
  510. _setAttribute( key, value ) {
  511. value = String( value );
  512. this._fireChange( 'attributes', this );
  513. if ( key == 'class' ) {
  514. parseClasses( this._classes, value );
  515. } else if ( key == 'style' ) {
  516. parseInlineStyles( this._styles, value );
  517. } else {
  518. this._attrs.set( key, value );
  519. }
  520. }
  521. /**
  522. * Removes attribute from the element.
  523. *
  524. * @protected
  525. * @param {String} key Attribute key.
  526. * @returns {Boolean} Returns true if an attribute existed and has been removed.
  527. * @fires module:engine/view/node~Node#change
  528. */
  529. _removeAttribute( key ) {
  530. this._fireChange( 'attributes', this );
  531. // Remove class attribute.
  532. if ( key == 'class' ) {
  533. if ( this._classes.size > 0 ) {
  534. this._classes.clear();
  535. return true;
  536. }
  537. return false;
  538. }
  539. // Remove style attribute.
  540. if ( key == 'style' ) {
  541. if ( this._styles.size > 0 ) {
  542. this._styles.clear();
  543. return true;
  544. }
  545. return false;
  546. }
  547. // Remove other attributes.
  548. return this._attrs.delete( key );
  549. }
  550. /**
  551. * Adds specified class.
  552. *
  553. * element._addClass( 'foo' ); // Adds 'foo' class.
  554. * element._addClass( [ 'foo', 'bar' ] ); // Adds 'foo' and 'bar' classes.
  555. *
  556. * @protected
  557. * @param {Array.<String>|String} className
  558. * @fires module:engine/view/node~Node#change
  559. */
  560. _addClass( className ) {
  561. this._fireChange( 'attributes', this );
  562. className = Array.isArray( className ) ? className : [ className ];
  563. className.forEach( name => this._classes.add( name ) );
  564. }
  565. /**
  566. * Removes specified class.
  567. *
  568. * element._removeClass( 'foo' ); // Removes 'foo' class.
  569. * element._removeClass( [ 'foo', 'bar' ] ); // Removes both 'foo' and 'bar' classes.
  570. *
  571. * @param {Array.<String>|String} className
  572. * @fires module:engine/view/node~Node#change
  573. */
  574. _removeClass( className ) {
  575. this._fireChange( 'attributes', this );
  576. className = Array.isArray( className ) ? className : [ className ];
  577. className.forEach( name => this._classes.delete( name ) );
  578. }
  579. /**
  580. * Adds style to the element.
  581. *
  582. * element._setStyle( 'color', 'red' );
  583. * element._setStyle( {
  584. * color: 'red',
  585. * position: 'fixed'
  586. * } );
  587. *
  588. * @protected
  589. * @param {String|Object} property Property name or object with key - value pairs.
  590. * @param {String} [value] Value to set. This parameter is ignored if object is provided as the first parameter.
  591. * @fires module:engine/view/node~Node#change
  592. */
  593. _setStyle( property, value ) {
  594. this._fireChange( 'attributes', this );
  595. if ( isPlainObject( property ) ) {
  596. const keys = Object.keys( property );
  597. for ( const key of keys ) {
  598. this._styles.set( key, property[ key ] );
  599. }
  600. } else {
  601. this._styles.set( property, value );
  602. }
  603. }
  604. /**
  605. * Removes specified style.
  606. *
  607. * element._removeStyle( 'color' ); // Removes 'color' style.
  608. * element._removeStyle( [ 'color', 'border-top' ] ); // Removes both 'color' and 'border-top' styles.
  609. *
  610. * @protected
  611. * @param {Array.<String>|String} property
  612. * @fires module:engine/view/node~Node#change
  613. */
  614. _removeStyle( property ) {
  615. this._fireChange( 'attributes', this );
  616. property = Array.isArray( property ) ? property : [ property ];
  617. property.forEach( name => this._styles.delete( name ) );
  618. }
  619. /**
  620. * Sets a custom property. Unlike attributes, custom properties are not rendered to the DOM,
  621. * so they can be used to add special data to elements.
  622. *
  623. * @protected
  624. * @param {String|Symbol} key
  625. * @param {*} value
  626. */
  627. _setCustomProperty( key, value ) {
  628. this._customProperties.set( key, value );
  629. }
  630. /**
  631. * Removes the custom property stored under the given key.
  632. *
  633. * @protected
  634. * @param {String|Symbol} key
  635. * @returns {Boolean} Returns true if property was removed.
  636. */
  637. _removeCustomProperty( key ) {
  638. return this._customProperties.delete( key );
  639. }
  640. /**
  641. * Returns block {@link module:engine/view/filler filler} offset or `null` if block filler is not needed.
  642. *
  643. * @abstract
  644. * @method module:engine/view/element~Element#getFillerOffset
  645. */
  646. }
  647. // Parses attributes provided to the element constructor before they are applied to an element. If attributes are passed
  648. // as an object (instead of `Map`), the object is transformed to the map. Attributes with `null` value are removed.
  649. // Attributes with non-`String` value are converted to `String`.
  650. //
  651. // @param {Object|Map} attrs Attributes to parse.
  652. // @returns {Map} Parsed attributes.
  653. function parseAttributes( attrs ) {
  654. if ( isPlainObject( attrs ) ) {
  655. attrs = objectToMap( attrs );
  656. } else {
  657. attrs = new Map( attrs );
  658. }
  659. for ( const [ key, value ] of attrs ) {
  660. if ( value === null ) {
  661. attrs.delete( key );
  662. } else if ( typeof value != 'string' ) {
  663. attrs.set( key, String( value ) );
  664. }
  665. }
  666. return attrs;
  667. }
  668. // Parses inline styles and puts property - value pairs into styles map.
  669. // Styles map is cleared before insertion.
  670. //
  671. // @param {Map.<String, String>} stylesMap Map to insert parsed properties and values.
  672. // @param {String} stylesString Styles to parse.
  673. function parseInlineStyles( stylesMap, stylesString ) {
  674. // `null` if no quote was found in input string or last found quote was a closing quote. See below.
  675. let quoteType = null;
  676. let propertyNameStart = 0;
  677. let propertyValueStart = 0;
  678. let propertyName = null;
  679. stylesMap.clear();
  680. // Do not set anything if input string is empty.
  681. if ( stylesString === '' ) {
  682. return;
  683. }
  684. // Fix inline styles that do not end with `;` so they are compatible with algorithm below.
  685. if ( stylesString.charAt( stylesString.length - 1 ) != ';' ) {
  686. stylesString = stylesString + ';';
  687. }
  688. // Seek the whole string for "special characters".
  689. for ( let i = 0; i < stylesString.length; i++ ) {
  690. const char = stylesString.charAt( i );
  691. if ( quoteType === null ) {
  692. // No quote found yet or last found quote was a closing quote.
  693. switch ( char ) {
  694. case ':':
  695. // Most of time colon means that property name just ended.
  696. // Sometimes however `:` is found inside property value (for example in background image url).
  697. if ( !propertyName ) {
  698. // Treat this as end of property only if property name is not already saved.
  699. // Save property name.
  700. propertyName = stylesString.substr( propertyNameStart, i - propertyNameStart );
  701. // Save this point as the start of property value.
  702. propertyValueStart = i + 1;
  703. }
  704. break;
  705. case '"':
  706. case '\'':
  707. // Opening quote found (this is an opening quote, because `quoteType` is `null`).
  708. quoteType = char;
  709. break;
  710. // eslint-disable-next-line no-case-declarations
  711. case ';':
  712. // Property value just ended.
  713. // Use previously stored property value start to obtain property value.
  714. const propertyValue = stylesString.substr( propertyValueStart, i - propertyValueStart );
  715. if ( propertyName ) {
  716. // Save parsed part.
  717. stylesMap.set( propertyName.trim(), propertyValue.trim() );
  718. }
  719. propertyName = null;
  720. // Save this point as property name start. Property name starts immediately after previous property value ends.
  721. propertyNameStart = i + 1;
  722. break;
  723. }
  724. } else if ( char === quoteType ) {
  725. // If a quote char is found and it is a closing quote, mark this fact by `null`-ing `quoteType`.
  726. quoteType = null;
  727. }
  728. }
  729. }
  730. // Parses class attribute and puts all classes into classes set.
  731. // Classes set s cleared before insertion.
  732. //
  733. // @param {Set.<String>} classesSet Set to insert parsed classes.
  734. // @param {String} classesString String with classes to parse.
  735. function parseClasses( classesSet, classesString ) {
  736. const classArray = classesString.split( /\s+/ );
  737. classesSet.clear();
  738. classArray.forEach( name => classesSet.add( name ) );
  739. }
  740. // Converts strings to Text and non-iterables to arrays.
  741. //
  742. // @param {String|module:engine/view/item~Item|Iterable.<String|module:engine/view/item~Item>}
  743. // @return {Iterable.<module:engine/view/node~Node>}
  744. function normalize( nodes ) {
  745. // Separate condition because string is iterable.
  746. if ( typeof nodes == 'string' ) {
  747. return [ new Text( nodes ) ];
  748. }
  749. if ( !isIterable( nodes ) ) {
  750. nodes = [ nodes ];
  751. }
  752. // Array.from to enable .map() on non-arrays.
  753. return Array.from( nodes )
  754. .map( node => {
  755. if ( typeof node == 'string' ) {
  756. return new Text( node );
  757. }
  758. if ( node instanceof TextProxy ) {
  759. return new Text( node.data );
  760. }
  761. return node;
  762. } );
  763. }