From aee772288285d33f80f791b71e1c35ca52ec65c7 Mon Sep 17 00:00:00 2001 From: c4605 Date: Sat, 14 Mar 2015 22:11:01 +0800 Subject: [PATCH] First Commit --- .editorconfig | 18 ++++++++++++ .gitattributes | 1 + .gitignore | 1 + .jshintrc | 28 ++++++++++++++++++ .travis.yml | 7 +++++ LICENSE | 21 ++++++++++++++ README.md | 36 ++++++++++++++++++++++++ index.js | 53 +++++++++++++++++++++++++++++++++++ package.json | 37 ++++++++++++++++++++++++ tests/fixtures/basic.html | 13 +++++++++ tests/fixtures/noangular.html | 13 +++++++++ tests/test.js | 46 ++++++++++++++++++++++++++++++ 12 files changed, 274 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .jshintrc create mode 100644 .travis.yml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json create mode 100644 tests/fixtures/basic.html create mode 100644 tests/fixtures/noangular.html create mode 100644 tests/test.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..606d2c6 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..176a458 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +* text=auto diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..72982fc --- /dev/null +++ b/.jshintrc @@ -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 + } +} diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..479bfb1 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: node_js +node_js: + - '0.10' +notifications: + email: + on_success: never + on_failure: always diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..c554779 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Gilad Peleg (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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..ac7f9a7 --- /dev/null +++ b/README.md @@ -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) diff --git a/index.js b/index.js new file mode 100644 index 0000000..41ddf78 --- /dev/null +++ b/index.js @@ -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() + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..b12c0c8 --- /dev/null +++ b/package.json @@ -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 " + ], + "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": "*" + } +} diff --git a/tests/fixtures/basic.html b/tests/fixtures/basic.html new file mode 100644 index 0000000..012a0ea --- /dev/null +++ b/tests/fixtures/basic.html @@ -0,0 +1,13 @@ + + + + + + fixture + + + +

{{test}}

+ + + diff --git a/tests/fixtures/noangular.html b/tests/fixtures/noangular.html new file mode 100644 index 0000000..c21763d --- /dev/null +++ b/tests/fixtures/noangular.html @@ -0,0 +1,13 @@ + + + + + + fixture + + + + + + + diff --git a/tests/test.js b/tests/test.js new file mode 100644 index 0000000..f45dc45 --- /dev/null +++ b/tests/test.js @@ -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() +})