by austincheney · 4y ago · 0 comments
I love the DOM but most developers would rather jump off a bridge than write some logic to walk a DOM. I have never understood why there is so much fear about the DOM, why that fear is so astonishingly prevalent, or the extremes people will go to in order to sidestep this phobia. So curious because this is a subject that takes about 2 hours to learn after which your career takes a different trajectory.
You can see this most clearly in DOM 1.0: https://www.w3.org/TR/1998/REC-DOM-Level-1-19981001/level-on... Look particularly at the "IDL specifications". That's Java, not Javascript!
Later versions have improved various elements of this, but the fundamental architecture is still there and still not looking much like what I would write down if I wanted to write out a similar system natively for Javascript, assuming this was still the 1990s and that meant I could also modify JS itself a bit.
When you learn how to work around these impedance mismatches you can do some good work, but the resulting code may not make much sense to your coworker. Fortunately, even in native JS nowadays, .querySelector and similar APIs make it unnecessary to have to "walk" the DOM much anymore.
I'd even argue you could cite the existence of a triple mismatch, in that the original DOM level 0, supported in Netscape 2.0, entirely precedes the idea of a writable DOM. (It wasn't until Netscape 4 that we got "layers", which were useless, and it was IE 4 that finally gave us manipulable DOMs as we know them today. [1]) So the standard that was originally written with the concept of introspection and largely read-only access (you could only write things that the browser could guarantee wouldn't require a reflow, and not necessarily all of those) got a lot of write access bodge onto it later.
Historically, it's a huge mess.
[1]: They didn't necessarily work the way we expect today for much longer than that, I crashed IE 4 a lot with stuff we'd consider trivial today, but it was the first browser to at least try presenting everything in the DOM as editable.
* https://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/
* https://www.w3.org/TR/DOM-Level-3-Core/
The DOM is a language agnostic API written without any particular programming languages in mind and written to simultaneously and universally support both HTML and XML Schema.
These are stylistic and organizational concerns still not enough to qualify the irrational behaviors favoring jumping off a bridge. This is concerning considering learning to use the concepts takes substantially less time than learning the APIs of modern JavaScript frameworks developers favor. There remains far more to this that is psychologically focused regardless of any technology.
But if you just want to blame almost all developers everywhere across several decades for mysteriously being unwilling to engage with a simple and wonderful technology that has no flaws, feel free. It's easy, but it leads to no further understanding or wisdom.
- The only way to remove a node is node.parentNode.removeChild(node), until you get past IE11 then you can use Element.remove() https://developer.mozilla.org/en-US/docs/Web/API/Element/rem...
- Text node normalization - because there are separate representation of the exact same HTML https://developer.mozilla.org/en-US/docs/Web/API/Node/normal...
- NodeLists are almost but not quite arrays. They could also be live or static but you can't inspect it directly to tell which is which https://developer.mozilla.org/en-US/docs/Web/API/NodeList. Similar almost-array nonsense with HTMLCollection.
- Historically IE and Netscape/Firefox supported two completely different set of API and it was a horrible mess. You can still see remnants of that API war in functions that does almost but not quite the same things such as textContent vs. innerText https://developer.mozilla.org/en-US/docs/Web/API/Node/textCo...,
document.createTreeWalker()
is your steadfast friendAlso, "astonishingly prevalent"? Come on!
My daughter doesn’t like driving my manual transmission car and cannot figure it out. That doesn’t mean my car has an unstable API. She does mention the same sort of bullshit about blaming the technology for her own incompetence though.