Launch Week Day 2: Introducing Reports: An Extensible Reporting Framework for Socket Data.Learn More
Socket
Book a DemoSign in
Socket

hi-xml2html

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hi-xml2html - npm Package Compare versions

Comparing version
1.1.0
to
1.2.0
+15
build/tags/base.d.ts
import { IBaseTag, IState } from "../types";
import { Tag } from "sax";
declare class BaseTag implements IBaseTag {
protected data: Tag;
state: IState;
protected className: string;
protected classNames: Set<string>;
protected tagName: string;
constructor(data: Tag, state: IState);
protected classNamesToString(): string;
protected getAttributes(): string;
protected openAfter(): string;
protected closeBefore(): string;
}
export default BaseTag;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class BaseTag {
constructor(data, state) {
this.data = data;
this.state = state;
this.classNames = new Set();
this.tagName = 'div';
}
classNamesToString() {
const className = (this.className == null) ?
this.data.name.replace(':', '').toLowerCase() :
this.className;
if (className.length)
this.classNames.add(className);
return (this.classNames.size) ?
` class="${[...this.classNames].join(' ')}"` :
'';
}
getAttributes() {
const attrs = this.data.attributes;
const keys = Object.keys(attrs);
return keys
.map((key) => {
const value = attrs[key];
key = key.replace(':', '-');
return ` data-${key}="${value}"`;
})
.join('');
}
openAfter() {
return '';
}
closeBefore() {
return '';
}
}
exports.default = BaseTag;
import BaseTag from "./base";
import { ICustomTag } from "../types";
declare class EmptyTag extends BaseTag implements ICustomTag {
open(): string;
close(): string;
}
export default EmptyTag;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const base_1 = require("./base");
class EmptyTag extends base_1.default {
open() {
return ' ';
}
close() {
return ' ';
}
}
exports.default = EmptyTag;
import BaseTag from "./base";
import { ICustomTag } from "../types";
declare class HtmlTag extends BaseTag implements ICustomTag {
open(): string;
close(): string;
}
export default HtmlTag;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const base_1 = require("./base");
class HtmlTag extends base_1.default {
open() {
return `<${this.tagName}${this.classNamesToString()}${this.getAttributes()}>${this.openAfter()}`;
}
close() {
return `${this.closeBefore()}</${this.tagName}>`;
}
}
exports.default = HtmlTag;
import BaseTag from "./base";
import { ICustomTag } from "../types";
declare class JsxTag extends BaseTag implements ICustomTag {
constructor(data: any, state: any);
open(): string;
close(): string;
}
export default JsxTag;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const utils_1 = require("../utils");
const base_1 = require("./base");
class JsxTag extends base_1.default {
constructor(data, state) {
super(data, state);
this.tagName = utils_1.formatTagName(this.data.name);
if (state.writeToOutput)
state.usedTags.add(this.tagName);
}
open() {
const slash = this.data.isSelfClosing ? '/' : '';
return `<${this.tagName}${this.getAttributes()}${slash}>${this.openAfter()}`;
}
close() {
return this.data.isSelfClosing ?
'' :
`${this.closeBefore()}</${this.tagName}>`;
}
}
exports.default = JsxTag;
import {formatTagName} from "../utils";
import {IBaseTag, IState} from "../types";
import {Tag} from "sax";
class BaseTag implements IBaseTag {
protected className: string;
protected classNames: Set<string> = new Set();
protected tagName: string = 'div';
constructor(protected data: Tag, public state: IState) {}
protected classNamesToString() {
const className = (this.className == null) ?
this.data.name.replace(':', '').toLowerCase() :
this.className;
if (className.length) this.classNames.add(className);
return (this.classNames.size) ?
` class="${[...this.classNames].join(' ')}"` :
'';
}
protected getAttributes() {
const attrs = this.data.attributes;
const keys = Object.keys(attrs);
return keys
.map((key) => {
const value = attrs[key];
// Rename the key if necessary
key = key.replace(':', '-');
return ` data-${key}="${value}"`
})
.join('');
}
protected openAfter() {
return '';
}
protected closeBefore() {
return '';
}
}
export default BaseTag;
import BaseTag from "./base";
import {ICustomTag} from "../types";
class EmptyTag extends BaseTag implements ICustomTag {
public open() {
return ' ';
}
public close() {
return ' ';
}
}
export default EmptyTag;
import BaseTag from "./base";
import {ICustomTag} from "../types";
class HtmlTag extends BaseTag implements ICustomTag {
public open() {
return `<${this.tagName}${this.classNamesToString()}${this.getAttributes()}>${this.openAfter()}`;
}
public close() {
return `${this.closeBefore()}</${this.tagName}>`;
}
}
export default HtmlTag;
import {formatTagName} from "../utils";
import BaseTag from "./base";
import {ICustomTag} from "../types";
class JsxTag extends BaseTag implements ICustomTag {
constructor(data, state) {
super(data, state);
this.tagName = formatTagName(this.data.name);
if (state.writeToOutput) state.usedTags.add(this.tagName);
}
public open() {
const slash = this.data.isSelfClosing ? '/' : '';
return `<${this.tagName}${this.getAttributes()}${slash}>${this.openAfter()}`;
}
public close() {
return this.data.isSelfClosing ?
'' :
`${this.closeBefore()}</${this.tagName}>`;
}
}
export default JsxTag;

