[html] use html language service

This commit is contained in:
Martin Aeschlimann 2016-09-14 23:31:30 +02:00
parent ef69b2cc39
commit 8d6a8863fd
28 changed files with 10 additions and 4967 deletions

View file

@ -1,5 +1,10 @@
// ATTENTION - THIS DIRECTORY CONTAINS THIRD PARTY OPEN SOURCE MATERIALS:
[{
"name": "js-beautify",
"version": "1.6.2",
"license": "MIT",
"repositoryURL": "https://github.com/beautify-web/js-beautify"
},{
"name": "textmate/html.tmbundle",
"version": "0.0.0",
"license": "TextMate Bundle License",

View file

@ -9,12 +9,14 @@
},
"dependencies": {
"vscode-languageserver": "^2.6.0-next.3",
"vscode-nls": "^1.0.4",
"vscode-uri": "^0.0.7"
"vscode-html-languageservice": "1.0.0-next.1",
"vscode-nls": "^1.0.4"
},
"scripts": {
"compile": "gulp compile-extension:json-server",
"watch": "gulp watch-extension:json-server",
"install-service-next": "npm install vscode-html-languageservice@next -f -S",
"install-service-local": "npm install ../../../../vscode-html-languageservice -f -S",
"install-server-next": "npm install vscode-languageserver@next -f -S",
"install-server-local": "npm install ../../../../vscode-languageserver-node/server -f -S"
}

View file

@ -9,7 +9,7 @@ import {
TextDocuments, TextDocument, InitializeParams, InitializeResult
} from 'vscode-languageserver';
import {HTMLDocument, getLanguageService, CompletionConfiguration, HTMLFormatConfiguration} from './service/htmlLanguageService';
import {HTMLDocument, getLanguageService, CompletionConfiguration, HTMLFormatConfiguration} from 'vscode-html-languageservice';
import * as nls from 'vscode-nls';
nls.config(process.env['VSCODE_NLS_CONFIG']);

View file

@ -1,51 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import {parse} from './parser/htmlParser';
import {doComplete} from './services/htmlCompletion';
import {format} from './services/htmlFormatter';
import {findDocumentLinks} from './services/htmlLinks';
import {findDocumentHighlights} from './services/htmlHighlighting';
import {TextDocument, Position, CompletionItem, CompletionList, Hover, Range, SymbolInformation, Diagnostic, TextEdit, DocumentHighlight, FormattingOptions, MarkedString, DocumentLink } from 'vscode-languageserver-types';
export {TextDocument, Position, CompletionItem, CompletionList, Hover, Range, SymbolInformation, Diagnostic, TextEdit, DocumentHighlight, FormattingOptions, MarkedString, DocumentLink };
export interface HTMLFormatConfiguration {
tabSize: number;
insertSpaces: boolean;
wrapLineLength: number;
unformatted: string;
indentInnerHtml: boolean;
preserveNewLines: boolean;
maxPreserveNewLines: number;
indentHandlebars: boolean;
endWithNewline: boolean;
extraLiners: string;
}
export interface CompletionConfiguration {
[provider: string]: boolean;
}
export declare type HTMLDocument = {};
export interface LanguageService {
parseHTMLDocument(document: TextDocument): HTMLDocument;
findDocumentHighlights(document: TextDocument, position: Position, htmlDocument: HTMLDocument): DocumentHighlight[];
doComplete(document: TextDocument, position: Position, htmlDocument: HTMLDocument, options?: CompletionConfiguration): CompletionList;
format(document: TextDocument, range: Range, options: HTMLFormatConfiguration): TextEdit[];
findDocumentLinks(document: TextDocument, workspacePath: string): DocumentLink[];
}
export function getLanguageService(): LanguageService {
return {
parseHTMLDocument: document => parse(document.getText()),
doComplete,
format,
findDocumentHighlights,
findDocumentLinks
};
}

View file

@ -1,9 +0,0 @@
// June 2012
// ATTENTION - THIS DIRECTORY CONTAINS THIRD PARTY OPEN SOURCE MATERIALS: THEY ARE CLEARED ONLY FOR LIMITED USE BY MONACO FOR THE MONACO PRODUCT. DO NOT USE OR SHARE THIS CODE WITHOUT APPROVAL PURSUANT TO THE MICROSOFT OPEN SOURCE SOFTWARE APPROVAL POLICY.APPROVAL
[{
"name": "js-beautify",
"version": "1.6.2",
"license": "MIT",
"repositoryURL": "https://github.com/beautify-web/js-beautify"
}]

View file

@ -1,18 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
export interface IBeautifyCSSOptions {
indent_size?: number; // (4) — indentation size,
indent_char?: string; // (space) — character to indent with,
selector_separator_newline?: boolean; // (true) - separate selectors with newline or not (e.g. "a,\nbr" or "a, br")
end_with_newline?: boolean; // (false) - end with a newline
newline_between_rules?: boolean; // (true) - add a new line after every css rule
}
export interface IBeautifyCSS {
(value:string, options:IBeautifyCSSOptions): string;
}
export declare var css_beautify:IBeautifyCSS;

View file

@ -1,493 +0,0 @@
// copied https://raw.githubusercontent.com/beautify-web/js-beautify/master/js/lib/beautify-css.js
/*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */
/*
The MIT License (MIT)
Copyright (c) 2007-2013 Einar Lielmanis and contributors.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
CSS Beautifier
---------------
Written by Harutyun Amirjanyan, (amirjanyan@gmail.com)
Based on code initially developed by: Einar Lielmanis, <einar@jsbeautifier.org>
http://jsbeautifier.org/
Usage:
css_beautify(source_text);
css_beautify(source_text, options);
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
e.g
css_beautify(css_source_text, {
'indent_size': 1,
'indent_char': '\t',
'selector_separator': ' ',
'end_with_newline': false,
'newline_between_rules': true
});
*/
// http://www.w3.org/TR/CSS21/syndata.html#tokenization
// http://www.w3.org/TR/css3-syntax/
(function() {
function css_beautify(source_text, options) {
options = options || {};
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 indentCharacter = options.indent_char || ' ';
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';
// compatibility
if (typeof indentSize === "string") {
indentSize = parseInt(indentSize, 10);
}
if(options.indent_with_tabs){
indentCharacter = '\t';
indentSize = 1;
}
eol = eol.replace(/\\r/, '\r').replace(/\\n/, '\n')
// tokenizer
var whiteRe = /^\s+$/;
var wordRe = /[\w$\-_]/;
var pos = -1,
ch;
var parenLevel = 0;
function next() {
ch = source_text.charAt(++pos);
return ch || '';
}
function peek(skipWhitespace) {
var result = '';
var prev_pos = pos;
if (skipWhitespace) {
eatWhitespace();
}
result = source_text.charAt(pos + 1) || '';
pos = prev_pos - 1;
next();
return result;
}
function eatString(endChars) {
var start = pos;
while (next()) {
if (ch === "\\") {
next();
} else if (endChars.indexOf(ch) !== -1) {
break;
} else if (ch === "\n") {
break;
}
}
return source_text.substring(start, pos + 1);
}
function peekString(endChar) {
var prev_pos = pos;
var str = eatString(endChar);
pos = prev_pos - 1;
next();
return str;
}
function eatWhitespace() {
var result = '';
while (whiteRe.test(peek())) {
next();
result += ch;
}
return result;
}
function skipWhitespace() {
var result = '';
if (ch && whiteRe.test(ch)) {
result = ch;
}
while (whiteRe.test(next())) {
result += ch;
}
return result;
}
function eatComment(singleLine) {
var start = pos;
singleLine = peek() === "/";
next();
while (next()) {
if (!singleLine && ch === "*" && peek() === "/") {
next();
break;
} else if (singleLine && ch === "\n") {
return source_text.substring(start, pos);
}
}
return source_text.substring(start, pos) + ch;
}
function lookBack(str) {
return source_text.substring(pos - str.length, pos).toLowerCase() ===
str;
}
// Nested pseudo-class if we are insideRule
// and the next special character found opens
// a new block
function foundNestedPseudoClass() {
var openParen = 0;
for (var i = pos + 1; i < source_text.length; i++) {
var ch = source_text.charAt(i);
if (ch === "{") {
return true;
} else if (ch === '(') {
// pseudoclasses can contain ()
openParen += 1;
} else if (ch === ')') {
if (openParen == 0) {
return false;
}
openParen -= 1;
} else if (ch === ";" || ch === "}") {
return false;
}
}
return false;
}
// printer
var basebaseIndentString = source_text.match(/^[\t ]*/)[0];
var singleIndent = new Array(indentSize + 1).join(indentCharacter);
var indentLevel = 0;
var nestedLevel = 0;
function indent() {
indentLevel++;
basebaseIndentString += singleIndent;
}
function outdent() {
indentLevel--;
basebaseIndentString = basebaseIndentString.slice(0, -indentSize);
}
var print = {};
print["{"] = function(ch) {
print.singleSpace();
output.push(ch);
print.newLine();
};
print["}"] = function(ch) {
print.newLine();
output.push(ch);
print.newLine();
};
print._lastCharWhitespace = function() {
return whiteRe.test(output[output.length - 1]);
};
print.newLine = function(keepWhitespace) {
if (output.length) {
if (!keepWhitespace && output[output.length - 1] !== '\n') {
print.trim();
}
output.push('\n');
if (basebaseIndentString) {
output.push(basebaseIndentString);
}
}
};
print.singleSpace = function() {
if (output.length && !print._lastCharWhitespace()) {
output.push(' ');
}
};
print.preserveSingleSpace = function() {
if (isAfterSpace) {
print.singleSpace();
}
};
print.trim = function() {
while (print._lastCharWhitespace()) {
output.pop();
}
};
var output = [];
/*_____________________--------------------_____________________*/
var insideRule = false;
var insidePropertyValue = false;
var enteringConditionalGroup = false;
var top_ch = '';
var last_top_ch = '';
while (true) {
var whitespace = skipWhitespace();
var isAfterSpace = whitespace !== '';
var isAfterNewline = whitespace.indexOf('\n') !== -1;
last_top_ch = top_ch;
top_ch = ch;
if (!ch) {
break;
} else if (ch === '/' && peek() === '*') { /* css comment */
var header = indentLevel === 0;
if (isAfterNewline || header) {
print.newLine();
}
output.push(eatComment());
print.newLine();
if (header) {
print.newLine(true);
}
} else if (ch === '/' && peek() === '/') { // single line comment
if (!isAfterNewline && last_top_ch !== '{' ) {
print.trim();
}
print.singleSpace();
output.push(eatComment());
print.newLine();
} else if (ch === '@') {
print.preserveSingleSpace();
output.push(ch);
// strip trailing space, if present, for hash property checks
var variableOrRule = peekString(": ,;{}()[]/='\"");
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();
}
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('}'));
} else if (ch === '{') {
if (peek(true) === '}') {
eatWhitespace();
next();
print.singleSpace();
output.push("{}");
print.newLine();
if (newline_between_rules && indentLevel === 0) {
print.newLine(true);
}
} else {
indent();
print["{"](ch);
// when entering conditional groups, only rulesets are allowed
if (enteringConditionalGroup) {
enteringConditionalGroup = false;
insideRule = (indentLevel > nestedLevel);
} else {
// otherwise, declarations are also allowed
insideRule = (indentLevel >= nestedLevel);
}
}
} else if (ch === '}') {
outdent();
print["}"](ch);
insideRule = false;
insidePropertyValue = false;
if (nestedLevel) {
nestedLevel--;
}
if (newline_between_rules && indentLevel === 0) {
print.newLine(true);
}
} else if (ch === ":") {
eatWhitespace();
if ((insideRule || enteringConditionalGroup) &&
!(lookBack("&") || foundNestedPseudoClass())) {
// 'property: value' delimiter
// which could be in a conditional group query
insidePropertyValue = true;
output.push(':');
print.singleSpace();
} else {
// sass/less parent reference don't use a space
// sass nested pseudo-class don't use a space
if (peek() === ":") {
// pseudo-element
next();
output.push("::");
} else {
// pseudo-class
output.push(':');
}
}
} else if (ch === '"' || ch === '\'') {
print.preserveSingleSpace();
output.push(eatString(ch));
} else if (ch === ';') {
insidePropertyValue = false;
output.push(ch);
print.newLine();
} else if (ch === '(') { // may be a url
if (lookBack("url")) {
output.push(ch);
eatWhitespace();
if (next()) {
if (ch !== ')' && ch !== '"' && ch !== '\'') {
output.push(eatString(')'));
} else {
pos--;
}
}
} else {
parenLevel++;
print.preserveSingleSpace();
output.push(ch);
eatWhitespace();
}
} else if (ch === ')') {
output.push(ch);
parenLevel--;
} else if (ch === ',') {
output.push(ch);
eatWhitespace();
if (selectorSeparatorNewline && !insidePropertyValue && parenLevel < 1) {
print.newLine();
} else {
print.singleSpace();
}
} else if (ch === ']') {
output.push(ch);
} else if (ch === '[') {
print.preserveSingleSpace();
output.push(ch);
} else if (ch === '=') { // no whitespace before or after
eatWhitespace()
ch = '=';
output.push(ch);
} else {
print.preserveSingleSpace();
output.push(ch);
}
}
var sweetCode = '';
if (basebaseIndentString) {
sweetCode += basebaseIndentString;
}
sweetCode += output.join('').replace(/[\r\n\t ]+$/, '');
// establish end_with_newline
if (end_with_newline) {
sweetCode += '\n';
}
if (eol != '\n') {
sweetCode = sweetCode.replace(/[\n]/g, eol);
}
return sweetCode;
}
// https://developer.mozilla.org/en-US/docs/Web/CSS/At-rule
css_beautify.NESTED_AT_RULE = {
"@page": true,
"@font-face": true,
"@keyframes": true,
// also in CONDITIONAL_GROUP_RULE below
"@media": true,
"@supports": true,
"@document": true
};
css_beautify.CONDITIONAL_GROUP_RULE = {
"@media": true,
"@supports": true,
"@document": true
};
/*global define */
if (typeof define === "function" && define.amd) {
// Add support for AMD ( https://github.com/amdjs/amdjs-api/wiki/AMD#defineamd-property- )
define([], function() {
return {
css_beautify: css_beautify
};
});
} else if (typeof exports !== "undefined") {
// Add support for CommonJS. Just put this file somewhere on your require.paths
// and you will be able to `var html_beautify = require("beautify").html_beautify`.
exports.css_beautify = css_beautify;
} else if (typeof window !== "undefined") {
// If we're running a web page and don't have either of the above, add our one global
window.css_beautify = css_beautify;
} else if (typeof global !== "undefined") {
// If we don't even have window, try global.
global.css_beautify = css_beautify;
}
}());

View file

@ -1,85 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
export interface IBeautifyHTMLOptions {
/**
* indent <head> and <body> sections
* default false
*/
indent_inner_html?: boolean;
/**
* indentation size
* default 4
*/
indent_size?: number; // indentation size,
/**
* character to indent with
* default space
*/
indent_char?: string; // character to indent with,
/**
* maximum amount of characters per line (0 = disable)
* default 250
*/
wrap_line_length?: number;
/**
* put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line, or attempt to keep them where they are.
* "collapse" | "expand" | "end-expand" | "none"
* default "collapse"
*/
brace_style?: string;
/**
* list of tags, that shouldn't be reformatted
* defaults to inline tags
*/
unformatted?: string[];
/**
* "keep"|"separate"|"normal"
* default normal
*/
indent_scripts?: string;
/**
* whether existing line breaks before elements should be preserved. Only works before elements, not inside tags or for text.
* default true
*/
preserve_newlines?: boolean;
/**
* maximum number of line breaks to be preserved in one chunk
* default unlimited
*/
max_preserve_newlines?: number;
/**
* format and indent {{#foo}} and {{/foo}}
* default false
*/
indent_handlebars?: boolean;
/**
* end with a newline
* default false
*/
end_with_newline?: boolean;
/**
* List of tags that should have an extra newline before them.
* default [head,body,/html]
*/
extra_liners?: string[];
}
export interface IBeautifyHTML {
(value:string, options:IBeautifyHTMLOptions): string;
}
export declare var html_beautify:IBeautifyHTML;

File diff suppressed because it is too large Load diff

View file

@ -1,9 +0,0 @@
The MIT License (MIT)
Copyright (c) 2007-2013 Einar Lielmanis and contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View file

@ -1,11 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/*
* Mock for the JS formatter. Ignore formatting of JS content in HTML.
*/
export function js_beautify(js_source_text: string, options: any) {
// no formatting
return js_source_text;
}

View file

@ -1,123 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import { TokenType, createScanner } from './htmlScanner';
import { findFirst } from '../utils/arrays';
import { isEmptyElement, isSameTag } from './htmlTags';
export class Node {
public tag: string;
public closed: boolean;
public endTagStart: number;
constructor(public start: number, public end: number, public children: Node[], public parent: Node) {
}
public get firstChild(): Node { return this.children[0]; }
public get lastChild(): Node { return this.children.length ? this.children[this.children.length - 1] : void 0; }
public findNodeBefore(offset: number): Node {
let idx = findFirst(this.children, c => offset <= c.start) - 1;
if (idx >= 0) {
let child = this.children[idx];
if (offset > child.start) {
if (offset < child.end) {
return child.findNodeBefore(offset);
}
let lastChild = child.lastChild;
if (lastChild && lastChild.end === child.end) {
return child.findNodeBefore(offset);
}
return child;
}
}
return this;
}
public findNodeAt(offset: number): Node {
let idx = findFirst(this.children, c => offset <= c.start) - 1;
if (idx >= 0) {
let child = this.children[idx];
if (offset > child.start && offset <= child.end) {
return child.findNodeAt(offset);
}
}
return this;
}
}
export interface HTMLDocument {
roots: Node[];
findNodeBefore(offset: number): Node;
findNodeAt(offset: number): Node;
}
export function parse(text: string): HTMLDocument {
let scanner = createScanner(text);
let htmlDocument = new Node(0, text.length, [], null);
let curr = htmlDocument;
let endTagStart: number = -1;
let token = scanner.scan();
while (token !== TokenType.EOS) {
switch (token) {
case TokenType.StartTagOpen:
let child = new Node(scanner.getTokenOffset(), text.length, [], curr);
curr.children.push(child);
curr = child;
break;
case TokenType.StartTag:
curr.tag = scanner.getTokenText();
break;
case TokenType.StartTagClose:
curr.end = scanner.getTokenEnd(); // might be later set to end tag position
if (isEmptyElement(curr.tag) && curr !== htmlDocument) {
curr.closed = true;
curr = curr.parent;
}
break;
case TokenType.EndTagOpen:
endTagStart = scanner.getTokenOffset();
break;
case TokenType.EndTag:
let closeTag = scanner.getTokenText();
while (!isSameTag(curr.tag, closeTag) && curr !== htmlDocument) {
curr.end = endTagStart;
curr.closed = false;
curr = curr.parent;
}
if (curr !== htmlDocument) {
curr.closed = true;
curr.endTagStart = endTagStart;
}
break;
case TokenType.StartTagSelfClose:
if (curr !== htmlDocument) {
curr.closed = true;
curr.end = scanner.getTokenEnd();
curr = curr.parent;
}
break;
case TokenType.EndTagClose:
if (curr !== htmlDocument) {
curr.end = scanner.getTokenEnd();
curr = curr.parent;
}
break;
}
token = scanner.scan();
}
while (curr !== htmlDocument) {
curr.end = text.length;
curr.closed = false;
curr = curr.parent;
}
return {
roots: htmlDocument.children,
findNodeBefore: htmlDocument.findNodeBefore.bind(htmlDocument),
findNodeAt: htmlDocument.findNodeAt.bind(htmlDocument)
};
}

