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

https://github.com/lautr3k/lw.svg-parser/issues/4 #5

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 5 additions & 3 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Parser {
this.editor = null // Editor info { name, version, fingerprint }
this.document = null // Document info { width, height, viewBox }
this.defs = null // Defined <defs> (DOM) nodes list by id
this.symbols = null // Defined <symbol> (DOM) node with mandatory id
this.tags = null // Tag objects hierarchy

// Trace settings (Arc, Bezier)
Expand All @@ -26,7 +27,7 @@ class Parser {

// Supported tags by this lib
this.supportedTags = [
'svg', 'g', 'defs', 'use',
'svg', 'g', 'defs', 'symbol', 'use',
'line', 'polyline', 'polygon',
'rect', 'circle', 'ellipse', 'path',
'title', 'desc', 'image', 'text'
Expand Down Expand Up @@ -192,6 +193,7 @@ class Parser {
this.document = null
this.defs = {}
this.tags = null
this.symbols = {}

// Load input if provided
if (input) {
Expand Down Expand Up @@ -239,7 +241,7 @@ class Parser {
// Create base tag object
let tag = new Tag(element, parent)

// Exluded tag ?
// Excluded tag ?
if (this.skipTags.indexOf(tag.name) !== -1) {
return null // silent
}
Expand Down Expand Up @@ -270,7 +272,7 @@ class Parser {
})

// Empty group
if (['svg', 'g'].indexOf(tag.name) !== -1 && ! tag.children.length) {
if (['svg', 'g', 'defs', 'symbol', 'use'].indexOf(tag.name) !== -1 && ! tag.children.length) {
return this._skipTag(tag, 'empty')
}

Expand Down
40 changes: 30 additions & 10 deletions src/tagparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,29 +555,49 @@ class TagParser {
return false
}

_symbol() {
if (this.tag.element.id){
let collection=[];
this.tag.element.childNodes.forEach((node)=>{collection.push(node)})
this.parser.symbols[this.tag.element.id] = collection;
}

// Skipped tag
return false
}

_use() {
// Get the target id
let target = this.tag.getAttr('xlink:href').replace(/^#/, '')

// Try to get the defined element
let element = this.parser.defs[target]
let element = this.parser.defs[target] || this.parser.symbols[target];

if (! element) {
return this.parser._skipTag(this.tag, 'undefined reference [' + target + ']')
}

// Parse the defined element and set new parent from <use> tag parent. Supports array of nodes
let useElement=(elements) => {

// Parse the defined element and set new parent from <use> tag parent
let useTag = this.parser._parseElement(element, this.tag.parent)
if (!Array.isArray(elements)) { elements = [elements] }

if (! useTag) {
return this.parser._skipTag(this.tag, 'empty reference [' + target + ']')
}
elements.forEach((element)=>{
let useTag = this.parser._parseElement(element, this.tag.parent)

if (! useTag) {
return this.parser._skipTag(this.tag, 'empty reference [' + target + ']')
}

// Set matrix from real parent (<use>)
useTag.setMatrix(this.tag.matrix)
// Set matrix from real parent (<use>)
useTag.setMatrix(this.tag.matrix)

// Replace the use tag with new one
this.tag.parent.addChild(useTag)
})
}

// Replace the use tag with new one
this.tag.parent.addChild(useTag)
useElement(element);

// Skipped tag
return false
Expand Down