DOMite.st 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. Smalltalk createPackage: 'DOMite'!
  2. ProtoStream 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. I inherit from `ProtoStream`.
  12. Creation API:
  13. - `Domite new` creates an insertion point at the bottom of `<body>`.
  14. - `Domite newStream` 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.
  15. - `Domite fromElement: aDomElement` wraps an element and set the cursor to its end.
  16. CSS selector API:
  17. - `Domite at: aSelector` wraps an element found by `document.querySelector(aSelector)`.
  18. Manipulation API:
  19. - `aDomite << obj` inserts obj at the insertion point.
  20. - `aDomite resetContents` deletes contents of the wrapped element.
  21. Cursor moving API:
  22. Take this sample HTML, where `[n]` are just markers, not real content:
  23. ```
  24. <body>
  25. <h1>header</h1>
  26. [4]<p>[2]Hello[1]world[3]</p>[5]
  27. <small>footer</small>
  28. </body>
  29. ```
  30. If `d` is a `Domite` representing `[1]`, then:
  31. - `d setToStart` would move `d` to be at `[2]`,
  32. - `d setToEnd` would move `d` to be at `[3]`,
  33. - `d setToBefore` would move `d` to be at `[4]`, and
  34. - `d setToAfter` would move `d` to be at `[5]`.
  35. It is not presumed one would use `setToXxx`
  36. to actually move around in a single instance.
  37. It is envisioned this API will be used mostly
  38. in combination with `copy`, like
  39. `afterMe := self copy setToAfter`.!
  40. !Domite methodsFor: 'accessing'!
  41. attrAt: aString
  42. (element hasAttribute: aString)
  43. ifTrue: [ ^ element getAttribute: aString ]
  44. ifFalse: [ ^ nil ]
  45. !
  46. attrAt: aString put: anotherString
  47. element setAttribute: aString to: anotherString
  48. !
  49. element
  50. ^ element
  51. !
  52. element: anObject
  53. element := anObject
  54. !
  55. propAt: aString
  56. ^ element at: aString
  57. !
  58. propAt: aString put: anObject
  59. ^ element at: aString put: anObject
  60. !
  61. reference
  62. ^ reference
  63. !
  64. reference: anObject
  65. reference := anObject
  66. ! !
  67. !Domite methodsFor: 'deletion'!
  68. resetContents
  69. <
  70. var element = self['@element'], child;
  71. while (child = element.firstChild) element.removeChild(child);
  72. self['@reference'] = null;
  73. >
  74. ! !
  75. !Domite methodsFor: 'events'!
  76. off: aString unbind: aBlock
  77. self removeEventListener: aString block: aBlock useCapture: false
  78. !
  79. on: aString bind: aBlock
  80. self addEventListener: aString block: aBlock useCapture: false
  81. ! !
  82. !Domite methodsFor: 'initialization'!
  83. initialize
  84. super initialize.
  85. element := document body.
  86. reference := nil asJSON
  87. ! !
  88. !Domite methodsFor: 'insertion'!
  89. nextPut: anObject
  90. self nextPutString: anObject printString
  91. !
  92. nextPutDomNode: aDomElement
  93. self element
  94. insertBefore: aDomElement
  95. reference: self reference
  96. !
  97. nextPutString: aString
  98. self nextPutDomNode: (
  99. document createTextNode: aString asString )
  100. ! !
  101. !Domite methodsFor: 'positioning'!
  102. reset
  103. self reference: self element firstChild
  104. !
  105. setToAfter
  106. self
  107. reference: self element nextSibling;
  108. element: self element parentNode
  109. !
  110. setToBefore
  111. self
  112. reference: self element;
  113. element: self element parentNode
  114. !
  115. setToEnd
  116. self reference: nil asJSON "null"
  117. ! !
  118. !Domite methodsFor: 'streaming'!
  119. putOn: aStream
  120. aStream nextPutDomNode: self element
  121. ! !
  122. !Domite methodsFor: 'testing'!
  123. atEnd
  124. ^ self reference isNil
  125. !
  126. atStart
  127. ^ self reference = self element firstChild
  128. !
  129. canSetToUpperLevel
  130. ^ self element parentNode notNil
  131. !
  132. isInvalid
  133. ^ self element isNil
  134. ! !
  135. !Domite class methodsFor: 'instance creation'!
  136. at: aString
  137. ^ self fromElement: (document querySelector: aString)
  138. !
  139. fromElement: aDomElement
  140. ^ self new
  141. element: aDomElement;
  142. yourself
  143. !
  144. fromElement: aDomElement cursorBefore: anotherDomElement
  145. ^ self new
  146. element: aDomElement;
  147. referenceElement: anotherDomElement;
  148. yourself
  149. !
  150. newElement: aString
  151. ^ self fromElement: (document createElement: aString)
  152. !
  153. newStream
  154. ^ self fromElement: document createDocumentFragment
  155. ! !
  156. !ProtoStream methodsFor: '*DOMite'!
  157. nextPutDomNode: aNode
  158. self nextPut: aNode
  159. ! !