diff --git a/dom.d b/dom.d index e14b170..bd93f51 100644 --- a/dom.d +++ b/dom.d @@ -1,3 +1,23 @@ +/** + This is an html DOM implementation, started with cloning + what the browser offers in Javascript, but going well beyond + it in convenience. + + If you can do it in Javascript, you can probably do it with + this module. + + And much more. + + + Note: some of the documentation here writes html with added + spaces. That's because ddoc doesn't bother encoding html output, + and adding spaces is easier than using LT macros everywhere. + + + BTW: this file depends on arsd.characterencodings, so help it + correctly read files from the internet. You should be able to + get characterencodings.d from the same place you got this file. +*/ module arsd.dom; // NOTE: do *NOT* override toString on Element subclasses. It won't work. @@ -16,6 +36,7 @@ import std.string; import std.exception; import std.uri; import std.array; +import std.range; import std.stdio; @@ -28,8 +49,8 @@ import std.stdio; /// Document implements this interface with type = text/html (see Document.contentType for more info) /// and data = document.toString, so you can return Documents anywhere web.d expects FileResources. interface FileResource { - string contentType() const; - immutable(ubyte)[] getData() const; + string contentType() const; /// the content-type of the file. e.g. "text/html; charset=utf-8" or "image/png" + immutable(ubyte)[] getData() const; /// the data } @@ -166,6 +187,8 @@ body { /// This represents almost everything in the DOM. class Element { + // putting all the members up front + // this ought to be private. don't use it directly. Element[] children; @@ -186,6 +209,27 @@ class Element { ///. Element parentNode; + // the next few methods are for implementing interactive kind of things + private CssStyle _computedStyle; + + // these are here for event handlers. Don't forget that this library never fires events. + // (I'm thinking about putting this in a version statement so you don't have the baggage. The instance size of this class is 56 bytes right now.) + EventHandler[][string] bubblingEventHandlers; + EventHandler[][string] capturingEventHandlers; + EventHandler[string] defaultEventHandlers; + + void addEventListener(string event, EventHandler handler, bool useCapture = false) { + if(event.length > 2 && event[0..2] == "on") + event = event[2 .. $]; + + if(useCapture) + capturingEventHandlers[event] ~= handler; + else + bubblingEventHandlers[event] ~= handler; + } + + + // and now methods /// Convenience function to try to do the right thing for HTML. This is the main /// way I create elements. @@ -763,8 +807,6 @@ class Element { return _computedStyle; } - private CssStyle _computedStyle; - /// These properties are useless in most cases, but if you write a layout engine on top of this lib, they may be good version(browser) { void* expansionHook; ///ditto @@ -1230,7 +1272,7 @@ class Element { /// This sets the inner content of the element *without* trying to parse it. /// You can inject any code in there; this serves as an escape hatch from the dom. /// - /// The only times you might actually need it are for