First Commit

This commit is contained in:
c4605
2015-03-14 22:11:01 +08:00
committed by bolasblack
commit aee7722882
12 changed files with 274 additions and 0 deletions

18
.editorconfig Normal file
View File

@@ -0,0 +1,18 @@
# EditorConfig is awesome: http://EditorConfig.org
# top-most EditorConfig file
root = true
[*]
indent_style = space
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.{js,json,html,jade,md}]
indent_size = 4
[*.js]
jslint_happy = true

1
.gitattributes vendored Normal file
View File

@@ -0,0 +1 @@
* text=auto

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
node_modules/

28
.jshintrc Normal file
View File

@@ -0,0 +1,28 @@
{
"node": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
"latedef": true,
"newcap": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true,
"globals": {
"describe": false,
"it": false,
"before": false,
"beforeEach": false,
"after": false,
"afterEach": false
}
}

7
.travis.yml Normal file
View File

@@ -0,0 +1,7 @@
language: node_js
node_js:
- '0.10'
notifications:
email:
on_success: never
on_failure: always

21
LICENSE Normal file
View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Gilad Peleg <giladp007@gmail.com> (http://giladpeleg.com)
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.

36
README.md Normal file
View File

@@ -0,0 +1,36 @@
# [gulp](https://github.com/gulpjs/gulp)-angular-cloak
> Add `ng-cloak` automatically
## Install
Install with [npm](https://npmjs.org/package/gulp-angular-cloak)
```sh
npm install --save gulp-angular-cloak
```
## Usage
```js
var gulp = require('gulp');
var ngCloak = require('gulp-angular-cloak');
//simple usage
gulp.task('htmlify', function() {
gulp.src('public/**/*.html')
.pipe(ngCloak())
.pipe(gulp.dest('build/'));
});
//using jade as a pre-processer
gulp.task('htmlify', function() {
gulp.src('partials/**/*.jade')
.pipe(jade())
.pipe(ngCloak())
.pipe(gulp.dest('build/'));
});
```
## License
MIT @[c4605](https://github.com/bolasblack)

53
index.js Normal file
View File

@@ -0,0 +1,53 @@
'use strict'
var gutil = require('gulp-util')
var cheerio = require('cheerio')
var through = require('through2')
var pluginName = 'gulp-angular-cloak'
var pluginError = gutil.PluginError
module.exports = function(opts) {
return through.obj(function(file, enc, cb) {
if (file.isNull()) {
cb(null, file)
return
}
if (file.isStream()) {
cb(new pluginError(pluginName, 'Streaming not supported'))
return
}
var content = file.contents.toString('utf8')
if (needCloak(content)) {
file.contents = new Buffer(replace(content))
}
return cb(null, file)
})
function needCloak(content) {
return /{{/.test(content)
}
function replace(content) {
var $ = cheerio.load(content, {
xmlMode: false,
decodeEntities: false,
normalizeWhitespace: false,
recognizeSelfClosing: true
})
$('*').filter(function(index, elem) {
return elem.children.some(function(child) {
return child.type === 'text'
})
}).filter(function(index, elem) {
return needCloak($(elem).text())
}).each(function(index, elem) {
$(elem).attr('data-ng-cloak', 'data-ng-cloak')
});
return $.html()
}
}

37
package.json Normal file
View File

@@ -0,0 +1,37 @@
{
"name": "gulp-angular-cloak",
"version": "0.0.1",
"description": "Add ng-cloak automatically",
"repository": "bolasblack/gulp-angular-cloak",
"license": "MIT",
"author": [
"c4605 <bolasblack@gmail.com>"
],
"main": "index.js",
"files": [
"index.js"
],
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"watchTest": "mocha -R spec -w ./tests/*.js",
"test": "mocha -R spec ./tests/*.js"
},
"keywords": [
"gulpplugin",
"gulp",
"angular",
"ng-cloak",
"ng"
],
"dependencies": {
"cheerio": "^0.18.0",
"gulp-util": "^3.0.1",
"through2": "^0.6.3"
},
"devDependencies": {
"expect.js": "^0.3.1",
"mocha": "*"
}
}

13
tests/fixtures/basic.html vendored Normal file
View File

@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<meta charset="utf-8">
<title>fixture</title>
</head>
<body>
<p>{{test}}</p>
</body>
</html>

13
tests/fixtures/noangular.html vendored Normal file
View File

@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>fixture</title>
</head>
<body>
</body>
</html>

46
tests/test.js Normal file
View File

@@ -0,0 +1,46 @@
'use strict'
var assert = require('assert')
var fs = require('fs')
var gutil = require('gulp-util')
var expect = require('expect.js')
var transformer = require('../index')
it('should handle a no angular file', function (cb) {
var stream = transformer()
var filename = './tests/fixtures/noangular.html'
var testFile = fs.readFileSync(filename)
stream.on('data', function (file) {
expect(file.contents.toString()).to.equal(testFile.toString())
})
stream.on('end', cb)
stream.write(new gutil.File({
contents: new Buffer(testFile.toString())
}))
stream.end()
})
it('should handle a basic angular app', function (cb) {
var stream = transformer()
var filename = './tests/fixtures/basic.html'
var testFile = fs.readFileSync(filename)
stream.on('data', function (file) {
var contents = file.contents.toString('utf8')
expect(file.contents.toString()).to.not.equal(testFile.toString())
expect(/\s+data-ng-cloak/.test(contents)).to.be.ok()
});
stream.on('end', cb)
stream.write(new gutil.File({
contents: new Buffer(testFile.toString())
}))
stream.end()
})