View file

@ -1,451 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as nls from 'vscode-nls';
let localize = nls.loadMessageBundle();
export enum TokenType {
StartCommentTag,
Comment,
EndCommentTag,
StartTagOpen,
StartTagClose,
StartTagSelfClose,
StartTag,
EndTagOpen,
EndTagClose,
EndTag,
DelimiterAssign,
AttributeName,
AttributeValue,
StartDoctypeTag,
Doctype,
EndDoctypeTag,
Content,
Whitespace,
Unknown,
Script,
Styles,
EOS
}
export interface IToken {
type: TokenType;
offset: number;
len: number;
}
class MultiLineStream {
private source: string;
private len: number;
private position: number;
constructor(source: string, position: number) {
this.source = source;
this.len = source.length;
this.position = position;
}
public eos(): boolean {
return this.len <= this.position;
}
public getSource(): string {
return this.source;
}
public pos(): number {
return this.position;
}
public goBackTo(pos: number): void {
this.position = pos;
}
public goBack(n: number): void {
this.position -= n;
}
public advance(n: number): void {
this.position += n;
}
public goToEnd(): void {
this.position = this.source.length;
}
public nextChar(): number {
return this.source.charCodeAt(this.position++) || 0;
}
public peekChar(n: number = 0): number {
return this.source.charCodeAt(this.position + n) || 0;
}
public advanceIfChar(ch: number): boolean {
if (ch === this.source.charCodeAt(this.position)) {
this.position++;
return true;
}
return false;
}
public advanceIfChars(ch: number[]): boolean {
let i: number;
if (this.position + ch.length > this.source.length) {
return false;
}
for (i = 0; i < ch.length; i++) {
if (this.source.charCodeAt(this.position + i) !== ch[i]) {
return false;
}
}
this.advance(i);
return true;
}
public advanceIfRegExp(regex: RegExp): string {
let str = this.source.substr(this.position);
let match = str.match(regex);
if (match) {
this.position = this.position + match.index + match[0].length;
return match[0];
}
return '';
}
public advanceUntilRegExp(regex: RegExp): string {
let str = this.source.substr(this.position);
let match = str.match(regex);
if (match) {
this.position = this.position + match.index;
return match[0];
}
return '';
}
public advanceUntilChar(ch: number): boolean {
while (this.position < this.source.length) {
if (this.source.charCodeAt(this.position) === ch) {
return true;
}
this.advance(1);
}
return false;
}
public advanceUntilChars(ch: number[]): boolean {
while (this.position + ch.length < this.source.length) {
for (let i = 0; i < ch.length; i++) {
if (this.source.charCodeAt(this.position + i) !== ch[i]) {
break;
}
return true;
}
this.advance(1);
}
return false;
}
public skipWhitespace(): boolean {
let n = this.advanceWhileChar(ch => {
return ch === _WSP || ch === _TAB || ch === _NWL || ch === _LFD || ch === _CAR;
});
return n > 0;
}
public advanceWhileChar(condition: (ch: number) => boolean): number {
let posNow = this.position;
while (this.position < this.len && condition(this.source.charCodeAt(this.position))) {
this.position++;
}
return this.position - posNow;
}
}
const _BNG = '!'.charCodeAt(0);
const _MIN = '-'.charCodeAt(0);
const _LAN = '<'.charCodeAt(0);
const _RAN = '>'.charCodeAt(0);
const _FSL = '/'.charCodeAt(0);
const _EQS = '='.charCodeAt(0);
const _DQO = '"'.charCodeAt(0);
const _SQO = '\''.charCodeAt(0);
const _NWL = '\n'.charCodeAt(0);
const _CAR = '\r'.charCodeAt(0);
const _LFD = '\f'.charCodeAt(0);
const _WSP = ' '.charCodeAt(0);
const _TAB = '\t'.charCodeAt(0);
export enum ScannerState {
WithinContent,
AfterOpeningStartTag,
AfterOpeningEndTag,
WithinDoctype,
WithinTag,
WithinEndTag,
WithinComment,
WithinScriptContent,
WithinStyleContent,
AfterAttributeName,
BeforeAttributeValue
}
export interface Scanner {
scan(): TokenType;
getTokenType(): TokenType;
getTokenOffset(): number;
getTokenLength(): number;
getTokenEnd(): number;
getTokenText(): string;
getTokenError(): string;
getScannerState(): ScannerState;
}
const htmlScriptContents = {
'text/x-handlebars-template': true
};
export function createScanner(input: string, initialOffset = 0, initialState: ScannerState = ScannerState.WithinContent): Scanner {
let stream = new MultiLineStream(input, initialOffset);
let state = initialState;
let tokenOffset: number = 0;
let tokenType: number = void 0;
let tokenError: string;
let hasSpaceAfterTag: boolean;
let lastTag: string;
let lastAttributeName: string;
let lastTypeValue: string;
function nextElementName(): string {
return stream.advanceIfRegExp(/^[_:\w][_:\w-.\d]*/).toLowerCase();
}
function nextAttributeName(): string {
return stream.advanceIfRegExp(/^[^\s"'>/=\x00-\x0F\x7F\x80-\x9F]*/).toLowerCase();
}
function finishToken(offset: number, type: TokenType, errorMessage?: string): TokenType {
tokenType = type;
tokenOffset = offset;
tokenError = errorMessage;
return type;
}
function scan(): TokenType {
let offset = stream.pos();
if (stream.eos()) {
return finishToken(offset, TokenType.EOS);
}
let errorMessage;
switch (state) {
case ScannerState.WithinComment:
if (stream.advanceIfChars([_MIN, _MIN, _RAN])) { // -->
state = ScannerState.WithinContent;
return finishToken(offset, TokenType.EndCommentTag);
}
stream.advanceUntilChars([_MIN, _MIN, _RAN]); // -->
return finishToken(offset, TokenType.Comment);
case ScannerState.WithinDoctype:
if (stream.advanceIfChar(_RAN)) {
state = ScannerState.WithinContent;
return finishToken(offset, TokenType.EndDoctypeTag);
}
stream.advanceUntilChar(_RAN); // >
return finishToken(offset, TokenType.Doctype);
case ScannerState.WithinContent:
if (stream.advanceIfChar(_LAN)) { // <
if (!stream.eos() && stream.peekChar() === _BNG) { // !
if (stream.advanceIfChars([_BNG, _MIN, _MIN])) { // <!--
state = ScannerState.WithinComment;
return finishToken(offset, TokenType.StartCommentTag);
}
if (stream.advanceIfRegExp(/^!doctype/i)) {
state = ScannerState.WithinDoctype;
return finishToken(offset, TokenType.StartDoctypeTag);
}
}
if (stream.advanceIfChar(_FSL)) { // /
state = ScannerState.AfterOpeningEndTag;
return finishToken(offset, TokenType.EndTagOpen);
}
state = ScannerState.AfterOpeningStartTag;
return finishToken(offset, TokenType.StartTagOpen);
}
stream.advanceUntilChar(_LAN);
return finishToken(offset, TokenType.Content);
case ScannerState.AfterOpeningEndTag:
let tagName = nextElementName();
if (tagName.length > 0) {
state = ScannerState.WithinEndTag;
return finishToken(offset, TokenType.EndTag);
}
if (stream.skipWhitespace()) { // white space is not valid here
return finishToken(offset, TokenType.Whitespace, localize('error.unexpectedWhitespace', 'Tag name must directly follow the open bracket.'));
}
stream.advanceUntilChar(_RAN);
state = ScannerState.WithinEndTag;
return finishToken(offset, TokenType.Unknown, localize('error.endTagNameExpected', 'End tag name expected.'));
case ScannerState.WithinEndTag:
if (stream.skipWhitespace()) { // white space is valid here
return finishToken(offset, TokenType.Whitespace);
}
if (stream.advanceIfChar(_RAN)) { // >
state = ScannerState.WithinContent;
return finishToken(offset, TokenType.EndTagClose);
}
errorMessage = localize('error.tagNameExpected', 'Closing bracket expected.');
break;
case ScannerState.AfterOpeningStartTag:
lastTag = nextElementName();
lastTypeValue = null;
lastAttributeName = null;
if (lastTag.length > 0) {
hasSpaceAfterTag = false;
state = ScannerState.WithinTag;
return finishToken(offset, TokenType.StartTag);
}
if (stream.skipWhitespace()) { // white space is not valid here
return finishToken(offset, TokenType.Whitespace, localize('error.unexpectedWhitespace', 'Tag name must directly follow the open bracket.'));
}
stream.advanceUntilChar(_RAN);
state = ScannerState.WithinTag;
return finishToken(offset, TokenType.Unknown, localize('error.startTagNameExpected', 'Start tag name expected.'));
case ScannerState.WithinTag:
if (stream.skipWhitespace()) {
hasSpaceAfterTag = true; // remember that we have seen a whitespace
return finishToken(offset, TokenType.Whitespace);
}
if (hasSpaceAfterTag) {
lastAttributeName = nextAttributeName();
if (lastAttributeName.length > 0) {
state = ScannerState.AfterAttributeName;
hasSpaceAfterTag = false;
return finishToken(offset, TokenType.AttributeName);
}
}
if (stream.advanceIfChars([_FSL, _RAN])) { // />
state = ScannerState.WithinContent;
return finishToken(offset, TokenType.StartTagSelfClose);
}
if (stream.advanceIfChar(_RAN)) { // >
if (lastTag === 'script') {
if (lastTypeValue && htmlScriptContents[lastTypeValue]) {
// stay in html
state = ScannerState.WithinContent;
} else {
state = ScannerState.WithinScriptContent;
}
} else if (lastTag === 'style') {
state = ScannerState.WithinStyleContent;
} else {
state = ScannerState.WithinContent;
}
return finishToken(offset, TokenType.StartTagClose);
}
stream.advance(1);
return finishToken(offset, TokenType.Unknown, localize('error.unexpectedCharacterInTag', 'Unexpected character in tag.'));
case ScannerState.AfterAttributeName:
if (stream.skipWhitespace()) {
hasSpaceAfterTag = true;
return finishToken(offset, TokenType.Whitespace);
}
if (stream.advanceIfChar(_EQS)) {
state = ScannerState.BeforeAttributeValue;
return finishToken(offset, TokenType.DelimiterAssign);
}
state = ScannerState.WithinTag;
return scan(); // no advance yet - jump to WithinTag
case ScannerState.BeforeAttributeValue:
if (stream.skipWhitespace()) {
return finishToken(offset, TokenType.Whitespace);
}
let attributeValue = stream.advanceIfRegExp(/^[^\s"'`=<>]+/);
if (attributeValue.length > 0) {
if (lastAttributeName === 'type') {
lastTypeValue = attributeValue;
}
state = ScannerState.WithinTag;
hasSpaceAfterTag = false;
return finishToken(offset, TokenType.AttributeValue);
}
let ch = stream.peekChar();
if (ch === _SQO || ch === _DQO) {
stream.advance(1); // consume quote
if (stream.advanceUntilChar(ch)) {
stream.advance(1); // consume quote
}
if (lastAttributeName === 'type') {
lastTypeValue = stream.getSource().substring(offset + 1, stream.pos() - 1);
}
state = ScannerState.WithinTag;
hasSpaceAfterTag = false;
return finishToken(offset, TokenType.AttributeValue);
}
state = ScannerState.WithinTag;
hasSpaceAfterTag = false;
return scan(); // no advance yet - jump to WithinTag
case ScannerState.WithinScriptContent:
// see http://stackoverflow.com/questions/14574471/how-do-browsers-parse-a-script-tag-exactly
let sciptState = 1;
while (!stream.eos()) {
let match = stream.advanceIfRegExp(/<!--|-->|<\/?script\s*\/?>?/i);
if (match.length === 0) {
stream.goToEnd();
return finishToken(offset, TokenType.Script);
} else if (match === '<!--') {
if (sciptState === 1) {
sciptState = 2;
}
} else if (match === '-->') {
sciptState = 1;
} else if (match[1] !== '/') { // <script
if (sciptState === 2) {
sciptState = 3;
}
} else { // </script
if (sciptState === 3) {
sciptState = 2;
} else {
stream.goBack(match.length); // to the beginning of the closing tag
break;
}
}
}
state = ScannerState.WithinContent;
if (offset < stream.pos()) {
return finishToken(offset, TokenType.Script);
}
return scan(); // no advance yet - jump to content
case ScannerState.WithinScriptContent:
stream.advanceUntilRegExp(/<\/style/i);
state = ScannerState.WithinContent;
if (offset < stream.pos()) {
return finishToken(offset, TokenType.Styles);
}
return scan(); // no advance yet - jump to content
}
stream.advance(1);
state = ScannerState.WithinContent;
return finishToken(offset, TokenType.Unknown, errorMessage);
}
return {
scan,
getTokenType: () => tokenType,
getTokenOffset: () => tokenOffset,
getTokenLength: () => stream.pos() - tokenOffset,
getTokenEnd: () => stream.pos(),
getTokenText: () => stream.getSource().substring(tokenOffset, stream.pos()),
getScannerState: () => state,
getTokenError: () => tokenError
};
}

View file

@ -1,644 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
/*!
BEGIN THIRD PARTY
*/
/*--------------------------------------------------------------------------------------------
* This file is based on or incorporates material from the projects listed below (Third Party IP).
* The original copyright notice and the license under which Microsoft received such Third Party IP,
* are set forth below. Such licenses and notices are provided for informational purposes only.
* Microsoft licenses the Third Party IP to you under the licensing terms for the Microsoft product.
* Microsoft reserves all other rights not expressly granted under this agreement, whether by implication,
* estoppel or otherwise.
*--------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------
* Copyright © 2015 W3C® (MIT, ERCIM, Keio, Beihang). This software or document includes includes material copied
* from or derived from HTML 5.1 W3C Working Draft (http://www.w3.org/TR/2015/WD-html51-20151008/.)"
*--------------------------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------------------------
* Ionic Main Site (https://github.com/driftyco/ionic-site).
* Copyright Drifty Co. http://drifty.com/.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
* MERCHANTABLITY OR NON-INFRINGEMENT.
*
* See the Apache Version 2.0 License for specific language governing permissions
* and limitations under the License.
*--------------------------------------------------------------------------------------------*/
import strings = require('../utils/strings');
import arrays = require('../utils/arrays');
import * as nls from 'vscode-nls';
let localize = nls.loadMessageBundle();
export const EMPTY_ELEMENTS: string[] = ['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'keygen', 'link', 'menuitem', 'meta', 'param', 'source', 'track', 'wbr'];
export function isEmptyElement(e: string): boolean {
return e && arrays.binarySearch(EMPTY_ELEMENTS, e.toLowerCase(), (s1: string, s2: string) => s1.localeCompare(s2)) >= 0;
}
export function isSameTag(t1: string, t2: string): boolean {
return t1 && t2 && t1.toLowerCase() === t2.toLowerCase();
}
export interface IHTMLTagProvider {
getId(): string;
isApplicable(languageId: string);
collectTags(collector: (tag: string, label: string) => void): void;
collectAttributes(tag: string, collector: (attribute: string, type: string) => void): void;
collectValues(tag: string, attribute: string, collector: (value: string) => void): void;
}
export interface ITagSet {
[tag: string]: HTMLTagSpecification;
}
export class HTMLTagSpecification {
constructor(public label: string, public attributes: string[] = []) { }
}
interface IValueSets {
[tag: string]: string[];
}
// HTML tag information sourced from http://www.w3.org/TR/2015/WD-html51-20151008/
export const HTML_TAGS: ITagSet = {
// The root element
html: new HTMLTagSpecification(
localize('tags.html', 'The html element represents the root of an HTML document.'),
['manifest']),
// Document metadata
head: new HTMLTagSpecification(
localize('tags.head', 'The head element represents a collection of metadata for the Document.')),
title: new HTMLTagSpecification(
localize('tags.title', 'The title element represents the document\'s title or name. Authors should use titles that identify their documents even when they are used out of context, for example in a user\'s history or bookmarks, or in search results. The document\'s title is often different from its first heading, since the first heading does not have to stand alone when taken out of context.')),
base: new HTMLTagSpecification(
localize('tags.base', 'The base element allows authors to specify the document base URL for the purposes of resolving relative URLs, and the name of the default browsing context for the purposes of following hyperlinks. The element does not represent any content beyond this information.'),
['href', 'target']),
link: new HTMLTagSpecification(
localize('tags.link', 'The link element allows authors to link their document to other resources.'),
['href', 'crossorigin:xo', 'rel', 'media', 'hreflang', 'type', 'sizes']),
meta: new HTMLTagSpecification(
localize('tags.meta', 'The meta element represents various kinds of metadata that cannot be expressed using the title, base, link, style, and script elements.'),
['name', 'http-equiv', 'content', 'charset']),
style: new HTMLTagSpecification(
localize('tags.style', 'The style element allows authors to embed style information in their documents. The style element is one of several inputs to the styling processing model. The element does not represent content for the user.'),
['media', 'nonce', 'type', 'scoped:v']),
// Sections
body: new HTMLTagSpecification(
localize('tags.body', 'The body element represents the content of the document.'),
['onafterprint', 'onbeforeprint', 'onbeforeunload', 'onhashchange', 'onlanguagechange', 'onmessage', 'onoffline', 'ononline', 'onpagehide', 'onpageshow', 'onpopstate', 'onstorage', 'onunload']),
article: new HTMLTagSpecification(
localize('tags.article', 'The article element represents a complete, or self-contained, composition in a document, page, application, or site and that is, in principle, independently distributable or reusable, e.g. in syndication. This could be a forum post, a magazine or newspaper article, a blog entry, a user-submitted comment, an interactive widget or gadget, or any other independent item of content. Each article should be identified, typically by including a heading (h1h6 element) as a child of the article element.')),
section: new HTMLTagSpecification(
localize('tags.section', 'The section element represents a generic section of a document or application. A section, in this context, is a thematic grouping of content. Each section should be identified, typically by including a heading ( h1- h6 element) as a child of the section element.')),
nav: new HTMLTagSpecification(
localize('tags.nav', 'The nav element represents a section of a page that links to other pages or to parts within the page: a section with navigation links.')),
aside: new HTMLTagSpecification(
localize('tags.aside', 'The aside element represents a section of a page that consists of content that is tangentially related to the content around the aside element, and which could be considered separate from that content. Such sections are often represented as sidebars in printed typography.')),
h1: new HTMLTagSpecification(
localize('tags.h1', 'The h1 element represents a section heading.')),
h2: new HTMLTagSpecification(
localize('tags.h2', 'The h2 element represents a section heading.')),
h3: new HTMLTagSpecification(
localize('tags.h3', 'The h3 element represents a section heading.')),
h4: new HTMLTagSpecification(
localize('tags.h4', 'The h4 element represents a section heading.')),
h5: new HTMLTagSpecification(
localize('tags.h5', 'The h5 element represents a section heading.')),
h6: new HTMLTagSpecification(
localize('tags.h6', 'The h6 element represents a section heading.')),
header: new HTMLTagSpecification(
localize('tags.header', 'The header element represents introductory content for its nearest ancestor sectioning content or sectioning root element. A header typically contains a group of introductory or navigational aids. When the nearest ancestor sectioning content or sectioning root element is the body element, then it applies to the whole page.')),
footer: new HTMLTagSpecification(
localize('tags.footer', 'The footer element represents a footer for its nearest ancestor sectioning content or sectioning root element. A footer typically contains information about its section such as who wrote it, links to related documents, copyright data, and the like.')),
address: new HTMLTagSpecification(
localize('tags.address', 'The address element represents the contact information for its nearest article or body element ancestor. If that is the body element, then the contact information applies to the document as a whole.')),
// Grouping content
p: new HTMLTagSpecification(
localize('tags.p', 'The p element represents a paragraph.')),
hr: new HTMLTagSpecification(
localize('tags.hr', 'The hr element represents a paragraph-level thematic break, e.g. a scene change in a story, or a transition to another topic within a section of a reference book.')),
pre: new HTMLTagSpecification(
localize('tags.pre', 'The pre element represents a block of preformatted text, in which structure is represented by typographic conventions rather than by elements.')),
blockquote: new HTMLTagSpecification(
localize('tags.blockquote', 'The blockquote element represents content that is quoted from another source, optionally with a citation which must be within a footer or cite element, and optionally with in-line changes such as annotations and abbreviations.'),
['cite']),
ol: new HTMLTagSpecification(
localize('tags.ol', 'The ol element represents a list of items, where the items have been intentionally ordered, such that changing the order would change the meaning of the document.'),
['reversed:v', 'start', 'type:lt']),
ul: new HTMLTagSpecification(
localize('tags.ul', 'The ul element represents a list of items, where the order of the items is not important — that is, where changing the order would not materially change the meaning of the document.')),
li: new HTMLTagSpecification(
localize('tags.li', 'The li element represents a list item. If its parent element is an ol, ul, or menu element, then the element is an item of the parent element\'s list, as defined for those elements. Otherwise, the list item has no defined list-related relationship to any other li element.'),
['value']),
dl: new HTMLTagSpecification(
localize('tags.dl', 'The dl element represents an association list consisting of zero or more name-value groups (a description list). A name-value group consists of one or more names (dt elements) followed by one or more values (dd elements), ignoring any nodes other than dt and dd elements. Within a single dl element, there should not be more than one dt element for each name.')),
dt: new HTMLTagSpecification(
localize('tags.dt', 'The dt element represents the term, or name, part of a term-description group in a description list (dl element).')),
dd: new HTMLTagSpecification(
localize('tags.dd', 'The dd element represents the description, definition, or value, part of a term-description group in a description list (dl element).')),
figure: new HTMLTagSpecification(
localize('tags.figure', 'The figure element represents some flow content, optionally with a caption, that is self-contained (like a complete sentence) and is typically referenced as a single unit from the main flow of the document.')),
figcaption: new HTMLTagSpecification(
localize('tags.figcaption', 'The figcaption element represents a caption or legend for the rest of the contents of the figcaption element\'s parent figure element, if any.')),
main: new HTMLTagSpecification(
localize('tags.main', 'The main element represents the main content of the body of a document or application. The main content area consists of content that is directly related to or expands upon the central topic of a document or central functionality of an application.')),
div: new HTMLTagSpecification(
localize('tags.div', 'The div element has no special meaning at all. It represents its children. It can be used with the class, lang, and title attributes to mark up semantics common to a group of consecutive elements.')),
// Text-level semantics
a: new HTMLTagSpecification(
localize('tags.a', 'If the a element has an href attribute, then it represents a hyperlink (a hypertext anchor) labeled by its contents.'),
['href', 'target', 'download', 'ping', 'rel', 'hreflang', 'type']),
em: new HTMLTagSpecification(
localize('tags.em', 'The em element represents stress emphasis of its contents.')),
strong: new HTMLTagSpecification(
localize('tags.strong', 'The strong element represents strong importance, seriousness, or urgency for its contents.')),
small: new HTMLTagSpecification(
localize('tags.small', 'The small element represents side comments such as small print.')),
s: new HTMLTagSpecification(
localize('tags.s', 'The s element represents contents that are no longer accurate or no longer relevant.')),
cite: new HTMLTagSpecification(
localize('tags.cite', 'The cite element represents a reference to a creative work. It must include the title of the work or the name of the author(person, people or organization) or an URL reference, or a reference in abbreviated form as per the conventions used for the addition of citation metadata.')),
q: new HTMLTagSpecification(
localize('tags.q', 'The q element represents some phrasing content quoted from another source.'),
['cite']),
dfn: new HTMLTagSpecification(
localize('tags.dfn', 'The dfn element represents the defining instance of a term. The paragraph, description list group, or section that is the nearest ancestor of the dfn element must also contain the definition(s) for the term given by the dfn element.')),
abbr: new HTMLTagSpecification(
localize('tags.abbr', 'The abbr element represents an abbreviation or acronym, optionally with its expansion. The title attribute may be used to provide an expansion of the abbreviation. The attribute, if specified, must contain an expansion of the abbreviation, and nothing else.')),
ruby: new HTMLTagSpecification(
localize('tags.ruby', 'The ruby element allows one or more spans of phrasing content to be marked with ruby annotations. Ruby annotations are short runs of text presented alongside base text, primarily used in East Asian typography as a guide for pronunciation or to include other annotations. In Japanese, this form of typography is also known as furigana. Ruby text can appear on either side, and sometimes both sides, of the base text, and it is possible to control its position using CSS. A more complete introduction to ruby can be found in the Use Cases & Exploratory Approaches for Ruby Markup document as well as in CSS Ruby Module Level 1. [RUBY-UC] [CSSRUBY]')),
rb: new HTMLTagSpecification(
localize('tags.rb', 'The rb element marks the base text component of a ruby annotation. When it is the child of a ruby element, it doesn\'t represent anything itself, but its parent ruby element uses it as part of determining what it represents.')),
rt: new HTMLTagSpecification(
localize('tags.rt', 'The rt element marks the ruby text component of a ruby annotation. When it is the child of a ruby element or of an rtc element that is itself the child of a ruby element, it doesn\'t represent anything itself, but its ancestor ruby element uses it as part of determining what it represents.')),
// <rtc> is not yet supported by 2+ browsers
//rtc: new HTMLTagSpecification(
// localize('tags.rtc', 'The rtc element marks a ruby text container for ruby text components in a ruby annotation. When it is the child of a ruby element it doesn\'t represent anything itself, but its parent ruby element uses it as part of determining what it represents.')),
rp: new HTMLTagSpecification(
localize('tags.rp', 'The rp element is used to provide fallback text to be shown by user agents that don\'t support ruby annotations. One widespread convention is to provide parentheses around the ruby text component of a ruby annotation.')),
// <data> is not yet supported by 2+ browsers
//data: new HTMLTagSpecification(
// localize('tags.data', 'The data element represents its contents, along with a machine-readable form of those contents in the value attribute.')),
time: new HTMLTagSpecification(
localize('tags.time', 'The time element represents its contents, along with a machine-readable form of those contents in the datetime attribute. The kind of content is limited to various kinds of dates, times, time-zone offsets, and durations, as described below.'),
['datetime']),
code: new HTMLTagSpecification(
localize('tags.code', 'The code element represents a fragment of computer code. This could be an XML element name, a file name, a computer program, or any other string that a computer would recognize.')),
var: new HTMLTagSpecification(
localize('tags.var', 'The var element represents a variable. This could be an actual variable in a mathematical expression or programming context, an identifier representing a constant, a symbol identifying a physical quantity, a function parameter, or just be a term used as a placeholder in prose.')),
samp: new HTMLTagSpecification(
localize('tags.samp', 'The samp element represents sample or quoted output from another program or computing system.')),
kbd: new HTMLTagSpecification(
localize('tags.kbd', 'The kbd element represents user input (typically keyboard input, although it may also be used to represent other input, such as voice commands).')),
sub: new HTMLTagSpecification(
localize('tags.sub', 'The sub element represents a subscript.')),
sup: new HTMLTagSpecification(
localize('tags.sup', 'The sup element represents a superscript.')),
i: new HTMLTagSpecification(
localize('tags.i', 'The i element represents a span of text in an alternate voice or mood, or otherwise offset from the normal prose in a manner indicating a different quality of text, such as a taxonomic designation, a technical term, an idiomatic phrase from another language, transliteration, a thought, or a ship name in Western texts.')),
b: new HTMLTagSpecification(
localize('tags.b', 'The b element represents a span of text to which attention is being drawn for utilitarian purposes without conveying any extra importance and with no implication of an alternate voice or mood, such as key words in a document abstract, product names in a review, actionable words in interactive text-driven software, or an article lede.')),
u: new HTMLTagSpecification(
localize('tags.u', 'The u element represents a span of text with an unarticulated, though explicitly rendered, non-textual annotation, such as labeling the text as being a proper name in Chinese text (a Chinese proper name mark), or labeling the text as being misspelt.')),
mark: new HTMLTagSpecification(
localize('tags.mark', 'The mark element represents a run of text in one document marked or highlighted for reference purposes, due to its relevance in another context. When used in a quotation or other block of text referred to from the prose, it indicates a highlight that was not originally present but which has been added to bring the reader\'s attention to a part of the text that might not have been considered important by the original author when the block was originally written, but which is now under previously unexpected scrutiny. When used in the main prose of a document, it indicates a part of the document that has been highlighted due to its likely relevance to the user\'s current activity.')),
bdi: new HTMLTagSpecification(
localize('tags.bdi', 'The bdi element represents a span of text that is to be isolated from its surroundings for the purposes of bidirectional text formatting. [BIDI]')),
bdo: new HTMLTagSpecification(
localize('tags.dbo', 'The bdo element represents explicit text directionality formatting control for its children. It allows authors to override the Unicode bidirectional algorithm by explicitly specifying a direction override. [BIDI]')),
span: new HTMLTagSpecification(
localize('tags.span', 'The span element doesn\'t mean anything on its own, but can be useful when used together with the global attributes, e.g. class, lang, or dir. It represents its children.')),
br: new HTMLTagSpecification(
localize('tags.br', 'The br element represents a line break.')),
wbr: new HTMLTagSpecification(
localize('tags.wbr', 'The wbr element represents a line break opportunity.')),
// Edits
ins: new HTMLTagSpecification(
localize('tags.ins', 'The ins element represents an addition to the document.')),
del: new HTMLTagSpecification(
localize('tags.del', 'The del element represents a removal from the document.'),
['cite', 'datetime']),
// Embedded content
picture: new HTMLTagSpecification(
localize('tags.picture', 'The picture element is a container which provides multiple sources to its contained img element to allow authors to declaratively control or give hints to the user agent about which image resource to use, based on the screen pixel density, viewport size, image format, and other factors. It represents its children.')),
img: new HTMLTagSpecification(
localize('tags.img', 'An img element represents an image.'),
['alt', 'src', 'srcset', 'crossorigin:xo', 'usemap', 'ismap:v', 'width', 'height']),
iframe: new HTMLTagSpecification(
localize('tags.iframe', 'The iframe element represents a nested browsing context.'),
['src', 'srcdoc', 'name', 'sandbox:sb', 'seamless:v', 'allowfullscreen:v', 'width', 'height']),
embed: new HTMLTagSpecification(
localize('tags.embed', 'The embed element provides an integration point for an external (typically non-HTML) application or interactive content.'),
['src', 'type', 'width', 'height']),
object: new HTMLTagSpecification(
localize('tags.object', 'The object element can represent an external resource, which, depending on the type of the resource, will either be treated as an image, as a nested browsing context, or as an external resource to be processed by a plugin.'),
['data', 'type', 'typemustmatch:v', 'name', 'usemap', 'form', 'width', 'height']),
param: new HTMLTagSpecification(
localize('tags.param', 'The param element defines parameters for plugins invoked by object elements. It does not represent anything on its own.'),
['name', 'value']),
video: new HTMLTagSpecification(
localize('tags.video', 'A video element is used for playing videos or movies, and audio files with captions.'),
['src', 'crossorigin:xo', 'poster', 'preload:pl', 'autoplay:v', 'mediagroup', 'loop:v', 'muted:v', 'controls:v', 'width', 'height']),
audio: new HTMLTagSpecification(
localize('tags.audio', 'An audio element represents a sound or audio stream.'),
['src', 'crossorigin:xo', 'preload:pl', 'autoplay:v', 'mediagroup', 'loop:v', 'muted:v', 'controls:v']),
source: new HTMLTagSpecification(
localize('tags.source', 'The source element allows authors to specify multiple alternative media resources for media elements. It does not represent anything on its own.'),
// 'When the source element has a parent that is a picture element, the source element allows authors to specify multiple alternative source sets for img elements.'
['src', 'type']),
track: new HTMLTagSpecification(
localize('tags.track', 'The track element allows authors to specify explicit external timed text tracks for media elements. It does not represent anything on its own.'),
['default:v', 'kind:tk', 'label', 'src', 'srclang']),
map: new HTMLTagSpecification(
localize('tags.map', 'The map element, in conjunction with an img element and any area element descendants, defines an image map. The element represents its children.'),
['name']),
area: new HTMLTagSpecification(
localize('tags.area', 'The area element represents either a hyperlink with some text and a corresponding area on an image map, or a dead area on an image map.'),
['alt', 'coords', 'shape:sh', 'href', 'target', 'download', 'ping', 'rel', 'hreflang', 'type']),
// Tabular data
table: new HTMLTagSpecification(
localize('tags.table', 'The table element represents data with more than one dimension, in the form of a table.'),
['sortable:v', 'border']),
caption: new HTMLTagSpecification(
localize('tags.caption', 'The caption element represents the title of the table that is its parent, if it has a parent and that is a table element.')),
colgroup: new HTMLTagSpecification(
localize('tags.colgroup', 'The colgroup element represents a group of one or more columns in the table that is its parent, if it has a parent and that is a table element.'),
['span']),
col: new HTMLTagSpecification(
localize('tags.col', 'If a col element has a parent and that is a colgroup element that itself has a parent that is a table element, then the col element represents one or more columns in the column group represented by that colgroup.'),
['span']),
tbody: new HTMLTagSpecification(
localize('tags.tbody', 'The tbody element represents a block of rows that consist of a body of data for the parent table element, if the tbody element has a parent and it is a table.')),
thead: new HTMLTagSpecification(
localize('tags.thead', 'The thead element represents the block of rows that consist of the column labels (headers) for the parent table element, if the thead element has a parent and it is a table.')),
tfoot: new HTMLTagSpecification(
localize('tags.tfoot', 'The tfoot element represents the block of rows that consist of the column summaries (footers) for the parent table element, if the tfoot element has a parent and it is a table.')),
tr: new HTMLTagSpecification(
localize('tags.tr', 'The tr element represents a row of cells in a table.')),
td: new HTMLTagSpecification(
localize('tags.td', 'The td element represents a data cell in a table.'),
['colspan', 'rowspan', 'headers']),
th: new HTMLTagSpecification(
localize('tags.th', 'The th element represents a header cell in a table.'),
['colspan', 'rowspan', 'headers', 'scope:s', 'sorted', 'abbr']),
// Forms
form: new HTMLTagSpecification(
localize('tags.form', 'The form element represents a collection of form-associated elements, some of which can represent editable values that can be submitted to a server for processing.'),
['accept-charset', 'action', 'autocomplete:o', 'enctype:et', 'method:m', 'name', 'novalidate:v', 'target']),
label: new HTMLTagSpecification(
localize('tags.label', 'The label element represents a caption in a user interface. The caption can be associated with a specific form control, known as the label element\'s labeled control, either using the for attribute, or by putting the form control inside the label element itself.'),
['form', 'for']),
input: new HTMLTagSpecification(
localize('tags.input', 'The input element represents a typed data field, usually with a form control to allow the user to edit the data.'),
['accept', 'alt', 'autocomplete:inputautocomplete', 'autofocus:v', 'checked:v', 'dirname', 'disabled:v', 'form', 'formaction', 'formenctype:et', 'formmethod:fm', 'formnovalidate:v', 'formtarget', 'height', 'inputmode:im', 'list', 'max', 'maxlength', 'min', 'minlength', 'multiple:v', 'name', 'pattern', 'placeholder', 'readonly:v', 'required:v', 'size', 'src', 'step', 'type:t', 'value', 'width']),
button: new HTMLTagSpecification(
localize('tags.button', 'The button element represents a button labeled by its contents.'),
['autofocus:v', 'disabled:v', 'form', 'formaction', 'formenctype:et', 'formmethod:fm', 'formnovalidate:v', 'formtarget', 'name', 'type:bt', 'value']),
select: new HTMLTagSpecification(
localize('tags.select', 'The select element represents a control for selecting amongst a set of options.'),
['autocomplete:inputautocomplete', 'autofocus:v', 'disabled:v', 'form', 'multiple:v', 'name', 'required:v', 'size']),
datalist: new HTMLTagSpecification(
localize('tags.datalist', 'The datalist element represents a set of option elements that represent predefined options for other controls. In the rendering, the datalist element represents nothing and it, along with its children, should be hidden.')),
optgroup: new HTMLTagSpecification(
localize('tags.optgroup', 'The optgroup element represents a group of option elements with a common label.'),
['disabled:v', 'label']),
option: new HTMLTagSpecification(
localize('tags.option', 'The option element represents an option in a select element or as part of a list of suggestions in a datalist element.'),
['disabled:v', 'label', 'selected:v', 'value']),
textarea: new HTMLTagSpecification(
localize('tags.textarea', 'The textarea element represents a multiline plain text edit control for the element\'s raw value. The contents of the control represent the control\'s default value.'),
['autocomplete:inputautocomplete', 'autofocus:v', 'cols', 'dirname', 'disabled:v', 'form', 'inputmode:im', 'maxlength', 'minlength', 'name', 'placeholder', 'readonly:v', 'required:v', 'rows', 'wrap:w']),
output: new HTMLTagSpecification(
localize('tags.output', 'The output element represents the result of a calculation performed by the application, or the result of a user action.'),
['for', 'form', 'name']),
progress: new HTMLTagSpecification(
localize('tags.progress', 'The progress element represents the completion progress of a task. The progress is either indeterminate, indicating that progress is being made but that it is not clear how much more work remains to be done before the task is complete (e.g. because the task is waiting for a remote host to respond), or the progress is a number in the range zero to a maximum, giving the fraction of work that has so far been completed.'),
['value', 'max']),
meter: new HTMLTagSpecification(
localize('tags.meter', 'The meter element represents a scalar measurement within a known range, or a fractional value; for example disk usage, the relevance of a query result, or the fraction of a voting population to have selected a particular candidate.'),
['value', 'min', 'max', 'low', 'high', 'optimum']),
fieldset: new HTMLTagSpecification(
localize('tags.fieldset', 'The fieldset element represents a set of form controls optionally grouped under a common name.'),
['disabled:v', 'form', 'name']),
legend: new HTMLTagSpecification(
localize('tags.legend', 'The legend element represents a caption for the rest of the contents of the legend element\'s parent fieldset element, if any.')),
// Interactive elements
details: new HTMLTagSpecification(
localize('tags.details', 'The details element represents a disclosure widget from which the user can obtain additional information or controls.'),
['open:v']),
summary: new HTMLTagSpecification(
localize('tags.summary', 'The summary element represents a summary, caption, or legend for the rest of the contents of the summary element\'s parent details element, if any.')),
// <menu> and <menuitem> are not yet supported by 2+ browsers
//menu: new HTMLTagSpecification(
// localize('tags.menu', 'The menu element represents a list of commands.'),
// ['type:mt', 'label']),
//menuitem: new HTMLTagSpecification(
// localize('tags.menuitem', 'The menuitem element represents a command that the user can invoke from a popup menu (either a context menu or the menu of a menu button).')),
dialog: new HTMLTagSpecification(
localize('tags.dialog', 'The dialog element represents a part of an application that a user interacts with to perform a task, for example a dialog box, inspector, or window.')),
// Scripting
script: new HTMLTagSpecification(
localize('tags.script', 'The script element allows authors to include dynamic script and data blocks in their documents. The element does not represent content for the user.'),
['src', 'type', 'charset', 'async:v', 'defer:v', 'crossorigin:xo', 'nonce']),
noscript: new HTMLTagSpecification(
localize('tags.noscript', 'The noscript element represents nothing if scripting is enabled, and represents its children if scripting is disabled. It is used to present different markup to user agents that support scripting and those that don\'t support scripting, by affecting how the document is parsed.')),
template: new HTMLTagSpecification(
localize('tags.template', 'The template element is used to declare fragments of HTML that can be cloned and inserted in the document by script.')),
canvas: new HTMLTagSpecification(
localize('tags.canvas', 'The canvas element provides scripts with a resolution-dependent bitmap canvas, which can be used for rendering graphs, game graphics, art, or other visual images on the fly.'),
['width', 'height'])
};
// Ionic tag information sourced from Ionic main website (https://github.com/driftyco/ionic-site)
export const IONIC_TAGS: ITagSet = {
'ion-checkbox': new HTMLTagSpecification(localize('tags.ion.checkbox', 'The checkbox is no different than the HTML checkbox input, except it\'s styled differently. The checkbox behaves like any AngularJS checkbox.'),
['name', 'ng-false-value', 'ng-model', 'ng-true-value']),
'ion-content': new HTMLTagSpecification(localize('tags.ion.content', 'The ionContent directive provides an easy to use content area that can be configured to use Ionic\'s custom Scroll View, or the built-in overflow scrolling of the browser.'),
['delegate-handle', 'direction:scrolldir', 'has-bouncing:b', 'locking:b', 'on-scroll', 'on-scroll-complete', 'overflow-scroll:b', 'padding:b', 'scroll:b', 'scrollbar-x:b', 'scrollbar-y:b', 'start-x', 'start-y']),
'ion-delete-button': new HTMLTagSpecification(localize('tags.ion.deletebutton', 'Child of ionItem'),
[]),
'ion-footer-bar': new HTMLTagSpecification(localize('tags.ion.footerbar', 'Adds a fixed footer bar below some content. Can also be a subfooter (higher up) if the "bar-subfooter" class is applied.'),
['align-title:align', 'keyboard-attach:v']),
'ion-header-bar': new HTMLTagSpecification(localize('tags.ion.headerbar', 'Adds a fixed header bar above some content. Can also be a subheader (lower down) if the "bar-subheader" class is applied.'),
['align-title:align', 'no-tap-scroll:b']),
'ion-infinite-scroll': new HTMLTagSpecification(localize('tags.ion.infinitescroll', 'Child of ionContent or ionScroll. The ionInfiniteScroll directive allows you to call a function whenever the user gets to the bottom of the page or near the bottom of the page.'),
['distance', 'icon', 'immediate-check:b', 'on-infinite', 'spinner']),
'ion-input': new HTMLTagSpecification(localize('tags.ion.input', 'ionInput is meant for text type inputs only. Ionic uses an actual <input type="text"> HTML element within the component, with Ionic wrapping to better handle the user experience and interactivity.'),
['type:inputtype', 'clearInput:v']),
'ion-item': new HTMLTagSpecification(localize('tags.ion.item', 'Child of ionList.'),
[]),
'ion-list': new HTMLTagSpecification(localize('tags.ion.list', 'The List is a widely used interface element in almost any mobile app, and can include content ranging from basic text all the way to buttons, toggles, icons, and thumbnails.'),
['can-swipe:b', 'delegate-handle', 'show-delete:b', 'show-reorder:b', 'type:listtype']),
'ion-modal-view': new HTMLTagSpecification(localize('tags.ion.modalview', 'The Modal is a content pane that can go over the user\'s main view temporarily. Usually used for making a choice or editing an item.'),
[]),
'ion-nav-back-button': new HTMLTagSpecification(localize('tags.ion.navbackbutton', 'Child of ionNavBar. Creates a back button inside an ionNavBar. The back button will appear when the user is able to go back in the current navigation stack.'),
[]),
'ion-nav-bar': new HTMLTagSpecification(localize('tags.ion.navbar', 'If you have an ionNavView directive, you can also create an <ion-nav-bar>, which will create a topbar that updates as the application state changes.'),
['align-title:align', 'delegate-handle', 'no-tap-scroll:b']),
'ion-nav-buttons': new HTMLTagSpecification(localize('tags.ion.navbuttons', 'Child of ionNavView. Use ionNavButtons to set the buttons on your ionNavBar from within an ionView.'),
['side:navsides']),
'ion-nav-title': new HTMLTagSpecification(localize('tags.ion.navtitle', 'Child of ionNavView. The ionNavTitle directive replaces an ionNavBar title text with custom HTML from within an ionView template.'),
[]),
'ion-nav-view': new HTMLTagSpecification(localize('tags.ion.navview', 'The ionNavView directive is used to render templates in your application. Each template is part of a state. States are usually mapped to a url, and are defined programatically using angular-ui-router.'),
['name']),
'ion-option-button': new HTMLTagSpecification(localize('tags.ion.optionbutton', 'Child of ionItem. Creates an option button inside a list item, that is visible when the item is swiped to the left by the user.'),
[]),
'ion-pane': new HTMLTagSpecification(localize('tags.ion.pane', 'A simple container that fits content, with no side effects. Adds the "pane" class to the element.'),
[]),
'ion-popover-view': new HTMLTagSpecification(localize('tags.ion.popoverview', 'The Popover is a view that floats above an app\'s content. Popovers provide an easy way to present or gather information from the user.'),
[]),
'ion-radio': new HTMLTagSpecification(localize('tags.ion.radio', 'The radio ionRirective is no different than the HTML radio input, except it\'s styled differently. The ionRadio behaves like AngularJS radio input.'),
['disabled:b', 'icon', 'name', 'ng-disabled:b', 'ng-model', 'ng-value', 'value']),
'ion-refresher': new HTMLTagSpecification(localize('tags.ion.refresher', 'Child of ionContent or ionScroll. Allows you to add pull-to-refresh to a scrollView. Place it as the first child of your ionContent or ionScroll element.'),
['disable-pulling-rotation:b', 'on-pulling', 'on-refresh', 'pulling-icon', 'pulling-text', 'refreshing-icon', 'spinner']),
'ion-reorder-button': new HTMLTagSpecification(localize('tags.ion.reorderbutton', 'Child of ionItem.'),
['on-reorder']),
'ion-scroll': new HTMLTagSpecification(localize('tags.ion.scroll', 'Creates a scrollable container for all content inside.'),
['delegate-handle', 'direction:scrolldir', 'has-bouncing:b', 'locking:b', 'max-zoom', 'min-zoom', 'on-refresh', 'on-scroll', 'paging:b', 'scrollbar-x:b', 'scrollbar-y:b', 'zooming:b']),
'ion-side-menu': new HTMLTagSpecification(localize('tags.ion.sidemenu', 'Child of ionSideMenus. A container for a side menu, sibling to an ionSideMenuContent directive.'),
['is-enabled:b', 'expose-aside-when', 'side:navsides', 'width']),
'ion-side-menu-content': new HTMLTagSpecification(localize('tags.ion.sidemenucontent', 'Child of ionSideMenus. A container for the main visible content, sibling to one or more ionSideMenu directives.'),
['drag-content:b', 'edge-drag-threshold']),
'ion-side-menus': new HTMLTagSpecification(localize('tags.ion.sidemenus', 'A container element for side menu(s) and the main content. Allows the left and/or right side menu to be toggled by dragging the main content area side to side.'),
['delegate-handle', 'enable-menu-with-back-views:b']),
'ion-slide': new HTMLTagSpecification(localize('tags.ion.slide', 'Child of ionSlideBox. Displays a slide inside of a slidebox.'),
[]),
'ion-slide-box': new HTMLTagSpecification(localize('tags.ion.slidebox', 'The Slide Box is a multi-page container where each page can be swiped or dragged between.'),
['active-slide', 'auto-play:b', 'delegate-handle', 'does-continue:b', 'on-slide-changed', 'pager-click', 'show-pager:b', 'slide-interval']),
'ion-spinner': new HTMLTagSpecification(localize('tags.ion.spinner', 'The ionSpinner directive provides a variety of animated spinners.'),
['icon']),
'ion-tab': new HTMLTagSpecification(localize('tags.ion.tab', 'Child of ionTabs. Contains a tab\'s content. The content only exists while the given tab is selected.'),
['badge', 'badge-style', 'disabled', 'hidden', 'href', 'icon', 'icon-off', 'icon-on', 'ng-click', 'on-deselect', 'on-select', 'title']),
'ion-tabs': new HTMLTagSpecification(localize('tags.ion.tabs', 'Powers a multi-tabbed interface with a tab bar and a set of "pages" that can be tabbed through.'),
['delegate-handle']),
'ion-title': new HTMLTagSpecification(localize('tags.ion.title', 'ion-title is a component that sets the title of the ionNavbar'),
[]),
'ion-toggle': new HTMLTagSpecification(localize('tags.ion.toggle', 'A toggle is an animated switch which binds a given model to a boolean. Allows dragging of the switch\'s nub. The toggle behaves like any AngularJS checkbox otherwise.'),
['name', 'ng-false-value', 'ng-model', 'ng-true-value', 'toggle-class']),
'ion-view ': new HTMLTagSpecification(localize('tags.ion.view', 'Child of ionNavView. A container for view content and any navigational and header bar information.'),
['cache-view:b', 'can-swipe-back:b', 'hide-back-button:b', 'hide-nav-bar:b', 'view-title'])
};
export function getHTML5TagProvider(): IHTMLTagProvider {
var globalAttributes = [
'aria-activedescendant', 'aria-atomic:b', 'aria-autocomplete:autocomplete', 'aria-busy:b', 'aria-checked:tristate', 'aria-colcount', 'aria-colindex', 'aria-colspan', 'aria-controls', 'aria-current:current', 'aria-describedat',
'aria-describedby', 'aria-disabled:b', 'aria-dropeffect:dropeffect', 'aria-errormessage', 'aria-expanded:u', 'aria-flowto', 'aria-grabbed:u', 'aria-haspopup:b', 'aria-hidden:b', 'aria-invalid:invalid', 'aria-kbdshortcuts',
'aria-label', 'aria-labelledby', 'aria-level', 'aria-live:live', 'aria-modal:b', 'aria-multiline:b', 'aria-multiselectable:b', 'aria-orientation:orientation', 'aria-owns', 'aria-placeholder', 'aria-posinset', 'aria-pressed:tristate',
'aria-readonly:b', 'aria-relevant:relevant', 'aria-required:b', 'aria-roledescription', 'aria-rowcount', 'aria-rowindex', 'aria-rowspan', 'aria-selected:u', 'aria-setsize', 'aria-sort:sort', 'aria-valuemax', 'aria-valuemin', 'aria-valuenow', 'aria-valuetext',
'accesskey', 'class', 'contenteditable:b', 'contextmenu', 'dir:d', 'draggable:b', 'dropzone', 'hidden:v', 'id', 'itemid', 'itemprop', 'itemref', 'itemscope:v', 'itemtype', 'lang', 'role:roles', 'spellcheck:b', 'style', 'tabindex',
'title', 'translate:y'];
var eventHandlers = ['onabort', 'onblur', 'oncanplay', 'oncanplaythrough', 'onchange', 'onclick', 'oncontextmenu', 'ondblclick', 'ondrag', 'ondragend', 'ondragenter', 'ondragleave', 'ondragover', 'ondragstart',
'ondrop', 'ondurationchange', 'onemptied', 'onended', 'onerror', 'onfocus', 'onformchange', 'onforminput', 'oninput', 'oninvalid', 'onkeydown', 'onkeypress', 'onkeyup', 'onload', 'onloadeddata', 'onloadedmetadata',
'onloadstart', 'onmousedown', 'onmousemove', 'onmouseout', 'onmouseover', 'onmouseup', 'onmousewheel', 'onpause', 'onplay', 'onplaying', 'onprogress', 'onratechange', 'onreset', 'onresize', 'onreadystatechange', 'onscroll',
'onseeked', 'onseeking', 'onselect', 'onshow', 'onstalled', 'onsubmit', 'onsuspend', 'ontimeupdate', 'onvolumechange', 'onwaiting'];
var valueSets: IValueSets = {
b: ['true', 'false'],
u: ['true', 'false', 'undefined'],
o: ['on', 'off'],
y: ['yes', 'no'],
w: ['soft', 'hard'],
d: ['ltr', 'rtl', 'auto'],
m: ['GET', 'POST', 'dialog'],
fm: ['GET', 'POST'],
s: ['row', 'col', 'rowgroup', 'colgroup'],
t: ['hidden', 'text', 'search', 'tel', 'url', 'email', 'password', 'datetime', 'date', 'month', 'week', 'time', 'datetime-local', 'number', 'range', 'color', 'checkbox', 'radio', 'file', 'submit', 'image', 'reset', 'button'],
im: ['verbatim', 'latin', 'latin-name', 'latin-prose', 'full-width-latin', 'kana', 'kana-name', 'katakana', 'numeric', 'tel', 'email', 'url'],
bt: ['button', 'submit', 'reset', 'menu'],
lt: ['1', 'a', 'A', 'i', 'I'],
mt: ['context', 'toolbar'],
mit: ['command', 'checkbox', 'radio'],
et: ['application/x-www-form-urlencoded', 'multipart/form-data', 'text/plain'],
tk: ['subtitles', 'captions', 'descriptions', 'chapters', 'metadata'],
pl: ['none', 'metadata', 'auto'],
sh: ['circle', 'default', 'poly', 'rect'],
xo: ['anonymous', 'use-credentials'],
sb: ['allow-forms', 'allow-modals', 'allow-pointer-lock', 'allow-popups', 'allow-popups-to-escape-sandbox', 'allow-same-origin', 'allow-scripts', 'allow-top-navigation'],
tristate: ['true', 'false', 'mixed', 'undefined'],
inputautocomplete: ['additional-name', 'address-level1', 'address-level2', 'address-level3', 'address-level4', 'address-line1', 'address-line2', 'address-line3', 'bday', 'bday-year', 'bday-day', 'bday-month', 'billing', 'cc-additional-name', 'cc-csc', 'cc-exp', 'cc-exp-month', 'cc-exp-year', 'cc-family-name', 'cc-given-name', 'cc-name', 'cc-number', 'cc-type', 'country', 'country-name', 'current-password', 'email', 'family-name', 'fax', 'given-name', 'home', 'honorific-prefix', 'honorific-suffix', 'impp', 'language', 'mobile', 'name', 'new-password', 'nickname', 'organization', 'organization-title', 'pager', 'photo', 'postal-code', 'sex', 'shipping', 'street-address', 'tel-area-code', 'tel', 'tel-country-code', 'tel-extension', 'tel-local', 'tel-local-prefix', 'tel-local-suffix', 'tel-national', 'transaction-amount', 'transaction-currency', 'url', 'username', 'work'],
autocomplete: ['inline', 'list', 'both', 'none'],
current: ['page', 'step', 'location', 'date', 'time', 'true', 'false'],
dropeffect: ['copy', 'move', 'link', 'execute', 'popup', 'none'],
invalid: ['grammar', 'false', 'spelling', 'true'],
live: ['off', 'polite', 'assertive'],
orientation: ['vertical', 'horizontal', 'undefined'],
relevant: ['additions', 'removals', 'text', 'all', 'additions text'],
sort: ['ascending', 'descending', 'none', 'other'],
roles: ['alert', 'alertdialog', 'button', 'checkbox', 'dialog', 'gridcell', 'link', 'log', 'marquee', 'menuitem', 'menuitemcheckbox', 'menuitemradio', 'option', 'progressbar', 'radio', 'scrollbar', 'searchbox', 'slider',
'spinbutton', 'status', 'switch', 'tab', 'tabpanel', 'textbox', 'timer', 'tooltip', 'treeitem', 'combobox', 'grid', 'listbox', 'menu', 'menubar', 'radiogroup', 'tablist', 'tree', 'treegrid',
'application', 'article', 'cell', 'columnheader', 'definition', 'directory', 'document', 'feed', 'figure', 'group', 'heading', 'img', 'list', 'listitem', 'math', 'none', 'note', 'presentation', 'region', 'row', 'rowgroup',
'rowheader', 'separator', 'table', 'term', 'text', 'toolbar',
'banner', 'complementary', 'contentinfo', 'form', 'main', 'navigation', 'region', 'search']
};
return {
getId: () => 'html5',
isApplicable: () => true,
collectTags: (collector: (tag: string, label: string) => void) => collectTagsDefault(collector, HTML_TAGS),
collectAttributes: (tag: string, collector: (attribute: string, type: string) => void) => {
collectAttributesDefault(tag, collector, HTML_TAGS, globalAttributes);
eventHandlers.forEach(handler => {
collector(handler, 'event');
});
},
collectValues: (tag: string, attribute: string, collector: (value: string) => void) => collectValuesDefault(tag, attribute, collector, HTML_TAGS, globalAttributes, valueSets)
};
}
export function getAngularTagProvider(): IHTMLTagProvider {
var customTags: { [tag: string]: string[] } = {
input: ['ng-model', 'ng-required', 'ng-minlength', 'ng-maxlength', 'ng-pattern', 'ng-trim'],
select: ['ng-model'],
textarea: ['ng-model', 'ng-required', 'ng-minlength', 'ng-maxlength', 'ng-pattern', 'ng-trim']
};
var globalAttributes = ['ng-app', 'ng-bind', 'ng-bind-html', 'ng-bind-template', 'ng-blur', 'ng-change', 'ng-checked', 'ng-class', 'ng-class-even', 'ng-class-odd',
'ng-click', 'ng-cloak', 'ng-controller', 'ng-copy', 'ng-csp', 'ng-cut', 'ng-dblclick', 'ng-disabled', 'ng-focus', 'ng-form', 'ng-hide', 'ng-href', 'ng-if',
'ng-include', 'ng-init', 'ng-jq', 'ng-keydown', 'ng-keypress', 'ng-keyup', 'ng-list', 'ng-model-options', 'ng-mousedown', 'ng-mouseenter', 'ng-mouseleave',
'ng-mousemove', 'ng-mouseover', 'ng-mouseup', 'ng-non-bindable', 'ng-open', 'ng-options', 'ng-paste', 'ng-pluralize', 'ng-readonly', 'ng-repeat', 'ng-selected',
'ng-show', 'ng-src', 'ng-srcset', 'ng-style', 'ng-submit', 'ng-switch', 'ng-transclude', 'ng-value'
];
return {
getId: () => 'angular1',
isApplicable: (languageId) => languageId === 'html',
collectTags: (collector: (tag: string) => void) => {
// no extra tags
},
collectAttributes: (tag: string, collector: (attribute: string, type: string) => void) => {
if (tag) {
var attributes = customTags[tag];
if (attributes) {
attributes.forEach((a) => {
collector(a, null);
collector('data-' + a, null);
});
}
}
globalAttributes.forEach((a) => {
collector(a, null);
collector('data-' + a, null);
});
},
collectValues: (tag: string, attribute: string, collector: (value: string) => void) => {
// no values
}
};
}
export function getIonicTagProvider(): IHTMLTagProvider {
var customTags: { [tag: string]: string[] } = {
a: ['nav-direction:navdir', 'nav-transition:trans'],
button: ['menu-toggle:menusides']
};
var globalAttributes = ['collection-repeat', 'force-refresh-images:b', 'ion-stop-event', 'item-height', 'item-render-buffer', 'item-width', 'menu-close:v',
'on-double-tap', 'on-drag', 'on-drag-down', 'on-drag-left', 'on-drag-right', 'on-drag-up', 'on-hold', 'on-release', 'on-swipe', 'on-swipe-down', 'on-swipe-left',
'on-swipe-right', 'on-swipe-up', 'on-tap', 'on-touch'];
var valueSets: IValueSets = {
align: ['center', 'left', 'right'],
b: ['true', 'false'],
inputtype: ['email', 'number', 'password', 'search', 'tel', 'text', 'url'],
listtype: ['card', 'list-inset'],
menusides: ['left', 'right'],
navdir: ['back', 'enter', 'exit', 'forward', 'swap'],
navsides: ['left', 'primary', 'right', 'secondary'],
scrolldir: ['x', 'xy', 'y'],
trans: ['android', 'ios', 'none']
};
return {
getId: () => 'ionic',
isApplicable: (languageId) => languageId === 'html',
collectTags: (collector: (tag: string, label: string) => void) => collectTagsDefault(collector, IONIC_TAGS),
collectAttributes: (tag: string, collector: (attribute: string, type: string) => void) => {
collectAttributesDefault(tag, collector, IONIC_TAGS, globalAttributes);
if (tag) {
var attributes = customTags[tag];
if (attributes) {
attributes.forEach((a) => {
var segments = a.split(':');
collector(segments[0], segments[1]);
});
}
}
},
collectValues: (tag: string, attribute: string, collector: (value: string) => void) => collectValuesDefault(tag, attribute, collector, IONIC_TAGS, globalAttributes, valueSets, customTags)
};
}
function collectTagsDefault(collector: (tag: string, label: string) => void, tagSet: ITagSet): void {
for (var tag in tagSet) {
collector(tag, tagSet[tag].label);
}
}
function collectAttributesDefault(tag: string, collector: (attribute: string, type: string) => void, tagSet: ITagSet, globalAttributes: string[]): void {
globalAttributes.forEach(attr => {
var segments = attr.split(':');
collector(segments[0], segments[1]);
});
if (tag) {
var tags = tagSet[tag];
if (tags) {
var attributes = tags.attributes;
if (attributes) {
attributes.forEach(attr => {
var segments = attr.split(':');
collector(segments[0], segments[1]);
});
}
}
}
}
function collectValuesDefault(tag: string, attribute: string, collector: (value: string) => void, tagSet: ITagSet, globalAttributes: string[], valueSets: IValueSets, customTags?: { [tag: string]: string[] }): void {
var prefix = attribute + ':';
var processAttributes = (attributes: string[]) => {
attributes.forEach((attr) => {
if (attr.length > prefix.length && strings.startsWith(attr, prefix)) {
var typeInfo = attr.substr(prefix.length);
if (typeInfo === 'v') {
collector(attribute);
} else {
var values = valueSets[typeInfo];
if (values) {
values.forEach(collector);
}
}
}
});
};
if (tag) {
var tags = tagSet[tag];
if (tags) {
var attributes = tags.attributes;
if (attributes) {
processAttributes(attributes);
}
}
}
processAttributes(globalAttributes);
if (customTags) {
var customTagAttributes = customTags[tag];
if (customTagAttributes) {
processAttributes(customTagAttributes);
}
}
}
/*!
END THIRD PARTY
*/

View file

@ -1,39 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import {IHTMLTagProvider} from './htmlTags';
export function getRazorTagProvider() : IHTMLTagProvider {
var customTags : { [tag:string]: string[]} = {
a: ['asp-action', 'asp-controller', 'asp-fragment', 'asp-host', 'asp-protocol', 'asp-route'],
div: ['asp-validation-summary'],
form: ['asp-action', 'asp-controller', 'asp-anti-forgery'],
input: ['asp-for', 'asp-format'],
label: ['asp-for'],
select: ['asp-for', 'asp-items'],
span: ['asp-validation-for']
};
return {
getId: () => 'razor',
isApplicable: (languageId) => languageId === 'razor',
collectTags: (collector: (tag: string) => void) => {
// no extra tags
},
collectAttributes: (tag: string, collector: (attribute: string, type: string) => void) => {
if (tag) {
var attributes = customTags[tag];
if (attributes) {
attributes.forEach(a => collector(a, null));
}
}
},
collectValues: (tag: string, attribute: string, collector: (value: string) => void) => {
// no values
}
};
}

View file

@ -1,211 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import {TextDocument, Position, CompletionList, CompletionItemKind, Range} from 'vscode-languageserver-types';
import {HTMLDocument} from '../parser/htmlParser';
import {TokenType, createScanner, ScannerState} from '../parser/htmlScanner';
import {getHTML5TagProvider, getAngularTagProvider, getIonicTagProvider} from '../parser/htmlTags';
import {getRazorTagProvider} from '../parser/razorTags';
import {CompletionConfiguration} from '../htmlLanguageService';
let allTagProviders = [
getHTML5TagProvider(),
getAngularTagProvider(),
getIonicTagProvider(),
getRazorTagProvider()
];
export function doComplete(document: TextDocument, position: Position, doc: HTMLDocument, settings?: CompletionConfiguration): CompletionList {
let result: CompletionList = {
isIncomplete: false,
items: []
};
let tagProviders = allTagProviders.filter(p => p.isApplicable(document.languageId) && (!settings || !!settings[p.getId()]));
let offset = document.offsetAt(position);
let node = doc.findNodeBefore(offset);
if (!node) {
return result;
}
let scanner = createScanner(document.getText(), node.start);
let currentTag: string;
let currentAttributeName: string;
function getReplaceRange(replaceStart: number): Range {
if (replaceStart > offset) {
replaceStart = offset;
}
return { start: document.positionAt(replaceStart), end: document.positionAt(offset) };
}
function collectOpenTagSuggestions(afterOpenBracket: number): CompletionList {
let range = getReplaceRange(afterOpenBracket);
tagProviders.forEach((provider) => {
provider.collectTags((tag, label) => {
result.items.push({
label: tag,
kind: CompletionItemKind.Property,
documentation: label,
textEdit: { newText: tag, range: range }
});
});
});
return result;
}
function collectCloseTagSuggestions(afterOpenBracket: number, matchingOnly: boolean): CompletionList {
let range = getReplaceRange(afterOpenBracket);
let contentAfter = document.getText().substr(offset);
let closeTag = contentAfter.match(/^\s*>/) ? '' : '>';
let curr = node;
while (curr) {
let tag = curr.tag;
if (tag && !curr.closed) {
result.items.push({
label: '/' + tag,
kind: CompletionItemKind.Property,
filterText: '/' + tag + closeTag,
textEdit: { newText: '/' + tag + closeTag, range: range }
});
return result;
}
curr = curr.parent;
}
if (matchingOnly) {
return result;
}
tagProviders.forEach((provider) => {
provider.collectTags((tag, label) => {
result.items.push({
label: '/' + tag,
kind: CompletionItemKind.Property,
documentation: label,
filterText: '/' + tag + closeTag,
textEdit: { newText: '/' + tag + closeTag, range: range }
});
});
});
return result;
}
function collectTagSuggestions(tagStart: number): CompletionList {
collectOpenTagSuggestions(tagStart);
collectCloseTagSuggestions(tagStart, true);
return result;
}
function collectAttributeNameSuggestions(nameStart: number): CompletionList {
let range = getReplaceRange(nameStart);
tagProviders.forEach((provider) => {
provider.collectAttributes(currentTag, (attribute, type) => {
let codeSnippet = attribute;
if (type !== 'v') {
codeSnippet = codeSnippet + '="{{}}"';
}
result.items.push({
label: attribute,
kind: type === 'handler' ? CompletionItemKind.Function : CompletionItemKind.Value,
textEdit: { newText: codeSnippet, range: range }
});
});
});
return result;
}
function collectAttributeValueSuggestions(valueStart: number): CompletionList {
let range = getReplaceRange(valueStart);
tagProviders.forEach((provider) => {
provider.collectValues(currentTag, currentAttributeName, (value) => {
let codeSnippet = '"' + value + '"';
result.items.push({
label: value,
filterText: codeSnippet,
kind: CompletionItemKind.Unit,
textEdit: { newText: codeSnippet, range: range }
});
});
});
return result;
}
let token = scanner.scan();
while (token !== TokenType.EOS && scanner.getTokenOffset() <= offset) {
switch (token) {
case TokenType.StartTagOpen:
if (scanner.getTokenEnd() === offset) {
return collectTagSuggestions(offset);
}
break;
case TokenType.StartTag:
if (scanner.getTokenOffset() <= offset && offset <= scanner.getTokenEnd()) {
return collectOpenTagSuggestions(scanner.getTokenOffset());
}
currentTag = scanner.getTokenText();
break;
case TokenType.AttributeName:
if (scanner.getTokenOffset() <= offset && offset <= scanner.getTokenEnd()) {
return collectAttributeNameSuggestions(scanner.getTokenOffset());
}
currentAttributeName = scanner.getTokenText();
break;
case TokenType.DelimiterAssign:
if (scanner.getTokenEnd() === offset) {
return collectAttributeValueSuggestions(scanner.getTokenEnd());
}
break;
case TokenType.AttributeValue:
if (scanner.getTokenOffset() <= offset && offset <= scanner.getTokenEnd()) {
return collectAttributeValueSuggestions(scanner.getTokenOffset());
}
break;
case TokenType.Whitespace:
case TokenType.Unknown:
if (offset <= scanner.getTokenEnd()) {
switch (scanner.getScannerState()) {
case ScannerState.AfterOpeningStartTag:
return collectTagSuggestions(scanner.getTokenOffset());
case ScannerState.WithinTag:
case ScannerState.AfterAttributeName:
return collectAttributeNameSuggestions(scanner.getTokenEnd());
case ScannerState.BeforeAttributeValue:
return collectAttributeValueSuggestions(scanner.getTokenEnd());
case ScannerState.AfterOpeningEndTag:
return collectCloseTagSuggestions(scanner.getTokenOffset() - 1, false);
}
}
break;
case TokenType.EndTagOpen:
if (offset <= scanner.getTokenEnd()) {
return collectCloseTagSuggestions(scanner.getTokenOffset() + 1, false);
}
break;
case TokenType.EndTag:
if (offset <= scanner.getTokenEnd()) {
let text = document.getText();
let start = scanner.getTokenOffset() - 1;
while (start >= 0) {
let ch = text.charAt(start);
if (ch === '/') {
return collectCloseTagSuggestions(start, false);
} else if (!isWhiteSpace(ch)) {
break;
}
start--;
}
}
break;
}
token = scanner.scan();
}
return result;
}
function isWhiteSpace(s: string): boolean {
return /^\s*$/.test(s);
}

View file

@ -1,57 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import {HTMLFormatConfiguration} from '../htmlLanguageService';
import {TextDocument, Range, TextEdit} from 'vscode-languageserver-types';
import {IBeautifyHTMLOptions, html_beautify} from '../lib/beautify-html';
export function format(document: TextDocument, range: Range, options: HTMLFormatConfiguration): TextEdit[] {
let value = document.getText();
if (range) {
let startOffset = document.offsetAt(range.start);
let endOffset = document.offsetAt(range.end);
value = value.substring(startOffset, endOffset);
}
let htmlOptions: IBeautifyHTMLOptions = {
indent_size: options.insertSpaces ? options.tabSize : 1,
indent_char: options.insertSpaces ? ' ' : '\t',
wrap_line_length: getFormatOption(options, 'wrapLineLength', 120),
unformatted: getTagsFormatOption(options, 'unformatted', void 0),
indent_inner_html: getFormatOption(options, 'indentInnerHtml', false),
preserve_newlines: getFormatOption(options, 'preserveNewLines', false),
max_preserve_newlines: getFormatOption(options, 'maxPreserveNewLines', void 0),
indent_handlebars: getFormatOption(options, 'indentHandlebars', false),
end_with_newline: getFormatOption(options, 'endWithNewline', false),
extra_liners: getTagsFormatOption(options, 'extraLiners', void 0),
};
let result = html_beautify(value, htmlOptions);
return [{
range: range,
newText: result
}];
}
function getFormatOption(options: HTMLFormatConfiguration, key: string, dflt: any): any {
if (options && options.hasOwnProperty(key)) {
let value = options[key];
if (value !== null) {
return value;
}
}
return dflt;
}
function getTagsFormatOption(options: HTMLFormatConfiguration, key: string, dflt: string[]): string[] {
let list = <string>getFormatOption(options, key, null);
if (typeof list === 'string') {
if (list.length > 0) {
return list.split(',').map(t => t.trim().toLowerCase());
}
return [];
}
return dflt;
}

View file

@ -1,49 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import {HTMLDocument} from '../parser/htmlParser';
import {TokenType, createScanner} from '../parser/htmlScanner';
import {TextDocument, Range, Position, DocumentHighlightKind, DocumentHighlight} from 'vscode-languageserver-types';
export function findDocumentHighlights(document: TextDocument, position: Position, htmlDocument: HTMLDocument): DocumentHighlight[] {
let offset = document.offsetAt(position);
let node = htmlDocument.findNodeAt(offset);
if (!node.tag) {
return [];
}
let result = [];
let startTagRange = getTagNameRange(TokenType.StartTag, document, node.start);
let endTagRange = typeof node.endTagStart === 'number' && getTagNameRange(TokenType.EndTag, document, node.endTagStart);
if (startTagRange && covers(startTagRange, position) || endTagRange && covers(endTagRange, position)) {
if (startTagRange) {
result.push({ kind: DocumentHighlightKind.Read, range: startTagRange });
}
if (endTagRange) {
result.push({ kind: DocumentHighlightKind.Read, range: endTagRange });
}
}
return result;
}
function isBeforeOrEqual(pos1: Position, pos2: Position) {
return pos1.line < pos2.line || (pos1.line === pos2.line && pos1.character <= pos2.character);
}
function covers(range: Range, position: Position) {
return isBeforeOrEqual(range.start, position) && isBeforeOrEqual(position, range.end);
}
function getTagNameRange(tokenType: TokenType, document: TextDocument, startOffset: number): Range {
let scanner = createScanner(document.getText(), startOffset);
let token = scanner.scan();
while (token !== TokenType.EOS && token !== tokenType) {
token = scanner.scan();
}
if (token !== TokenType.EOS) {
return { start: document.positionAt(scanner.getTokenOffset()), end: document.positionAt(scanner.getTokenEnd()) };
}
return null;
}

View file

@ -1,117 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import {TokenType, createScanner} from '../parser/htmlScanner';
import {TextDocument, Range} from 'vscode-languageserver-types';
import * as paths from '../utils/paths';
import * as strings from '../utils/strings';
import Uri from 'vscode-uri';
import {DocumentLink} from '../htmlLanguageService';
function stripQuotes(url: string): string {
return url
.replace(/^'([^']+)'$/, (substr, match1) => match1)
.replace(/^"([^"]+)"$/, (substr, match1) => match1);
}
function getWorkspaceUrl(modelAbsoluteUri: Uri, rootAbsoluteUrl: Uri, tokenContent: string): string {
if (/^\s*javascript\:/i.test(tokenContent) || /^\s*\#/i.test(tokenContent)) {
return null;
}
if (/^\s*https?:\/\//i.test(tokenContent) || /^\s*file:\/\//i.test(tokenContent)) {
// Absolute link that needs no treatment
return tokenContent.replace(/^\s*/g, '');
}
if (/^\s*\/\//i.test(tokenContent)) {
// Absolute link (that does not name the protocol)
let pickedScheme = 'http';
if (modelAbsoluteUri.scheme === 'https') {
pickedScheme = 'https';
}
return pickedScheme + ':' + tokenContent.replace(/^\s*/g, '');
}
let modelPath = paths.dirname(modelAbsoluteUri.path);
let alternativeResultPath: string = null;
if (tokenContent.length > 0 && tokenContent.charAt(0) === '/') {
alternativeResultPath = tokenContent;
} else {
alternativeResultPath = paths.join(modelPath, tokenContent);
alternativeResultPath = alternativeResultPath.replace(/^(\/\.\.)+/, '');
}
let potentialResult = modelAbsoluteUri.with({ path: alternativeResultPath }).toString(true);
let rootAbsoluteUrlStr = rootAbsoluteUrl && rootAbsoluteUrl.toString(true);
if (rootAbsoluteUrlStr && strings.startsWith(modelAbsoluteUri.toString(true), rootAbsoluteUrlStr)) {
// The `rootAbsoluteUrl` is set and matches our current model
// We need to ensure that this `potentialResult` does not escape `rootAbsoluteUrl`
let commonPrefixLength = strings.commonPrefixLength(rootAbsoluteUrlStr, potentialResult);
if (strings.endsWith(rootAbsoluteUrlStr, '/')) {
commonPrefixLength = potentialResult.lastIndexOf('/', commonPrefixLength) + 1;
}
return rootAbsoluteUrlStr + potentialResult.substr(commonPrefixLength);
}
return potentialResult;
}
function createLink(document: TextDocument, rootAbsoluteUrl: Uri, attributeValue: string, startOffset: number, endOffset: number): DocumentLink {
let documentUri = Uri.parse(document.uri);
let tokenContent = stripQuotes(attributeValue);
if (tokenContent.length < attributeValue.length) {
startOffset++;
endOffset--;
}
let workspaceUrl = getWorkspaceUrl(documentUri, rootAbsoluteUrl, tokenContent);
if (!workspaceUrl) {
return null;
}
return {
range: Range.create(document.positionAt(startOffset), document.positionAt(endOffset)),
target: workspaceUrl
};
}
export function findDocumentLinks(document: TextDocument, workspacePath: string): DocumentLink[] {
let newLinks: DocumentLink[] = [];
let rootAbsoluteUrl: Uri = null;
if (workspacePath) {
// The workspace can be null in the no folder opened case
if (workspacePath.charAt(workspacePath.length - 1) !== '/') {
workspacePath = workspacePath + '/';
}
rootAbsoluteUrl = Uri.parse(workspacePath);
}
let scanner = createScanner(document.getText(), 0);
let token = scanner.scan();
let afterHrefOrSrc = false;
while (token !== TokenType.EOS) {
switch (token) {
case TokenType.AttributeName:
let attributeName = scanner.getTokenText();
afterHrefOrSrc = attributeName === 'src' || attributeName === 'href';
break;
case TokenType.AttributeValue:
if (afterHrefOrSrc) {
let attributeValue = scanner.getTokenText();
let link = createLink(document, rootAbsoluteUrl, attributeValue, scanner.getTokenOffset(), scanner.getTokenEnd());
if (link) {
newLinks.push(link);
}
afterHrefOrSrc = false;
}
break;
}
token = scanner.scan();
}
return newLinks;
}

View file

@ -1,397 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import * as htmlLanguageService from '../htmlLanguageService';
import {CompletionList, TextDocument, CompletionItemKind} from 'vscode-languageserver-types';
import {applyEdits} from './textEditSupport';
export interface ItemDescription {
label: string;
documentation?: string;
kind?: CompletionItemKind;
insertText?: string;
overwriteBefore?: number;
resultText?: string;
notAvailable?: boolean;
}
function asPromise<T>(result: T): Promise<T> {
return Promise.resolve(result);
}
export let assertCompletion = function (completions: CompletionList, expected: ItemDescription, document: TextDocument, offset: number) {
let matches = completions.items.filter(completion => {
return completion.label === expected.label;
});
if (expected.notAvailable) {
assert.equal(matches.length, 0, expected.label + " should not existing is results");
return;
}
assert.equal(matches.length, 1, expected.label + " should only existing once: Actual: " + completions.items.map(c => c.label).join(', '));
if (expected.documentation) {
assert.equal(matches[0].documentation, expected.documentation);
}
if (expected.kind) {
assert.equal(matches[0].kind, expected.kind);
}
if (expected.insertText) {
assert.equal(matches[0].insertText || matches[0].textEdit.newText, expected.insertText);
}
if (expected.resultText) {
assert.equal(applyEdits(document, [matches[0].textEdit]), expected.resultText);
}
if (expected.insertText && typeof expected.overwriteBefore === 'number' && matches[0].textEdit) {
let text = document.getText();
let expectedText = text.substr(0, offset - expected.overwriteBefore) + expected.insertText + text.substr(offset);
let actualText = applyEdits(document, [matches[0].textEdit]);
if (actualText !== expectedText) {
assert.equal(actualText, expectedText);
}
}
};
let testCompletionFor = function (value: string, expected: { count?: number, items?: ItemDescription[] }, settings?: htmlLanguageService.CompletionConfiguration): Thenable<void> {
let offset = value.indexOf('|');
value = value.substr(0, offset) + value.substr(offset + 1);
let ls = htmlLanguageService.getLanguageService();
let document = TextDocument.create('test://test/test.html', 'html', 0, value);
let position = document.positionAt(offset);
let htmlDoc = ls.parseHTMLDocument(document);
let list = ls.doComplete(document, position, htmlDoc, settings);
if (expected.count) {
assert.equal(list.items, expected.count);
}
if (expected.items) {
for (let item of expected.items) {
assertCompletion(list, item, document, offset);
}
}
return Promise.resolve();
};
function run(tests: Thenable<void>[], testDone) {
Promise.all(tests).then(() => {
testDone();
}, (error) => {
testDone(error);
});
}
suite('HTML Completion', () => {
test('Complete', function (testDone): any {
run([
testCompletionFor('<|', {
items: [
{ label: 'iframe', resultText: '<iframe' },
{ label: 'h1', resultText: '<h1' },
{ label: 'div', resultText: '<div' },
]
}),
testCompletionFor('< |', {
items: [
{ label: 'iframe', resultText: '<iframe' },
{ label: 'h1', resultText: '<h1' },
{ label: 'div', resultText: '<div' },
]
}),
testCompletionFor('<h|', {
items: [
{ label: 'html', resultText: '<html' },
{ label: 'h1', resultText: '<h1' },
{ label: 'header', resultText: '<header' },
]
}),
testCompletionFor('<input|', {
items: [
{ label: 'input', resultText: '<input' },
]
}),
testCompletionFor('<input |', {
items: [
{ label: 'type', resultText: '<input type="{{}}"' },
{ label: 'style', resultText: '<input style="{{}}"' },
{ label: 'onmousemove', resultText: '<input onmousemove="{{}}"' },
]
}),
testCompletionFor('<input t|', {
items: [
{ label: 'type', resultText: '<input type="{{}}"' },
{ label: 'tabindex', resultText: '<input tabindex="{{}}"' },
]
}),
testCompletionFor('<input type="text" |', {
items: [
{ label: 'style', resultText: '<input type="text" style="{{}}"' },
{ label: 'type', resultText: '<input type="text" type="{{}}"' },
{ label: 'size', resultText: '<input type="text" size="{{}}"' },
]
}),
testCompletionFor('<input type="text" s|', {
items: [
{ label: 'style', resultText: '<input type="text" style="{{}}"' },
{ label: 'src', resultText: '<input type="text" src="{{}}"' },
{ label: 'size', resultText: '<input type="text" size="{{}}"' },
]
}),
testCompletionFor('<input di| type="text"', {
items: [
{ label: 'disabled', resultText: '<input disabled type="text"' },
{ label: 'dir', resultText: '<input dir="{{}}" type="text"' },
]
}),
testCompletionFor('<input disabled | type="text"', {
items: [
{ label: 'dir', resultText: '<input disabled dir="{{}}" type="text"' },
{ label: 'style', resultText: '<input disabled style="{{}}" type="text"' },
]
}),
testCompletionFor('<input type=|', {
items: [
{ label: 'text', resultText: '<input type="text"' },
{ label: 'checkbox', resultText: '<input type="checkbox"' },
]
}),
testCompletionFor('<input type="c|', {
items: [
{ label: 'color', resultText: '<input type="color"' },
{ label: 'checkbox', resultText: '<input type="checkbox"' },
]
}),
testCompletionFor('<input type= |', {
items: [
{ label: 'color', resultText: '<input type= "color"' },
{ label: 'checkbox', resultText: '<input type= "checkbox"' },
]
}),
testCompletionFor('<input src="c" type="color|" ', {
items: [
{ label: 'color', resultText: '<input src="c" type="color"" ' },
]
}),
testCompletionFor('<input src="c" type=color| ', {
items: [
{ label: 'color', resultText: '<input src="c" type="color" ' },
]
}),
testCompletionFor('<div dir=|></div>', {
items: [
{ label: 'ltr', resultText: '<div dir="ltr"></div>' },
{ label: 'rtl', resultText: '<div dir="rtl"></div>' },
]
}),
testCompletionFor('<ul><|>', {
items: [
{ label: '/ul', resultText: '<ul></ul>' },
{ label: 'li', resultText: '<ul><li>' },
]
}),
testCompletionFor('<ul><li><|', {
items: [
{ label: '/li', resultText: '<ul><li></li>' },
{ label: 'a', resultText: '<ul><li><a' },
]
}),
testCompletionFor('<goo></|>', {
items: [
{ label: '/goo', resultText: '<goo></goo>' },
]
}),
testCompletionFor('<foo></f|', {
items: [
{ label: '/foo', resultText: '<foo></foo>' },
]
}),
testCompletionFor('<foo></ |>', {
items: [
{ label: '/foo', resultText: '<foo></foo>' },
]
}),
testCompletionFor('<span></ s|', {
items: [
{ label: '/span', resultText: '<span></span>' },
]
}),
testCompletionFor('<li><br></ |>', {
items: [
{ label: '/li', resultText: '<li><br></li>' },
]
}),
testCompletionFor('<foo><br/></ f|>', {
items: [
{ label: '/foo', resultText: '<foo><br/></foo>' },
]
}),
testCompletionFor('<li><div/></|', {
items: [
{ label: '/li', resultText: '<li><div/></li>' },
]
}),
testCompletionFor('<li><br/|>', { count: 0 }),
testCompletionFor('<li><br>a/|', { count: 0 }),
testCompletionFor('<foo><bar></bar></| ', {
items: [
{ label: '/foo', resultText: '<foo><bar></bar></foo> ' },
]
}),
testCompletionFor('<body><div><div></div></div></| >', {
items: [
{ label: '/body', resultText: '<body><div><div></div></div></body >' },
]
}),
testCompletionFor(['<body>', ' <div>', ' </|'].join('\n'), {
items: [
{ label: '/div', resultText: ['<body>', ' <div>', ' </div>'].join('\n') },
]
})
], testDone);
});
test('Handlebar Completion', function (testDone) {
run([
testCompletionFor('<script id="entry-template" type="text/x-handlebars-template"> <| </script>', {
items: [
{ label: 'div', resultText: '<script id="entry-template" type="text/x-handlebars-template"> <div </script>' },
]
})
], testDone);
});
test('Complete aria', function (testDone): any {
let expectedAriaAttributes = [
{ label: 'aria-activedescendant' },
{ label: 'aria-atomic' },
{ label: 'aria-autocomplete' },
{ label: 'aria-busy' },
{ label: 'aria-checked' },
{ label: 'aria-colcount' },
{ label: 'aria-colindex' },
{ label: 'aria-colspan' },
{ label: 'aria-controls' },
{ label: 'aria-current' },
{ label: 'aria-describedat' },
{ label: 'aria-describedby' },
{ label: 'aria-disabled' },
{ label: 'aria-dropeffect' },
{ label: 'aria-errormessage' },
{ label: 'aria-expanded' },
{ label: 'aria-flowto' },
{ label: 'aria-grabbed' },
{ label: 'aria-haspopup' },
{ label: 'aria-hidden' },
{ label: 'aria-invalid' },
{ label: 'aria-kbdshortcuts' },
{ label: 'aria-label' },
{ label: 'aria-labelledby' },
{ label: 'aria-level' },
{ label: 'aria-live' },
{ label: 'aria-modal' },
{ label: 'aria-multiline' },
{ label: 'aria-multiselectable' },
{ label: 'aria-orientation' },
{ label: 'aria-owns' },
{ label: 'aria-placeholder' },
{ label: 'aria-posinset' },
{ label: 'aria-pressed' },
{ label: 'aria-readonly' },
{ label: 'aria-relevant' },
{ label: 'aria-required' },
{ label: 'aria-roledescription' },
{ label: 'aria-rowcount' },
{ label: 'aria-rowindex' },
{ label: 'aria-rowspan' },
{ label: 'aria-selected' },
{ label: 'aria-setsize' },
{ label: 'aria-sort' },
{ label: 'aria-valuemax' },
{ label: 'aria-valuemin' },
{ label: 'aria-valuenow' },
{ label: 'aria-valuetext' }
];
run([
testCompletionFor('<div |> </div >', { items: expectedAriaAttributes }),
testCompletionFor('<span |> </span >', { items: expectedAriaAttributes }),
testCompletionFor('<input |> </input >', { items: expectedAriaAttributes })
], testDone);
});
test('Complete Angular', function (testDone): any {
run([
testCompletionFor('<body |> </body >', {
items: [
{ label: 'ng-controller', resultText: '<body ng-controller="{{}}"> </body >' },
{ label: 'data-ng-controller', resultText: '<body data-ng-controller="{{}}"> </body >' },
]
}),
testCompletionFor('<li |> </li >', {
items: [
{ label: 'ng-repeat', resultText: '<li ng-repeat="{{}}"> </li >' },
{ label: 'data-ng-repeat', resultText: '<li data-ng-repeat="{{}}"> </li >' },
]
}),
testCompletionFor('<input |> </input >', {
items: [
{ label: 'ng-model', resultText: '<input ng-model="{{}}"> </input >' },
{ label: 'data-ng-model', resultText: '<input data-ng-model="{{}}"> </input >' },
]
})
], testDone);
});
test('Complete Ionic', function (testDone): any {
run([
// Try some Ionic tags
testCompletionFor('<|', {
items: [
{ label: 'ion-checkbox', resultText: '<ion-checkbox' },
{ label: 'ion-content', resultText: '<ion-content' },
]
})
], testDone);
});
test('Settings', function (testDone): any {
run([
testCompletionFor('<|', {
items: [
{ label: 'ion-checkbox' },
{ label: 'div', notAvailable: true },
]
}, { html5: false, ionic: true, angular1: false }),
testCompletionFor('<|', {
items: [
{ label: 'ion-checkbox', notAvailable: true },
{ label: 'div' },
]
}, { html5: true, ionic: false, angular1: false }),
testCompletionFor('<input |> </input >', {
items: [
{ label: 'ng-model', notAvailable: true },
{ label: 'type' },
]
}, { html5: true, ionic: false, angular1: false }),
], testDone);
});
})

View file

@ -1,69 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import * as htmlLanguageService from '../htmlLanguageService';
import {TextDocument} from 'vscode-languageserver-types';
export function assertHighlights(value: string, expectedMatches: number[], elementName: string): void {
let offset = value.indexOf('|');
value = value.substr(0, offset) + value.substr(offset + 1);
let document = TextDocument.create('test://test/test.html', 'html', 0, value);
let position = document.positionAt(offset);
let ls = htmlLanguageService.getLanguageService();
let htmlDoc = ls.parseHTMLDocument(document);
let highlights = ls.findDocumentHighlights(document, position, htmlDoc);
assert.equal(highlights.length, expectedMatches.length);
for (let i = 0; i < highlights.length; i++) {
let actualStartOffset = document.offsetAt(highlights[i].range.start);
assert.equal(actualStartOffset, expectedMatches[i]);
let actualEndOffset = document.offsetAt(highlights[i].range.end);
assert.equal(actualEndOffset, expectedMatches[i] + elementName.length);
assert.equal(document.getText().substring(actualStartOffset, actualEndOffset), elementName);
}
}
suite('HTML Highlighting', () => {
test('Single', function (): any {
assertHighlights('|<html></html>', [], null);
assertHighlights('<|html></html>', [1, 8], 'html');
assertHighlights('<h|tml></html>', [1, 8], 'html');
assertHighlights('<htm|l></html>', [1, 8], 'html');
assertHighlights('<html|></html>', [1, 8], 'html');
assertHighlights('<html>|</html>', [], null);
assertHighlights('<html><|/html>', [], null);
assertHighlights('<html></|html>', [1, 8], 'html');
assertHighlights('<html></h|tml>', [1, 8], 'html');
assertHighlights('<html></ht|ml>', [1, 8], 'html');
assertHighlights('<html></htm|l>', [1, 8], 'html');
assertHighlights('<html></html|>', [1, 8], 'html');
assertHighlights('<html></html>|', [], null);
});
test('Nested', function (): any {
assertHighlights('<html>|<div></div></html>', [], null);
assertHighlights('<html><|div></div></html>', [7, 13], 'div');
assertHighlights('<html><div>|</div></html>', [], null);
assertHighlights('<html><div></di|v></html>', [7, 13], 'div');
assertHighlights('<html><div><div></div></di|v></html>', [7, 24], 'div');
assertHighlights('<html><div><div></div|></div></html>', [12, 18], 'div');
assertHighlights('<html><div><div|></div></div></html>', [12, 18], 'div');
assertHighlights('<html><div><div></div></div></h|tml>', [1, 30], 'html');
assertHighlights('<html><di|v></div><div></div></html>', [7, 13], 'div');
assertHighlights('<html><div></div><div></d|iv></html>', [18, 24], 'div');
});
test('Selfclosed', function (): any {
assertHighlights('<html><|div/></html>', [7], 'div');
assertHighlights('<html><|br></html>', [7], 'br');
assertHighlights('<html><div><d|iv/></div></html>', [12], 'div');
});
});

View file

@ -1,85 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import {TextDocument} from 'vscode-languageserver-types';
import * as htmlLanguageService from '../htmlLanguageService';
suite('HTML Link Detection', () => {
function testLinkCreation(modelUrl: string, rootUrl: string, tokenContent: string, expected: string): void {
let document = TextDocument.create(modelUrl, 'html', 0, `<a href="${tokenContent}">`);
let ls = htmlLanguageService.getLanguageService();
let links = ls.findDocumentLinks(document, rootUrl);
assert.equal(links[0] && links[0].target, expected);
}
function testLinkDetection(value: string, expectedLinkLocations: number[]): void {
let document = TextDocument.create('test://test/test.html', 'html', 0, value);
let ls = htmlLanguageService.getLanguageService();
let links = ls.findDocumentLinks(document, 'test://test');
assert.deepEqual(links.map(l => l.range.start.character), expectedLinkLocations);
}
test('Link creation', () => {
testLinkCreation('inmemory://model/1', null, 'javascript:void;', null);
testLinkCreation('inmemory://model/1', null, ' \tjavascript:alert(7);', null);
testLinkCreation('inmemory://model/1', null, ' #relative', null);
testLinkCreation('inmemory://model/1', null, 'file:///C:\\Alex\\src\\path\\to\\file.txt', 'file:///C:\\Alex\\src\\path\\to\\file.txt');
testLinkCreation('inmemory://model/1', null, 'http://www.microsoft.com/', 'http://www.microsoft.com/');
testLinkCreation('inmemory://model/1', null, 'https://www.microsoft.com/', 'https://www.microsoft.com/');
testLinkCreation('inmemory://model/1', null, '//www.microsoft.com/', 'http://www.microsoft.com/');
testLinkCreation('inmemory://model/1', null, '../../a.js', 'inmemory://model/a.js');
testLinkCreation('inmemory://model/1', 'inmemory://model/', 'javascript:void;', null);
testLinkCreation('inmemory://model/1', 'inmemory://model/', ' \tjavascript:alert(7);', null);
testLinkCreation('inmemory://model/1', 'inmemory://model/', ' #relative', null);
testLinkCreation('inmemory://model/1', 'inmemory://model/', 'file:///C:\\Alex\\src\\path\\to\\file.txt', 'file:///C:\\Alex\\src\\path\\to\\file.txt');
testLinkCreation('inmemory://model/1', 'inmemory://model/', 'http://www.microsoft.com/', 'http://www.microsoft.com/');
testLinkCreation('inmemory://model/1', 'inmemory://model/', 'https://www.microsoft.com/', 'https://www.microsoft.com/');
testLinkCreation('inmemory://model/1', 'inmemory://model/', ' //www.microsoft.com/', 'http://www.microsoft.com/');
testLinkCreation('inmemory://model/1', 'inmemory://model/', '../../a.js', 'inmemory://model/a.js');
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, 'javascript:void;', null);
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, ' \tjavascript:alert(7);', null);
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, ' #relative', null);
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, 'file:///C:\\Alex\\src\\path\\to\\file.txt', 'file:///C:\\Alex\\src\\path\\to\\file.txt');
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, 'http://www.microsoft.com/', 'http://www.microsoft.com/');
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, 'https://www.microsoft.com/', 'https://www.microsoft.com/');
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, ' //www.microsoft.com/', 'http://www.microsoft.com/');
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, 'a.js', 'file:///c:/Alex/src/path/to/a.js');
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, '/a.js', 'file:///a.js');
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', 'javascript:void;', null);
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', ' \tjavascript:alert(7);', null);
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', ' #relative', null);
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', null, 'file:///C:\\Alex\\src\\path\\to\\file.txt', 'file:///C:\\Alex\\src\\path\\to\\file.txt');
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', 'http://www.microsoft.com/', 'http://www.microsoft.com/');
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', 'https://www.microsoft.com/', 'https://www.microsoft.com/');
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', 'https://www.microsoft.com/?q=1#h', 'https://www.microsoft.com/?q=1#h');
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', ' //www.microsoft.com/', 'http://www.microsoft.com/');
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', 'a.js', 'file:///c:/Alex/src/path/to/a.js');
testLinkCreation('file:///C:/Alex/src/path/to/file.txt', 'file:///C:/Alex/src/', '/a.js', 'file:///c:/Alex/src/a.js');
testLinkCreation('https://www.test.com/path/to/file.txt', null, 'file:///C:\\Alex\\src\\path\\to\\file.txt', 'file:///C:\\Alex\\src\\path\\to\\file.txt');
testLinkCreation('https://www.test.com/path/to/file.txt', null, '//www.microsoft.com/', 'https://www.microsoft.com/');
testLinkCreation('https://www.test.com/path/to/file.txt', 'https://www.test.com', '//www.microsoft.com/', 'https://www.microsoft.com/');
// invalid uris don't throw
testLinkCreation('https://www.test.com/path/to/file.txt', 'https://www.test.com', '%', 'https://www.test.com/path/to/%');
// Bug #18314: Ctrl + Click does not open existing file if folder's name starts with 'c' character
testLinkCreation('file:///c:/Alex/working_dir/18314-link-detection/test.html', 'file:///c:/Alex/working_dir/18314-link-detection/', '/class/class.js', 'file:///c:/Alex/working_dir/18314-link-detection/class/class.js');
});
test('Link detection', () => {
testLinkDetection('<img src="foo.png">', [10]);
testLinkDetection('<a href="http://server/foo.html">', [9]);
});
});

