vscode-html-languageservice
Advanced tools
Comparing version 2.0.1 to 2.0.2-next.1
// copied https://raw.githubusercontent.com/beautify-web/js-beautify/master/js/lib/beautify-css.js | ||
/*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */ | ||
@@ -8,3 +7,3 @@ /* | ||
Copyright (c) 2007-2013 Einar Lielmanis and contributors. | ||
Copyright (c) 2007-2017 Einar Lielmanis, Liam Newman, and contributors. | ||
@@ -45,9 +44,11 @@ Permission is hereby granted, free of charge, to any person | ||
The options are (default in brackets): | ||
indent_size (4) — indentation size, | ||
indent_char (space) — character to indent with, | ||
selector_separator_newline (true) - separate selectors with newline or | ||
not (e.g. "a,\nbr" or "a, br") | ||
end_with_newline (false) - end with a newline | ||
newline_between_rules (true) - add a new line after every css rule | ||
indent_size (4) — indentation size, | ||
indent_char (space) — character to indent with, | ||
preserve_newlines (default false) - whether existing line breaks should be preserved, | ||
selector_separator_newline (true) - separate selectors with newline or | ||
not (e.g. "a,\nbr" or "a, br") | ||
end_with_newline (false) - end with a newline | ||
newline_between_rules (true) - add a new line after every css rule | ||
space_around_selector_separator (false) - ensure space around selector separators: | ||
'>', '+', '~' (e.g. "a>b" -> "a > b") | ||
e.g | ||
@@ -60,3 +61,4 @@ | ||
'end_with_newline': false, | ||
'newline_between_rules': true | ||
'newline_between_rules': true, | ||
'space_around_selector_separator': true | ||
}); | ||
@@ -69,21 +71,46 @@ */ | ||
(function() { | ||
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 css_beautify(source_text, options) { | ||
options = options || {}; | ||
// Allow the setting of language/file-type specific options | ||
// with inheritance of overall settings | ||
options = mergeOpts(options, 'css'); | ||
source_text = source_text || ''; | ||
// HACK: newline parsing inconsistent. This brute force normalizes the input. | ||
source_text = source_text.replace(/\r\n|[\r\u2028\u2029]/g, '\n') | ||
var indentSize = options.indent_size || 4; | ||
var indentSize = options.indent_size ? parseInt(options.indent_size, 10) : 4; | ||
var indentCharacter = options.indent_char || ' '; | ||
var preserve_newlines = (options.preserve_newlines === undefined) ? false : options.preserve_newlines; | ||
var selectorSeparatorNewline = (options.selector_separator_newline === undefined) ? true : options.selector_separator_newline; | ||
var end_with_newline = (options.end_with_newline === undefined) ? false : options.end_with_newline; | ||
var newline_between_rules = (options.newline_between_rules === undefined) ? true : options.newline_between_rules; | ||
var eol = options.eol ? options.eol : '\n'; | ||
var space_around_combinator = (options.space_around_combinator === undefined) ? false : options.space_around_combinator; | ||
space_around_combinator = space_around_combinator || ((options.space_around_selector_separator === undefined) ? false : options.space_around_selector_separator); | ||
var eol = options.eol ? options.eol : 'auto'; | ||
// compatibility | ||
if (typeof indentSize === "string") { | ||
indentSize = parseInt(indentSize, 10); | ||
} | ||
if(options.indent_with_tabs){ | ||
if (options.indent_with_tabs) { | ||
indentCharacter = '\t'; | ||
@@ -93,8 +120,16 @@ indentSize = 1; | ||
eol = eol.replace(/\\r/, '\r').replace(/\\n/, '\n') | ||
if (eol === 'auto') { | ||
eol = '\n'; | ||
if (source_text && lineBreak.test(source_text || '')) { | ||
eol = source_text.match(lineBreak)[0]; | ||
} | ||
} | ||
eol = eol.replace(/\\r/, '\r').replace(/\\n/, '\n'); | ||
// HACK: newline parsing inconsistent. This brute force normalizes the input. | ||
source_text = source_text.replace(allLineBreaks, '\n'); | ||
// tokenizer | ||
var whiteRe = /^\s+$/; | ||
var wordRe = /[\w$\-_]/; | ||
@@ -199,3 +234,3 @@ var pos = -1, | ||
} else if (ch === ')') { | ||
if (openParen == 0) { | ||
if (openParen === 0) { | ||
return false; | ||
@@ -211,2 +246,12 @@ } | ||
function removeWhiteSpaceOnEmptyLines(input) { | ||
var output = input.split('\n'); | ||
for (var i = 0; i < output.length; i++) { | ||
if (output[i].trim().length === 0) { | ||
output[i] = ''; | ||
} | ||
} | ||
return output.join('\n'); | ||
} | ||
// printer | ||
@@ -289,2 +334,4 @@ var basebaseIndentString = source_text.match(/^[\t ]*/)[0]; | ||
var isAfterNewline = whitespace.indexOf('\n') !== -1; | ||
var newLines = whitespace.replace(/ /g, '').replace(/\t/g, ''); | ||
var isAfterEmptyline = newLines.indexOf('\n\n') !== -1; | ||
last_top_ch = top_ch; | ||
@@ -308,3 +355,3 @@ top_ch = ch; | ||
} else if (ch === '/' && peek() === '/') { // single line comment | ||
if (!isAfterNewline && last_top_ch !== '{' ) { | ||
if (!isAfterNewline && last_top_ch !== '{') { | ||
print.trim(); | ||
@@ -317,27 +364,33 @@ } | ||
print.preserveSingleSpace(); | ||
output.push(ch); | ||
// strip trailing space, if present, for hash property checks | ||
var variableOrRule = peekString(": ,;{}()[]/='\""); | ||
// deal with less propery mixins @{...} | ||
if (peek() === '{') { | ||
output.push(eatString('}')); | ||
} else { | ||
output.push(ch); | ||
if (variableOrRule.match(/[ :]$/)) { | ||
// we have a variable or pseudo-class, add it and insert one space before continuing | ||
next(); | ||
variableOrRule = eatString(": ").replace(/\s$/, ''); | ||
output.push(variableOrRule); | ||
print.singleSpace(); | ||
} | ||
// strip trailing space, if present, for hash property checks | ||
var variableOrRule = peekString(": ,;{}()[]/='\""); | ||
variableOrRule = variableOrRule.replace(/\s$/, '') | ||
if (variableOrRule.match(/[ :]$/)) { | ||
// we have a variable or pseudo-class, add it and insert one space before continuing | ||
next(); | ||
variableOrRule = eatString(": ").replace(/\s$/, ''); | ||
output.push(variableOrRule); | ||
print.singleSpace(); | ||
} | ||
// might be a nesting at-rule | ||
if (variableOrRule in css_beautify.NESTED_AT_RULE) { | ||
nestedLevel += 1; | ||
if (variableOrRule in css_beautify.CONDITIONAL_GROUP_RULE) { | ||
enteringConditionalGroup = true; | ||
variableOrRule = variableOrRule.replace(/\s$/, ''); | ||
// might be a nesting at-rule | ||
if (variableOrRule in css_beautify.NESTED_AT_RULE) { | ||
nestedLevel += 1; | ||
if (variableOrRule in css_beautify.CONDITIONAL_GROUP_RULE) { | ||
enteringConditionalGroup = true; | ||
} | ||
} | ||
} | ||
} else if (ch === '#' && peek() === '{') { | ||
print.preserveSingleSpace(); | ||
output.push(eatString('}')); | ||
print.preserveSingleSpace(); | ||
output.push(eatString('}')); | ||
} else if (ch === '{') { | ||
@@ -379,11 +432,19 @@ if (peek(true) === '}') { | ||
if ((insideRule || enteringConditionalGroup) && | ||
!(lookBack("&") || foundNestedPseudoClass())) { | ||
!(lookBack("&") || foundNestedPseudoClass()) && | ||
!lookBack("(")) { | ||
// 'property: value' delimiter | ||
// which could be in a conditional group query | ||
insidePropertyValue = true; | ||
output.push(':'); | ||
print.singleSpace(); | ||
if (!insidePropertyValue) { | ||
insidePropertyValue = true; | ||
print.singleSpace(); | ||
} | ||
} else { | ||
// sass/less parent reference don't use a space | ||
// sass nested pseudo-class don't use a space | ||
// preserve space before pseudoclasses/pseudoelements, as it means "in any child" | ||
if (lookBack(" ") && output[output.length - 1] !== " ") { | ||
output.push(" "); | ||
} | ||
if (peek() === ":") { | ||
@@ -433,2 +494,17 @@ // pseudo-element | ||
} | ||
} else if ((ch === '>' || ch === '+' || ch === '~') && | ||
!insidePropertyValue && parenLevel < 1) { | ||
//handle combinator spacing | ||
if (space_around_combinator) { | ||
print.singleSpace(); | ||
output.push(ch); | ||
print.singleSpace(); | ||
} else { | ||
output.push(ch); | ||
eatWhitespace(); | ||
// squash extra whitespace | ||
if (ch && whiteRe.test(ch)) { | ||
ch = ''; | ||
} | ||
} | ||
} else if (ch === ']') { | ||
@@ -440,7 +516,15 @@ output.push(ch); | ||
} else if (ch === '=') { // no whitespace before or after | ||
eatWhitespace() | ||
eatWhitespace(); | ||
ch = '='; | ||
output.push(ch); | ||
} else { | ||
print.preserveSingleSpace(); | ||
if (isAfterEmptyline && preserve_newlines) { | ||
var newLineCount = newLines.split('\n').length - 2; | ||
for (var i = 0; i < newLineCount; i++) { | ||
print.newLine(true); | ||
} | ||
eatWhitespace(); | ||
} else { | ||
print.preserveSingleSpace(); | ||
} | ||
output.push(ch); | ||
@@ -458,2 +542,6 @@ } | ||
if (preserve_newlines) { | ||
sweetCode = removeWhiteSpaceOnEmptyLines(sweetCode); | ||
} | ||
// establish end_with_newline | ||
@@ -464,3 +552,3 @@ if (end_with_newline) { | ||
if (eol != '\n') { | ||
if (eol !== '\n') { | ||
sweetCode = sweetCode.replace(/[\n]/g, eol); | ||
@@ -467,0 +555,0 @@ } |
@@ -305,3 +305,3 @@ // copied from https://raw.githubusercontent.com/beautify-web/js-beautify/master/js/lib/beautify-html.js | ||
if (this.traverse_whitespace()) { | ||
if (handlebarsStarted < 2 && this.traverse_whitespace()) { | ||
this.space_or_wrap(content); | ||
@@ -495,3 +495,4 @@ continue; | ||
for (var count = 0; count < alignment_size; count++) { | ||
content.push(indent_character); | ||
// only ever further indent with spaces since we're trying to align characters | ||
content.push(' '); | ||
} | ||
@@ -563,6 +564,8 @@ } | ||
if (tag_complete.indexOf('\n') !== -1) { //if there's a line break, thats where the tag name ends | ||
// must check for space first otherwise the tag could have the first attribute included, and | ||
// then not un-indent correctly | ||
if (tag_complete.indexOf(' ') !== -1) { //if there's whitespace, thats where the tag name ends | ||
tag_index = tag_complete.indexOf(' '); | ||
} else 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(' '); | ||
} else if (tag_complete.charAt(0) === '{') { | ||
@@ -569,0 +572,0 @@ tag_index = tag_complete.indexOf('}'); |
@@ -38,4 +38,31 @@ (function (factory) { | ||
}); | ||
test('Id and classes', function () { | ||
var content = '<html id=\'root\'><body id="Foo" class="bar"><div class="a b"></div></body></html>'; | ||
var expected = [ | ||
{ name: 'html#root', kind: vscode_languageserver_types_1.SymbolKind.Field, containerName: '', location: vscode_languageserver_types_1.Location.create(TEST_URI, vscode_languageserver_types_1.Range.create(0, 0, 0, 80)) }, | ||
{ name: 'body#Foo.bar', kind: vscode_languageserver_types_1.SymbolKind.Field, containerName: 'html#root', location: vscode_languageserver_types_1.Location.create(TEST_URI, vscode_languageserver_types_1.Range.create(0, 16, 0, 73)) }, | ||
{ name: 'div.a.b', kind: vscode_languageserver_types_1.SymbolKind.Field, containerName: 'body#Foo.bar', location: vscode_languageserver_types_1.Location.create(TEST_URI, vscode_languageserver_types_1.Range.create(0, 43, 0, 66)) }, | ||
]; | ||
testSymbolsFor(content, expected); | ||
}); | ||
test('Self closing', function () { | ||
var content = '<html><br id="Foo"><br id=Bar></html>'; | ||
var expected = [ | ||
{ name: 'html', kind: vscode_languageserver_types_1.SymbolKind.Field, containerName: '', location: vscode_languageserver_types_1.Location.create(TEST_URI, vscode_languageserver_types_1.Range.create(0, 0, 0, 37)) }, | ||
{ name: 'br#Foo', kind: vscode_languageserver_types_1.SymbolKind.Field, containerName: 'html', location: vscode_languageserver_types_1.Location.create(TEST_URI, vscode_languageserver_types_1.Range.create(0, 6, 0, 19)) }, | ||
{ name: 'br#Bar', kind: vscode_languageserver_types_1.SymbolKind.Field, containerName: 'html', location: vscode_languageserver_types_1.Location.create(TEST_URI, vscode_languageserver_types_1.Range.create(0, 19, 0, 30)) }, | ||
]; | ||
testSymbolsFor(content, expected); | ||
}); | ||
test('No attrib', function () { | ||
var content = '<html><body><div></div></body></html>'; | ||
var expected = [ | ||
{ name: 'html', kind: vscode_languageserver_types_1.SymbolKind.Field, containerName: '', location: vscode_languageserver_types_1.Location.create(TEST_URI, vscode_languageserver_types_1.Range.create(0, 0, 0, 37)) }, | ||
{ name: 'body', kind: vscode_languageserver_types_1.SymbolKind.Field, containerName: 'html', location: vscode_languageserver_types_1.Location.create(TEST_URI, vscode_languageserver_types_1.Range.create(0, 6, 0, 30)) }, | ||
{ name: 'div', kind: vscode_languageserver_types_1.SymbolKind.Field, containerName: 'body', location: vscode_languageserver_types_1.Location.create(TEST_URI, vscode_languageserver_types_1.Range.create(0, 12, 0, 23)) } | ||
]; | ||
testSymbolsFor(content, expected); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=symbols.test.js.map |
{ | ||
"name": "vscode-html-languageservice", | ||
"version": "2.0.1", | ||
"version": "2.0.2-next.1", | ||
"description": "Language service for HTML", | ||
@@ -19,2 +19,3 @@ "main": "./lib/htmlLanguageService.js", | ||
"typescript": "^2.1.5", | ||
"cpy-cli": "^1.0.1", | ||
"@types/node": "^6.0.51", | ||
@@ -30,4 +31,4 @@ "@types/mocha": "^2.2.33" | ||
"prepublish": "npm run compile", | ||
"compile": "tsc -p ./src && cp ./src/beautify/*.js ./lib/beautify", | ||
"watch": "cp ./src/beautify/*.js ./lib/beautify && tsc -w -p ./src", | ||
"compile": "tsc -p ./src && cpy ./src/beautify/*.js ./lib/beautify", | ||
"watch": "cpy ./src/beautify/*.js ./lib/beautify && tsc -w -p ./src", | ||
"test": "npm run compile && mocha", | ||
@@ -34,0 +35,0 @@ "install-types-next": "npm install vscode-languageserver-types@next -f -S" |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
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
320351
5431
5
2