michael-brade / LaTeX.js

JavaScript LaTeX to HTML5 translator
https://latex.js.org
MIT License
770 stars 58 forks source link

global variables "window" and "document" defined in html-generator.ls come from svgdom? #74

Closed seuliang closed 5 years ago

seuliang commented 5 years ago

form code el = document.createElement type in html-generator.ls , i realized that the dom definition used here is from svgdom, is it right ?
i want to know does traditional javascript dom api work here?

michael-brade commented 5 years ago

Yes, it does. Because here https://github.com/michael-brade/LaTeX.js/blob/1f70d80d961d1fe7d62fbb8497f71c79702df8a7/src/html-generator.ls#L17-L19 you can see that svgdom is only used if the traditional DOM is not available, i.e., we are not running in a browser.

seuliang commented 5 years ago

Thank you.
but there is no buildin variable named "global" in JS, how to check "global.window"?

michael-brade commented 5 years ago

Well, you are right - partly. NodeJS has global, browsers have window. And the new thing is globalThis. I can use global here without browsers complaining because rollup takes the NodeJS code and transforms it into browser-compatible code. It effectively becomes:

var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};