parse-simple-svg-meta-pmb
v0.1.0
Published
Given the source of a simple SVG image, find basic info about its outermost SVG tag.
Downloads
17
Readme
parse-simple-svg-meta-pmb
Given the source of a simple SVG image, find basic info about its outermost SVG tag.
Not an XML parser
This package is not supposed to be an XML parser. It can understand only a very simple subset of SVG. The intent is to limit complexity low enough to allow using very defensive RegExps for parsing. The downside of this simplicity is that any fancy shenanigans in the SVG code will cause the parser to just give up and fail.
Examples of what's NOT supported:
- ⛔ Namespaces on tags
- ⛔ Anything about hierarchy or closing tags
- ⛔ Reliable detection of malformed tags.
- ⛔ Characters outside the Basic Latin block.
- ⛔ Most control characters in the Basic Latin block.
- ⛔ Reliably preserving whitespace in attribute values.
- ⛔ Character entities (e.g.
"
or'
) - ⛔ Length units other than
px
and user units. - ⛔ User units other than pixels.
- ⛔ Lengths less than or equal to zero.
API
This module exports one function:
findSvgMeta(svg)
svg
is the source code of your image as a string.
Returns a dictionary object with at least these properties:
widthPx
,widthFrac
,heightPx
,heightFrac
: Dimensions of the image.- If a dimension is given as absolute length, its
…Px
value will be set accordingly, and its…Frac
value will be0
. - If a dimension is given as percentage, its
…Frac
value will be set to the fraction represented by the percentage, e.g."42%"
→0.42
. - Missing/empty width/height on the outermost SVG tag are defined in the
SVG spec to mean 100% of the respective viewport dimension,
so their
…Frac
will be1
and their…Px
will be0
.
- If a dimension is given as absolute length, its
Usage
see the tests.
Known issues
- Needs more/better tests and docs.
License
ISC