Platform-DOM.st 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. Smalltalk createPackage: 'Platform-DOM'!
  2. Object subclass: #PlatformDom
  3. instanceVariableNames: ''
  4. package: 'Platform-DOM'!
  5. !PlatformDom class methodsFor: 'converting'!
  6. toArray: aDomList
  7. <inlineJS: 'return Array.prototype.slice.call(aDomList)'>
  8. ! !
  9. !PlatformDom class methodsFor: 'creation'!
  10. newCustomEvent: aString detail: anObject
  11. <inlineJS: 'return new CustomEvent(aString, {detail: anObject})'>
  12. ! !
  13. !PlatformDom class methodsFor: 'testing'!
  14. isDomNode: anObject
  15. <inlineJS: '
  16. return anObject.nodeType > 0 &&
  17. Object.prototype.toString.call(anObject) !!== "[object Object]"
  18. '>
  19. !
  20. isFeasible
  21. <inlineJS: '
  22. if (typeof document === "undefined") return false;
  23. try {
  24. var d = document.createElement("div"),
  25. f = document.createDocumentFragment(),
  26. t = document.createTextNode("Hello, Amber!!");
  27. f.appendChild(t);
  28. d.insertBefore(f, null);
  29. return d.innerHTML === "Hello, Amber!!";
  30. } catch (e) {
  31. return false;
  32. }
  33. '>
  34. ! !
  35. !CharacterArray methodsFor: '*Platform-DOM'!
  36. asDomNode
  37. ^ document createTextNode: self asString
  38. ! !
  39. !Collection methodsFor: '*Platform-DOM'!
  40. asDomNode
  41. | fragment |
  42. fragment := document createDocumentFragment.
  43. self do: [ :each | fragment appendChild: each asDomNode ].
  44. ^ fragment
  45. ! !
  46. !JSObjectProxy methodsFor: '*Platform-DOM'!
  47. asDomNode
  48. (PlatformDom isDomNode: jsObject)
  49. ifTrue: [ ^ jsObject ]
  50. ifFalse: [ ^ super asDomNode ]
  51. ! !
  52. !String methodsFor: '*Platform-DOM'!
  53. htmlTextContent
  54. <inlineJS: 'var d=document.createElement("div");d.innerHTML=self;return d.textContent||d.innerText;'>
  55. ! !