Loading...

prototype-scriptaculous@googlegroups.com

[Prev] Thread [Next]  |  [Prev] Date [Next]

[Proto-Scripty] Re: FInd and iterate over ProcessingInstruction nodes? T.J. Crowder Thu Apr 28 03:00:19 2011

Walter,

Unfortunately, I think you're out of luck, because I don't think
browsers retain processing instructions in the DOM. (Obviously if you
were dealing with the original XML, that would be different.) At
least, when I tried it, Firefox just completely dropped them and
Chrome converted them into comments (and sadly, the Comment node type
doesn't have any properties, so you can't get at them that way).

Which is too bad, because even without Prototype supporting them, it's
not hard to create a DOM walker that would have found them if they
were there:

* * * *
function walk(instructions, node) {
    var child;

    switch (node.nodeType) {
        case Node.PROCESSING_INSTRUCTION_NODE:
            instructions.push(node.data);
            break;
        case Node.ELEMENT_NODE:
            for (child = node.firstChild; child; child =
child.nextSibling) {
                walk(instructions, child);
            }
            break;
    }

    return instructions;
}

var instructions = walk([], document.documentElement);
* * * *

But again, that doesn't work, because the nodes just aren't there as
far as I can tell.

There is an element that you can include /nearly/ anywhere, though:
`script`. And in fact, the HTML5 specification even says[1] that
`script` "... allows authors to include dynamic script AND DATA blocks
in their documents." (My emphasis.)

So you might have your pre-processor convert them into `script`
elements, perhaps of type "x-application/procinst", e.g.:

<body>
  <?pb Edition: current; Page: xxiii?>
  <h1>Now This Is Interesting</h1>
  <p>Interesting stuff here</p>
  <?pb Edition: current; Page: xxiv?>
  <h1>Even More Interesting</h1>

becomes

<body>
  <script type='x-application/procinst'>pb Edition: current; Page:
xxiii</script>
  <h1>Now This Is Interesting</h1>
  <p>Interesting stuff here</p>
  <script type='x-application/procinst'>pb Edition: current; Page:
xxiv</script>
  <h1>Even More Interesting</h1>

A quick test (http://jsbin.com/ijolu3) indicates that even IE6 knows
to leave `script` contents of an unknown type alone. I tried that on
current versions of Chrome, Firefox, and Safari, as well as IE6, IE7,
and IE9.

It isn't, strictly speaking *valid* because (for instance) you can't
have a `script` element that's an immediate child of a `ul`. But
firstly, that seems like an error in the spec (why not?), and
secondly, the browsers don't seem to mind.

[1] http://www.w3.org/TR/html5/scripting-1.html#script

FWIW,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

On Apr 28, 3:26 am, Walter Davis <[EMAIL PROTECTED]> wrote:
> That would work if I was grepping through the source, but I'm trying  
> to pick this thing out of the DOM so I can hook onto it and add a  
> visible element near it. I can't seem to find a way to access it  
> there. Thanks for the suggestion, though.
>
> Walter
>
> On Apr 27, 2011, at 8:32 PM, kstubs wrote:
>
>
>
>
>
>
>
> > Probably your best best is REGEX
>
> > result = subject.match(/<\?[a-zA-Z0-9 :;]*\?>/img);
>
> > result is an array of matches.  So, result[0], result[1], and so on...
>
> > --
> > 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 
> > athttp://groups.google.com/group/prototype-scriptaculous?hl=en
> > .

-- 
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.