dummer.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. var dummer, object, sanitizer,
  2. __hasProp = {}.hasOwnProperty;
  3. object = require('utila').object;
  4. sanitizer = require('./sanitizer');
  5. module.exports = dummer = {
  6. toDom: function(o) {
  7. if (!Array.isArray(o)) {
  8. if (!object.isBareObject(o)) {
  9. throw Error("toDom() only accepts arrays and bare objects as input");
  10. }
  11. }
  12. o = sanitizer.sanitize(o);
  13. return dummer._children(o);
  14. },
  15. _children: function(a, parent) {
  16. var children, node, prev, v, _i, _len;
  17. if (parent == null) {
  18. parent = null;
  19. }
  20. children = [];
  21. prev = null;
  22. for (_i = 0, _len = a.length; _i < _len; _i++) {
  23. v = a[_i];
  24. if (typeof v === 'string') {
  25. node = dummer._textNode(v);
  26. } else {
  27. node = dummer._objectToDom(v, parent);
  28. node.prev = null;
  29. node.next = null;
  30. node.parent = parent;
  31. if (prev != null) {
  32. node.prev = prev;
  33. prev.next = node;
  34. }
  35. prev = node;
  36. }
  37. children.push(node);
  38. }
  39. return children;
  40. },
  41. _objectToDom: function(o) {
  42. var attribs, children, i, k, key, name, node, v, val, _ref;
  43. i = 0;
  44. for (k in o) {
  45. if (!__hasProp.call(o, k)) continue;
  46. v = o[k];
  47. if (i > 0) {
  48. throw Error("_objectToDom() only accepts an object with one key/value");
  49. }
  50. key = k;
  51. val = v;
  52. i++;
  53. }
  54. node = {};
  55. if (typeof key !== 'string') {
  56. throw Error("_objectToDom()'s key must be a string of tag name and classes");
  57. }
  58. if (typeof val === 'string') {
  59. children = [dummer._textNode(val)];
  60. } else if (Array.isArray(val)) {
  61. children = dummer._children(val, node);
  62. } else {
  63. inspect(o);
  64. throw Error("_objectToDom()'s key's value must only be a string or an array");
  65. }
  66. node.type = 'tag';
  67. _ref = dummer._parseTag(key), name = _ref.name, attribs = _ref.attribs;
  68. node.name = name;
  69. node.attribs = attribs;
  70. node.children = children;
  71. return node;
  72. },
  73. _textNode: function(s) {
  74. return {
  75. type: 'text',
  76. data: s
  77. };
  78. },
  79. _nameRx: /^[a-zA-Z\-\_]{1}[a-zA-Z0-9\-\_]*$/,
  80. _parseTag: function(k) {
  81. var attribs, classes, cls, id, m, name, parts;
  82. if (!k.match(/^[a-zA-Z0-9\#\-\_\.\[\]\"\'\=\,\s]+$/) || k.match(/^[0-9]+/)) {
  83. throw Error("cannot parse tag `" + k + "`");
  84. }
  85. attribs = {};
  86. parts = {
  87. name: '',
  88. attribs: attribs
  89. };
  90. if (m = k.match(/^([^\.#]+)/)) {
  91. name = m[1];
  92. if (!name.match(dummer._nameRx)) {
  93. throw Error("tag name `" + name + "` is not valid");
  94. }
  95. parts.name = name;
  96. k = k.substr(name.length, k.length);
  97. }
  98. if (m = k.match(/^#([a-zA-Z0-9\-]+)/)) {
  99. id = m[1];
  100. if (!id.match(dummer._nameRx)) {
  101. throw Error("tag id `" + id + "` is not valid");
  102. }
  103. attribs.id = id;
  104. k = k.substr(id.length + 1, k.length);
  105. }
  106. classes = [];
  107. while (m = k.match(/\.([a-zA-Z0-9\-\_]+)/)) {
  108. cls = m[1];
  109. if (!cls.match(dummer._nameRx)) {
  110. throw Error("tag class `" + cls + "` is not valid");
  111. }
  112. classes.push(cls);
  113. k = k.replace('.' + cls, '');
  114. }
  115. if (classes.length) {
  116. attribs["class"] = classes.join(" ");
  117. }
  118. return parts;
  119. }
  120. };