Platform-DOM.st 1.4 KB

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