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

Hast #3

Merged
merged 3 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 15 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function parse(source) {
while (i < source.length && source[i] !== '<') text += source[i++];

if (/\S/.test(text)) {
currentElement.children.push(text);
currentElement.children.push({ type: 'text', value: text });
}

if (source[i] === '<') {
Expand All @@ -68,11 +68,12 @@ export function parse(source) {

if (char === '/') return closingTag;

const name = getName();
const tagName = getName();

const element = {
name,
attributes: {},
type: 'element',
tagName,
properties: {},
children: []
};

Expand All @@ -84,7 +85,7 @@ export function parse(source) {

let attribute;
while (i < source.length && (attribute = getAttribute())) {
element.attributes[attribute.name] = attribute.value;
element.properties[attribute.name] = attribute.value;
}

let selfClosing = false;
Expand Down Expand Up @@ -125,12 +126,12 @@ export function parse(source) {
}

function closingTag() {
const name = getName();
const tagName = getName();

if (!name) error('Expected tag name');
if (!tagName) error('Expected tag name');

if (name !== currentElement.name) {
error(`Expected closing tag </${name}> to match opening tag <${currentElement.name}>`);
if (tagName !== currentElement.tagName) {
error(`Expected closing tag </${tagName}> to match opening tag <${currentElement.tagName}>`);
}

if (source[i] !== '>') {
Expand Down Expand Up @@ -226,6 +227,9 @@ export function parse(source) {
error('Unexpected end of input');
}

if (root.name === 'svg') root.metadata = header;
return root;
if (root.tagName === 'svg') root.metadata = header;
return {
type: 'root',
children: [root]
};
}
22 changes: 14 additions & 8 deletions test/samples/attribute-with-number/output.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
{
"name": "line",
"attributes": {
"x1": 0,
"y1": 0,
"x2": 10,
"y2": 0
},
"children": []
"type": "root",
"children": [
{
"type": "element",
"tagName": "line",
"properties": {
"x1": 0,
"y1": 0,
"x2": 10,
"y2": 0
},
"children": []
}
]
}
28 changes: 18 additions & 10 deletions test/samples/cdata/output.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
{
"name": "svg",
"metadata": "",
"attributes": {},
"type": "root",
"children": [
{
"name": "defs",
"attributes": {},
"type": "element",
"tagName": "svg",
"metadata": "",
"properties": {},
"children": [
{
"name": "style",
"attributes": {
"type": "text/css"
},
"type": "element",
"tagName": "defs",
"properties": {},
"children": [
"\n\t\t\trect {\n\t\t\t\tfill: red;\n\t\t\t\tstroke: blue;\n\t\t\t\tstroke-width: 3\n\t\t\t}\n\t\t\t"
{
"type": "element",
"tagName": "style",
"properties": {
"type": "text/css"
},
"children": [
"\n\t\t\trect {\n\t\t\t\tfill: red;\n\t\t\t\tstroke: blue;\n\t\t\t\tstroke-width: 3\n\t\t\t}\n\t\t\t"
]
}
]
}
]
Expand Down
25 changes: 16 additions & 9 deletions test/samples/children/output.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
{
"metadata": "",
"name": "svg",
"attributes": {
"xmlns": "http://www.w3.org/2000/svg"
},
"type": "root",
"children": [
{
"name": "path",
"attributes": {
"d": "M0,0 L10,0 L10,10 L0,10Z"
"type": "element",
"metadata": "",
"tagName": "svg",
"properties": {
"xmlns": "http://www.w3.org/2000/svg"
},
"children": []
"children": [
{
"type": "element",
"tagName": "path",
"properties": {
"d": "M0,0 L10,0 L10,10 L0,10Z"
},
"children": []
}
]
}
]
}
12 changes: 9 additions & 3 deletions test/samples/comment/output.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"name": "g",
"attributes": {},
"children": []
"type": "root",
"children": [
{
"type": "element",
"tagName": "g",
"properties": {},
"children": []
}
]
}
61 changes: 35 additions & 26 deletions test/samples/consecutive-paths/output.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,42 @@
{
"name": "svg",
"metadata": "",
"attributes": {
"viewBox": "0 0 50 10"
},
"type": "root",
"children": [
{
"name": "path",
"attributes": {
"d": "M0 0L10 0 10 10 0 10 Z",
"fill": "green"
"type": "element",
"tagName": "svg",
"metadata": "",
"properties": {
"viewBox": "0 0 50 10"
},
"children": []
},
{
"name": "path",
"attributes": {
"d": "M20 0L30 0 30 10 20 10 Z",
"fill": "blue"
},
"children": []
},
{
"name": "path",
"attributes": {
"d": "M40 0L50 0 50 10 40 10 Z",
"fill": "red"
},
"children": []
"children": [
{
"type": "element",
"tagName": "path",
"properties": {
"d": "M0 0L10 0 10 10 0 10 Z",
"fill": "green"
},
"children": []
},
{
"type": "element",
"tagName": "path",
"properties": {
"d": "M20 0L30 0 30 10 20 10 Z",
"fill": "blue"
},
"children": []
},
{
"type": "element",
"tagName": "path",
"properties": {
"d": "M40 0L50 0 50 10 40 10 Z",
"fill": "red"
},
"children": []
}
]
}
]
}
14 changes: 10 additions & 4 deletions test/samples/doctype/output.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"metadata": "<?xml version=\"1.0\" standalone=\"no\"?>\n<!-- Generator: Adobe Illustrator 18.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0 -->\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"name": "svg",
"attributes": {},
"children": []
"type": "root",
"children": [
{
"type": "element",
"metadata": "<?xml version=\"1.0\" standalone=\"no\"?>\n<!-- Generator: Adobe Illustrator 18.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0 -->\n<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n",
"tagName": "svg",
"properties": {},
"children": []
}
]
}
22 changes: 14 additions & 8 deletions test/samples/double-quoted-attributes/output.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
{
"name": "rect",
"attributes": {
"x": 0,
"y": 0,
"width": 100,
"height": 100
},
"children": []
"type": "root",
"children": [
{
"type": "element",
"tagName": "rect",
"properties": {
"x": 0,
"y": 0,
"width": 100,
"height": 100
},
"children": []
}
]
}
14 changes: 10 additions & 4 deletions test/samples/empty-svg/output.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
{
"metadata": "",
"name": "svg",
"attributes": {},
"children": []
"type": "root",
"children": [
{
"type": "element",
"tagName": "svg",
"metadata": "",
"properties": {},
"children": []
}
]
}
16 changes: 11 additions & 5 deletions test/samples/non-self-closing-element/output.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"name": "path",
"attributes": {
"d": "M0,0 L10,0 L10,10 L0,10Z"
},
"children": []
"type": "root",
"children": [
{
"type": "element",
"tagName": "path",
"properties": {
"d": "M0,0 L10,0 L10,10 L0,10Z"
},
"children": []
}
]
}
16 changes: 11 additions & 5 deletions test/samples/self-closing-element/output.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"name": "path",
"attributes": {
"d": "M0,0 L10,0 L10,10 L0,10Z"
},
"children": []
"type": "root",
"children": [
{
"type": "element",
"tagName": "path",
"properties": {
"d": "M0,0 L10,0 L10,10 L0,10Z"
},
"children": []
}
]
}
22 changes: 14 additions & 8 deletions test/samples/single-quoted-attributes/output.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
{
"name": "rect",
"attributes": {
"x": 0,
"y": 0,
"width": 100,
"height": 100
},
"children": []
"type": "root",
"children": [
{
"type": "element",
"tagName": "rect",
"properties": {
"x": 0,
"y": 0,
"width": 100,
"height": 100
},
"children": []
}
]
}