Skip to content
This repository was archived by the owner on Apr 22, 2020. It is now read-only.

Commit 624f420

Browse files
issue 96: fix highlighting of style and script elements that have no content.
1 parent 077b31a commit 624f420

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/prettify.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ window['_pr_isIE6'] = function () {
878878
}
879879

880880
isEmbedded = style.length >= 5 && 'lang-' === style.substring(0, 5);
881-
if (isEmbedded && !(match && match[1])) {
881+
if (isEmbedded && !(match && typeof match[1] === 'string')) {
882882
isEmbedded = false;
883883
style = PR_SOURCE;
884884
}
@@ -895,8 +895,14 @@ window['_pr_isIE6'] = function () {
895895
var embeddedSource = match[1];
896896
var embeddedSourceStart = token.indexOf(embeddedSource);
897897
var embeddedSourceEnd = embeddedSourceStart + embeddedSource.length;
898+
if (match[2]) {
899+
// If embeddedSource can be blank, then it would match at the
900+
// beginning which would cause us to infinitely recurse on the
901+
// entire token, so we catch the right context in match[2].
902+
embeddedSourceEnd = token.length - match[2].length;
903+
embeddedSourceStart = embeddedSourceEnd - embeddedSource.length;
904+
}
898905
var lang = style.substring(5);
899-
var size = decorations.length - 10;
900906
// Decorate the left of the embedded source
901907
appendDecorations(
902908
basePos + tokenStart,
@@ -1200,9 +1206,9 @@ window['_pr_isIE6'] = function () {
12001206
[PR_PUNCTUATION, /^(?:<[%?]|[%?]>)/],
12011207
['lang-', /^<xmp\b[^>]*>([\s\S]+?)<\/xmp\b[^>]*>/i],
12021208
// Unescaped content in javascript. (Or possibly vbscript).
1203-
['lang-js', /^<script\b[^>]*>([\s\S]+?)<\/script\b[^>]*>/i],
1209+
['lang-js', /^<script\b[^>]*>([\s\S]*?)(<\/script\b[^>]*>)/i],
12041210
// Contains unescaped stylesheet content
1205-
['lang-css', /^<style\b[^>]*>([\s\S]+?)<\/style\b[^>]*>/i],
1211+
['lang-css', /^<style\b[^>]*>([\s\S]*?)(<\/style\b[^>]*>)/i],
12061212
['lang-in.tag', /^(<\/?[a-z][^<>]*>)/i]
12071213
]),
12081214
['default-markup', 'htm', 'html', 'mxml', 'xhtml', 'xml', 'xsl']);

tests/prettify_test.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,8 @@ <h1>HTML</h1>
491491
&lt;head>
492492
&lt;title&gt;Fibonacci number&lt;/title&gt;
493493
&lt;style&gt;&lt;!-- BODY { text-decoration: blink } --&gt;&lt;/style&gt;
494+
&lt;script src="foo.js"&gt;&lt;/script&gt;
495+
&lt;script src="bar.js"&gt;&lt;/script&gt;
494496
&lt;/head&gt;
495497
&lt;body>
496498
&lt;noscript&gt;
@@ -1597,6 +1599,10 @@ <h1>Issue 93 -- C# verbatim strings</h1>
15971599
'`PLN BODY `END`PUN{`END`PLN `END`KWDtext-decoration`END`PUN:`END' +
15981600
'`PLN blink `END`PUN}`END`PLN `END`COM--&gt;`END`TAG&lt;/' +
15991601
'style&gt;`END`PLN<br>' +
1602+
'&nbsp; &nbsp; `END`TAG&lt;script`END`PLN `END`ATNsrc`END`PUN=`END' +
1603+
'`ATV"foo.js"`END`TAG&gt;&lt;/script&gt;`END`PLN<br>' +
1604+
'&nbsp; &nbsp; `END`TAG&lt;script`END`PLN `END`ATNsrc`END`PUN=`END' +
1605+
'`ATV"bar.js"`END`TAG&gt;&lt;/script&gt;`END`PLN<br>' +
16001606
'&nbsp; `END`TAG&lt;/head&gt;`END`PLN<br>' +
16011607
'&nbsp; `END`TAG&lt;body&gt;`END`PLN<br>' +
16021608
'&nbsp; &nbsp; `END`TAG&lt;noscript&gt;`END`PLN<br>' +

0 commit comments

Comments
 (0)