The Importance of the DOM
Join the DZone community and get the full member experience.Join For Free
choosing the dom
this article looks a little deeper into the importance of the dom as a intermediary, and points out some of the strengths and weaknesses of it. the dom , of course, is the document object model within a browser, that models the content and layout of a page.
uis via rendering/layout engines and the dom
rendering/layout engines (wikipedia links: gecko for firefox , webkit for safari and chrome , trident for ie ) traverse the dom , and and processes elements they understand. the aim is to position, represent and render elements according to what it knows inline, or from related css . i will conveniently ignore html5 canvas for now. there is a handy comparison on wikipedia of capabilities of the layout engines.
the rendering/layout engine can only process what it knows. i previously talked of glass-ceilings and used the lack of a ‘image drop-down’ as an example of that . in that case “image drop-down” may be easy for humans to understand but the rendering engine needs it represented in the dom as a series of regular <div> <ul> <li> <span> items, that are unlike the <select> that is canonical for ‘drop-down’. it is like that for every advanced effect, i suggest. thus to push ahead in terms of capability, the rendering/layout engine needs to be enhanced. there are four main browsers now, meaning that a committee needs to decide what new affortdnces and capabilities the dom can encode, and that needs to be rendered and interacted with.
by design, the dom and browsers pertain to presentation and interaction only. there is a server part of the application, that is most likely handling business logic, and persistence. the server and browser speak http to each other of course, and therefore the overloaded term thin client applies to this class of application. another “thin” aspect of this type of application is the lazy way that additional “pages” of the application are retrieved at the time of first use. the whole application is delivered incrementally to the browser, while in use, leading to a hugely important faster start.
the dom is an intermediary between code and the renderer
the dom , as an intermediate layer, is two-way. you can program it at any stage, including sending it more html . it can also be queried to retrieve html or specific attributes. there a lot of pros to this, and some cons (as i’ll outline later).
all html -centric web technologies mutate the dom
cappuccino is about as advanced as you can get for user-experience that works without plugins. here’s their floor-plan example showing the dom in the style of a html app:
note that it also uses canvas tactically. the bed in the circle in that screenshot is via canvas.
scalable vector graphics ( svg )
svg , in firebug, does represent itself as a set of elements that are expandable. as such, it uses a scene-graph rather than a bitmap/buffer to represent its details.
uis rendered by visiting code (and recursing into increasingly small rectangles)
generally speaking most non- dom graphics apis deal with the leasing of rectangles of pixels from a parent container (browser). you can see the relevant node in the dom , but can’t see specifics. this class of ui technology, is certainly thicker than the thin-client class above.
these technologies typically have a ‘paint’ or ‘drawrect’ (or similar) method/function for a rectangle, that when invoked will repaint itself to a graphics plane. often there are threading restrictions for repainting or changing variable state that would cause a repaint. a repaint of everything would be a call into the outer-most instances first (that paint method), and each of those could call into many other paint() methods that are part of the composite makeup of that rectangle. for example a textfield could have a border and the area you type into. this is decomposed into at least two sub-components, meaning two further delegations to ‘paint’. object-orientated inheritance can, sadly, play a part too. the paint() method/function could be overridden, and may not deliberately call the parent’s paint() as part of its invocation. it’s easy to make a performance mess this way. do any i/o during one of those paint() invocations (or just code badly) and your finished product is going to suck. this can also mean there are deep invocation stacks during rendering, and implicitly a lot of code execution. since computers became hand-held, the trade-off between economic graphics systems, and fastest cpus has been constantly revisited. this is true for browser-plugin based versions of the technology where applicable, and for the desktop incarnations. the only difference is where the rectangle is leased from (browser or os), and what constraints are placed upon the app (security managers, sandboxes, reduced apis).
there is a key element of this category of renderer/layout engine: there is no intermediary between code and the renderer, they are intermingled.
flash leases a rectangle in a viewport of the browser via an <object> element, and renders it’s own stuff in there. it is mostly known for it’s in-browser capability, but can also run standalone on a desktop. within the leased rectangle, smaller and smaller rectangles are leased by code pertains to increasingly specialized components. flex/actionscript: the has a paint() style function at the base of graphics widgets. there is some xml to make the experience more designer friendly too. apple led an informal campaign to sunset flash usage when the iphone was launched without the ability to run it’s apps.
sun/oracle’s swing (1997 and onwards) is another fatter ui technology. the bad: unnecessarily deep stacks. the good: advanced layers, huge control, customizability and alpha-channel stuff. when it was first released performance was a real problem because of slower cpus had then. certainly no phone-level chip could run it until 2001. later, sun tried to push ahead with javafx but to my mind only confused the message. since we’re hyping the importance of the dom , and debugging tools like firebug presently, there is an excellent tool called swingexplorer that acts like firebug for a running swing app. that allows navigation of the object model for the swing containers/components, which approximates to the dom , but without the ‘document’ aspect as it’s a live object model (directed graph). the downside is that it has to be programmatically inserted, because the java applet plugin for browsers, doesn’t have a further plugin for the likes of firebug (but could easily).
oracle’s swing also works as a desktop ui technology. take a look at intellij idea to see how sophisticated swing can get on the desktop.
google’s android, though java, doesn’t use swing. licensing more than performance must have been the reasons in 2007. there is a way to embed webkit in these java applications (and gain the doms benefits), and it’s increasingly the case that developers do that. there is no android browser-plugin to compete with the sun/oracle java plugin for regular browsers though. that seems like a missed opportunity. kevin hickey (a colleague) goes into a broad elaboration of that android environment .
microsoft’s .net bits and pieces
silverlight was, the cross platform rectangle-leasing technology that microsoft touted as a flash and swing-applet replacement, for ria rectangles in web pages. it is not any more, as it appears that microsoft are shifting towards more dom -like designs going forward. the windows 8 metro interface is a clue to that. xaml is the technology foundation, and xaml browser applications ( xbap ) is allegedly the way to go for cross-browser rich internet applications using this technology. there’s not a lot of recent examples though. colleague david nelson has promised a follow-up on that.
- no dom intermediate
- yes, uses leased rectangles
- actual code participates in paint/repaint
there was a technology called display postscript that flourished in the next days. the technology could have had a dom , in the style of the html /browser one, but did not. one of two ways to program the view, was via snippets of postscript, but it was a one-way trip. you could feel the graphics subsystem snippets of postscript, but you could not navigate a dom to retrieve textual postscript nodes/properties for it. postscript was fiendishly hard to program anyway.
in your attempts to make your ria , it’s not hard to encounter a feature/design that exceeds what the html and the dom is ordinarily capable of. i’ve wrote about that glass ceiling before. i did again a few days ago, to show the dom tricks necessary to break through it . that you avoided 20 days of work, for a 1 day problem (were the technology more capable), is a relief to all concerned. the risk remains though that during the build-out of a sophisticated ria using the dom , that you’ll encounter something really hard, that should have been simple.
this was the “con” i was alluding to earlier.
cut & paste
html is very cut and paste-able. if i take the super image drop-down thing i’ve used in that “glass ceiling revisited” article, and select a fair bit more than that in the browser, i can paste that into a number of places that recognize html in the clipboard. gmail’s new-email page is one such place. here it is after i have pasted that into chrome:
it looks the same for gmail in a safari browser. when i hit send, and check how that appears when it arrives somewhere, it’s messed up a little (and shows the behind the scenes trick that marghoob suleman’s jquery extension did), but it is still html in the email. that could easily improve as browsers improve.
microsoft word tries to accept the html , but does not do too well. worse again are openoffice and apple pages.
the point is that i can only do this when there is something like the dom involved. all the widgets are in together in one document. in this case all the text was placed on the page using the same canonical text rendering aspect of webkit. when i placed the caret with the mouse and slid to perform selection, the clipboard was filled with something meaningful. for the other type of rendering strategy (visiting ‘paint’ methods recursively), the best you can hope for is a bitmap to represent what was copied. today, i don’t know of a fat-client ui technology that allows you to select a field label like “created from html select element” and the contents of the input-field in one go.
you can print pages, and there is a fair change that they have been coded to have a layout that’s pertinent to printing as well as display. the same layout engine kicks in, but makes different choices as to who to represent, and how to represent it.
with the dom , assistive technologies have a much better chance of representing an interface to a sight-impaired user. that said, i’m guessing the more apparently spurious <div> elements there are, the more confusing that gets. representation could be via an audio facility, or via via refreshable braille displays . assistive technologies are much harder for non- dom designs.
the dom , a dom ?
the dom , as it has evolved over the years, is a very powerful technology, but it doesn’t remove the need for more native graphics programming apis. especially while there are glass-ceilings, and no easy ability to extend it. more native graphics technologies could adopt some of the same “document object model” designs (display postscript was close 15 years ago). webos was close a few years ago, but only insofar as it expanded the dom we know into a whole platform api .
thanks to jeff wishnie, kevin hickey and david nelson for contributions :)
Published at DZone with permission of Paul Hammant, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.