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

Adding PIs to extracted content

Highlighted
Newbie

Adding PIs to extracted content

Hi all,

I am modifying some code and it just isn't wanting to do what I am it to do 🙂 so probably user error!

The code is in jscript here is a small outline of the code:

doc = Application.activeDocument;
sel = doc.textSelection;
content = sel.extractContents();

// does some modifying of an attribute
// I would then like to add a processing instruction to the beginning and end of the selection.

// stick modified selection back in place
sel.insertNode(content);

My problem is that I have tried a few things to add the processing instruction and I can't get it to work. I found the method createProcessingInstruction( target, data) , but I think I am calling it wrong, or I don't have the correct insertBefore/After...

Any ideas?

Thanks,
Ellen

Tags (2)
6 REPLIES 6
Highlighted

Adding PIs to extracted content

Hi Ellen-



Can you give a little more detail? What is the code you're trying to
use to create the PI's and to insert them? What is the result of each
operation-what error messages are you receiving, and where do they
occur?



--Clay




Highlighted

RE: Adding PIs to extracted content

Here is more of the code:

// get a reference to the parent document
doc = Application.activeDocument;
sel = doc.textSelection;

// pull the selection out
content = sel.extractContents();

kids = content.childNodes;
// stick modified selection back in place
var piStart = Application.activeDocument.createProcessingInstruction('Start', 'type="+String+");
var piEnd = Application.activeDocument.createProcessingInstruction('End', 'type="+String+");

Application.activeDocument.insertBefore(piStart, kids.item(0));
Application.activeDocument.insertAfter(piEnd, kids.item(i-1));
sel.insertNode(content);

I got a little farther once I decided that the document fragment that I was extracting too was now the activeDocument. I least I think that is correct. The insertBefore seems to be giving me the error "DOM exception:node not found in current context."

I think I am having problems with my thinking about the "nodes" and "objects".

Thanks, Ellen

Highlighted

Adding PIs to extracted content

Hi Ellen-



OK, I think your problem is that you are using extractContents(), which
removes the selection from the document. That's why you're getting the
"node not found" error.



There are a couple of ways around this:



1) You can stop extracting the contents at all, but modify them in
place, something like this:



doc = Application.activeDocument;

sel = doc.textSelection;

// keep your current pi definitions...

sel.insertNode(piStart);

sel.collapse(false); // set insertion point to end of selection

sel.insertNode(piEnd);



2) You can extract the contents, but then you have to modify the
documentFragment created thereby, something like this:



doc = Application.activeDocument;

sel = doc.textSelection;

content = sel.extractContents();

// keep your PI definitions...

content.insertBefore(piStart,content.firstChild);

content.appendChild(piEnd);

sel.insertNode(content);



Either of those should work fine.



--Clay




Highlighted

RE: Adding PIs to extracted content

When I added:

var rootFragDoc = Application.activeDocument.documentElement;
rootFragDoc.insertBefore(piStart);

This put a PI in the large document not the extracted content, and at the end of the document right before the root end tag.

In Reply to Ellen Headrick:

Here is more of the code:

// get a reference to the parent document
doc = Application.activeDocument;
sel = doc.textSelection;

// pull the selection out
content = sel.extractContents();

kids = content.childNodes;
// stick modified selection back in place
var piStart = Application.activeDocument.createProcessingInstruction('Start', 'type="+String+");
var piEnd = Application.activeDocument.createProcessingInstruction('End', 'type="+String+");

Application.activeDocument.insertBefore(piStart, kids.item(0));
Application.activeDocument.insertAfter(piEnd, kids.item(i-1));
sel.insertNode(content);

I got a little farther once I decided that the document fragment that I was extracting too was now the activeDocument. I least I think that is correct. The insertBefore seems to be giving me the error "DOM exception:node not found in current context."

I think I am having problems with my thinking about the "nodes" and "objects".

Thanks, Ellen

Highlighted

Adding PIs to extracted content

You're welcome. 🙂



The DOM model for DocumentFragments and Ranges can be maddening--I've
been there myself many times--so I'm glad to help ease someone else's
pain.


Highlighted

RE: Adding PIs to extracted content

Clay,

THANK YOU, I owe you dinner when you come to Colorado!

Thanks,
Ellen

Announcements