aredridel / html5

Event-driven HTML5 Parser in Javascript
http://dinhe.net/~aredridel/projects/js/html5/
MIT License
590 stars 168 forks source link

Serialize outputs the doctype at the end / DOM Level2 Unavailable #29

Open stevesims opened 13 years ago

stevesims commented 13 years ago

If I try to serialize out a document, html5 is outputting the doctype at the end, rather than the beginning.

My simple-ish test-case code is:

var jsdom = require('jsdom');
var HTML5 = require('html5');
var window = jsdom.jsdom(null, null, {parser: HTML5, features: { QuerySelector: true }}).createWindow();
var parser = new HTML5.Parser({document: window.document});
parser.parse("<!DOCTYPE html><html><head></head><body></body></html>");
console.log(HTML5.serialize(window.document))

What I see outputted to console is:

<html><head></head><body></body></html><!DOCTYPE html>
aredridel commented 13 years ago

Alright. I'll see what I can do.

aredridel commented 13 years ago

Ah, you're getting a DOM Level 1 from JSDOM -- Level 2 is required to get doctypes right, I think.

aredridel commented 13 years ago

What I end up getting is TypeError: Object #<Object> has no method 'createDocumentType' running your code.

I'm working with @tmpvar and @isaacs to get npm and/or jsdom to export the level 2 stuff more easily to fix this properly.

stevesims commented 13 years ago

I must admit that I struggled quite a bit to get html5 up and running - I've been doing browser-based JS for years, but am fairly new to node.js. It took me a while to figure out that I needed to set up a NODE_PATH environment variable to point to places where npm was installing things, otherwise html5 was just refusing to see jsdom at all. (Getting document.querySelector was important for me - I only managed to work that out by searching through jsdom's test code...) Chances are I've not got things set up quite "right", which probably explains why we're seeing differing results.

For now I've worked around this problem by using a regex to move the doctype from the end of the outputted HTML to the beginning.

I'll keep an eye out for updates - would be nice to remove my bodge. :-)

aredridel commented 13 years ago

Sweet. I'll keep working on the integration issues. It's mostly about getting jsdom's API to be more friendly to use via npm. I'll see about that!

tmpvar commented 13 years ago

yeah, to be honest jsdom is pretty hosed on my local machine. I'll spend some time getting back to "a good place" this weekend

aredridel commented 13 years ago

Awesome! Poke me by twitter if you want help testing anything.