View file

@ -1,85 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import {Node, HTMLDocument, parse} from '../parser/htmlParser';
suite('HTML Parser', () => {
function toJSON(node: Node): any {
return { tag: node.tag, start: node.start, end: node.end, endTagStart: node.endTagStart, closed: node.closed, children: node.children.map(toJSON) };
}
function assertDocument(input: string, expected: any) {
let document = parse(input);
assert.deepEqual(document.roots.map(toJSON), expected);
}
function assertNodeBefore(input: string, offset: number, expectedTag: string) {
let document = parse(input);
let node = document.findNodeBefore(offset);
assert.equal(node ? node.tag : '', expectedTag, "offset " + offset);
}
test('Simple', () => {
assertDocument('<html></html>', [{ tag: 'html', start: 0, end: 13, endTagStart: 6, closed: true, children: [] }]);
assertDocument('<html><body></body></html>', [{ tag: 'html', start: 0, end: 26, endTagStart: 19, closed: true, children: [{ tag: 'body', start: 6, end: 19, endTagStart: 12, closed: true, children: [] }] }]);
assertDocument('<html><head></head><body></body></html>', [{ tag: 'html', start: 0, end: 39, endTagStart: 32, closed: true, children: [{ tag: 'head', start: 6, end: 19, endTagStart: 12, closed: true, children: [] }, { tag: 'body', start: 19, end: 32, endTagStart: 25, closed: true, children: [] }] }]);
});
test('SelfClose', () => {
assertDocument('<br/>', [{ tag: 'br', start: 0, end: 5, endTagStart: void 0, closed: true, children: [] }]);
assertDocument('<div><br/><span></span></div>', [{ tag: 'div', start: 0, end: 29, endTagStart: 23, closed: true, children: [{ tag: 'br', start: 5, end: 10, endTagStart: void 0, closed: true, children: [] }, { tag: 'span', start: 10, end: 23, endTagStart: 16, closed: true, children: [] }] }]);
});
test('EmptyTag', () => {
assertDocument('<meta>', [{ tag: 'meta', start: 0, end: 6, endTagStart: void 0, closed: true, children: [] }]);
assertDocument('<div><input type="button"><span><br><br></span></div>', [{
tag: 'div', start: 0, end: 53, endTagStart: 47, closed: true, children: [
{ tag: 'input', start: 5, end: 26, endTagStart: void 0, closed: true, children: [] },
{ tag: 'span', start: 26, end: 47, endTagStart: 40, closed: true, children: [{ tag: 'br', start: 32, end: 36, endTagStart: void 0, closed: true, children: [] }, { tag: 'br', start: 36, end: 40, endTagStart: void 0, closed: true, children: [] }] }
]
}]);
});
test('MissingTags', () => {
assertDocument('</meta>', []);
assertDocument('<div></div></div>', [{ tag: 'div', start: 0, end: 11, endTagStart: 5, closed: true, children: [] }]);
assertDocument('<div><div></div>', [{ tag: 'div', start: 0, end: 16, endTagStart: void 0, closed: false, children: [{ tag: 'div', start: 5, end: 16, endTagStart: 10, closed: true, children: [] }] }]);
assertDocument('<title><div></title>', [{ tag: 'title', start: 0, end: 20, endTagStart: 12, closed: true, children: [{ tag: 'div', start: 7, end: 12, endTagStart: void 0, closed: false, children: [] }] }]);
assertDocument('<h1><div><span></h1>', [{ tag: 'h1', start: 0, end: 20, endTagStart: 15, closed: true, children: [{ tag: 'div', start: 4, end: 15, endTagStart: void 0, closed: false, children: [{ tag: 'span', start: 9, end: 15, endTagStart: void 0, closed: false, children: [] }] }] }]);
});
test('FindNodeBefore', () => {
let str = '<div><input type="button"><span><br><hr></span></div>';
assertNodeBefore(str, 0, void 0);
assertNodeBefore(str, 1, 'div');
assertNodeBefore(str, 5, 'div');
assertNodeBefore(str, 6, 'input');
assertNodeBefore(str, 25, 'input');
assertNodeBefore(str, 26, 'input');
assertNodeBefore(str, 27, 'span');
assertNodeBefore(str, 32, 'span');
assertNodeBefore(str, 33, 'br');
assertNodeBefore(str, 36, 'br');
assertNodeBefore(str, 37, 'hr');
assertNodeBefore(str, 40, 'hr');
assertNodeBefore(str, 41, 'hr');
assertNodeBefore(str, 42, 'hr');
assertNodeBefore(str, 47, 'span');
assertNodeBefore(str, 48, 'span');
assertNodeBefore(str, 52, 'span');
assertNodeBefore(str, 53, 'div');
});
test('FindNodeBefore - incomplete node', () => {
let str = '<div><span><br></div>';
assertNodeBefore(str, 15, 'br');
assertNodeBefore(str, 18, 'br');
assertNodeBefore(str, 21, 'div');
});
});

