Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent mutation if fragment param when appending #8

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
*\.sh
/node_modules
/npm-debug.log
.nyc_output
coverage
8 changes: 8 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"all": true,
"cache": false,
"reporter": [
"lcov",
"text"
]
}
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
language: node_js
node_js:
- "4"
- "5"
- "6"
- "8"

notifications:
email: false
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# jsonml.js

[![](https://img.shields.io/travis/benjycui/jsonml.js.svg?style=flat-square)](https://travis-ci.org/benjycui/jsonml.js)
[![npm package](https://img.shields.io/npm/v/jsonml.js.svg?style=flat-square)](https://www.npmjs.org/package/jsonml.js)
[![NPM downloads](http://img.shields.io/npm/dm/jsonml.js.svg?style=flat-square)](https://npmjs.org/package/jsonml.js)
[![Dependency Status](https://david-dm.org/benjycui/jsonml.js.svg?style=flat-square)](https://david-dm.org/benjycui/jsonml.js)
[![Build Status](https://travis-ci.org/CondeNast/jsonml.js.svg?branch=master)](https://travis-ci.org/CondeNast/jsonml.js)

JsonML-related tools.

## Forked Library

From from [benjycui/jsonml.js](https://github.com/benjycui/jsonml.js) since it is used by many CondeNast applications but not actively supported. Fork created by [pgoldrbx](https://github.com/pgoldrbx).

## Usage

```bash
Expand Down
10 changes: 6 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const isRaw = require('./html').isRaw;

const isArray = Array.isArray || function isArray(val) {
return val instanceof Array;
};
Expand Down Expand Up @@ -89,11 +91,11 @@ exports.appendChild = function appendChild(parent, child) {

if (isArray(child) && child[0] === '') {
// result was multiple JsonML sub-trees (i.e. documentFragment)
child.shift();// remove fragment ident
const fragments = child.slice(1);

// directly append children
while (child.length) {
appendChild(parent, child.shift(), arguments[2]);
while (fragments.length) {
appendChild(parent, fragments.shift(), arguments[2]);
}

} else if (child && 'object' === typeof child) {
Expand All @@ -110,7 +112,7 @@ exports.appendChild = function appendChild(parent, child) {
// result was a JsonML node
parent.push(child);

} else if (exports.isRaw(child)) {
} else if (isRaw(child)) {

// result was a JsonML node
parent.push(child);
Expand Down
19 changes: 16 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "jsonml.js",
"name": "@condenast/jsonml.js",
"version": "0.1.1",
"description": "JsonML-related tools.",
"main": "index.js",
Expand All @@ -8,7 +8,7 @@
"eslint-fix": "eslint --fix ./lib/utils.js ./test",
"babel": "babel src --out-dir lib",
"pub": "npm run babel && npm publish && rm -rf lib",
"test": "mocha"
"test": "nyc mocha"
},
"babel": {
"presets": [
Expand All @@ -21,13 +21,25 @@
"xml",
"html"
],
"repository": {
"type": "git",
"url": "https://github.com/CondeNast/jsonml.js"
},
"bugs": {
"url": "https://github.com/CondeNast/jsonml.js/issues"
},
"homepage": "https://github.com/CondeNast/jsonml.js",
"contributors": [
{
"name": "Stephen M. McKamey"
},
{
"name": "Benjy Cui",
"email": "[email protected]"
},
{
"name": "Phil Gold",
"email": "[email protected]"
}
],
"license": "MIT",
Expand All @@ -40,6 +52,7 @@
"babel-preset-es2015": "^6.6.0",
"eslint": "^2.10.2",
"eslint-config-egg": "^2.0.0",
"mocha": "^2.4.5"
"mocha": "^2.4.5",
"nyc": "^11.6.0"
}
}
19 changes: 10 additions & 9 deletions test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const assert = require('assert');
const html = require('../lib/html');
const utils = require('../lib/utils');

describe('utils', function() {
Expand Down Expand Up @@ -261,15 +262,17 @@ describe('utils', function() {
assert.deepEqual(jml, ['div', ['em', 'hello'], ['strong', 'world']]);
});

// @TODO - is this an intentional mutation?
it('should remove fragment contents', function() {
it('should not mutate child fragment param', function() {
const jml = ['div'];
const fragment = ['',
['em', 'hello'],
['strong', 'world'],
];
utils.appendChild(jml, fragment);
assert.deepEqual(fragment, []);
assert.deepEqual(fragment, ['',
['em', 'hello'],
['strong', 'world'],
]);
});
});

Expand Down Expand Up @@ -311,18 +314,16 @@ describe('utils', function() {
});

describe('when appending a raw child', function() {
// @TODO - this will currently throw an error due to missing function `isRaw`
xit('should append to the parent', function() {
it('should append to the parent', function() {
const jml = ['div'];
const child = new function RawChild() {};
const child = html.raw('string value');
utils.appendChild(jml, child);
assert.strictEqual(jml, ['div', child]);
assert.deepEqual(jml, ['div', child]);
});
});

describe('when appending attributes', function() {
// @TODO - this will currently throw an error due to missing function `isRaw`
xit('should add attributes to the parent element', function() {
it('should add attributes to the parent element', function() {
const jml = ['div'];
const attrs = { a: 1 };
utils.appendChild(jml, attrs);
Expand Down