Commit 79539170 authored by Joel Einbinder's avatar Joel Einbinder Committed by Commit Bot

DevTools: Pretty print scripts with "text/javascript" in html files

We were not considering that type attributes in scripts could be
wrapped in quotes.

Change-Id: Id2fd4859c1e1a68bd28bebe6fea46a62b58e417a
Reviewed-on: https://chromium-review.googlesource.com/1043463
Commit-Queue: Joel Einbinder <einbinder@chromium.org>
Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#556187}
parent 7080df99
...@@ -14,7 +14,7 @@ Correct mapping for <comment> ...@@ -14,7 +14,7 @@ Correct mapping for <comment>
Running: testInlineJavascript Running: testInlineJavascript
====== 8< ------ ====== 8< ------
<html> <html>
<script> <script type="text/javascript">
for (var i = 0; i < 10; ++i) for (var i = 0; i < 10; ++i)
console.log('test ' + i); console.log('test ' + i);
</script> </script>
......
...@@ -18,7 +18,8 @@ ...@@ -18,7 +18,8 @@
function testInlineJavascript(next) { function testInlineJavascript(next) {
var mappingQueries = ['console', 'test', '</html']; var mappingQueries = ['console', 'test', '</html'];
testFormatter( testFormatter(
'<html><script>for(var i=0;i<10;++i)console.log(\'test \'+i);<\/script></html>', mappingQueries, next); '<html><script type="text/javascript">for(var i=0;i<10;++i)console.log(\'test \'+i);<\/script></html>',
mappingQueries, next);
}, },
function testInlineCSS(next) { function testInlineCSS(next) {
......
...@@ -116,9 +116,7 @@ FormatterWorker.HTMLFormatter = class { ...@@ -116,9 +116,7 @@ FormatterWorker.HTMLFormatter = class {
if (isBodyToken && element.name === 'script') { if (isBodyToken && element.name === 'script') {
this._builder.addNewLine(); this._builder.addNewLine();
this._builder.increaseNestingLevel(); this._builder.increaseNestingLevel();
const mimeType = if (this._scriptTagIsJavaScript(element)) {
element.openTag.attributes.has('type') ? element.openTag.attributes.get('type').toLowerCase() : null;
if (!mimeType || FormatterWorker.HTMLFormatter.SupportedJavaScriptMimeTypes.has(mimeType)) {
this._jsFormatter.format(this._text, this._lineEndings, token.startOffset, token.endOffset); this._jsFormatter.format(this._text, this._lineEndings, token.startOffset, token.endOffset);
} else { } else {
this._builder.addToken(token.value, token.startOffset); this._builder.addToken(token.value, token.startOffset);
...@@ -133,10 +131,30 @@ FormatterWorker.HTMLFormatter = class { ...@@ -133,10 +131,30 @@ FormatterWorker.HTMLFormatter = class {
this._builder.addToken(token.value, token.startOffset); this._builder.addToken(token.value, token.startOffset);
} }
/**
* @param {!FormatterWorker.HTMLModel.Element} element
* @return {boolean}
*/
_scriptTagIsJavaScript(element) {
if (!element.openTag.attributes.has('type'))
return true;
let type = element.openTag.attributes.get('type').toLowerCase();
if (!type)
return true;
const isWrappedInQuotes = /^(["\'])(.*)\1$/.exec(type.trim());
if (isWrappedInQuotes)
type = isWrappedInQuotes[2];
return FormatterWorker.HTMLFormatter.SupportedJavaScriptMimeTypes.has(type.trim());
}
}; };
FormatterWorker.HTMLFormatter.SupportedJavaScriptMimeTypes = FormatterWorker.HTMLFormatter.SupportedJavaScriptMimeTypes = new Set([
new Set(['text/javascript', 'text/ecmascript', 'application/javascript', 'application/ecmascript']); 'application/ecmascript', 'application/javascript', 'application/x-ecmascript', 'application/x-javascript',
'text/ecmascript', 'text/javascript', 'text/javascript1.0', 'text/javascript1.1', 'text/javascript1.2',
'text/javascript1.3', 'text/javascript1.4', 'text/javascript1.5', 'text/jscript', 'text/livescript',
'text/x-ecmascript', 'text/x-javascript'
]);
/** /**
* @unrestricted * @unrestricted
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment