ContextNode.js 747 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import Node from './Node.js';
  2. class ContextNode extends Node {
  3. constructor( node, context = {} ) {
  4. super();
  5. this.node = node;
  6. this.context = context;
  7. }
  8. setContextValue( name, value ) {
  9. this.context[ name ] = value;
  10. return this;
  11. }
  12. getContextValue( name ) {
  13. return this.context[ name ];
  14. }
  15. getNodeType( builder ) {
  16. return this.node.getNodeType( builder );
  17. }
  18. generate( builder, output ) {
  19. const previousContext = builder.getContext();
  20. builder.setContext( Object.assign( {}, builder.context, this.context ) );
  21. const snippet = this.node.build( builder, output );
  22. builder.setContext( previousContext );
  23. return snippet;
  24. }
  25. }
  26. ContextNode.prototype.isContextNode = true;
  27. export default ContextNode;