Skip to content

Commit

Permalink
Auto closing tag
Browse files Browse the repository at this point in the history
  • Loading branch information
f2face committed Nov 6, 2023
1 parent 08abc6f commit b0718a2
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 26 deletions.
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@ const element2 = node.line(/* ... */);
const element3 = node.text(/* ... */);
svg.appendChild(element1, element2, element3);

// Add other element (advanced)
// Add other/custom element (advanced)
const gradient = RawElement.create(
'gradient', // Element/tag name
true, // Closing tag
{ id: 'myGradient' } /* ... */
'gradient', // Element or tag name
{ id: 'myGradient' }, // Attributes
[
/* Content or child element(s) inside this `gradient` element */
]
);
const defs = RawElement.create('defs', true).appendChild(gradient);
const defs = RawElement.create('defs').appendChild(gradient);
svg.appendChild(defs);

// Add styling
const style = Style.create({}, '#myGradient { /* ... */ }');
// Add style
const style = Style.create({}, '#myText { font-weight: bold; }');
svg.appendChild(style);

// Render SVG
const data = svg.render();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "svg-artisan",
"version": "0.2.2",
"version": "0.3.0",
"description": "Simple SVG builder for Node.",
"main": "./dist/index.js",
"files": [
Expand Down
4 changes: 1 addition & 3 deletions src/RawElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BaseElement } from './elements/BaseElement';
export class RawElement extends BaseElement {
constructor(
public readonly name: string,
public readonly hasClosingTag = true,
attributes?: ElementAttributes,
content?: ElementContent
) {
Expand All @@ -13,10 +12,9 @@ export class RawElement extends BaseElement {

public static create(
name: string,
hasClosingTag: boolean,
attributes?: ElementAttributes,
content?: ElementContent
) {
return new RawElement(name, hasClosingTag, attributes, content);
return new RawElement(name, attributes, content);
}
}
1 change: 0 additions & 1 deletion src/Svg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { BaseElement } from './elements/BaseElement';
*/
export class Svg extends BaseElement {
public readonly name = 'svg';
public readonly hasClosingTag = true;

/**
* Creates a new SVG root element.
Expand Down
5 changes: 2 additions & 3 deletions src/elements/BaseElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { SvgElement } from '../interfaces';

export abstract class BaseElement implements SvgElement {
public abstract readonly name: string;
public abstract readonly hasClosingTag: boolean;

protected attributesMap?: Map<string, AttributeValue>;
protected content: string;
Expand Down Expand Up @@ -93,15 +92,15 @@ export abstract class BaseElement implements SvgElement {
let attrs = '';
if (this.attributesMap) {
attrs = ` `;
attrs += [...this.attributesMap]
attrs += Array.from(this.attributesMap.entries())
.map(([key, value]) =>
value !== null ? `${key}="${value}"` : `${key}`
)
.join(' ');
}

let element = `<${this.name}${attrs}`;
if (this.hasClosingTag) {
if (this.content.length) {
element += '>';
element += this.content;
element += `</${this.name}>`;
Expand Down
1 change: 0 additions & 1 deletion src/elements/Style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { BaseElement } from './BaseElement';

export class Style extends BaseElement {
public readonly name = 'style';
public readonly hasClosingTag = true;

constructor(attributes?: ElementAttributes, content?: string) {
super(attributes, content);
Expand Down
1 change: 0 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export interface SvgElement {
name: string;
hasClosingTag: boolean;
render(): string;
}

Expand Down
18 changes: 9 additions & 9 deletions src/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,46 @@ import { RawElement } from './RawElement';
import { ElementAttributes, ElementContent } from './types';

export function a(attributes?: ElementAttributes, content?: ElementContent) {
return RawElement.create('a', true, attributes, content);
return RawElement.create('a', attributes, content);
}

export function g(attributes?: ElementAttributes, content?: ElementContent) {
return RawElement.create('g', true, attributes, content);
return RawElement.create('g', attributes, content);
}

export function circle(
attributes?: ElementAttributes,
content?: ElementContent
) {
return RawElement.create('circle', true, attributes, content);
return RawElement.create('circle', attributes, content);
}

export function text(attributes?: ElementAttributes, content?: ElementContent) {
return RawElement.create('text', true, attributes, content);
return RawElement.create('text', attributes, content);
}

export function tspan(
attributes?: ElementAttributes,
content?: ElementContent
) {
return RawElement.create('tspan', true, attributes, content);
return RawElement.create('tspan', attributes, content);
}

export function foreignObject(
attributes?: ElementAttributes,
content?: ElementContent
) {
return RawElement.create('foreignObject', true, attributes, content);
return RawElement.create('foreignObject', attributes, content);
}

export function line(attributes?: ElementAttributes, content?: ElementContent) {
return RawElement.create('line', true, attributes, content);
return RawElement.create('line', attributes, content);
}

export function rect(attributes?: ElementAttributes, content?: ElementContent) {
return RawElement.create('rect', true, attributes, content);
return RawElement.create('rect', attributes, content);
}

export function path(attributes?: ElementAttributes, content?: ElementContent) {
return RawElement.create('path', true, attributes, content);
return RawElement.create('path', attributes, content);
}

0 comments on commit b0718a2

Please sign in to comment.