Sorry, the diff of this file is not supported yet

+6
-4

@@ -1,5 +0,7 @@

import { ISettings } from "./types";
import BaseTag from './base-tag';
export { BaseTag };
declare var _default: (xmlString: string, settings?: ISettings) => Promise<string>;
import { ISettings, IState } from "./types";
import HtmlTag from './tags/html';
import JsxTag from './tags/jsx';
import EmptyTag from './tags/empty';
export { EmptyTag, HtmlTag, JsxTag };
declare var _default: (xmlString: string, settings?: ISettings) => Promise<IState>;
export default _default;
+7
-3

@@ -8,4 +8,8 @@ "use strict";

const parse_text_1 = require("./parse-text");
const base_tag_1 = require("./base-tag");
exports.BaseTag = base_tag_1.default;
const html_1 = require("./tags/html");
exports.HtmlTag = html_1.default;
const jsx_1 = require("./tags/jsx");
exports.JsxTag = jsx_1.default;
const empty_1 = require("./tags/empty");
exports.EmptyTag = empty_1.default;
exports.default = (xmlString, settings = {}) => new Promise((resolve, reject) => {

@@ -18,4 +22,4 @@ const state = new state_1.default(settings);

parser.onerror = (e) => reject(e);
parser.onend = () => resolve(state.wrapOutput());
parser.onend = () => resolve(state);
parser.write(xmlString).close();
});
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const base_tag_1 = require("./base-tag");
exports.default = (state) => (node) => {

@@ -9,3 +8,3 @@ if (state.startFromTag === node.name)

state.tags[node.name] :
base_tag_1.default;
state.GenericTag;
const tag = new Tag(node, state);

@@ -12,0 +11,0 @@ const open = tag.open();

import OpenTags from './open-tags';
import PreviousNodes from './previous-nodes';
import { ISettings, IState } from "../types";
import { ISettings, IState, TagClasses } from "../types";
declare class State implements IState {
private componentsPath;
private output;
jsx: boolean;
output: string;
GenericTag: any;
openTags: OpenTags;
previousNodes: PreviousNodes;
startFromTag: any;
tagClass: TagClasses;
tags: any;

@@ -17,5 +18,3 @@ tagsToSkip: any;

appendHtml(str: any): void;
wrapOutput(): string;
private wrapJsx();
}
export default State;

@@ -5,14 +5,15 @@ "use strict";

const previous_nodes_1 = require("./previous-nodes");
const jsx_1 = require("../tags/jsx");
const html_1 = require("../tags/html");
const empty_1 = require("../tags/empty");
class State {
constructor(settings) {
this.output = '';
this.jsx = false;
this.openTags = new open_tags_1.default();
this.previousNodes = new previous_nodes_1.default();
this.tagClass = 'html';
this.usedTags = new Set();
this.writeToOutput = false;
let { componentsPath, jsx, startFromTag, tags, tagsToSkip } = settings;
let { componentsPath, startFromTag, tagClass, tags, tagsToSkip } = settings;
this.componentsPath = (componentsPath == null) ? 'components' : componentsPath;
if (jsx)
this.jsx = true;
this.startFromTag = startFromTag;

@@ -24,2 +25,7 @@ if (startFromTag == null)

this.appendHtml(false);
if (tagClass != null && tagClass.length)
this.tagClass = tagClass;
this.GenericTag = this.tagClass === 'html' ?
html_1.default :
this.tagClass === 'jsx' ? jsx_1.default : empty_1.default;
}

@@ -30,17 +36,3 @@ appendHtml(str) {

}
wrapOutput() {
return (this.jsx) ? this.wrapJsx() : this.output;
}
wrapJsx() {
const tags = [...this.usedTags].join(', ');
return (`import * as React from 'react';
import { ${tags} } from '${this.componentsPath}';
export default () => (
<div className="wrapper">
${this.output}
</div>
);`);
}
}
exports.default = State;
import { Tag } from "sax";
export interface IBaseTag {
state: IState;
}
export interface ICustomTag extends IBaseTag {
close(): string;
open(): string;
}
export declare type TagClasses = 'html' | 'jsx' | 'empty';
export interface IState {
jsx: boolean;
openTags: any;
previousNodes: any;
startFromTag: string;
tagClass: TagClasses;
tags: any;

@@ -17,3 +20,2 @@ tagsToSkip: string[];

appendHtml(str: string): void;
wrapOutput(): string;
[prop: string]: any;

@@ -23,4 +25,4 @@ }

componentsPath?: string;
jsx?: boolean;
startFromTag?: string;
tagClass?: TagClasses;
tags?: Object;

@@ -27,0 +29,0 @@ tagsToSkip?: any[];

@@ -0,1 +1,4 @@

### v1.2.0 (2017/4/3 16:42)
* Add EmptyTag
### v1.1.0 (2017/3/31 15:5)

@@ -2,0 +5,0 @@

{
"name": "hi-xml2html",
"version": "1.1.0",
"version": "1.2.0",
"description": "",

@@ -9,2 +9,3 @@ "main": "build/index.js",

"bump": "hi-bump",
"test": "node test/index.js",
"watch": "tsc -w"

@@ -11,0 +12,0 @@ },

@@ -6,9 +6,11 @@ import * as sax from "sax";

import parseText from './parse-text';
import {ISettings} from "./types";
import {ISettings, IState} from "./types";
import BaseTag from './base-tag';
export { BaseTag } ;
import HtmlTag from './tags/html';
import JsxTag from './tags/jsx';
import EmptyTag from './tags/empty';
export { EmptyTag, HtmlTag, JsxTag } ;
export default (xmlString: string, settings: ISettings = {}) =>
new Promise<string>((resolve, reject) => {
new Promise<IState>((resolve, reject) => {
const state = new State(settings);

@@ -20,4 +22,4 @@ const parser = sax.parser(true, {});

parser.onerror = (e) => reject(e);
parser.onend = () => resolve(state.wrapOutput());
parser.onend = () => resolve(state);
parser.write(xmlString).close();
});

@@ -1,2 +0,1 @@

import BaseTag from './base-tag';
import {Tag} from "sax";

@@ -6,5 +5,6 @@

if (state.startFromTag === node.name) state.writeToOutput = true;
const Tag = Object.keys(state.tags).indexOf(node.name) > -1 ?
state.tags[node.name] :
BaseTag;
state.GenericTag;
const tag = new Tag(node, state);

@@ -11,0 +11,0 @@ const open = tag.open();

import OpenTags from './open-tags';
import PreviousNodes from './previous-nodes';
import {ISettings, IState} from "../types";
import {ICustomTag, ISettings, IState, TagClasses} from "../types";
import JsxTag from "../tags/jsx";
import HtmlTag from "../tags/html";
import EmptyTag from "../tags/empty";
class State implements IState {
private componentsPath: string;
private output: string = '';
public output: string = '';
public jsx = false;
public GenericTag;
public openTags = new OpenTags();
public previousNodes = new PreviousNodes();
public startFromTag;
public tagClass: TagClasses = 'html';
public tags;

@@ -19,5 +23,4 @@ public tagsToSkip;

constructor(settings: ISettings) {
let { componentsPath, jsx, startFromTag, tags, tagsToSkip } = settings;
let { componentsPath, startFromTag, tagClass, tags, tagsToSkip } = settings;
this.componentsPath = (componentsPath == null) ? 'components' : componentsPath;
if (jsx) this.jsx = true;
this.startFromTag = startFromTag;

@@ -27,3 +30,7 @@ if (startFromTag == null) this.writeToOutput = true;

this.tagsToSkip = (tagsToSkip == null) ? [] : tagsToSkip;
this.appendHtml(false)
this.appendHtml(false);
if (tagClass != null && tagClass.length) this.tagClass = tagClass;
this.GenericTag = this.tagClass === 'html' ?
HtmlTag :
this.tagClass === 'jsx' ? JsxTag : EmptyTag;
}

@@ -34,24 +41,4 @@

}
public wrapOutput() {
return (this.jsx) ? this.wrapJsx() : this.output;
}
private wrapJsx(): string {
const tags = [...this.usedTags].join(', ');
return (
// Do not indent!
`import * as React from 'react';
import { ${tags} } from '${this.componentsPath}';
export default () => (
<div className="wrapper">
${this.output}
</div>
);`
);
}
}
export default State;

@@ -5,2 +5,5 @@ import {Tag} from "sax";

state: IState;
}
export interface ICustomTag extends IBaseTag {
close(): string;

@@ -10,8 +13,11 @@ open(): string;

export type TagClasses = 'html' | 'jsx' | 'empty';
export interface IState {
jsx: boolean;
openTags;
previousNodes;
startFromTag: string;
tagClass: TagClasses;
tags;
// ToDo make more flexibel: [{name: 'hi', rend: 'super'}]
tagsToSkip: string[];

@@ -21,3 +27,2 @@ usedTags: Set<string>;

appendHtml(str: string): void;
wrapOutput(): string;
[prop: string]: any;

@@ -30,5 +35,2 @@ }

// Output JSX instead of HTML.
jsx?: boolean;
// When the parser encouters this tag name, the parser starts writing

@@ -38,2 +40,3 @@ // to this.output. The tag name should be a unique tag (like <body>).

tagClass?: TagClasses;
// Maps a tag name (key) to a tag class (value). The tag class may extend

@@ -40,0 +43,0 @@ // BaseTag. If a tag is not in the map, BaseTag is used to generate output.

@@ -8,2 +8,3 @@ const fs = require('fs');

const jsxOutput = `${path}test.jsx`;
const emptyOutput = `${path}test.empty`;

@@ -13,14 +14,19 @@ const main = async () => {

const html = await xml2html(xmlString, {
const htmlState = await xml2html(xmlString, {
startFromTag: 'text',
});
fs.writeFileSync(htmlOutput, html, 'utf-8');
fs.writeFileSync(htmlOutput, htmlState.output, 'utf-8');
const jsx = await xml2html(xmlString, {
const jsxState = await xml2html(xmlString, {
componentsPath: 'client/components/entry',
jsx: true,
tagClass: 'jsx',
startFromTag: 'body',
});
fs.writeFileSync(jsxOutput, jsx, 'utf-8');
fs.writeFileSync(jsxOutput, jsxState.output, 'utf-8');
const emptyState = await xml2html(xmlString, {
tagClass: 'empty',
startFromTag: 'body',
});
fs.writeFileSync(emptyOutput, emptyState.output, 'utf-8');
};

@@ -27,0 +33,0 @@

@@ -1,8 +0,3 @@

import * as React from 'react';
import { Body, Div, Pb, Address, AddrLine, MdPostmark, Ab, P, Lb, Choice, Unclear, Hi, Del, C, Add, Retrace, Gap, Space, Supplied, Note } from 'client/components/entry';
<Body>
export default () => (
<div className="wrapper">
<Body>
<Div>

@@ -561,4 +556,2 @@ <Pb data-n="envelope-v" data-xml-id="env-v" data-facs="#zone-env-v"/>

</Div>
</Body>
</div>
);
</Body>
import {formatTagName} from "./utils";
import {IBaseTag, IState} from "./types";
import {Tag} from "sax";
class BaseTag implements IBaseTag {
private className: string;
private classNames: Set<string> = new Set();
private tagName: string = 'div';
public close;
public open;
constructor(private data: Tag, public state: IState) {
this.open = state.jsx ? this.openJSX : this.openHTML;
this.close = state.jsx ? this.closeJSX : this.closeHTML;
if (state.jsx) {
this.tagName = formatTagName(this.data.name);
}
if (state.writeToOutput) state.usedTags.add(this.tagName);
}
private classNamesToString() {
const className = (this.className == null) ?
this.data.name.replace(':', '').toLowerCase() :
this.className;
if (className.length) this.classNames.add(className);
return (this.classNames.size) ?
` class="${[...this.classNames].join(' ')}"` :
'';
}
private getAttributes() {
const attrs = this.data.attributes;
const keys = Object.keys(attrs);
return keys
.map((key) => {
const value = attrs[key];
// Rename the key if necessary
key = key.replace(':', '-');
return ` data-${key}="${value}"`
})
.join('');
}
private openHTML() {
return `<${this.tagName}${this.classNamesToString()}${this.getAttributes()}>${this.openAfter()}`;
}
private openJSX() {
const slash = this.data.isSelfClosing ? '/' : '';
return `<${this.tagName}${this.getAttributes()}${slash}>${this.openAfter()}`;
}
protected openAfter() {
return '';
}
protected closeBefore() {
return '';
}
private closeHTML() {
return `${this.closeBefore()}</${this.tagName}>`;
}
private closeJSX() {
return this.data.isSelfClosing ?
'' :
`${this.closeBefore()}</${this.tagName}>`;
}
}
export default BaseTag;