Platform-DOM.st 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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: 'node creation'!
  10. newCustomEvent: aString detail: anObject
  11. <inlineJS: 'return new CustomEvent(aString, {detail: anObject})'>
  12. !
  13. newDocumentFragment
  14. <inlineJS: 'return document.createDocumentFragment()'>
  15. !
  16. newElement: aString
  17. <inlineJS: 'return document.createElement(aString)'>
  18. !
  19. newTextNode: aString
  20. <inlineJS: 'return document.createTextNode(aString)'>
  21. ! !
  22. !PlatformDom class methodsFor: 'testing'!
  23. isDomNode: anObject
  24. <inlineJS: '
  25. return anObject.nodeType > 0 &&
  26. Object.prototype.toString.call(anObject) !!== "[object Object]"
  27. '>
  28. !
  29. isFeasible
  30. <inlineJS: '
  31. if (typeof document === "undefined") return false;
  32. try {
  33. var d = document.createElement("div"),
  34. f = document.createDocumentFragment(),
  35. t = document.createTextNode("Hello, Amber!!");
  36. f.appendChild(t);
  37. d.insertBefore(f, null);
  38. return d.innerHTML === "Hello, Amber!!";
  39. } catch (e) {
  40. return false;
  41. }
  42. '>
  43. ! !
  44. !CharacterArray methodsFor: '*Platform-DOM'!
  45. asDomNode
  46. ^ PlatformDom newTextNode: self asString
  47. ! !
  48. !Collection methodsFor: '*Platform-DOM'!
  49. asDomNode
  50. | fragment |
  51. fragment := PlatformDom newDocumentFragment.
  52. self do: [ :each | fragment appendChild: each asDomNode ].
  53. ^ fragment
  54. ! !
  55. !JSObjectProxy methodsFor: '*Platform-DOM'!
  56. asDomNode
  57. (PlatformDom isDomNode: jsObject)
  58. ifTrue: [ ^ jsObject ]
  59. ifFalse: [ ^ super asDomNode ]
  60. ! !