Skip to content

Commit

Permalink
🐛 fixed: "return !checked;" and "strings template"
Browse files Browse the repository at this point in the history
  • Loading branch information
zswang committed Apr 26, 2017
1 parent 85fe67a commit 7fc2f4a
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 10 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jhtmls",
"version": "1.1.3",
"version": "1.1.11",
"homepage": "https://github.com/zswang/jhtmls",
"description": "JS and HTML alternate javascript template",
"main": "jhtmls.js",
Expand Down
37 changes: 33 additions & 4 deletions jhtmls.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
* @author
* zswang (http://weibo.com/zswang)
* zinkey (http://weibo.com/zinkey)
* @version 1.1.3
* @date 2017-01-16
* @version 1.1.11
* @date 2017-04-26
*/
/*<function name="encodeHTML">*/
var htmlEncodeDict = {
Expand Down Expand Up @@ -119,6 +119,31 @@
console.log(jhtmls.isOutput('hello'));
// > true
```
* @example isOutput():No semicolon "foo()"
```js
console.log(jhtmls.isOutput('foo()'));
// > false
```
* @example isOutput():Not symbol "return !todo.completed"
```js
console.log(jhtmls.isOutput('return !todo.completed'));
// > false
```
* @example isOutput():Strings Template "`${name}`"
```js
console.log(jhtmls.isOutput('`${name}`'));
// > false
```
* @example isOutput():Strings Template "\`\`\`js"
```js
console.log(jhtmls.isOutput('\`\`\`js'));
// > true
```
* @example isOutput():Url "http://jhtmls.com/"
```js
console.log(jhtmls.isOutput('http://jhtmls.com/'));
// > true
```
'''</example>'''
*/
function jhtmls_isOutput(line) {
Expand All @@ -134,12 +159,16 @@
}
// 非 JavaScript 字符开头
// 示例:#、<div>、汉字
if (/^[ \w\t_$]*([^&\^?|\n\w\/'"{}\[\]+\-():;, \t=\.$_]|:\/\/).*$/.test(line)) {
if (/^[ \w\t_$]*([^&\^?|\n\w\/'"{}\[\]+\-():;,!` \t=\.$_]|:\/\/).*$/.test(line)) {
return true;
}
// ```
if (/^\s*[`\-+'"]{3,}/.test(line)) {
return true;
}
// 不是 else 等单行语句
// 示例:hello world
if (/^(?!\s*(else|do|try|finally|void|typeof\s[\w$_]*)\s*$)[^'":;{}()\[\],\n|=&\/^?]+$/.test(line)) {
if (/^(?!\s*(else|do|try|finally|void|typeof\s[\w$_]*)\s*$)[^'"`!:;{}()\[\],\n|=&\/^?]+$/.test(line)) {
return true;
}
return false;
Expand Down
2 changes: 1 addition & 1 deletion jhtmls.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "jhtmls",
"title": "jhtmls",
"description": "JS and HTML alternate javascript template",
"version": "1.1.3",
"version": "1.1.11",
"homepage": "http://jhtmls.com/",
"main": "jhtmls.js",
"author": [
Expand Down Expand Up @@ -47,7 +47,7 @@
"lint": "jshint src/*.js",
"test": "istanbul cover --hook-run-in-context node_modules/mocha/bin/_mocha -- -R spec",
"example": "gulp example",
"mocha": "mocha",
"mocha": "npm run example && mocha",
"dist": "npm run _update_version && npm run example && npm run _dist && npm run test"
},
"files": [
Expand Down
34 changes: 32 additions & 2 deletions src/jhtmls.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,31 @@
console.log(jhtmls.isOutput('hello'));
// > true
```
* @example isOutput():No semicolon "foo()"
```js
console.log(jhtmls.isOutput('foo()'));
// > false
```
* @example isOutput():Not symbol "return !todo.completed"
```js
console.log(jhtmls.isOutput('return !todo.completed'));
// > false
```
* @example isOutput():Strings Template "`${name}`"
```js
console.log(jhtmls.isOutput('`${name}`'));
// > false
```
* @example isOutput():Strings Template "\`\`\`js"
```js
console.log(jhtmls.isOutput('\`\`\`js'));
// > true
```
* @example isOutput():Url "http://jhtmls.com/"
```js
console.log(jhtmls.isOutput('http://jhtmls.com/'));
// > true
```
'''</example>'''
*/
function jhtmls_isOutput(line) {
Expand All @@ -129,13 +154,18 @@

// 非 JavaScript 字符开头
// 示例:#、<div>、汉字
if (/^[ \w\t_$]*([^&\^?|\n\w\/'"{}\[\]+\-():;, \t=\.$_]|:\/\/).*$/.test(line)) {
if (/^[ \w\t_$]*([^&\^?|\n\w\/'"{}\[\]+\-():;,!` \t=\.$_]|:\/\/).*$/.test(line)) {
return true;
}

// ```
if (/^\s*[`\-+'"]{3,}/.test(line)) {
return true;
}

// 不是 else 等单行语句
// 示例:hello world
if (/^(?!\s*(else|do|try|finally|void|typeof\s[\w$_]*)\s*$)[^'":;{}()\[\],\n|=&\/^?]+$/.test(line)) {
if (/^(?!\s*(else|do|try|finally|void|typeof\s[\w$_]*)\s*$)[^'"`!:;{}()\[\],\n|=&\/^?]+$/.test(line)) {
return true;
}
return false;
Expand Down
25 changes: 25 additions & 0 deletions test/jhtmls.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,31 @@ describe("src/jhtmls.js", function () {
examplejs_print(jhtmls.isOutput('hello'));
assert.equal(examplejs_printLines.join("\n"), "true"); examplejs_printLines = [];
});
it("isOutput():No semicolon \"foo()\"", function() {
examplejs_printLines = [];
examplejs_print(jhtmls.isOutput('foo()'));
assert.equal(examplejs_printLines.join("\n"), "false"); examplejs_printLines = [];
});
it("isOutput():Not symbol \"return !todo.completed\"", function() {
examplejs_printLines = [];
examplejs_print(jhtmls.isOutput('return !todo.completed'));
assert.equal(examplejs_printLines.join("\n"), "false"); examplejs_printLines = [];
});
it("isOutput():Strings Template \"`${name}`\"", function() {
examplejs_printLines = [];
examplejs_print(jhtmls.isOutput('`${name}`'));
assert.equal(examplejs_printLines.join("\n"), "false"); examplejs_printLines = [];
});
it("isOutput():Strings Template \"\\`\\`\\`js\"", function() {
examplejs_printLines = [];
examplejs_print(jhtmls.isOutput('\`\`\`js'));
assert.equal(examplejs_printLines.join("\n"), "true"); examplejs_printLines = [];
});
it("isOutput():Url \"http://jhtmls.com/\"", function() {
examplejs_printLines = [];
examplejs_print(jhtmls.isOutput('http://jhtmls.com/'));
assert.equal(examplejs_printLines.join("\n"), "true"); examplejs_printLines = [];
});
it("build():base", function() {
examplejs_printLines = [];
examplejs_print(typeof jhtmls.build('print: #{name}'));
Expand Down

0 comments on commit 7fc2f4a

Please sign in to comment.