createElement.ts 975 B

12345678910111213141516171819202122232425262728293031323334
  1. /**
  2. * createElement
  3. * Edited from:
  4. * @link https://stackoverflow.com/a/42405694
  5. */
  6. function createElement(tag, attrs, children) {
  7. var element = document.createElement(tag);
  8. for (let name in attrs) {
  9. if (name && attrs.hasOwnProperty(name)) {
  10. let value = attrs[name];
  11. if (name == "dangerouslySetInnerHTML") {
  12. element.innerHTML = value.__html;
  13. }
  14. else if (value === true) {
  15. element.setAttribute(name, name);
  16. } else if (value !== false && value != null) {
  17. element.setAttribute(name, value.toString());
  18. }
  19. }
  20. }
  21. for (let i = 2; i < arguments.length; i++) {
  22. let child = arguments[i];
  23. if (child) {
  24. element.appendChild(
  25. child.nodeType == null ?
  26. document.createTextNode(child.toString()) : child);
  27. }
  28. }
  29. return element;
  30. }
  31. export default createElement;