saneObjectToDom.js 3.0 KB

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