css-select-adapter.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. var baseCssAdapter = require('css-select-base-adapter');
  3. /**
  4. * DOMUtils API for SVGO AST (used by css-select)
  5. */
  6. var svgoCssSelectAdapterMin = {
  7. // is the node a tag?
  8. // isTag: ( node:Node ) => isTag:Boolean
  9. isTag: function(node) {
  10. return node.isElem();
  11. },
  12. // get the parent of the node
  13. // getParent: ( node:Node ) => parentNode:Node
  14. // returns null when no parent exists
  15. getParent: function(node) {
  16. return node.parentNode || null;
  17. },
  18. // get the node's children
  19. // getChildren: ( node:Node ) => children:[Node]
  20. getChildren: function(node) {
  21. return node.content || [];
  22. },
  23. // get the name of the tag
  24. // getName: ( elem:ElementNode ) => tagName:String
  25. getName: function(elemAst) {
  26. return elemAst.elem;
  27. },
  28. // get the text content of the node, and its children if it has any
  29. // getText: ( node:Node ) => text:String
  30. // returns empty string when there is no text
  31. getText: function(node) {
  32. return node.content[0].text || node.content[0].cdata || '';
  33. },
  34. // get the attribute value
  35. // getAttributeValue: ( elem:ElementNode, name:String ) => value:String
  36. // returns null when attribute doesn't exist
  37. getAttributeValue: function(elem, name) {
  38. return elem.hasAttr(name) ? elem.attr(name).value : null;
  39. }
  40. };
  41. // use base adapter for default implementation
  42. var svgoCssSelectAdapter = baseCssAdapter(svgoCssSelectAdapterMin);
  43. module.exports = svgoCssSelectAdapter;