DOMite.st 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. Smalltalk createPackage: 'DOMite'!
  2. Object subclass: #Domite
  3. instanceVariableNames: 'element reference'
  4. package: 'DOMite'!
  5. !Domite commentStamp!
  6. I am (hopefully thin) wrapper around the notion of "cursor in a page".
  7. I represent a DOM node _and_ a point where
  8. to insert new content into it.
  9. So I play both the role of a container that inserts
  10. as well as the role of an element being inserted.
  11. Creation API:
  12. - `Domite new` creates an insertion point at the bottom of `<body>`.
  13. - `Domite open` is unique way to create pieces of content. It creates an instance "floating in thin air" (wrapper around DOM DocumentFragment) that can be filled with any contents and then inserted in a page.
  14. - `Domite fromElement: aDomElement` wraps an element and set the cursor to its end.
  15. Manipulation API:
  16. - `aDomite insertDomite:` and `aDomite insertString:` insert either a Domite or a text content at the insertion point.
  17. - `aDomite clearHere` deletes contents of the wrapped element.
  18. Cursor moving API:
  19. Take this sample HTML, where `[n]` are just markers, not real content:
  20. ```
  21. <body>
  22. <h1>header</h1>
  23. [4]<p>[2]Hello[1]world[3]</p>[5]
  24. <small>footer</small>
  25. </body>
  26. ```
  27. If `d` is a `Domite` representing `[1]`, then:
  28. - `d seekHereStart` would move `d` to be at `[2]`,
  29. - `d seekHereEnd` would move `d` to be at `[3]`,
  30. - `d seekBeforeHere` would move `d` to be at `[4]`, and
  31. - `d seekAfterHere` would move `d` to be at `[5]`.
  32. It is not presumed one would use `seekXxx`
  33. to actually move around in a single instance.
  34. It is envisioned this API will be used mostly
  35. in combination with `copy`, like
  36. `afterMe := self copy seekAfterHere`.!
  37. !Domite methodsFor: 'accessing'!
  38. element
  39. ^ element
  40. !
  41. element: anObject
  42. element := anObject
  43. !
  44. reference
  45. ^ reference
  46. !
  47. reference: anObject
  48. reference := anObject
  49. ! !
  50. !Domite methodsFor: 'deletion'!
  51. clearHere
  52. <
  53. var element = self['@element'], child;
  54. while (child = element.firstChild) element.removeChild(child);
  55. self['@reference'] = null;
  56. >
  57. ! !
  58. !Domite methodsFor: 'initialization'!
  59. initialize
  60. super initialize.
  61. element := document body.
  62. reference := nil asJSON
  63. ! !
  64. !Domite methodsFor: 'insertion'!
  65. insertDomite: aDomite
  66. self insertElement: aDomite element
  67. !
  68. insertElement: aDomElement
  69. self element
  70. insertBefore: aDomElement
  71. reference: self reference
  72. !
  73. insertString: aString
  74. self insertElement: (
  75. document createTextNode: aString asString )
  76. ! !
  77. !Domite methodsFor: 'navigation'!
  78. seekAfterHere
  79. self
  80. reference: self element nextSibling;
  81. element: self element parentNode
  82. !
  83. seekBeforeHere
  84. self
  85. reference: self element;
  86. element: self element parentNode
  87. !
  88. seekHereEnd
  89. self reference: nil asJSON "null"
  90. !
  91. seekHereStart
  92. self reference: self element firstChild
  93. ! !
  94. !Domite methodsFor: 'testing'!
  95. canSeekOutOfHere
  96. ^ self element parentNode notNil
  97. ! !
  98. !Domite class methodsFor: 'instance creation'!
  99. fromElement: aDomElement
  100. ^ self new
  101. element: aDomElement;
  102. yourself
  103. !
  104. fromElement: aDomElement cursorBefore: anotherDomElement
  105. ^ self new
  106. element: aDomElement;
  107. referenceElement: anotherDomElement;
  108. yourself
  109. !
  110. newElement: aString
  111. ^ self fromElement: (document createElement: aString)
  112. !
  113. open
  114. ^ self fromElement: document createDocumentFragment
  115. ! !