WebKit Objective-C DOM library conformance

A burning question:

What does the Cocoa/Objective-C -based DOM implementation included with WebKit claim to support as far as the DOM Level 2 and 3 specifications are concerned?

Many web developers are familiar with checking for a browser’s supported DOM features in JavaScript via a DOM Document object. For example, the following line will tell you if your browser supports the optional CSS Module of the DOM Level 2 spec:


alert(document.hasFeature("CSS", "2.0"));

The hasFeature method is itself part of the DOM Level 1 (language-independent) Core standard… specifically it is a method of the DOMImplementation interface (that browser document objects generally implement). So you’re pretty much assured that this method will exist in any DOM implementation in any language (or else you’re dealing with a pretty shitty DOM implementation and should switch to another).

Anyhow, enough exposition… while searching desperately for a C, C++, or Objective-C-based CSS Parser for an upcoming Cocoa project (details to come) it occurred to me that WebKit’s (as-yet-undocumented) Objective-C DOM implementation (available since Tiger) had at least some support for many of the optional DOM Level 2 Modules. These are the StyleSheet, CSS, Range, Traversal, Events… etc.

Perhaps WebKit supports the StyleSheet and CSS Modules and I could use those APIs to achieve some of my goals using pure Objective-C/Cocoa? That would be cool!

So I set about to see what WebKit’s DOM implementation claims to support… here’s what I found:


NSString *Level1 = @"1.0";
NSString *Level2 = @"2.0";
NSString *Level3 = @"3.0";

DOMDocument *doc = [[webView mainFrame] DOMDocument];
DOMImplementation *impl = [doc implementation];

NSArray *DOM1Features  = [NSArray arrayWithObjects:
    @"Core", @"HTML", nil];

NSArray *DOM2Features = [NSArray arrayWithObjects:
    @"Core", @"XML", @"HTML", @"Views", @"CSS", @"CSS2", @"Traversal",
    @"Range", @"Events", @"StyleSheets", @"UIEvents", @"MutationEvents",
    @"MouseEvents", @"HTMLEvents", nil];

NSArray *DOM3Features = [NSArray arrayWithObjects:
    @"Core", @"L&S", @"Validation", nil];    

NSString *feature;
NSEnumerator *e = [DOM1Features objectEnumerator];
while (feature = [e nextObject]) {
    NSLog(@"%@ %@, %i", feature, Level1, [impl hasFeature:feature :Level1]);
}

e = [DOM2Features objectEnumerator];
while (feature = [e nextObject]) {
    NSLog(@"%@ %@, %i", feature, Level2, [impl hasFeature:feature :Level2]);
}

e = [DOM3Features objectEnumerator];
while (feature = [e nextObject]) {
    NSLog(@"%@ %@, %i", feature, Level3, [impl hasFeature:feature :Level3]);
}

Even if you don’t know Objective-C, the code above is pretty straightforward. You should be able to see that I am looping through each DOM Module in each DOM Level (version) and testing the WebKit’s implementation for support of that Module/Level.

Here’s the result:


          Core 1.0, 1
          HTML 1.0, 1
          Core 2.0, 1
           XML 2.0, 1
          HTML 2.0, 1
         Views 2.0, 1
           CSS 2.0, 1
          CSS2 2.0, 1
     Traversal 2.0, 1
         Range 2.0, 1
        Events 2.0, 1
   StyleSheets 2.0, 1
      UIEvents 2.0, 1
MutationEvents 2.0, 1
   MouseEvents 2.0, 1
    HTMLEvents 2.0, 1
          Core 3.0, 0
           L&S 3.0, 0
    Validation 3.0, 0

Interesting. WebKit’s Objective-C DOM library claims to support all of DOM Levels 1 and 2. I haven’t dug through all of the headers… but it looks like all of the pieces are at least in place. Whether they are all fully functional and relatively solid is probably a little less sure. Some of those modules are pretty obscure (has anyone ever used MutationEvents?)!

Anyway.. I’ve been exploring SytleSheets and CSS for about an hour now, and so far so good… seems solid!


About this entry