zzyengineer / google-code-prettify

Automatically exported from code.google.com/p/google-code-prettify
Apache License 2.0
0 stars 0 forks source link

Multiple Calls of prettyPrint() Ineffective #204

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I had a process that Ajax'd in some external code, inserted it into a target 
pre tag and the called prettyPrint(). This added additional markup and styles 
with each invocation.

Studying the code, there is no way to target a single node.

I fixed this by adding a target parameter, if it existed, instead of the 
getElementsByTagName on code, pre and xmp, I did a getElementById on the target.

Now I can call prettyPrint('target') without scanning all the other tags and 
adding unnecessary content to the DOM.

I also dropped xmp because it is deprecated.

....

function prettyPrint(target, opt_whenDone) {
  function byTagName(tn) { return document.getElementsByTagName(tn); }
  // fetch a list of nodes to rewrite
  var codeSegments = [byTagName('pre'), byTagName('code')];
  var elements = [];
  if (target){ 
    codeSegments = [document.getElementById(target)];
    elements.push(codeSegments[0]);
  } else {
    for (var i = 0; i < codeSegments.length; ++i) {
      for (var j = 0, n = codeSegments[i].length; j < n; ++j) {
        elements.push(codeSegments[i][j]);
      }
    }
  }
  codeSegments = null;

  .....

Thought this might be a useful addition to the library!

Original issue reported on code.google.com by willabee...@gmail.com on 9 Apr 2012 at 4:02

GoogleCodeExporter commented 9 years ago
http://code.google.com/p/google-code-prettify/source/detail?r=242 incorporates 
your idea.  An optional argument now lets the caller specify the root under 
which to search using getElementsByTagName.  This will be included in the next 
release.

Original comment by mikesamuel@gmail.com on 5 Feb 2013 at 2:53