I need to import a variety of outlines, marked up in varying ways --
XOXO, HTML, XHTML, etc., without knowing what my app is receiving
before it arrives. I use the Ajax object to access the content, and
the text of the document always appears in the responseText field of
the Ajax.Response. (If it happens to be XML, there's a Document in
Ajax.Response.responseXML as well, but I can't depend on that since
the outlines aren't always XML.) I do _not_ want to import the
responseText into the main Document -- it must be heavily processed.
I have written code (see below), which works, but seems to be rather
clunky; in particular the lines:
// turn tag soup into something searchable
respDoc = document.createDocumentFragment();
var respRoot = document.createElement("foo");
respDoc.appendChild(respRoot);
respRoot.innerHTML = ajaxResponse.responseText;
Is there some clean way to turn ajaxResponse.responseText into a tree
of DOM nodes (or better yet, DOM nodes and extended Elements), that
strips out any JavaScript?
Here's my code:
// helper function
// listRoots is an Array
function findListRoots(parentNode, listRoots) {
var children, c, child, nodeNameL;
children = parentNode.childNodes;
for (c=0; c<children.length; ++c) {
child = children[c];
if (child.nodeType === Node.ELEMENT_NODE) {
nodeNameL = child.nodeName.toLowerCase();
if (nodeNameL === "ul" || nodeNameL === "ol") {
listRoots.push(child);
} else {
findListRoots(child, listRoots);
}
}
}
}
// the onSuccess function for Ajax
function respondImportXOXO(ajaxResponse) {
var respDoc;
var outlineRoots = [];
var lists, i, listElem, className;
var h1Elem;
// turn tag soup into something searchable
respDoc = document.createDocumentFragment();
var respRoot = document.createElement("foo");
respDoc.appendChild(respRoot);
respRoot.innerHTML = ajaxResponse.responseText;
// finds title or h1 element
var projectTitle;
if (typeof respDoc.title === "string" && respDoc.title.length > 0) {
projectTitle = respDoc.title;
} else {
respDoc = respRoot;
h1Elem = respDoc.getElementsByTagName("h1")[0];
if (h1Elem !== undefined) {
projectTitle = h1Elem.textContent;
} else {
projectTitle = "My Project";
}
}
log("projectTitle: \"" + projectTitle + "\"");
respDoc = respRoot;
// finds outline roots
lists = respDoc.getElementsByTagName("ul");
for (i=0; i<lists.length; ++i) {
listElem = lists[i];
log(listElem);
className = listElem.getAttribute("class");
if (typeof className === "string") {
if (className.indexOf("xoxo") >= 0) { // match
outlineRoots.push(listElem);
log ("outline root (XOXO)");
}
}
}
lists = respDoc.getElementsByTagName("ol");
for (i=0; i<lists.length; ++i) {
listElem = lists[i];
log(listElem);
className = listElem.getAttribute("class");
if (typeof className === "string") {
if (className.indexOf("xoxo") >= 0) { // match
outlineRoots.push(listElem);
log ("outline root (XOXO)");
}
}
}
// if no outline roots defined by class, use all lowest-level lists
if (outlineRoots.length === 0) {
findListRoots(respDoc, outlineRoots)
}
// import each outline root
log(outlineRoots.length + " outline roots");
for (i=0; i<outlineRoots.length; ++i) {
try {
// process outlineRoots[i]
} catch (error) {
log(error);
if (typeof error.message === "string") {
alert(error.message);
} else {
//alert(Object.toJSON(error));
alert(error);
}
}
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Prototype & script.aculo.us" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---