DOMite.st 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. ^ aDomite
  68. !
  69. insertElement: aDomElement
  70. self element
  71. insertBefore: aDomElement
  72. reference: self reference
  73. !
  74. insertString: aString
  75. self insertElement: (
  76. document createTextNode: aString asString )
  77. ! !
  78. !Domite methodsFor: 'navigation'!
  79. seekAfterHere
  80. self
  81. reference: self element nextSibling;
  82. element: self element parentNode
  83. !
  84. seekBeforeHere
  85. self
  86. reference: self element;
  87. element: self element parentNode
  88. !
  89. seekHereEnd
  90. self reference: nil asJSON "null"
  91. !
  92. seekHereStart
  93. self reference: self element firstChild
  94. ! !
  95. !Domite methodsFor: 'testing'!
  96. canSeekOutOfHere
  97. ^ self element parentNode notNil
  98. ! !
  99. !Domite class methodsFor: 'instance creation'!
  100. fromElement: aDomElement
  101. ^ self new
  102. element: aDomElement;
  103. yourself
  104. !
  105. fromElement: aDomElement cursorBefore: anotherDomElement
  106. ^ self new
  107. element: aDomElement;
  108. referenceElement: anotherDomElement;
  109. yourself
  110. !
  111. newElement: aString
  112. ^ self fromElement: (document createElement: aString)
  113. !
  114. open
  115. ^ self fromElement: document createDocumentFragment
  116. ! !