cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

The PTC Community email address has changed to community-mailer@ptc.com. Learn more.

Inserting A Comment Before The Root Element

ptc-953960
1-Newbie

Inserting A Comment Before The Root Element

Inserting a comment node before the root node is not as simple
as I thought it should be.

I devised a way and share it accordingly:

test document:


<mydoc>
<mytitle>Sample MyDoc</mytitle>
<mybody>

First
para


</mybody>
</mydoc>

test JavaScript:

//
// source D:/work/acl/cloning/commentTrial1.js
//

var globalObject = this;

(function(){
// create a testing namespace
globalObject["commentTrial1"] = {};
// use "cls" for convenience
cls = globalObject["commentTrial1"];
cls.identify = function(){
print("you have called commentTrial1.identify()");
}
cls.insertCommentBeforeRoot = function(text) {
var doc = Application.activeDocument;
// scrub to avoid interpolation of quotes
var scrubText = text.replace(/\"/,"\\\" );
var commentNode = doc.createComment(text);

// from
file:///C:/Program%20Files/Arbortext/Editor/docs/javadoc/aom/index.html
// getDocumentElement()
// This is a convenience attribute that allows direct access
// to the child node that is the root element of the document.
// For HTML documents, this is the element with the tagName
"HTML".
//var root = doc.getDocumentElement();
//root.insertBefore(commentNode);
var nl = doc.getChildNodes();
var root;
for (var i=0; i<nl.getlength(); i++)=" {<br="/> print(nl.item(i).getNodeType() + " " +
nl.item(i).getNodeValue());
// 1 = Element
// 8 = Comment
if (nl.item(i).getNodeType() == "1") {
root = nl.item(i);
print("found root element");
break;
}
}
var parentRoot = root.getParentNode();
//parentRoot.appendChild(commentNode); // places after
closing tag
parentRoot.insertBefore(commentNode, root);

}
}) ();
commentTrial1.insertCommentBeforeRoot("This comment should be
before the root tag;"
+ " created by commentTrial1.insertCommentBeforeRoot()");

--



Oracle Email Signature Logo
John Laurence Poole | Principal Software Engineer | 650.607.0853
Oracle User Assistance Engineering
M/S 2op1070
500 Oracle Parkway
Redwood Shores CA 94065-1677

Oracle Instant Chat: john.poole

The statements and opinions expressed here are my own and do
not necessarily represent those of Oracle Corporation.

1 REPLY 1

delete:

// scrub to avoid interpolation of quotes
var scrubText = text.replace(/\"/,"\\\" );

add at the end a 2nd test:

commentTrial1.insertCommentBeforeRoot("Here's a test of quotes: He
said,\"Hello World.\");

Top Tags