This commit is contained in:
Mark Pilgrim
2009-08-05 14:49:32 -07:00
parent 202511e983
commit fb0aa874df
17 changed files with 231 additions and 197 deletions
+46 -11
View File
@@ -11,11 +11,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Changes from upstream:
// - use class=pp instead of class=prettyprint to declare blocks-to-colorize
// - removed support for <xmp>
// - added support for <kbd> and <samp>
/**
* @fileoverview
@@ -36,6 +32,9 @@
* <script type="text/javascript" src="/path/to/prettify.js"></script>
* 2) define style rules. See the example page for examples.
* 3) mark the <pre> and <code> tags in your source with class=pp.
* You can also use the (html deprecated) <xmp> tag, but the pretty printer
* needs to do more substantial DOM manipulations to support that, so some
* css styles may not be preserved.
* That's it. I wanted to keep the API as simple as possible, so there's no
* need to specify which language the code is in.
*
@@ -269,6 +268,11 @@ window['_pr_isIE6'] = function () {
.replace(pr_nbspEnt, ' ');
}
/** is the given node's innerHTML normally unescaped? */
function isRawContent(node) {
return 'XMP' === node.tagName;
}
function normalizedHtml(node, out) {
switch (node.nodeType) {
case 1: // an element
@@ -541,6 +545,10 @@ window['_pr_isIE6'] = function () {
if (PR_innerHtmlWorks) {
var content = node.innerHTML;
// XMP tags contain unescaped entities so require special handling.
if (isRawContent(node)) {
content = textToHtml(content);
}
return content;
}
@@ -603,13 +611,14 @@ window['_pr_isIE6'] = function () {
'[^<]+' // A run of characters other than '<'
+ '|<\!--[\\s\\S]*?--\>' // an HTML comment
+ '|<!\\[CDATA\\[[\\s\\S]*?\\]\\]>' // a CDATA section
+ '|</?[a-zA-Z][^>]*>' // a probable tag that should not be highlighted
// a probable tag that should not be highlighted
+ '|<\/?[a-zA-Z](?:[^>\"\']|\'[^\']*\'|\"[^\"]*\")*>'
+ '|<', // A '<' that does not begin a larger chunk
'g');
var pr_commentPrefix = /^<\!--/;
var pr_cdataPrefix = /^<\[CDATA\[/;
var pr_cdataPrefix = /^<!\[CDATA\[/;
var pr_brPrefix = /^<br\b/i;
var pr_tagNameRe = /^<(\/?)([a-zA-Z]+)/;
var pr_tagNameRe = /^<(\/?)([a-zA-Z][a-zA-Z0-9]*)/;
/** split markup into chunks of html tags (style null) and
* plain text (style {@link #PR_PLAIN}), converting tags which are
@@ -1273,7 +1282,8 @@ window['_pr_isIE6'] = function () {
document.getElementsByTagName('pre'),
document.getElementsByTagName('code'),
document.getElementsByTagName('kbd'),
document.getElementsByTagName('samp') ];
document.getElementsByTagName('samp'),
document.getElementsByTagName('xmp') ];
var elements = [];
for (var i = 0; i < codeSegments.length; ++i) {
for (var j = 0, n = codeSegments[i].length; j < n; ++j) {
@@ -1311,7 +1321,8 @@ window['_pr_isIE6'] = function () {
var nested = false;
for (var p = cs.parentNode; p; p = p.parentNode) {
if ((p.tagName === 'pre' || p.tagName === 'code' ||
p.tagName === 'kbd' || p.tagName === 'samp') &&
p.tagName === 'kbd' || p.tagName === 'samp' ||
p.tagName === 'xmp') &&
p.className && p.className.indexOf('pp') >= 0) {
nested = true;
break;
@@ -1348,7 +1359,31 @@ window['_pr_isIE6'] = function () {
var cs = prettyPrintingJob.sourceNode;
// push the prettified html back into the tag.
cs.innerHTML = newContent;
if (!isRawContent(cs)) {
// just replace the old html with the new
cs.innerHTML = newContent;
} else {
// we need to change the tag to a <pre> since <xmp>s do not allow
// embedded tags such as the span tags used to attach styles to
// sections of source code.
var pre = document.createElement('PRE');
for (var i = 0; i < cs.attributes.length; ++i) {
var a = cs.attributes[i];
if (a.specified) {
var aname = a.name.toLowerCase();
if (aname === 'class') {
pre.className = a.value; // For IE 6
} else {
pre.setAttribute(a.name, a.value);
}
}
}
pre.innerHTML = newContent;
// remove the old
cs.parentNode.replaceChild(pre, cs);
cs = pre;
}
// Replace <br>s with line-feeds so that copying and pasting works
// on IE 6.