I need a functions to create a DOM from a HTML document. I am experience in JavaScript but I cannot figure this out. Here is a skeleton of functions DreamweaverMX uses to create XML documents. Macromedia gives extension developers permission to use their functions and methods but I do think this will work with HTML files. Here is the functions and beneath them there are more details listing exactly what I need.
XMLDoc:
function XMLDoc(source, errFn) {
this.topNode = null;
this.errFn = errFn;
this.createXMLNode = _XMLDoc_createXMLNode;
this.error = _XMLDoc_error;
this.getUnderlyingXMLText = _XMLDoc_getUnderlyingXMLTe
xt;
this.handleNode = _XMLDoc_handleNode;
this.hasErrors = false;
this.insertNodeAfter = _XMLDoc_insertNodeAfter;
this.insertNodeInto = _XMLDoc_insertNodeInto;
this.parse = _XMLDoc_parse;
this.parseAttribute = _XMLDoc_parseAttribute;
this.parseDTD = _XMLDoc_parseDTD;
this.parsePI = _XMLDoc_parsePI;
this.parseTag = _XMLDoc_parseTag;
this.removeNodeFromTree = _XMLDoc_removeNodeFromTree
;
this.replaceNodeContents = _XMLDoc_replaceNodeContent
s;
this.source = source;
if (this.parse()) {
if (this.topNode != null) {
return this.error("expected close " + this.topNode.tagName);
} else {
return true;
}
}
return null;
}
_XMLDoc_parse:
function _XMLDoc_parse() {
var pos = 0;
err = false;
while (!err) {
var closing_tag_prefix = "";
var chpos = this.source.indexOf("<", pos);
var open_length = 1;
var open;
var close;
if (chpos == -1) {
break;
}
open = chpos;
var str = this.source.substring(pos,
open);
if (str.length != 0) {
err = !this.handleNode(new XMLNode("TEXT", this, str));
}
if (chpos == this.source.indexOf("<?", pos)) {
pos = this.parsePI(this.source, pos + 2);
if (pos == 0) {
err = true;
}
continue;
}
if (chpos == this.source.indexOf("<!DOC
TYPE", pos)) {
pos = this.parseDTD(this.source,
chpos + 9);
if (pos == 0) {
err = true;
}
continue;
}
if (chpos == this.source.indexOf("<!--"
, pos)) {
open_length = 4;
closing_tag_prefix = "--";
}
if (chpos == this.source.indexOf("<![CD
ATA[", pos)) {
open_length = 9;
closing_tag_prefix = "]]";
}
chpos = this.source.indexOf(closin
g_tag_pref
ix + ">", chpos);
if (chpos == -1) {
return this.error("expected closing tag sequence: " + closing_tag_prefix + ">");
}
close = chpos + closing_tag_prefix.length;
str = this.source.substring(open
+ 1, close);
var n = this.parseTag(str);
if (n) {
err = !this.handleNode(n);
}
pos = close + 1;
}
return !err;
}
_XMLDoc_handleNode:
function _XMLDoc_handleNode(current
) {
if ((current.nodeType == "COMMENT") && (this.topNode != null)) {
return this.topNode.addElement(cu
rrent);
} else {
if ((current.nodeType == "TEXT") || (current.nodeType == "CDATA")) {
if (this.topNode == null) {
if (trim(current.content, true, false) == "") {
return true;
} else {
return this.error("expected document node, found: " + current);
}
} else {
return this.topNode.addElement(cu
rrent);
}
} else {
if ((current.nodeType == "OPEN") || (current.nodeType == "SINGLE")) {
var success = false;
if (this.topNode == null) {
this.docNode = current;
current.parent = null;
success = true;
} else {
success = this.topNode.addElement(cu
rrent);
}
if (success && (current.nodeType != "SINGLE")) {
this.topNode = current;
}
current.nodeType = "ELEMENT";
return success;
} else {
if (current.nodeType == "CLOSE") {
if (this.topNode == null) {
return this.error("close tag without open: " + current.toString());
} else {
if (current.tagName != this.topNode.tagName) {
return this.error("expected closing " + this.topNode.tagName + ", found closing " + current.tagName);
} else {
this.topNode = this.topNode.getParent();
}
}
}
}
}
}
return true;
}
I have only included three main functions. The above is only used for a reference. I don't think it will work by it's self nor do I need all the extra functions it seems to have. Only the functions listed below.
I need to be able to do this:
var theDOM = new HTMLDOM(strHTML); // this builds a html document object model from a html document in a string.
var theNode = theDOM.getElementByID("sna
"); // This gets the node by it's ID. eg, "<td id='sna'>some text</td>" returns a node of this td tag;
var theNode = theDOM.getNodeByIDorName("
NameOrID")
;
var theParent = theNode.parentNode; // if tag was in
var intImgWidth = theNode.getAttribute('widt
h');
theNode.setAttribute("alig
n",curAlig
n); // sets tag attribute
theNode.removeAttribute("a
lign",curA
lign); // removes tag attribute
var tagName = theNode.tagName; // gets tagName. eg "<img src='fds.gif'>" returns "img"
var innerHTML = theNode.innerHTML(); // returns everything inside a tag. eg "<div>some text</div>" returns "some text"
var outerHTML = theNode.outerHTML(); // the same as above but with the tags included
theNode.innerHTML = "<div style='width:135'>some text</div>"; // this would set the innerHTML property
var sourceHTML = theDOM.source; // returns the whole document in the form of a string.
I dont know if this is a lot of work for an actual educated programmer. I am self-educated. If you build this from scratch and then decide to sell it then go for it. All I want is a licensed copy to include my project I dont care what you do with it. I can send you all the functions that go with this script in a separate email. You can also enumerate them in the DMX enviornment. You can do this by typing dw.getDocumentDOM().parent
Window in my script console extension I've built. Ask for it and I will send it to you. It is requires Dreamweaver MX.
Omicron Persei 8