vscode-html-languageservice
Advanced tools
Comparing version 2.0.0 to 2.0.1
@@ -27,3 +27,5 @@ import { TextDocument, Position, CompletionItem, CompletionList, Hover, Range, SymbolInformation, Diagnostic, TextEdit, DocumentHighlight, FormattingOptions, MarkedString, DocumentLink } from 'vscode-languageserver-types'; | ||
parent: Node; | ||
attributeNames?: string[]; | ||
attributes?: { | ||
[name: string]: string; | ||
}; | ||
} | ||
@@ -83,3 +85,3 @@ export declare enum TokenType { | ||
export interface DocumentContext { | ||
resolveReference(ref: string): string; | ||
resolveReference(ref: string, base?: string): string; | ||
} | ||
@@ -94,3 +96,4 @@ export interface LanguageService { | ||
findDocumentLinks(document: TextDocument, documentContext: DocumentContext): DocumentLink[]; | ||
findDocumentSymbols(document: TextDocument, htmlDocument: HTMLDocument): SymbolInformation[]; | ||
} | ||
export declare function getLanguageService(): LanguageService; |
@@ -7,3 +7,3 @@ (function (factory) { | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports", "./parser/htmlScanner", "./parser/htmlParser", "./services/htmlCompletion", "./services/htmlHover", "./services/htmlFormatter", "./services/htmlLinks", "./services/htmlHighlighting", "vscode-languageserver-types"], factory); | ||
define(["require", "exports", "./parser/htmlScanner", "./parser/htmlParser", "./services/htmlCompletion", "./services/htmlHover", "./services/htmlFormatter", "./services/htmlLinks", "./services/htmlHighlighting", "./services/htmlSymbolsProvider", "vscode-languageserver-types"], factory); | ||
} | ||
@@ -23,2 +23,3 @@ })(function (require, exports) { | ||
var htmlHighlighting_1 = require("./services/htmlHighlighting"); | ||
var htmlSymbolsProvider_1 = require("./services/htmlSymbolsProvider"); | ||
var vscode_languageserver_types_1 = require("vscode-languageserver-types"); | ||
@@ -84,3 +85,4 @@ exports.TextDocument = vscode_languageserver_types_1.TextDocument; | ||
findDocumentHighlights: htmlHighlighting_1.findDocumentHighlights, | ||
findDocumentLinks: htmlLinks_1.findDocumentLinks | ||
findDocumentLinks: htmlLinks_1.findDocumentLinks, | ||
findDocumentSymbols: htmlSymbolsProvider_1.findDocumentSymbols | ||
}; | ||
@@ -87,0 +89,0 @@ } |
@@ -25,2 +25,8 @@ (function (factory) { | ||
} | ||
Object.defineProperty(Node.prototype, "attributeNames", { | ||
get: function () { return Object.keys(this.attributes); }, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
; | ||
Node.prototype.isSameTag = function (tagInLowerCase) { | ||
@@ -74,2 +80,3 @@ return this.tag && tagInLowerCase && this.tag.length === tagInLowerCase.length && this.tag.toLowerCase() === tagInLowerCase; | ||
var endTagStart = -1; | ||
var pendingAttribute = null; | ||
var token = scanner.scan(); | ||
@@ -122,8 +129,16 @@ while (token !== htmlScanner_1.TokenType.EOS) { | ||
case htmlScanner_1.TokenType.AttributeName: | ||
var attributeNames = curr.attributeNames; | ||
if (!attributeNames) { | ||
curr.attributeNames = attributeNames = []; | ||
var attributeName = pendingAttribute = scanner.getTokenText(); | ||
var attributes = curr.attributes; | ||
if (!attributes) { | ||
curr.attributes = attributes = {}; | ||
} | ||
attributeNames.push(scanner.getTokenText()); | ||
attributes[pendingAttribute] = null; // Support valueless attributes such as 'checked' | ||
break; | ||
case htmlScanner_1.TokenType.AttributeValue: | ||
var value = scanner.getTokenText(); | ||
if (attributes && pendingAttribute) { | ||
attributes[pendingAttribute] = value; | ||
pendingAttribute = null; | ||
} | ||
break; | ||
} | ||
@@ -130,0 +145,0 @@ token = scanner.scan(); |
@@ -23,3 +23,3 @@ (function (factory) { | ||
} | ||
function getWorkspaceUrl(modelAbsoluteUri, tokenContent, documentContext) { | ||
function getWorkspaceUrl(modelAbsoluteUri, tokenContent, documentContext, base) { | ||
if (/^\s*javascript\:/i.test(tokenContent) || /^\s*\#/i.test(tokenContent) || /[\n\r]/.test(tokenContent)) { | ||
@@ -42,7 +42,7 @@ return null; | ||
if (documentContext) { | ||
return documentContext.resolveReference(tokenContent); | ||
return documentContext.resolveReference(tokenContent, base); | ||
} | ||
return tokenContent; | ||
} | ||
function createLink(document, documentContext, attributeValue, startOffset, endOffset) { | ||
function createLink(document, documentContext, attributeValue, startOffset, endOffset, base) { | ||
var documentUri = vscode_uri_1.default.parse(document.uri); | ||
@@ -57,3 +57,3 @@ var tokenContent = stripQuotes(attributeValue); | ||
} | ||
var workspaceUrl = getWorkspaceUrl(documentUri, tokenContent, documentContext); | ||
var workspaceUrl = getWorkspaceUrl(documentUri, tokenContent, documentContext, base); | ||
if (!workspaceUrl || !isValidURI(workspaceUrl)) { | ||
@@ -82,4 +82,12 @@ return null; | ||
var afterHrefOrSrc = false; | ||
var afterBase = false; | ||
var base = void 0; | ||
while (token !== htmlScanner_1.TokenType.EOS) { | ||
switch (token) { | ||
case htmlScanner_1.TokenType.StartTag: | ||
if (!base) { | ||
var tagName = scanner.getTokenText().toLowerCase(); | ||
afterBase = tagName === 'base'; | ||
} | ||
break; | ||
case htmlScanner_1.TokenType.AttributeName: | ||
@@ -92,6 +100,10 @@ var attributeName = scanner.getTokenText().toLowerCase(); | ||
var attributeValue = scanner.getTokenText(); | ||
var link = createLink(document, documentContext, attributeValue, scanner.getTokenOffset(), scanner.getTokenEnd()); | ||
var link = createLink(document, documentContext, attributeValue, scanner.getTokenOffset(), scanner.getTokenEnd(), base); | ||
if (link) { | ||
newLinks.push(link); | ||
} | ||
if (afterBase && typeof base === 'undefined') { | ||
base = stripQuotes(attributeValue); | ||
} | ||
afterBase = false; | ||
afterHrefOrSrc = false; | ||
@@ -98,0 +110,0 @@ } |
@@ -20,19 +20,23 @@ (function (factory) { | ||
suite('HTML Link Detection', function () { | ||
function getDocumentContext(documentUrl) { | ||
return { | ||
resolveReference: function (ref, base) { | ||
if (base) { | ||
documentUrl = url.resolve(documentUrl, base); | ||
} | ||
return url.resolve(documentUrl, ref); | ||
} | ||
}; | ||
} | ||
function testLinkCreation(modelUrl, tokenContent, expected) { | ||
var documentContext = { | ||
resolveReference: function (ref) { return url.resolve(modelUrl, ref); } | ||
}; | ||
var document = vscode_languageserver_types_1.TextDocument.create(modelUrl, 'html', 0, "<a href=\"" + tokenContent + "\">"); | ||
var ls = htmlLanguageService.getLanguageService(); | ||
var links = ls.findDocumentLinks(document, documentContext); | ||
var links = ls.findDocumentLinks(document, getDocumentContext(modelUrl)); | ||
assert.equal(links[0] && links[0].target, expected); | ||
} | ||
function testLinkDetection(value, expectedLinkLocations) { | ||
function testLinkDetection(value, expectedLinks) { | ||
var document = vscode_languageserver_types_1.TextDocument.create('test://test/test.html', 'html', 0, value); | ||
var documentContext = { | ||
resolveReference: function (ref) { return url.resolve(document.uri, ref); } | ||
}; | ||
var ls = htmlLanguageService.getLanguageService(); | ||
var links = ls.findDocumentLinks(document, documentContext); | ||
assert.deepEqual(links.map(function (l) { return l.range.start.character; }), expectedLinkLocations); | ||
var links = ls.findDocumentLinks(document, getDocumentContext(document.uri)); | ||
assert.deepEqual(links.map(function (l) { return ({ offset: l.range.start.character, target: l.target }); }), expectedLinks); | ||
} | ||
@@ -69,7 +73,9 @@ test('Link creation', function () { | ||
test('Link detection', function () { | ||
testLinkDetection('<img src="foo.png">', [10]); | ||
testLinkDetection('<a href="http://server/foo.html">', [9]); | ||
testLinkDetection('<img src="foo.png">', [{ offset: 10, target: 'test://test/foo.png' }]); | ||
testLinkDetection('<a href="http://server/foo.html">', [{ offset: 9, target: 'http://server/foo.html' }]); | ||
testLinkDetection('<img src="">', []); | ||
testLinkDetection('<LINK HREF="a.html">', [12]); | ||
testLinkDetection('<LINK HREF="a.html">', [{ offset: 12, target: 'test://test/a.html' }]); | ||
testLinkDetection('<LINK HREF="a.html\n>\n', []); | ||
testLinkDetection('<html><base href="docs/"><img src="foo.png"></html>', [{ offset: 18, target: 'test://test/docs/' }, { offset: 35, target: 'test://test/docs/foo.png' }]); | ||
testLinkDetection('<html><base href="http://www.example.com/page.html"><img src="foo.png"></html>', [{ offset: 18, target: 'http://www.example.com/page.html' }, { offset: 62, target: 'http://www.example.com/foo.png' }]); | ||
}); | ||
@@ -76,0 +82,0 @@ }); |
@@ -21,2 +21,5 @@ (function (factory) { | ||
} | ||
function toJSONWithAttributes(node) { | ||
return { tag: node.tag, attributes: node.attributes, children: node.children.map(toJSONWithAttributes) }; | ||
} | ||
function assertDocument(input, expected) { | ||
@@ -31,2 +34,6 @@ var document = htmlParser_1.parse(input); | ||
} | ||
function assertAttributes(input, expected) { | ||
var document = htmlParser_1.parse(input); | ||
assert.deepEqual(document.roots.map(toJSONWithAttributes), expected); | ||
} | ||
test('Simple', function () { | ||
@@ -84,4 +91,32 @@ assertDocument('<html></html>', [{ tag: 'html', start: 0, end: 13, endTagStart: 6, closed: true, children: [] }]); | ||
}); | ||
test('Attributes', function () { | ||
var str = '<div class="these are my-classes" id="test"><span aria-describedby="test"></span></div>'; | ||
assertAttributes(str, [{ | ||
tag: 'div', | ||
attributes: { | ||
class: '"these are my-classes"', | ||
id: '"test"' | ||
}, | ||
children: [{ | ||
tag: 'span', | ||
attributes: { | ||
'aria-describedby': '"test"' | ||
}, | ||
children: [] | ||
}] | ||
}]); | ||
}); | ||
test('Attributes without value', function () { | ||
var str = '<div checked id="test"></div>'; | ||
assertAttributes(str, [{ | ||
tag: 'div', | ||
attributes: { | ||
checked: null, | ||
id: '"test"' | ||
}, | ||
children: [] | ||
}]); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=parser.test.js.map |
{ | ||
"name": "vscode-html-languageservice", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Language service for HTML", | ||
@@ -5,0 +5,0 @@ "main": "./lib/htmlLanguageService.js", |
@@ -15,3 +15,4 @@ # vscode-html-languageservice | ||
- *format* formats the code at the given range. | ||
- *findDocumentLinks* finds al links in the document | ||
- *findDocumentLinks* finds all links in the document | ||
- *findDocumentSymbols* finds all the symbols in the document | ||
@@ -18,0 +19,0 @@ Installation |
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
313423
35
5323
22