Skip to content

Commit a9ffc3b

Browse files
committed
html language service and getAllDocumentLinks api
1 parent 96edd75 commit a9ffc3b

File tree

10 files changed

+161
-3
lines changed

10 files changed

+161
-3
lines changed

LICENSE-thirdParty

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
The MIT License (MIT)
33
Copyright (c) Microsoft
44

5+
## vscode-html-languageservice
6+
The MIT License (MIT)
7+
Copyright (c) Microsoft
8+
59
## test/thirparty/chai
610
MIT License
711
Copyright (c) 2017 Chai.js Assertion Library
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* global*/
22

3-
// importScripts("dist/language-worker.js");
4-
importScripts("dist/language-worker-debug.js"); // uncomment to debug
3+
importScripts("dist/language-worker.js");
4+
// importScripts("dist/language-worker-debug.js"); // uncomment to debug
55

66
function workerOK() {
77
if(self.CSSLanguageService){

test/html-worker-task.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* global*/
2+
3+
importScripts("dist/language-worker.js");
4+
// importScripts("dist/language-worker-debug.js"); // uncomment to debug
5+
6+
function workerOK() {
7+
if(self.HTMLLanguageService){
8+
postMessage({type: 'worker.ok'});
9+
}
10+
}
11+
12+
function getAllDocumentLinks(data) {
13+
const htmlMode = self.HTMLLanguageService.HTML_MODES[data.htmlMode];
14+
const links = self.HTMLLanguageService.getAllDocumentLinks(data.text, htmlMode, data.filePath);
15+
postMessage({type: "getAllDocumentLinks", links});
16+
}
17+
18+
self.addEventListener('message', (event) => {
19+
console.log('Worker received: ', event);
20+
const command = event.data.command;
21+
switch (command) {
22+
case 'workerOK': workerOK(); break;
23+
case 'getAllDocumentLinks': getAllDocumentLinks(event.data); break;
24+
default: console.error('unknown worker command: ', command);
25+
}
26+
}, false);

test/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
<script src="testInit.js"></script>
2525
<script src="test-css.worker.js"></script>
26+
<script src="test-html.worker.js"></script>
2627
</head>
2728
<body onload="mocha.run()">
2829
<div id="mocha"></div>

test/test-css.worker.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe(`web worker CSS Language tests`, function () {
55
let messageFromWorker = null;
66

77
before(async function () {
8-
worker = new Worker(`worker-task.js?debug=true`);
8+
worker = new Worker(`css-worker-task.js`);
99
console.log(worker);
1010
worker.onmessage= function (event) {
1111
console.log(`From Worker:`, event);

test/test-files/html-tests/a.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>html file</title>
6+
<link rel="stylesheet" href="mystyle.css"/>
7+
<link rel="stylesheet" href="http://domain:port/style.css"/>
8+
<link rel="stylesheet" href="file:///path/to/style.css"/>
9+
<link rel="stylesheet/less" type="text/css" href="sub/dir/styles.less" />
10+
<link rel="stylesheet/less" type="text/css" href="https://domain:port/styles.less" />
11+
</head>
12+
<body>
13+
html file
14+
</body>
15+
</html>

test/test-files/html-tests/b.htm

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>htm file</title>
6+
<link rel="stylesheet" href="mystyle.css"/>
7+
<link rel="stylesheet" href="http://domain:port/style.css"/>
8+
<link rel="stylesheet" href="file:///path/to/style.css"/>
9+
<link rel="stylesheet/less" type="text/css" href="sub/dir/styles.less" />
10+
<link rel="stylesheet/less" type="text/css" href="https://domain:port/styles.less" />
11+
</head>
12+
<body>
13+
htm file
14+
</body>
15+
</html>

test/test-files/html-tests/c.xhtml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8"/>
5+
<title>xhtml file</title>
6+
<link rel="stylesheet" href="mystyle.css"/>
7+
<link rel="stylesheet" href="http://domain:port/style.css"/>
8+
<link rel="stylesheet" href="file:///path/to/style.css"/>
9+
<link rel="stylesheet/less" type="text/css" href="sub/dir/styles.less" />
10+
<link rel="stylesheet/less" type="text/css" href="https://domain:port/styles.less" />
11+
</head>
12+
<body>
13+
xhtml file
14+
</body>
15+
</html>

test/test-files/html-tests/php.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<link rel="stylesheet" href="mystyle.css"/>
5+
<link rel="stylesheet" href="http://domain:port/style.css"/>
6+
<link rel="stylesheet" href="file:///path/to/style.css"/>
7+
<link rel="stylesheet/less" type="text/css" href="sub/dir/styles.less" />
8+
<link rel="stylesheet/less" type="text/css" href="https://domain:port/styles.less" />
9+
</head>
10+
<body>
11+
12+
<h1>My first PHP page</h1>
13+
14+
<?php
15+
echo "Hello World!";
16+
?>
17+
18+
</body>
19+
</html>

test/test-html.worker.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* global expect*/
2+
3+
describe(`web worker HTML Language tests`, function () {
4+
let worker;
5+
let messageFromWorker = null;
6+
7+
before(async function () {
8+
worker = new Worker(`html-worker-task.js`);
9+
console.log(worker);
10+
worker.onmessage= function (event) {
11+
console.log(`From Worker:`, event);
12+
messageFromWorker = event.data;
13+
};
14+
});
15+
16+
after(async function () {
17+
});
18+
19+
beforeEach(async function () {
20+
});
21+
22+
async function waitForWorkerMessage(message, timeoutMs) {
23+
let startTime = Date.now();
24+
return new Promise((resolve)=>{
25+
let interVal;
26+
function checkMessage() {
27+
if(messageFromWorker && messageFromWorker.type === message){
28+
resolve(messageFromWorker);
29+
clearInterval(interVal);
30+
}
31+
let elapsedTime = Date.now() - startTime;
32+
if(elapsedTime > timeoutMs){
33+
resolve(null);
34+
clearInterval(interVal);
35+
}
36+
}
37+
interVal = setInterval(checkMessage, 10);
38+
});
39+
}
40+
41+
it(`Should load HTML Language Service in worker`, async function () {
42+
messageFromWorker = null;
43+
worker.postMessage({command: `workerOK`});
44+
let output = await waitForWorkerMessage(`worker.ok`, 1000);
45+
expect(output).to.be.not.null;
46+
});
47+
48+
const files = ["a.html", "b.htm", "c.xhtml", "php.php"];
49+
const fileModes = ["html", "htm", "xhtml", "php"];
50+
for(let i=0;i<files.length; i++){
51+
const file = files[i];
52+
const fileMode = fileModes[i];
53+
it(`Should getAllDocumentLinks in ${file} file`, async function () {
54+
messageFromWorker = null;
55+
const text = await (await fetch(`test-files/html-tests/${file}`)).text();
56+
worker.postMessage({command: `getAllDocumentLinks`, text, htmlMode: fileMode, filePath: "/a.html"});
57+
let output = await waitForWorkerMessage(`getAllDocumentLinks`, 1000);
58+
const links = output.links;
59+
expect(links).to.deep.equal(["mystyle.css","http://domain:port/style.css",
60+
"file:///path/to/style.css","sub/dir/styles.less","https://domain:port/styles.less"]);
61+
});
62+
}
63+
});

0 commit comments

Comments
 (0)