-
Notifications
You must be signed in to change notification settings - Fork 857
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8549a96
commit 4e70b6f
Showing
7 changed files
with
219 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<pre class="docs-method-signature"><code>svg(strings)</code></pre> | ||
<p> | ||
This is the tagged template which converts a string into a <a href="#dia.Cell.markup">markup</a> object while declaring a <a href="#dia.Cell">Cell</a>. | ||
The resulting object contains fields which are described in <a href="#dia.Cell.markup.json">this part of the documentation.</a> | ||
SVG is converted into JSON markup as follows: | ||
<table> | ||
<tr> | ||
<th>Property</th> | ||
<th>Representation</th> | ||
</tr> | ||
<tr> | ||
<td>tagName</td> | ||
<td>SVG tag name.</td> | ||
</tr> | ||
<tr> | ||
<td>selector</td> | ||
<td><code>@selector</code> attribute.</td> | ||
</tr> | ||
<tr> | ||
<td>groupSelector</td> | ||
<td><code>@group-selector</code> attribute. Accepts comma-separated lists e.g. <code>@group-selector="group1, group2"</code>.</td> | ||
</tr> | ||
<tr> | ||
<td>namespaceURI</td> | ||
<td>The namespace URI of the element. It defaults to the SVG namespace <a target="_blank" href="https://www.w3.org/2000/svg"><code>"http://www.w3.org/2000/svg"</code></a>.</td> | ||
</tr> | ||
<tr> | ||
<td>attributes</td> | ||
<td>Attributes of the element.</td> | ||
</tr> | ||
<tr> | ||
<td>style</td> | ||
<td>The <code>style</code> attribute of the element parsed as key-value pairs.</td> | ||
</tr> | ||
<tr> | ||
<td>className</td> | ||
<td>The <code>class</code> attribute of the element.</td> | ||
</tr> | ||
<tr> | ||
<td>children</td> | ||
<td>The children of the element.</td> | ||
</tr> | ||
<tr> | ||
<td>textContent</td> | ||
<td>The text content of the element.</td> | ||
</tr> | ||
</table> | ||
</p> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
export function svg(strings) { | ||
const markup = parseFromSVGString(strings[0]); | ||
return markup; | ||
} | ||
|
||
function parseFromSVGString(str) { | ||
const parser = new DOMParser(); | ||
const markupString = `<svg>${str.trim()}</svg>`; | ||
const xmldocument = parser.parseFromString(markupString.replace(/@/g, ''), 'application/xml'); | ||
if (xmldocument.getElementsByTagName('parsererror')[0]) { | ||
throw new Error('Invalid SVG markup'); | ||
} | ||
const document = parser.parseFromString(markupString, 'text/html'); | ||
const svg = document.querySelector('svg'); | ||
return build(svg); | ||
} | ||
|
||
function build(root) { | ||
const markup = []; | ||
|
||
Array.from(root.children).forEach(node => { | ||
const markupNode = {}; | ||
const { tagName, attributes, textContent, namespaceURI, style } = node; | ||
|
||
markupNode.tagName = tagName; | ||
markupNode.namespaceURI = namespaceURI; | ||
|
||
const stylesObject = {}; | ||
for (var i = style.length; i--;) { | ||
var nameString = style[i]; | ||
stylesObject[nameString] = style.getPropertyValue(nameString); | ||
} | ||
markupNode.style = stylesObject; | ||
|
||
// selector fallbacks to tagName | ||
const selectorAttribute = attributes.getNamedItem('@selector'); | ||
if (selectorAttribute) { | ||
markupNode.selector = selectorAttribute.value; | ||
attributes.removeNamedItem('@selector'); | ||
} | ||
|
||
const groupSelectorAttribute = attributes.getNamedItem('@group-selector'); | ||
if (groupSelectorAttribute) { | ||
const groupSelectors = groupSelectorAttribute.value.split(','); | ||
markupNode.groupSelector = groupSelectors.map(s => s.trim()); | ||
|
||
attributes.removeNamedItem('@group-selector'); | ||
} | ||
|
||
const className = attributes.getNamedItem('class'); | ||
markupNode.className = (className ? className.value : null); | ||
|
||
if (textContent) { | ||
markupNode.textContent = textContent; | ||
} | ||
|
||
const nodeAttrs = {}; | ||
|
||
Array.from(attributes).forEach(nodeAttribute => { | ||
const { name, value } = nodeAttribute; | ||
nodeAttrs[name] = value; | ||
}); | ||
|
||
if (Object.keys(nodeAttrs).length > 0) { | ||
markupNode.attributes = nodeAttrs; | ||
} | ||
|
||
if (node.childElementCount > 0) { | ||
markupNode.children = build(node); | ||
} | ||
|
||
markup.push(markupNode); | ||
}); | ||
|
||
return markup; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters