8
0

utils.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module table/commands/utils
  7. */
  8. /**
  9. * Returns the parent table.
  10. *
  11. * @param {module:engine/model/position~Position} position
  12. * @returns {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment}
  13. */
  14. export function getParentTable( position ) {
  15. let parent = position.parent;
  16. while ( parent ) {
  17. if ( parent.name === 'table' ) {
  18. return parent;
  19. }
  20. parent = parent.parent;
  21. }
  22. }
  23. /**
  24. * A common method to update the numeric value. If a value is the default one, it will be unset.
  25. *
  26. * @param {String} key Attribute key.
  27. * @param {*} value The new attribute value.
  28. * @param {module:engine/model/item~Item} item Model item on which the attribute will be set.
  29. * @param {module:engine/model/writer~Writer} writer
  30. * @param {*} defaultValue Default attribute value. If a value is lower or equal, it will be unset.
  31. */
  32. export function updateNumericAttribute( key, value, item, writer, defaultValue = 1 ) {
  33. if ( value > defaultValue ) {
  34. writer.setAttribute( key, value, item );
  35. } else {
  36. writer.removeAttribute( key, item );
  37. }
  38. }