暂无描述

node.js 915B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // This object will be used as the prototype for Nodes when creating a
  2. // DOM-Level-1-compliant structure.
  3. var NodePrototype = module.exports = {
  4. get firstChild() {
  5. var children = this.children;
  6. return children && children[0] || null;
  7. },
  8. get lastChild() {
  9. var children = this.children;
  10. return children && children[children.length - 1] || null;
  11. },
  12. get nodeType() {
  13. return nodeTypes[this.type] || nodeTypes.element;
  14. }
  15. };
  16. var domLvl1 = {
  17. tagName: "name",
  18. childNodes: "children",
  19. parentNode: "parent",
  20. previousSibling: "prev",
  21. nextSibling: "next",
  22. nodeValue: "data"
  23. };
  24. var nodeTypes = {
  25. element: 1,
  26. text: 3,
  27. cdata: 4,
  28. comment: 8
  29. };
  30. Object.keys(domLvl1).forEach(function(key) {
  31. var shorthand = domLvl1[key];
  32. Object.defineProperty(NodePrototype, key, {
  33. get: function() {
  34. return this[shorthand] || null;
  35. },
  36. set: function(val) {
  37. this[shorthand] = val;
  38. return val;
  39. }
  40. });
  41. });