Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-han-css

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-han-css - npm Package Compare versions

Comparing version 0.2.0 to 0.3.0

83

lib/Han.js

@@ -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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc