node-han-css
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -1,67 +0,38 @@ | ||
var EventEmitter = require('events'); | ||
var Promise = require('bluebird'); | ||
var joinPath = require('path').join; | ||
var jsdom = require('jsdom'); | ||
var read = require('fs').readFileSync; | ||
const { JSDOM } = require('jsdom'); | ||
const { readFileSync } = require('fs'); | ||
var html = '<meta charset="utf-8"><body><div id="root"></div></body>'; | ||
var han = read(joinPath(require.resolve('han-css'), '../dist/han.min.js'), 'utf-8'); | ||
const html = '<meta charset="utf-8"><body><div id="root"></div></body>'; | ||
const han = readFileSync(require.resolve('han-css/dist/han.min.js'), 'utf-8'); | ||
function Han() { | ||
this._ready = false; | ||
this._eventEmitter = new EventEmitter(); | ||
this._eventEmitter.setMaxListeners(100); | ||
function defaultRenderer(han) { | ||
han.render(); | ||
} | ||
Han.prototype.ready = function () { | ||
return arguments.length > 0 ? | ||
this.__ready.apply(this, arguments) : | ||
Promise.fromCallback(this.__ready.bind(this)); | ||
}; | ||
Han.prototype.__ready = function (callback) { | ||
if (this._ready) { | ||
callback.call(this, null, this); | ||
return; | ||
class Han { | ||
constructor() { | ||
const dom = this.dom = new JSDOM(html, { runScripts: 'outside-only' }); | ||
dom.window.eval(han); | ||
} | ||
if (!this._initializing) { | ||
this._initializing = true; | ||
jsdom.env(html, { src: han }, function (error, window) { | ||
if (error) { | ||
throw error; | ||
} | ||
this._ready = true; | ||
this._initializing = false; | ||
this._window = window; | ||
this._eventEmitter.emit('ready'); | ||
}.bind(this)); | ||
render(content = '', renderer = defaultRenderer) { | ||
const { window } = this.dom; | ||
const { document } = window; | ||
const { documentElement } = document; | ||
const root = document.getElementById('root'); | ||
root.innerHTML = '' + content; | ||
const han = window.Han(root, documentElement); | ||
renderer(han); | ||
return root.innerHTML; | ||
} | ||
this._eventEmitter.once('ready', callback.bind(this, null, this)); | ||
}; | ||
Han.prototype.render = function (content, callback) { | ||
if (!this._ready) { | ||
throw new Error('Han#render() should be called after it is ready'); | ||
ready(callback) { | ||
console.warn('Han#ready() is deprecated,. Call Han#render() directly.'); | ||
if (typeof callback === 'function') { | ||
callback.call(this, null, this); | ||
} else { | ||
return Promise.resolve(this); | ||
} | ||
} | ||
} | ||
if (typeof content === 'undefined') { | ||
return ''; | ||
} | ||
var window = this._window; | ||
var document = window.document; | ||
var documentElement = document.documentElement; | ||
var root = document.getElementById('root'); | ||
root.innerHTML = '' + content; | ||
var han = window.Han(root, documentElement); | ||
if (typeof callback === 'undefined') { | ||
han.render(); | ||
} else { | ||
callback(han); | ||
} | ||
return root.innerHTML; | ||
}; | ||
module.exports = Han; |
{ | ||
"name": "node-han-css", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "An unofficial library to use han-css in Node", | ||
@@ -22,12 +22,10 @@ "main": "lib/index.js", | ||
"devDependencies": { | ||
"async": "^2.0.1", | ||
"expect.js": "^0.3.1", | ||
"mocha": "^3.0.0" | ||
"mocha": "^7.0.1" | ||
}, | ||
"dependencies": { | ||
"bluebird": "^3.4.1", | ||
"canvas": "^1.4.0", | ||
"canvas": "^2.6.1", | ||
"han-css": "^3.3.0", | ||
"jsdom": "^9.4.1" | ||
"jsdom": "^16.2.0" | ||
} | ||
} |
@@ -6,90 +6,32 @@ # node-han-css | ||
[中文](README-zh-CN.md) | ||
An unofficial library to use hanzi (漢字標準格式) in Node. | ||
## Requirements | ||
一个非官方的漢字標準格式 Node 库。 | ||
Before installing this package, you should first install the dependencies of [canvas][node-canvas]. | ||
## Installation / 安装 | ||
## Installation | ||
$ npm install --save node-han-css | ||
## Usage | ||
## Usage / 使用 | ||
First of all, require `node-han-css`. | ||
```js | ||
var Han = require('node-han-css'); | ||
``` | ||
const Han = require('node-han-css'); | ||
Second, create a new instance. Under the hood, it creates a new han-css | ||
environment with jsdom. | ||
const han = new Han(); | ||
```js | ||
var han = new Han(); | ||
``` | ||
console.log(han.render('Hello,世界')); | ||
// Hello<h-char unicode="ff0c" class="biaodian cjk bd-end bd-cop bd-jiya bd-hangable"><h-inner>,</h-inner></h-char>世界 | ||
Finally, call `han.ready()` to provide a callback to be called once | ||
the environment is ready. | ||
Note that the context of the callback is set to the instance of `Han` | ||
(i.e. `han`). You can call `han.render()` once the environment is | ||
ready. | ||
```js | ||
han.ready(function (error, han) { | ||
console.log(this.render('Hello,世界')); | ||
// Hello<h-char unicode="ff0c" class="biaodian cjk bd-end bd-cop bd-jiya bd-hangable"><h-inner>,</h-inner></h-char>世界 | ||
console.log(this.render('<em>Hi</em>')); | ||
// <em><h-word class="western"><h-char class="alphabet latin">H</h-char><h-char class="alphabet latin">i</h-char></h-word></em> | ||
}); | ||
console.log(han.render('<em>Hi</em>')); | ||
// <em>Hi</em> | ||
``` | ||
If no parameters are passed, it returns a Promise. | ||
By default, it internally calls [`render`](https://hanzi.pro/manual/js-api#render). | ||
You may also specify a custom renderer by passing a function to the second parameter. | ||
```js | ||
han.ready() | ||
.then(function (han) { | ||
console.log(han.render('Hello,世界')); | ||
// Hello<h-char unicode="ff0c" class="biaodian cjk bd-end bd-cop bd-jiya bd-hangable"><h-inner>,</h-inner></h-char>世界 | ||
内部默认调用 [`render`](https://hanzi.pro/manual/js-api#render),也可通过第二个参数自定义渲染的方法。 | ||
console.log(han.render('<em>Hi</em>')); | ||
// <em><h-word class="western"><h-char class="alphabet latin">H</h-char><h-char class="alphabet latin">i</h-char></h-word></em> | ||
}); | ||
``` | ||
You may call `han.ready()` multiple times, even when the environment | ||
is ready. `node-han-css` shares a single jsdom environment in every | ||
`Han` instance to accelerate the process. | ||
```js | ||
han.ready(function () { | ||
this.render('床前明月光,疑是地上霜。'); | ||
}); | ||
// After 5 seconds, the callback will be called immediately | ||
// since the environment will have been ready | ||
setTimeout(function () { | ||
han.ready(function () { | ||
this.render('举头望明月,低头思故乡。'); | ||
}); | ||
}, 5000); | ||
``` | ||
Also, you may manually call JavaScript API of han-css by using the | ||
optional second parameter. | ||
```js | ||
this.render('PHP是世界上……', function (han) { | ||
han.renderHWS(); | ||
}); | ||
han.render('PHP是世界上……', han => han.renderHWS()); | ||
// PHP<h-hws hidden=""> </h-hws>是世界上…… | ||
``` | ||
See [JavaScript API][hanzi-js-api] for more details. | ||
[node-canvas]: https://github.com/Automattic/node-canvas#installation | ||
[hanzi-js-api]: https://css.hanzi.co/manual/js-api |
var expect = require('expect.js'); | ||
var map = require('async/map'); | ||
var Han = require('..'); | ||
describe('Han', function () { | ||
describe('#ready()', function () { | ||
it('should invoke the callback when it is ready', function (done) { | ||
describe('#render()', function () { | ||
var han = new Han(); | ||
it('should return rendered html', () => { | ||
expect(han.render()).to.be(''); | ||
expect(han.render(0)).to.be('0'); | ||
expect(han.render('')).to.be(''); | ||
expect(han.render('Hello,世界')).to.be('Hello<h-char unicode="ff0c" class="biaodian cjk bd-end bd-cop bd-jiya bd-hangable"><h-inner>,</h-inner></h-char>世界'); | ||
expect(han.render('Hello世界!', han => han.renderHWS())).to.be('Hello<h-hws hidden=""> </h-hws>世界!'); | ||
}); | ||
}); | ||
describe('#ready() (deprecated)', () => { | ||
it('should invoke the callback when it is ready', done => { | ||
this.timeout(10000); | ||
@@ -17,48 +28,8 @@ var han = new Han(); | ||
it('should support Promise', function (done) { | ||
it('should support Promise', async () => { | ||
this.timeout(10000); | ||
var han = new Han(); | ||
han.ready().then(function (innerHan) { | ||
expect(innerHan).to.be(han); | ||
done(); | ||
}); | ||
expect(await han.ready()).to.be(han); | ||
}); | ||
}); | ||
describe('#render()', function () { | ||
var han = new Han(); | ||
before(function (done) { | ||
han.ready(done); | ||
}); | ||
it('should return rendered html', function () { | ||
expect(han.render()).to.be(''); | ||
expect(han.render(0)).to.be('0'); | ||
expect(han.render('')).to.be(''); | ||
expect(han.render('Hello,世界')).to.be('Hello<h-char unicode="ff0c" class="biaodian cjk bd-end bd-cop bd-jiya bd-hangable"><h-inner>,</h-inner></h-char>世界'); | ||
expect(han.render('Hello世界!', function (han) { | ||
han.renderHWS(); | ||
})).to.be('Hello<h-hws hidden=""> </h-hws>世界!'); | ||
}); | ||
it('should work with multiply calls', function (done) { | ||
var testIn = ['Hello world', 'Hello,世界', '<em>Emph</em>']; | ||
var testOut = [ | ||
"Hello world", | ||
"Hello<h-char unicode=\"ff0c\" class=\"biaodian cjk bd-end bd-cop bd-jiya bd-hangable\"><h-inner>,</h-inner></h-char>世界", | ||
"<em><h-word class=\"western\"><h-char class=\"alphabet latin\">E</h-char><h-char class=\"alphabet latin\">m</h-char><h-char class=\"alphabet latin\">p</h-char><h-char class=\"alphabet latin\">h</h-char></h-word></em>" | ||
]; | ||
var anotherHan = new Han(); | ||
map(testIn, function (content, callback) { | ||
anotherHan.ready(function () { | ||
callback(null, this.render(content)); | ||
}); | ||
}, function (err, results) { | ||
expect(results).to.eql(testOut); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3
2
5330
8
63
37
+ Added@mapbox/node-pre-gyp@1.0.11(transitive)
+ Added@tootallnate/once@1.1.2(transitive)
+ Addedabab@2.0.6(transitive)
+ Addedabbrev@1.1.1(transitive)
+ Addedacorn@7.4.18.14.0(transitive)
+ Addedacorn-globals@6.0.0(transitive)
+ Addedacorn-walk@7.2.0(transitive)
+ Addedagent-base@6.0.2(transitive)
+ Addedansi-regex@5.0.1(transitive)
+ Addedaproba@2.0.0(transitive)
+ Addedare-we-there-yet@2.0.0(transitive)
+ Addedbalanced-match@1.0.2(transitive)
+ Addedbrace-expansion@1.1.11(transitive)
+ Addedbrowser-process-hrtime@1.0.0(transitive)
+ Addedcanvas@2.11.2(transitive)
+ Addedchownr@2.0.0(transitive)
+ Addedcolor-support@1.1.3(transitive)
+ Addedconcat-map@0.0.1(transitive)
+ Addedconsole-control-strings@1.1.0(transitive)
+ Addedcssom@0.4.4(transitive)
+ Addedcssstyle@2.3.0(transitive)
+ Addeddata-urls@2.0.0(transitive)
+ Addeddebug@4.3.7(transitive)
+ Addeddecimal.js@10.4.3(transitive)
+ Addeddecompress-response@4.2.1(transitive)
+ Addeddelegates@1.0.0(transitive)
+ Addeddetect-libc@2.0.3(transitive)
+ Addeddomexception@2.0.1(transitive)
+ Addedemoji-regex@8.0.0(transitive)
+ Addedescodegen@2.1.0(transitive)
+ Addedestraverse@5.3.0(transitive)
+ Addedform-data@3.0.2(transitive)
+ Addedfs-minipass@2.1.0(transitive)
+ Addedfs.realpath@1.0.0(transitive)
+ Addedgauge@3.0.2(transitive)
+ Addedglob@7.2.3(transitive)
+ Addedhas-unicode@2.0.1(transitive)
+ Addedhtml-encoding-sniffer@2.0.1(transitive)
+ Addedhttp-proxy-agent@4.0.1(transitive)
+ Addedhttps-proxy-agent@5.0.1(transitive)
+ Addedinflight@1.0.6(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedis-fullwidth-code-point@3.0.0(transitive)
+ Addedis-potential-custom-element-name@1.0.1(transitive)
+ Addedjsdom@16.7.0(transitive)
+ Addedlodash@4.17.21(transitive)
+ Addedmake-dir@3.1.0(transitive)
+ Addedmimic-response@2.1.0(transitive)
+ Addedminimatch@3.1.2(transitive)
+ Addedminipass@3.3.65.0.0(transitive)
+ Addedminizlib@2.1.2(transitive)
+ Addedmkdirp@1.0.4(transitive)
+ Addedms@2.1.3(transitive)
+ Addednode-fetch@2.7.0(transitive)
+ Addednopt@5.0.0(transitive)
+ Addednpmlog@5.0.1(transitive)
+ Addednwsapi@2.2.13(transitive)
+ Addedobject-assign@4.1.1(transitive)
+ Addedonce@1.4.0(transitive)
+ Addedparse5@6.0.1(transitive)
+ Addedpath-is-absolute@1.0.1(transitive)
+ Addedquerystringify@2.2.0(transitive)
+ Addedreadable-stream@3.6.2(transitive)
+ Addedrequires-port@1.0.0(transitive)
+ Addedrimraf@3.0.2(transitive)
+ Addedsaxes@5.0.1(transitive)
+ Addedsemver@6.3.17.6.3(transitive)
+ Addedset-blocking@2.0.0(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedsimple-concat@1.0.1(transitive)
+ Addedsimple-get@3.1.1(transitive)
+ Addedstring-width@4.2.3(transitive)
+ Addedstring_decoder@1.3.0(transitive)
+ Addedstrip-ansi@6.0.1(transitive)
+ Addedtar@6.2.1(transitive)
+ Addedtough-cookie@4.1.4(transitive)
+ Addedtr46@2.1.0(transitive)
+ Addeduniversalify@0.2.0(transitive)
+ Addedurl-parse@1.5.10(transitive)
+ Addedutil-deprecate@1.0.2(transitive)
+ Addedw3c-hr-time@1.0.2(transitive)
+ Addedw3c-xmlserializer@2.0.0(transitive)
+ Addedwebidl-conversions@5.0.06.1.0(transitive)
+ Addedwhatwg-mimetype@2.3.0(transitive)
+ Addedwhatwg-url@5.0.08.7.0(transitive)
+ Addedwide-align@1.1.5(transitive)
+ Addedwrappy@1.0.2(transitive)
+ Addedws@7.5.10(transitive)
+ Addedxml-name-validator@3.0.0(transitive)
+ Addedxmlchars@2.2.0(transitive)
+ Addedyallist@4.0.0(transitive)
- Removedbluebird@^3.4.1
- Removedabab@1.0.4(transitive)
- Removedacorn@4.0.13(transitive)
- Removedacorn-globals@3.1.0(transitive)
- Removedajv@6.12.6(transitive)
- Removedarray-equal@1.0.2(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedbluebird@3.7.2(transitive)
- Removedcanvas@1.6.13(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcontent-type-parser@1.0.2(transitive)
- Removedcore-util-is@1.0.2(transitive)
- Removedcssstyle@0.2.37(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddeep-is@0.1.4(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedescodegen@1.14.3(transitive)
- Removedestraverse@4.3.0(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedfast-levenshtein@2.0.6(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhtml-encoding-sniffer@1.0.2(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjsdom@9.12.0(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedlevn@0.3.0(transitive)
- Removednwmatcher@1.4.4(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedoptionator@0.8.3(transitive)
- Removedparse5@1.5.1(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedprelude-ls@1.1.2(transitive)
- Removedqs@6.5.3(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsax@1.4.1(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removedtype-check@0.3.2(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)
- Removedwebidl-conversions@4.0.2(transitive)
- Removedwhatwg-url@4.8.0(transitive)
- Removedword-wrap@1.2.5(transitive)
- Removedxml-name-validator@2.0.1(transitive)
Updatedcanvas@^2.6.1
Updatedjsdom@^16.2.0