vscode-html-languageservice
Advanced tools
Comparing version 2.0.0-next.4 to 2.0.0-next.5
@@ -7,3 +7,3 @@ // copied from https://raw.githubusercontent.com/beautify-web/js-beautify/master/js/lib/beautify-html.js | ||
Copyright (c) 2007-2013 Einar Lielmanis and contributors. | ||
Copyright (c) 2007-2017 Einar Lielmanis, Liam Newman, and contributors. | ||
@@ -52,2 +52,3 @@ Permission is hereby granted, free of charge, to any person | ||
unformatted (defaults to inline tags) - list of tags, that shouldn't be reformatted | ||
content_unformatted (defaults to pre tag) - list of tags, that its content shouldn't be reformatted | ||
indent_scripts (default normal) - "keep"|"separate"|"normal" | ||
@@ -78,5 +79,5 @@ preserve_newlines (default true) - whether existing line breaks before elements should be preserved | ||
function trim(s) { | ||
return s.replace(/^\s+|\s+$/g, ''); | ||
} | ||
// function trim(s) { | ||
// return s.replace(/^\s+|\s+$/g, ''); | ||
// } | ||
@@ -88,5 +89,27 @@ function ltrim(s) { | ||
function rtrim(s) { | ||
return s.replace(/\s+$/g,''); | ||
return s.replace(/\s+$/g, ''); | ||
} | ||
function mergeOpts(allOptions, targetType) { | ||
var finalOpts = {}; | ||
var name; | ||
for (name in allOptions) { | ||
if (name !== targetType) { | ||
finalOpts[name] = allOptions[name]; | ||
} | ||
} | ||
//merge in the per type settings for the targetType | ||
if (targetType in allOptions) { | ||
for (name in allOptions[targetType]) { | ||
finalOpts[name] = allOptions[targetType][name]; | ||
} | ||
} | ||
return finalOpts; | ||
} | ||
var lineBreak = /\r\n|[\n\r\u2028\u2029]/; | ||
var allLineBreaks = new RegExp(lineBreak.source, 'g'); | ||
function style_html(html_source, options, js_beautify, css_beautify) { | ||
@@ -97,2 +120,4 @@ //Wrapper function to invoke all the necessary constructors and deal with the output. | ||
indent_inner_html, | ||
indent_body_inner_html, | ||
indent_head_inner_html, | ||
indent_size, | ||
@@ -103,2 +128,3 @@ indent_character, | ||
unformatted, | ||
content_unformatted, | ||
preserve_newlines, | ||
@@ -109,2 +135,5 @@ max_preserve_newlines, | ||
wrap_attributes_indent_size, | ||
is_wrap_attributes_force, | ||
is_wrap_attributes_force_expand_multiline, | ||
is_wrap_attributes_force_aligned, | ||
end_with_newline, | ||
@@ -116,5 +145,9 @@ extra_liners, | ||
// Allow the setting of language/file-type specific options | ||
// with inheritance of overall settings | ||
options = mergeOpts(options, 'html'); | ||
// backwards compatibility to 1.3.4 | ||
if ((options.wrap_line_length === undefined || parseInt(options.wrap_line_length, 10) === 0) && | ||
(options.max_char !== undefined && parseInt(options.max_char, 10) !== 0)) { | ||
(options.max_char !== undefined && parseInt(options.max_char, 10) !== 0)) { | ||
options.wrap_line_length = options.max_char; | ||
@@ -124,6 +157,8 @@ } | ||
indent_inner_html = (options.indent_inner_html === undefined) ? false : options.indent_inner_html; | ||
indent_body_inner_html = (options.indent_body_inner_html === undefined) ? true : options.indent_body_inner_html; | ||
indent_head_inner_html = (options.indent_head_inner_html === undefined) ? true : options.indent_head_inner_html; | ||
indent_size = (options.indent_size === undefined) ? 4 : parseInt(options.indent_size, 10); | ||
indent_character = (options.indent_char === undefined) ? ' ' : options.indent_char; | ||
brace_style = (options.brace_style === undefined) ? 'collapse' : options.brace_style; | ||
wrap_line_length = parseInt(options.wrap_line_length, 10) === 0 ? 32786 : parseInt(options.wrap_line_length || 250, 10); | ||
wrap_line_length = parseInt(options.wrap_line_length, 10) === 0 ? 32786 : parseInt(options.wrap_line_length || 250, 10); | ||
unformatted = options.unformatted || [ | ||
@@ -138,20 +173,24 @@ // https://www.w3.org/TR/html5/dom.html#phrasing-content | ||
// prexisting - not sure of full effect of removing, leaving in | ||
'acronym', 'address', 'big', 'dt', 'ins', 'small', 'strike', 'tt', | ||
'acronym', 'address', 'big', 'dt', 'ins', 'strike', 'tt', | ||
]; | ||
content_unformatted = options.content_unformatted || [ | ||
'pre', | ||
'h1', 'h2', 'h3', 'h4', 'h5', 'h6' | ||
]; | ||
preserve_newlines = (options.preserve_newlines === undefined) ? true : options.preserve_newlines; | ||
max_preserve_newlines = preserve_newlines ? | ||
(isNaN(parseInt(options.max_preserve_newlines, 10)) ? 32786 : parseInt(options.max_preserve_newlines, 10)) | ||
: 0; | ||
(isNaN(parseInt(options.max_preserve_newlines, 10)) ? 32786 : parseInt(options.max_preserve_newlines, 10)) : | ||
0; | ||
indent_handlebars = (options.indent_handlebars === undefined) ? false : options.indent_handlebars; | ||
wrap_attributes = (options.wrap_attributes === undefined) ? 'auto' : options.wrap_attributes; | ||
wrap_attributes_indent_size = (isNaN(parseInt(options.wrap_attributes_indent_size, 10))) ? indent_size : parseInt(options.wrap_attributes_indent_size, 10); | ||
is_wrap_attributes_force = wrap_attributes.substr(0, 'force'.length) === 'force'; | ||
is_wrap_attributes_force_expand_multiline = (wrap_attributes === 'force-expand-multiline'); | ||
is_wrap_attributes_force_aligned = (wrap_attributes === 'force-aligned'); | ||
end_with_newline = (options.end_with_newline === undefined) ? false : options.end_with_newline; | ||
extra_liners = (typeof options.extra_liners == 'object') && options.extra_liners ? | ||
extra_liners = (typeof options.extra_liners === 'object') && options.extra_liners ? | ||
options.extra_liners.concat() : (typeof options.extra_liners === 'string') ? | ||
options.extra_liners.split(',') : 'head,body,/html'.split(','); | ||
eol = options.eol ? options.eol : '\n'; | ||
eol = options.eol ? options.eol : 'auto'; | ||
if(options.indent_with_tabs){ | ||
if (options.indent_with_tabs) { | ||
indent_character = '\t'; | ||
@@ -161,4 +200,14 @@ indent_size = 1; | ||
eol = eol.replace(/\\r/, '\r').replace(/\\n/, '\n') | ||
if (eol === 'auto') { | ||
eol = '\n'; | ||
if (html_source && lineBreak.test(html_source || '')) { | ||
eol = html_source.match(lineBreak)[0]; | ||
} | ||
} | ||
eol = eol.replace(/\\r/, '\r').replace(/\\n/, '\n'); | ||
// HACK: newline parsing inconsistent. This brute force normalizes the input. | ||
html_source = html_source.replace(allLineBreaks, '\n'); | ||
function Parser() { | ||
@@ -178,2 +227,4 @@ | ||
this.indent_content = indent_inner_html; | ||
this.indent_body_inner_html = indent_body_inner_html; | ||
this.indent_head_inner_html = indent_head_inner_html; | ||
@@ -259,5 +310,5 @@ this.Utils = { //Uilities made available to the various functions | ||
content = [], | ||
space = false; //if a space is needed | ||
handlebarsStarted = 0; | ||
while (this.input.charAt(this.pos) !== '<') { | ||
while (this.input.charAt(this.pos) !== '<' || handlebarsStarted === 2) { | ||
if (this.pos >= this.input.length) { | ||
@@ -272,3 +323,16 @@ return content.length ? content.join('') : ['', 'TK_EOF']; | ||
input_char = this.input.charAt(this.pos); | ||
if (indent_handlebars) { | ||
if (input_char === '{') { | ||
handlebarsStarted += 1; | ||
} else if (handlebarsStarted < 2) { | ||
handlebarsStarted = 0; | ||
} | ||
if (input_char === '}' && handlebarsStarted > 0) { | ||
if (handlebarsStarted-- === 0) { | ||
break; | ||
} | ||
} | ||
// Handlebars parsing is complicated. | ||
@@ -291,3 +355,2 @@ // {{#foo}} and {{/foo}} are formatted tags. | ||
input_char = this.input.charAt(this.pos); | ||
this.pos++; | ||
@@ -304,3 +367,2 @@ this.line_char_count++; | ||
} | ||
var input_char = ''; | ||
var content = ''; | ||
@@ -376,6 +438,9 @@ var reg_match = new RegExp('</' + name + '\\s*>', 'igm'); | ||
first_attr = true, | ||
has_wrapped_attrs = false, | ||
tag_start, tag_end, | ||
tag_start_char, | ||
orig_pos = this.pos, | ||
orig_line_char_count = this.line_char_count; | ||
orig_line_char_count = this.line_char_count, | ||
is_tag_closed = false, | ||
tail; | ||
@@ -404,3 +469,2 @@ peek = peek !== undefined ? peek : false; | ||
space = true; | ||
} | ||
@@ -411,24 +475,49 @@ | ||
} | ||
tail = this.input.substr(this.pos - 1); | ||
if (is_wrap_attributes_force_expand_multiline && has_wrapped_attrs && !is_tag_closed && (input_char === '>' || input_char === '/')) { | ||
if (tail.match(/^\/?\s*>/)) { | ||
space = false; | ||
is_tag_closed = true; | ||
this.print_newline(false, content); | ||
this.print_indentation(content); | ||
} | ||
} | ||
if (content.length && content[content.length - 1] !== '=' && input_char !== '>' && space) { | ||
//no space after = or before > | ||
var wrapped = this.space_or_wrap(content); | ||
var indentAttrs = wrapped && input_char !== '/' && wrap_attributes !== 'force'; | ||
var indentAttrs = wrapped && input_char !== '/' && !is_wrap_attributes_force; | ||
space = false; | ||
if (!first_attr && wrap_attributes === 'force' && input_char !== '/') { | ||
this.print_newline(false, content); | ||
this.print_indentation(content); | ||
indentAttrs = true; | ||
if (is_wrap_attributes_force && input_char !== '/') { | ||
var force_first_attr_wrap = false; | ||
if (is_wrap_attributes_force_expand_multiline && first_attr) { | ||
var is_only_attribute = tail.match(/^\S*(="([^"]|\\")*")?\s*\/?\s*>/) !== null; | ||
force_first_attr_wrap = !is_only_attribute; | ||
} | ||
if (!first_attr || force_first_attr_wrap) { | ||
this.print_newline(false, content); | ||
this.print_indentation(content); | ||
indentAttrs = true; | ||
} | ||
} | ||
if (indentAttrs) { | ||
//indent attributes an auto or forced line-wrap | ||
for (var count = 0; count < wrap_attributes_indent_size; count++) { | ||
has_wrapped_attrs = true; | ||
//indent attributes an auto, forced, or forced-align line-wrap | ||
var alignment_size = wrap_attributes_indent_size; | ||
if (is_wrap_attributes_force_aligned) { | ||
alignment_size = content.indexOf(' ') + 1; | ||
} | ||
for (var count = 0; count < alignment_size; count++) { | ||
content.push(indent_character); | ||
} | ||
} | ||
for (var i = 0; i < content.length; i++) { | ||
if (content[i] === ' ') { | ||
first_attr = false; | ||
break; | ||
} | ||
if (first_attr) { | ||
for (var i = 0; i < content.length; i++) { | ||
if (content[i] === ' ') { | ||
first_attr = false; | ||
break; | ||
} | ||
} | ||
} | ||
@@ -491,3 +580,5 @@ } | ||
if (tag_complete.indexOf(' ') !== -1) { //if there's whitespace, thats where the tag name ends | ||
if (tag_complete.indexOf('\n') !== -1) { //if there's a line break, thats where the tag name ends | ||
tag_index = tag_complete.indexOf('\n'); | ||
} else if (tag_complete.indexOf(' ') !== -1) { //if there's whitespace, thats where the tag name ends | ||
tag_index = tag_complete.indexOf(' '); | ||
@@ -517,3 +608,5 @@ } else if (tag_complete.charAt(0) === '{') { | ||
} | ||
} else if (this.is_unformatted(tag_check, unformatted)) { // do not reformat the "unformatted" tags | ||
} else if (this.is_unformatted(tag_check, unformatted) || | ||
this.is_unformatted(tag_check, content_unformatted)) { | ||
// do not reformat the "unformatted" or "content_unformatted" tags | ||
comment = this.get_unformatted('</' + tag_check + '>', tag_complete); //...delegate to get_unformatted function | ||
@@ -525,4 +618,4 @@ content.push(comment); | ||
(tag_complete.search('type') === -1 || | ||
(tag_complete.search('type') > -1 && | ||
tag_complete.search(/\b(text|application)\/(x-)?(javascript|ecmascript|jscript|livescript|(ld\+)?json)/) > -1))) { | ||
(tag_complete.search('type') > -1 && | ||
tag_complete.search(/\b(text|application|dojo)\/(x-)?(javascript|ecmascript|jscript|livescript|(ld\+)?json|method|aspect)/) > -1))) { | ||
if (!peek) { | ||
@@ -534,3 +627,3 @@ this.record_tag(tag_check); | ||
(tag_complete.search('type') === -1 || | ||
(tag_complete.search('type') > -1 && tag_complete.search('text/css') > -1))) { | ||
(tag_complete.search('type') > -1 && tag_complete.search('text/css') > -1))) { | ||
if (!peek) { | ||
@@ -612,5 +705,10 @@ this.record_tag(tag_check); | ||
matched = true; | ||
} else if (comment.indexOf('{{!--') === 0) { // {{!-- handlebars comment | ||
delimiter = '--}}'; | ||
matched = true; | ||
} else if (comment.indexOf('{{!') === 0) { // {{! handlebars comment | ||
delimiter = '}}'; | ||
matched = true; | ||
if (comment.length === 5 && comment.indexOf('{{!--') === -1) { | ||
delimiter = '}}'; | ||
matched = true; | ||
} | ||
} else if (comment.indexOf('<?') === 0) { // {{! handlebars comment | ||
@@ -633,17 +731,17 @@ delimiter = '?>'; | ||
function tokenMatcher(delimiter) { | ||
var token = ''; | ||
var token = ''; | ||
var add = function (str) { | ||
var newToken = token + str.toLowerCase(); | ||
token = newToken.length <= delimiter.length ? newToken : newToken.substr(newToken.length - delimiter.length, delimiter.length); | ||
}; | ||
var add = function(str) { | ||
var newToken = token + str.toLowerCase(); | ||
token = newToken.length <= delimiter.length ? newToken : newToken.substr(newToken.length - delimiter.length, delimiter.length); | ||
}; | ||
var doesNotMatch = function () { | ||
return token.indexOf(delimiter) === -1; | ||
}; | ||
var doesNotMatch = function() { | ||
return token.indexOf(delimiter) === -1; | ||
}; | ||
return { | ||
add: add, | ||
doesNotMatch: doesNotMatch | ||
}; | ||
return { | ||
add: add, | ||
doesNotMatch: doesNotMatch | ||
}; | ||
} | ||
@@ -774,3 +872,3 @@ | ||
// HACK: newline parsing inconsistent. This brute force normalizes the input. | ||
this.input = this.input.replace(/\r\n|[\r\u2028\u2029]/g, '\n') | ||
this.input = this.input.replace(/\r\n|[\r\u2028\u2029]/g, '\n'); | ||
@@ -879,3 +977,8 @@ this.output = []; | ||
if (multi_parser.indent_content) { | ||
multi_parser.indent(); | ||
if ((multi_parser.indent_body_inner_html || !multi_parser.token_text.match(/<body(?:.*)>/)) && | ||
(multi_parser.indent_head_inner_html || !multi_parser.token_text.match(/<head(?:.*)>/))) { | ||
multi_parser.indent(); | ||
} | ||
multi_parser.indent_content = false; | ||
@@ -894,3 +997,3 @@ } | ||
if (multi_parser.last_token === 'TK_CONTENT' && multi_parser.last_text === '') { | ||
var tag_name = multi_parser.token_text.match(/\w+/)[0]; | ||
var tag_name = (multi_parser.token_text.match(/\w+/) || [])[0]; | ||
var tag_extracted_from_last_output = null; | ||
@@ -920,5 +1023,5 @@ if (multi_parser.output.length) { | ||
var foundIfOnCurrentLine = false; | ||
for (var lastCheckedOutput=multi_parser.output.length-1; lastCheckedOutput>=0; lastCheckedOutput--) { | ||
if (multi_parser.output[lastCheckedOutput] === '\n') { | ||
break; | ||
for (var lastCheckedOutput = multi_parser.output.length - 1; lastCheckedOutput >= 0; lastCheckedOutput--) { | ||
if (multi_parser.output[lastCheckedOutput] === '\n') { | ||
break; | ||
} else { | ||
@@ -1012,3 +1115,3 @@ if (multi_parser.output[lastCheckedOutput].match(/{{#if/)) { | ||
if (eol != '\n') { | ||
if (eol !== '\n') { | ||
sweet_code = sweet_code.replace(/[\n]/g, eol); | ||
@@ -1023,9 +1126,9 @@ } | ||
define(["require", "./beautify", "./beautify-css"], function(requireamd) { | ||
var js_beautify = requireamd("./beautify"); | ||
var css_beautify = requireamd("./beautify-css"); | ||
var js_beautify = requireamd("./beautify"); | ||
var css_beautify = requireamd("./beautify-css"); | ||
return { | ||
html_beautify: function(html_source, options) { | ||
return style_html(html_source, options, js_beautify.js_beautify, css_beautify.css_beautify); | ||
} | ||
html_beautify: function(html_source, options) { | ||
return style_html(html_source, options, js_beautify.js_beautify, css_beautify.css_beautify); | ||
} | ||
}; | ||
@@ -1032,0 +1135,0 @@ }); |
@@ -8,4 +8,5 @@ import { TextDocument, Position, CompletionItem, CompletionList, Hover, Range, SymbolInformation, Diagnostic, TextEdit, DocumentHighlight, FormattingOptions, MarkedString, DocumentLink } from 'vscode-languageserver-types'; | ||
unformatted: string; | ||
contentUnformatted: string; | ||
indentInnerHtml: boolean; | ||
wrapAttributes: 'auto' | 'force'; | ||
wrapAttributes: 'auto' | 'force' | 'force-aligned' | 'force-expand-multiline'; | ||
preserveNewLines: boolean; | ||
@@ -12,0 +13,0 @@ maxPreserveNewLines: number; |
@@ -33,2 +33,3 @@ (function (factory) { | ||
unformatted: getTagsFormatOption(options, 'unformatted', void 0), | ||
content_unformatted: getTagsFormatOption(options, 'contentUnformatted', void 0), | ||
indent_inner_html: getFormatOption(options, 'indentInnerHtml', false), | ||
@@ -35,0 +36,0 @@ preserve_newlines: getFormatOption(options, 'preserveNewLines', false), |
{ | ||
"name": "vscode-html-languageservice", | ||
"version": "2.0.0-next.4", | ||
"version": "2.0.0-next.5", | ||
"description": "Language service for HTML", | ||
@@ -5,0 +5,0 @@ "main": "./lib/htmlLanguageService.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
299899
5065