View file

@ -1,745 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import * as assert from 'assert';
import {Scanner, TokenType, ScannerState, createScanner} from '../parser/htmlScanner';
suite('HTML Scanner', () => {
interface Token {
offset: number;
type: TokenType;
content?: string;
}
function assertTokens(tests: { input: string; tokens: Token[]; }[]) {
let scannerState = ScannerState.WithinContent;
for (let t of tests) {
let scanner = createScanner(t.input, 0, scannerState);
let tokenType = scanner.scan();
let actual: Token[] = [];
while (tokenType !== TokenType.EOS) {
let actualToken: Token = { offset: scanner.getTokenOffset(), type: tokenType };
if (tokenType == TokenType.StartTag || tokenType == TokenType.EndTag) {
actualToken.content = t.input.substr(scanner.getTokenOffset(), scanner.getTokenLength());
}
actual.push(actualToken);
tokenType = scanner.scan();
}
assert.deepEqual(actual, t.tokens);
scannerState = scanner.getScannerState();
}
}
test('Open Start Tag #1', () => {
assertTokens([{
input: '<abc',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' }
]
}
]);
});
test('Open Start Tag #2', () => {
assertTokens([{
input: '<input',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'input' }
]
}
]);
});
test('Open Start Tag with Invalid Tag', () => {
assertTokens([{
input: '< abc',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.Whitespace },
{ offset: 2, type: TokenType.StartTag, content: 'abc' }
]
}
]);
});
test('Open Start Tag #3', () => {
assertTokens([{
input: '< abc>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.Whitespace },
{ offset: 2, type: TokenType.StartTag, content: 'abc' },
{ offset: 5, type: TokenType.StartTagClose },
]
}
]);
});
test('Open Start Tag #4', () => {
assertTokens([{
input: 'i <len;',
tokens: [
{ offset: 0, type: TokenType.Content },
{ offset: 2, type: TokenType.StartTagOpen },
{ offset: 3, type: TokenType.StartTag, content: 'len' },
{ offset: 6, type: TokenType.Unknown }
]
}
]);
});
test('Open Start Tag #5', () => {
assertTokens([{
input: '<',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen }
]
}
]);
});
test('Open End Tag', () => {
assertTokens([{
input: '</a',
tokens: [
{ offset: 0, type: TokenType.EndTagOpen },
{ offset: 2, type: TokenType.EndTag, content: 'a' }
]
}
]);
});
test('Complete Start Tag', () => {
assertTokens([{
input: '<abc>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' },
{ offset: 4, type: TokenType.StartTagClose }
]
}
]);
});
test('Complete Start Tag with Whitespace', () => {
assertTokens([{
input: '<abc >',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' },
{ offset: 4, type: TokenType.Whitespace },
{ offset: 5, type: TokenType.StartTagClose }
]
}
]);
});
test('bug 9809 - Complete Start Tag with Namespaceprefix', () => {
assertTokens([{
input: '<foo:bar>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'foo:bar' },
{ offset: 8, type: TokenType.StartTagClose }
]
}
]);
});
test('Complete End Tag', () => {
assertTokens([{
input: '</abc>',
tokens: [
{ offset: 0, type: TokenType.EndTagOpen },
{ offset: 2, type: TokenType.EndTag, content: 'abc' },
{ offset: 5, type: TokenType.EndTagClose }
]
}
]);
});
test('Complete End Tag with Whitespace', () => {
assertTokens([{
input: '</abc >',
tokens: [
{ offset: 0, type: TokenType.EndTagOpen },
{ offset: 2, type: TokenType.EndTag, content: 'abc' },
{ offset: 5, type: TokenType.Whitespace },
{ offset: 7, type: TokenType.EndTagClose }
]
}
]);
});
test('Empty Tag', () => {
assertTokens([{
input: '<abc />',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' },
{ offset: 4, type: TokenType.Whitespace },
{ offset: 5, type: TokenType.StartTagSelfClose }
]
}
]);
});
test('Embedded Content #1', () => {
assertTokens([{
input: '<script type="text/javascript">var i= 10;</script>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'script' },
{ offset: 7, type: TokenType.Whitespace },
{ offset: 8, type: TokenType.AttributeName },
{ offset: 12, type: TokenType.DelimiterAssign },
{ offset: 13, type: TokenType.AttributeValue },
{ offset: 30, type: TokenType.StartTagClose },
{ offset: 31, type: TokenType.Script },
{ offset: 41, type: TokenType.EndTagOpen },
{ offset: 43, type: TokenType.EndTag, content: 'script' },
{ offset: 49, type: TokenType.EndTagClose }
]
}
]);
});
test('Embedded Content #2', () => {
assertTokens([{
input: '<script type="text/javascript">',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'script' },
{ offset: 7, type: TokenType.Whitespace },
{ offset: 8, type: TokenType.AttributeName },
{ offset: 12, type: TokenType.DelimiterAssign },
{ offset: 13, type: TokenType.AttributeValue },
{ offset: 30, type: TokenType.StartTagClose }
]
}, {
input: 'var i= 10;',
tokens: [
{ offset: 0, type: TokenType.Script }
]
}, {
input: '</script>',
tokens: [
{ offset: 0, type: TokenType.EndTagOpen },
{ offset: 2, type: TokenType.EndTag, content: 'script' },
{ offset: 8, type: TokenType.EndTagClose }
]
}
]);
});
test('Embedded Content #3', () => {
assertTokens([{
input: '<script type="text/javascript">var i= 10;',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'script' },
{ offset: 7, type: TokenType.Whitespace },
{ offset: 8, type: TokenType.AttributeName },
{ offset: 12, type: TokenType.DelimiterAssign },
{ offset: 13, type: TokenType.AttributeValue },
{ offset: 30, type: TokenType.StartTagClose },
{ offset: 31, type: TokenType.Script }
]
}, {
input: '</script>',
tokens: [
{ offset: 0, type: TokenType.EndTagOpen },
{ offset: 2, type: TokenType.EndTag, content: 'script' },
{ offset: 8, type: TokenType.EndTagClose }
]
}
]);
});
test('Embedded Content #4', () => {
assertTokens([{
input: '<script type="text/javascript">',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'script' },
{ offset: 7, type: TokenType.Whitespace },
{ offset: 8, type: TokenType.AttributeName },
{ offset: 12, type: TokenType.DelimiterAssign },
{ offset: 13, type: TokenType.AttributeValue },
{ offset: 30, type: TokenType.StartTagClose }
]
}, {
input: 'var i= 10;</script>',
tokens: [
{ offset: 0, type: TokenType.Script },
{ offset: 10, type: TokenType.EndTagOpen },
{ offset: 12, type: TokenType.EndTag, content: 'script' },
{ offset: 18, type: TokenType.EndTagClose }
]
}
]);
});
test('Embedded Content #5', () => {
assertTokens([{
input: '<script type="text/plain">a\n<a</script>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'script' },
{ offset: 7, type: TokenType.Whitespace },
{ offset: 8, type: TokenType.AttributeName },
{ offset: 12, type: TokenType.DelimiterAssign },
{ offset: 13, type: TokenType.AttributeValue },
{ offset: 25, type: TokenType.StartTagClose },
{ offset: 26, type: TokenType.Script },
{ offset: 30, type: TokenType.EndTagOpen },
{ offset: 32, type: TokenType.EndTag, content: 'script' },
{ offset: 38, type: TokenType.EndTagClose }
]
}
]);
});
test('Embedded Content #6', () => {
assertTokens([{
input: '<script>a</script><script>b</script>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'script' },
{ offset: 7, type: TokenType.StartTagClose },
{ offset: 8, type: TokenType.Script },
{ offset: 9, type: TokenType.EndTagOpen },
{ offset: 11, type: TokenType.EndTag, content: 'script' },
{ offset: 17, type: TokenType.EndTagClose },
{ offset: 18, type: TokenType.StartTagOpen },
{ offset: 19, type: TokenType.StartTag, content: 'script' },
{ offset: 25, type: TokenType.StartTagClose },
{ offset: 26, type: TokenType.Script },
{ offset: 27, type: TokenType.EndTagOpen },
{ offset: 29, type: TokenType.EndTag, content: 'script' },
{ offset: 35, type: TokenType.EndTagClose }
]
}
]);
});
test('Embedded Content #7', () => {
assertTokens([{
input: '<script type="text/javascript"></script>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'script' },
{ offset: 7, type: TokenType.Whitespace },
{ offset: 8, type: TokenType.AttributeName },
{ offset: 12, type: TokenType.DelimiterAssign },
{ offset: 13, type: TokenType.AttributeValue },
{ offset: 30, type: TokenType.StartTagClose },
{ offset: 31, type: TokenType.EndTagOpen },
{ offset: 33, type: TokenType.EndTag, content: 'script' },
{ offset: 39, type: TokenType.EndTagClose }
]
}
]);
});
test('Embedded Content #8', () => {
assertTokens([{
input: '<script>var i= 10;</script>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'script' },
{ offset: 7, type: TokenType.StartTagClose },
{ offset: 8, type: TokenType.Script },
{ offset: 18, type: TokenType.EndTagOpen },
{ offset: 20, type: TokenType.EndTag, content: 'script' },
{ offset: 26, type: TokenType.EndTagClose }
]
}
]);
});
test('Embedded Content #9', () => {
assertTokens([{
input: '<script type="text/javascript" src="main.js"></script>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'script' },
{ offset: 7, type: TokenType.Whitespace },
{ offset: 8, type: TokenType.AttributeName },
{ offset: 12, type: TokenType.DelimiterAssign },
{ offset: 13, type: TokenType.AttributeValue },
{ offset: 30, type: TokenType.Whitespace },
{ offset: 31, type: TokenType.AttributeName },
{ offset: 34, type: TokenType.DelimiterAssign },
{ offset: 35, type: TokenType.AttributeValue },
{ offset: 44, type: TokenType.StartTagClose },
{ offset: 45, type: TokenType.EndTagOpen },
{ offset: 47, type: TokenType.EndTag, content: 'script' },
{ offset: 53, type: TokenType.EndTagClose }
]
}
]);
});
test('Embedded Content #10', () => {
assertTokens([{
input: '<script><!-- alert("<script></script>"); --></script>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'script' },
{ offset: 7, type: TokenType.StartTagClose },
{ offset: 8, type: TokenType.Script },
{ offset: 44, type: TokenType.EndTagOpen },
{ offset: 46, type: TokenType.EndTag, content: 'script' },
{ offset: 52, type: TokenType.EndTagClose }
]
}
]);
});
test('Embedded Content #11', () => {
assertTokens([{
input: '<script><!-- alert("<script></script>"); </script>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'script' },
{ offset: 7, type: TokenType.StartTagClose },
{ offset: 8, type: TokenType.Script },
{ offset: 41, type: TokenType.EndTagOpen },
{ offset: 43, type: TokenType.EndTag, content: 'script' },
{ offset: 49, type: TokenType.EndTagClose }
]
}
]);
});
test('Embedded Content #12', () => {
assertTokens([{
input: '<script><!-- alert("</script>"); </script>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'script' },
{ offset: 7, type: TokenType.StartTagClose },
{ offset: 8, type: TokenType.Script },
{ offset: 20, type: TokenType.EndTagOpen },
{ offset: 22, type: TokenType.EndTag, content: 'script' },
{ offset: 28, type: TokenType.EndTagClose },
{ offset: 29, type: TokenType.Content },
{ offset: 33, type: TokenType.EndTagOpen },
{ offset: 35, type: TokenType.EndTag, content: 'script' },
{ offset: 41, type: TokenType.EndTagClose }
]
}
]);
});
test('Embedded Content #13', () => {
assertTokens([{
input: '<script> alert("<script></script>"); </script>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'script' },
{ offset: 7, type: TokenType.StartTagClose },
{ offset: 8, type: TokenType.Script },
{ offset: 24, type: TokenType.EndTagOpen },
{ offset: 26, type: TokenType.EndTag, content: 'script' },
{ offset: 32, type: TokenType.EndTagClose },
{ offset: 33, type: TokenType.Content },
{ offset: 37, type: TokenType.EndTagOpen },
{ offset: 39, type: TokenType.EndTag, content: 'script' },
{ offset: 45, type: TokenType.EndTagClose }
]
}
]);
});
test('Tag with Attribute', () => {
assertTokens([{
input: '<abc foo="bar">',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' },
{ offset: 4, type: TokenType.Whitespace },
{ offset: 5, type: TokenType.AttributeName },
{ offset: 8, type: TokenType.DelimiterAssign },
{ offset: 9, type: TokenType.AttributeValue },
{ offset: 14, type: TokenType.StartTagClose }
]
}
]);
});
test('Tag with Empty Attribute Value', () => {
assertTokens([{
input: '<abc foo=\'bar\'>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' },
{ offset: 4, type: TokenType.Whitespace },
{ offset: 5, type: TokenType.AttributeName },
{ offset: 8, type: TokenType.DelimiterAssign },
{ offset: 9, type: TokenType.AttributeValue },
{ offset: 14, type: TokenType.StartTagClose }
]
}
]);
});
test('Tag with empty attributes', () => {
assertTokens([{
input: '<abc foo="">',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' },
{ offset: 4, type: TokenType.Whitespace },
{ offset: 5, type: TokenType.AttributeName },
{ offset: 8, type: TokenType.DelimiterAssign },
{ offset: 9, type: TokenType.AttributeValue },
{ offset: 11, type: TokenType.StartTagClose }
]
}
]);
});
test('Tag with Attributes', () => {
assertTokens([{
input: '<abc foo="bar" bar=\'foo\'>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' },
{ offset: 4, type: TokenType.Whitespace },
{ offset: 5, type: TokenType.AttributeName },
{ offset: 8, type: TokenType.DelimiterAssign },
{ offset: 9, type: TokenType.AttributeValue },
{ offset: 14, type: TokenType.Whitespace },
{ offset: 15, type: TokenType.AttributeName },
{ offset: 18, type: TokenType.DelimiterAssign },
{ offset: 19, type: TokenType.AttributeValue },
{ offset: 24, type: TokenType.StartTagClose }
]
}
]);
});
test('Tag with Attributes, no quotes', () => {
assertTokens([{
input: '<abc foo=bar bar=help-me>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' },
{ offset: 4, type: TokenType.Whitespace },
{ offset: 5, type: TokenType.AttributeName },
{ offset: 8, type: TokenType.DelimiterAssign },
{ offset: 9, type: TokenType.AttributeValue },
{ offset: 12, type: TokenType.Whitespace },
{ offset: 13, type: TokenType.AttributeName },
{ offset: 16, type: TokenType.DelimiterAssign },
{ offset: 17, type: TokenType.AttributeValue },
{ offset: 24, type: TokenType.StartTagClose }
]
}
]);
});
test('Tag with Attribute And Whitespace', () => {
assertTokens([{
input: '<abc foo= "bar">',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' },
{ offset: 4, type: TokenType.Whitespace },
{ offset: 5, type: TokenType.AttributeName },
{ offset: 8, type: TokenType.DelimiterAssign },
{ offset: 9, type: TokenType.Whitespace },
{ offset: 11, type: TokenType.AttributeValue },
{ offset: 16, type: TokenType.StartTagClose }
]
}
]);
});
test('Tag with Attribute And Whitespace #2', () => {
assertTokens([{
input: '<abc foo = "bar">',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' },
{ offset: 4, type: TokenType.Whitespace },
{ offset: 5, type: TokenType.AttributeName },
{ offset: 8, type: TokenType.Whitespace },
{ offset: 9, type: TokenType.DelimiterAssign },
{ offset: 10, type: TokenType.Whitespace },
{ offset: 11, type: TokenType.AttributeValue },
{ offset: 16, type: TokenType.StartTagClose }
]
}
]);
});
test('Tag with Name-Only-Attribute #1', () => {
assertTokens([{
input: '<abc foo>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' },
{ offset: 4, type: TokenType.Whitespace },
{ offset: 5, type: TokenType.AttributeName },
{ offset: 8, type: TokenType.StartTagClose }
]
}
]);
});
test('Tag with Name-Only-Attribute #2', () => {
assertTokens([{
input: '<abc foo bar>',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' },
{ offset: 4, type: TokenType.Whitespace },
{ offset: 5, type: TokenType.AttributeName },
{ offset: 8, type: TokenType.Whitespace },
{ offset: 9, type: TokenType.AttributeName },
{ offset: 12, type: TokenType.StartTagClose }
]
}
]);
});
test('Tag with Interesting Attribute Name', () => {
assertTokens([{
input: '<abc foo!@#="bar">',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' },
{ offset: 4, type: TokenType.Whitespace },
{ offset: 5, type: TokenType.AttributeName },
{ offset: 11, type: TokenType.DelimiterAssign },
{ offset: 12, type: TokenType.AttributeValue },
{ offset: 17, type: TokenType.StartTagClose }
]
}
]);
});
test('Tag with Angular Attribute Name', () => {
assertTokens([{
input: '<abc #myinput (click)="bar" [value]="someProperty" *ngIf="someCondition">',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' },
{ offset: 4, type: TokenType.Whitespace },
{ offset: 5, type: TokenType.AttributeName },
{ offset: 13, type: TokenType.Whitespace },
{ offset: 14, type: TokenType.AttributeName },
{ offset: 21, type: TokenType.DelimiterAssign },
{ offset: 22, type: TokenType.AttributeValue },
{ offset: 27, type: TokenType.Whitespace },
{ offset: 28, type: TokenType.AttributeName },
{ offset: 35, type: TokenType.DelimiterAssign },
{ offset: 36, type: TokenType.AttributeValue },
{ offset: 50, type: TokenType.Whitespace },
{ offset: 51, type: TokenType.AttributeName },
{ offset: 56, type: TokenType.DelimiterAssign },
{ offset: 57, type: TokenType.AttributeValue },
{ offset: 72, type: TokenType.StartTagClose }
]
}
]);
});
test('Tag with Invalid Attribute Value', () => {
assertTokens([{
input: '<abc foo=">',
tokens: [
{ offset: 0, type: TokenType.StartTagOpen },
{ offset: 1, type: TokenType.StartTag, content: 'abc' },
{ offset: 4, type: TokenType.Whitespace },
{ offset: 5, type: TokenType.AttributeName },
{ offset: 8, type: TokenType.DelimiterAssign },
{ offset: 9, type: TokenType.AttributeValue }
]
}
]);
});
test('Simple Comment 1', () => {
assertTokens([{
input: '<!--a-->',
tokens: [
{ offset: 0, type: TokenType.StartCommentTag },
{ offset: 4, type: TokenType.Comment },
{ offset: 5, type: TokenType.EndCommentTag }
]
}
]);
});
test('Simple Comment 2', () => {
assertTokens([{
input: '<!--a>foo bar</a -->',
tokens: [
{ offset: 0, type: TokenType.StartCommentTag },
{ offset: 4, type: TokenType.Comment },
{ offset: 17, type: TokenType.EndCommentTag }
]
}
]);
});
test('Multiline Comment', () => {
assertTokens([{
input: '<!--a>\nfoo \nbar</a -->',
tokens: [
{ offset: 0, type: TokenType.StartCommentTag },
{ offset: 4, type: TokenType.Comment },
{ offset: 19, type: TokenType.EndCommentTag }
]
}
]);
});
test('Simple Doctype', () => {
assertTokens([{
input: '<!Doctype a>',
tokens: [
{ offset: 0, type: TokenType.StartDoctypeTag },
{ offset: 9, type: TokenType.Doctype },
{ offset: 11, type: TokenType.EndDoctypeTag }
]
}
]);
});
test('Simple Doctype #2', () => {
assertTokens([{
input: '<!doctype a>',
tokens: [
{ offset: 0, type: TokenType.StartDoctypeTag },
{ offset: 9, type: TokenType.Doctype },
{ offset: 11, type: TokenType.EndDoctypeTag }
]
}
]);
});
test('Simple Doctype #4', () => {
assertTokens([{
input: '<!DOCTYPE a\n"foo" \'bar\'>',
tokens: [
{ offset: 0, type: TokenType.StartDoctypeTag },
{ offset: 9, type: TokenType.Doctype },
{ offset: 23, type: TokenType.EndDoctypeTag }
]
}
]);
});
});

