A Twitch.tv viewer reward and games system.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.8 KiB

  1. 'use strict';
  2. var Node = require('./node');
  3. var Block = require('./block');
  4. /**
  5. * Initialize a `Attrs` node.
  6. *
  7. * @api public
  8. */
  9. var Attrs = module.exports = function Attrs() {
  10. this.attributeNames = [];
  11. this.attrs = [];
  12. this.attributeBlocks = [];
  13. };
  14. // Inherit from `Node`.
  15. Attrs.prototype = Object.create(Node.prototype);
  16. Attrs.prototype.constructor = Attrs;
  17. Attrs.prototype.type = 'Attrs';
  18. /**
  19. * Set attribute `name` to `val`, keep in mind these become
  20. * part of a raw js object literal, so to quote a value you must
  21. * '"quote me"', otherwise or example 'user.name' is literal JavaScript.
  22. *
  23. * @param {String} name
  24. * @param {String} val
  25. * @param {Boolean} escaped
  26. * @return {Tag} for chaining
  27. * @api public
  28. */
  29. Attrs.prototype.setAttribute = function(name, val, escaped){
  30. this.attributeNames = this.attributeNames || [];
  31. if (name !== 'class' && this.attributeNames.indexOf(name) !== -1) {
  32. throw new Error('Duplicate attribute "' + name + '" is not allowed.');
  33. }
  34. this.attributeNames.push(name);
  35. this.attrs.push({ name: name, val: val, escaped: escaped });
  36. return this;
  37. };
  38. /**
  39. * Remove attribute `name` when present.
  40. *
  41. * @param {String} name
  42. * @api public
  43. */
  44. Attrs.prototype.removeAttribute = function(name){
  45. for (var i = 0, len = this.attrs.length; i < len; ++i) {
  46. if (this.attrs[i] && this.attrs[i].name == name) {
  47. delete this.attrs[i];
  48. }
  49. }
  50. };
  51. /**
  52. * Get attribute value by `name`.
  53. *
  54. * @param {String} name
  55. * @return {String}
  56. * @api public
  57. */
  58. Attrs.prototype.getAttribute = function(name){
  59. for (var i = 0, len = this.attrs.length; i < len; ++i) {
  60. if (this.attrs[i] && this.attrs[i].name == name) {
  61. return this.attrs[i].val;
  62. }
  63. }
  64. };
  65. Attrs.prototype.addAttributes = function (src) {
  66. this.attributeBlocks.push(src);
  67. };