utils.js 994 B

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module link/utils
  7. */
  8. const linkElementSymbol = Symbol( 'linkElement' );
  9. /**
  10. * Returns `true` if a given view node is the link element.
  11. *
  12. * @param {module:engine/view/node~Node} node
  13. * @return {Boolean}
  14. */
  15. export function isLinkElement( node ) {
  16. return node.is( 'attributeElement' ) && !!node.getCustomProperty( linkElementSymbol );
  17. }
  18. /**
  19. * Creates link {@link module:engine/view/attributeelement~AttributeElement} with provided `href` attribute.
  20. *
  21. * @param {String} href
  22. * @return {module:engine/view/attributeelement~AttributeElement}
  23. */
  24. export function createLinkElement( href, writer ) {
  25. // Priority 5 - https://github.com/ckeditor/ckeditor5-link/issues/121.
  26. const linkElement = writer.createAttributeElement( 'a', { href }, { priority: 5 } );
  27. writer.setCustomProperty( linkElementSymbol, true, linkElement );
  28. return linkElement;
  29. }