View file

@ -1,23 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
import {TextDocument, TextEdit} from 'vscode-languageserver-types';
import * as assert from 'assert';
export function applyEdits(document: TextDocument, edits: TextEdit[]): string {
let text = document.getText();
let sortedEdits = edits.sort((a, b) => document.offsetAt(b.range.start) - document.offsetAt(a.range.start));
let lastOffset = text.length;
sortedEdits.forEach(e => {
let startOffset = document.offsetAt(e.range.start);
let endOffset = document.offsetAt(e.range.end);
assert.ok(startOffset <= endOffset);
assert.ok(endOffset <= lastOffset);
text = text.substring(0, startOffset) + e.newText + text.substring(endOffset, text.length);
lastOffset = startOffset;
});
return text;
}

View file

@ -1,44 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
/**
* Takes a sorted array and a function p. The array is sorted in such a way that all elements where p(x) is false
* are located before all elements where p(x) is true.
* @returns the least x for which p(x) is true or array.length if no element fullfills the given function.
*/
export function findFirst<T>(array: T[], p: (x: T) => boolean): number {
let low = 0, high = array.length;
if (high === 0) {
return 0; // no children
}
while (low < high) {
let mid = Math.floor((low + high) / 2);
if (p(array[mid])) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
export function binarySearch<T>(array: T[], key: T, comparator: (op1: T, op2: T) => number): number {
let low = 0,
high = array.length - 1;
while (low <= high) {
let mid = ((low + high) / 2) | 0;
let comp = comparator(array[mid], key);
if (comp < 0) {
low = mid + 1;
} else if (comp > 0) {
high = mid - 1;
} else {
return mid;
}
}
return -(low + 1);
}

View file

@ -1,76 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
export const enum CharCode {
Slash = 47,
Backslash = 92
}
/**
* @returns the directory name of a path.
*/
export function dirname(path: string): string {
var idx = ~path.lastIndexOf('/') || ~path.lastIndexOf('\\');
if (idx === 0) {
return '.';
} else if (~idx === 0) {
return path[0];
} else {
return path.substring(0, ~idx);
}
}
/**
* @returns the base name of a path.
*/
export function basename(path: string): string {
var idx = ~path.lastIndexOf('/') || ~path.lastIndexOf('\\');
if (idx === 0) {
return path;
} else if (~idx === path.length - 1) {
return basename(path.substring(0, path.length - 1));
} else {
return path.substr(~idx + 1);
}
}
/**
* @returns {{.far}} from boo.far or the empty string.
*/
export function extname(path: string): string {
path = basename(path);
var idx = ~path.lastIndexOf('.');
return idx ? path.substring(~idx) : '';
}
export const join: (...parts: string[]) => string = function () {
// Not using a function with var-args because of how TS compiles
// them to JS - it would result in 2*n runtime cost instead
// of 1*n, where n is parts.length.
let value = '';
for (let i = 0; i < arguments.length; i++) {
let part = arguments[i];
if (i > 0) {
// add the separater between two parts unless
// there already is one
let last = value.charCodeAt(value.length - 1);
if (last !== CharCode.Slash && last !== CharCode.Backslash) {
let next = part.charCodeAt(0);
if (next !== CharCode.Slash && next !== CharCode.Backslash) {
value += '/';
}
}
}
value += part;
}
return value;
};

View file

@ -1,50 +0,0 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
export function startsWith(haystack: string, needle: string): boolean {
if (haystack.length < needle.length) {
return false;
}
for (let i = 0; i < needle.length; i++) {
if (haystack[i] !== needle[i]) {
return false;
}
}
return true;
}
/**
* Determines if haystack ends with needle.
*/
export function endsWith(haystack: string, needle: string): boolean {
let diff = haystack.length - needle.length;
if (diff > 0) {
return haystack.lastIndexOf(needle) === diff;
} else if (diff === 0) {
return haystack === needle;
} else {
return false;
}
}
/**
* @returns the length of the common prefix of the two strings.
*/
export function commonPrefixLength(a: string, b: string): number {
let i: number,
len = Math.min(a.length, b.length);
for (i = 0; i < len; i++) {
if (a.charCodeAt(i) !== b.charCodeAt(i)) {
return i;
}
}
return len;
}