Global

Members

SVGReader

Source:

SVG parser for the Lasersaur. Converts SVG DOM to a flat collection of paths.

Copyright (c) 2011 Nortd Labs Open Source by the terms of the Gnu Public License (GPL3) or higher.

Code inspired by cake.js, canvg.js, svg2obj.py, and Squirtle. Thank you for open sourcing your work!

Usage: var boundarys = SVGReader.parse(svgstring, config)

Features:

width and height, viewBox clipping. paths, rectangles, ellipses, circles, lines, polylines and polygons nested transforms transform lists (transform="rotate(30) translate(2,2) scale(4)") non-pixel units (cm, mm, in, pt, pc) 'style' attribute and presentation attributes curves, arcs, cirles, ellipses tesellated according to tolerance

Intentinally not Supported: markers masking em, ex, % units text (needs to be converted to paths) raster images style sheets

ToDo: check for out of bounds geometry

Methods

addFormatter(formatter)

Source:

Adds a new formatter to the engine.

Parameters:
Name Type Description
formatter Object

The formatter to add.

addListener(type, listener) → {void}

Source:

Adds a listener for a given event type.

Parameters:
Name Type Description
type String

The type of event to add a listener for.

listener function

The function to call when the event occurs.

Returns:
Type
void

addRule(rule)

Source:

Adds a new rule to the engine.

Parameters:
Name Type Description
rule Object

The rule to add.

advance(tokenTypes, channel) → {void}

Source:

Keeps reading from the token stream until either one of the specified token types is found or until the end of the input is reached.

Parameters:
Name Type Description
tokenTypes int | Array.<int>

Either a single token type or an array of token types that the next token should be. If an array is passed, it's assumed that the token must be one of these.

channel variant

(Optional) The channel to read from. If not provided, reads from the default (unnamed) channel.

Returns:
Type
void

atRuleToken(first, startLine, startCol) → {Object}

Source:

Produces a token for any at-rule. If the at-rule is unknown, then the token is for a single "@" character.

Parameters:
Name Type Description
first String

The first character for the token.

startLine int

The beginning line for the character.

startCol int

The beginning column for the character.

Returns:

A token object.

Type
Object

(static) calculate(The) → {parserlib.css.Specificity}

Source:

Calculates the specificity of the given selector.

Parameters:
Name Type Description
The parserlib.css.Selector

selector to calculate specificity for.

Returns:

The specificity of the selector.

Type
parserlib.css.Specificity

charToken(c, startLine, startCol) → {Object}

Source:

Produces a character token based on the given character and location in the stream. If there's a special (non-standard) token name, this is used; otherwise CHAR is used.

Parameters:
Name Type Description
c String

The character for the token.

startLine int

The beginning line for the character.

startCol int

The beginning column for the character.

Returns:

A token object.

Type
Object

clearRules()

Source:

Clears all rule from the engine.

commentToken(first, startLine, startCol) → {Object}

Source:

Produces a character token based on the given character and location in the stream. If there's a special (non-standard) token name, this is used; otherwise CHAR is used.

Parameters:
Name Type Description
first String

The first character for the token.

startLine int

The beginning line for the character.

startCol int

The beginning column for the character.

Returns:

A token object.

Type
Object

compare(other) → {int}

Source:

Compare this specificity to another.

Parameters:
Name Type Description
other Specificity

The other specificity to compare to.

Returns:

-1 if the other specificity is larger, 1 if smaller, 0 if equal.

Type
int

comparisonToken(c, startLine, startCol) → {Object}

Source:

Produces a comparison token based on the given character and location in the stream. The next character must be read and is already known to be an equals sign.

Parameters:
Name Type Description
c String

The character for the token.

startLine int

The beginning line for the character.

startCol int

The beginning column for the character.

Returns:

A token object.

Type
Object

count() → {int}

Source:

Returns the total number of parts in the value.

Returns:

The total number of parts in the value.

Type
int

createToken(tt, value, startLine, startCol, options) → {Object}

Source:

Produces a token based on available data and the current reader position information. This method is called by other private methods to create tokens and is never called directly.

Parameters:
Name Type Description
tt int

The token type.

value String

The text value of the token.

startLine int

The beginning line for the character.

startCol int

The beginning column for the character.

options Object

(Optional) Specifies a channel property to indicate that a different channel should be scanned and/or a hide property indicating that the token should be hidden.

Returns:

A token object.

Type
Object

(static) createTokenData(tokens) → {Array}

Source:

Accepts an array of token information and outputs an array of token data containing key-value mappings and matching functions that the TokenStream needs.

Parameters:
Name Type Description
tokens Array

An array of token descriptors.

Returns:

An array of processed token data.

Type
Array

drop() → {void}

Source:

Drops the last saved bookmark.

Returns:
Type
void

endFormat() → {String}

Source:

Return content to be printed after all file results.

Returns:

to append after all results

Type
String

endFormat() → {String}

Source:

Return content to be printed after all file results.

Returns:

to append after all results

Type
String

endFormat() → {String}

Source:

Return closing root XML tag.

Returns:

to append after all results

Type
String

endFormat() → {String}

Source:

Return closing root XML tag.

Returns:

to append after all results

Type
String

endFormat() → {String}

Source:

Return content to be printed after all file results.

Returns:

to append after all results

Type
String

endFormat() → {String}

Source:

Return closing root XML tag.

Returns:

to append after all results

Type
String

endFormat() → {String}

Source:

Return closing root XML tag.

Returns:

to append after all results

Type
String

eof() → {Boolean}

Source:

Determines if you're at the end of the input.

Returns:

True if there's no more input, false otherwise.

Type
Boolean

error(message, line, col, rule)

Source:

Report an error.

Parameters:
Name Type Description
message String

The message to store.

line int

The line number.

col int

The column number.

rule Object

The rule this message relates to.

fire(event) → {void}

Source:

Fires an event based on the passed-in object.

Parameters:
Name Type Description
event Object | String

An object with at least a 'type' attribute or a string indicating the event name.

Returns:
Type
void

format(result, filename, formatId, options) → {String}

Source:

Formats the results in a particular format for a single file.

Parameters:
Name Type Description
result Object

The results returned from CSSLint.verify().

filename String

The filename for which the results apply.

formatId String

The name of the formatter to use.

options Object

(Optional) for special output handling.

Returns:

A formatted string for the results.

Type
String

formatResults(results, filename, options) → {String}

Source:

Given CSS Lint results for a file, return output for this format.

Parameters:
Name Type Description
results Object

with error and warning messages

filename String

relative file path

options Object

(Optional) specifies special handling of output

Returns:

output for results

Type
String

formatResults(results, filename, options) → {String}

Source:

Given CSS Lint results for a file, return output for this format.

Parameters:
Name Type Description
results Object

with error and warning messages

filename String

relative file path

options Object

(Optional) specifies special handling of output

Returns:

output for results

Type
String

formatResults(results, filename, options) → {String}

Source:

Given CSS Lint results for a file, return output for this format.

Parameters:
Name Type Description
results Object

with error and warning messages

filename String

relative file path

options Object

(UNUSED for now) specifies special handling of output

Returns:

output for results

Type
String

formatResults(results, filename) → {String}

Source:

Given CSS Lint results for a file, return output for this format.

Parameters:
Name Type Description
results Object

with error and warning messages

filename String

relative file path (Unused)

Returns:

output for results

Type
String

formatResults(results, filename, options) → {String}

Source:

Given CSS Lint results for a file, return output for this format.

Parameters:
Name Type Description
results Object

with error and warning messages

filename String

relative file path

options Object

(UNUSED for now) specifies special handling of output

Returns:

output for results

Type
String

formatResults(results, filename, options) → {String}

Source:

Given CSS Lint results for a file, return output for this format.

Parameters:
Name Type Description
results Object

with error and warning messages

filename String

relative file path

options Object

(UNUSED for now) specifies special handling of output

Returns:

output for results

Type
String

formatResults(results, filename, options) → {String}

Source:

Given CSS Lint results for a file, return output for this format.

Parameters:
Name Type Description
results Object

with error and warning messages

filename String

relative file path

options Object

(UNUSED for now) specifies special handling of output

Returns:

output for results

Type
String

(static) fromToken(token) → {parserlib.css.PropertyValuePart}

Source:

Create a new syntax unit based solely on the given token. Convenience method for creating a new syntax unit when it represents a single token instead of multiple.

Parameters:
Name Type Description
token Object

The token object to represent.

Returns:

The object representing the token.

Type
parserlib.css.PropertyValuePart

(static) fromToken(token) → {parserlib.util.SyntaxUnit}

Source:

Create a new syntax unit based solely on the given token. Convenience method for creating a new syntax unit when it represents a single token instead of multiple.

Parameters:
Name Type Description
token Object

The token object to represent.

Returns:

The object representing the token.

Type
parserlib.util.SyntaxUnit

get() → {int}

Source:

Consumes the next token from the token stream.

Returns:

The token type of the token that was just consumed.

Type
int

getCol() → {int}

Source:

Returns the column of the character to be read next.

Returns:

The column of the character to be read next.

Type
int

getEmbeddedRuleset(text, ruleset) → {Object}

Source:

Returns a ruleset object based on embedded rules.

Parameters:
Name Type Description
text String

A string of css containing embedded rules.

ruleset Object

A ruleset object to modify.

Returns:

A ruleset object.

Type
Object

getFormatter(formatId) → {Object}

Source:

Retrieves a formatter for use.

Parameters:
Name Type Description
formatId String

The name of the format to retrieve.

Returns:

The formatter or undefined.

Type
Object

getLine() → {int}

Source:

Returns the row of the character to be read next.

Returns:

The row of the character to be read next.

Type
int

getRules()

Source:

Returns the rule objects.

Returns:

An array of rule objects.

getRuleset()

Source:

Returns a ruleset configuration object with all current rules.

Returns:

A ruleset object.

hasFormat(formatId) → {Boolean}

Source:

Indicates if the given format is supported.

Parameters:
Name Type Description
formatId String

The ID of the format to check.

Returns:

True if the format exists, false if not.

Type
Boolean

hashToken(first, startLine, startCol) → {Object}

Source:

Produces a hash token based on the specified information. The first character provided is the pound sign (#) and then this method reads a name afterward.

Parameters:
Name Type Description
first String

The first character (#) in the hash name.

startLine int

The beginning line for the character.

startCol int

The beginning column for the character.

Returns:

A token object.

Type
Object

hasNext() → {Boolean}

Source:

Indicates if there are more parts of the property value.

Returns:

True if there are more parts, false if not.

Type
Boolean

htmlCommentEndToken(first, startLine, startCol) → {Object}

Source:

Produces a CDC or CHAR token based on the specified information. The first character is provided and the rest is read by the function to determine the correct token to create.

Parameters:
Name Type Description
first String

The first character in the token.

startLine int

The beginning line for the character.

startCol int

The beginning column for the character.

Returns:

A token object.

Type
Object

htmlCommentStartToken(first, startLine, startCol) → {Object}

Source:

Produces a CDO or CHAR token based on the specified information. The first character is provided and the rest is read by the function to determine the correct token to create.

Parameters:
Name Type Description
first String

The first character in the token.

startLine int

The beginning line for the character.

startCol int

The beginning column for the character.

Returns:

A token object.

Type
Object

identOrFunctionToken(first, startLine, startCol) → {Object}

Source:

Produces an IDENT or FUNCTION token based on the specified information. The first character is provided and the rest is read by the function to determine the correct token to create.

Parameters:
Name Type Description
first String

The first character in the identifier.

startLine int

The beginning line for the character.

startCol int

The beginning column for the character.

Returns:

A token object.

Type
Object

importantToken(first, startLine, startCol) → {Object}

Source:

Produces an IMPORTANT_SYM or CHAR token based on the specified information. The first character is provided and the rest is read by the function to determine the correct token to create.

Parameters:
Name Type Description
first String

The first character in the token.

startLine int

The beginning line for the character.

startCol int

The beginning column for the character.

Returns:

A token object.

Type
Object

info(message, line, col, rule)

Source:

Report some informational text.

Parameters:
Name Type Description
message String

The message to store.

line int

The line number.

col int

The column number.

rule Object

The rule this message relates to.

isAny()

Source:

Determines if the next part(s) of the given expression are any of the given types.

isAnyOfGroup()

Source:

Determines if the next part(s) of the given expression are one of a group.

isFirst() → {Boolean}

Source:

Indicates if the iterator is positioned at the first item.

Returns:

True if positioned at first item, false if not.

Type
Boolean

isType()

Source:

Determines if the next part(s) of the given expression are of a given type.

LA(The) → {Object}

Source:

Looks ahead a certain number of tokens and returns the token at that position. This will throw an error if you lookahead past the end of input, past the size of the lookahead buffer, or back past the first token in the lookahead buffer.

Parameters:
Name Type Description
The int

index of the token type to retrieve. 0 for the current token, 1 for the next, -1 for the previous, etc.

Returns:

The token of the token in the given position.

Type
Object

LA(The) → {int}

Source:

Looks ahead a certain number of tokens and returns the token type at that position. This will throw an error if you lookahead past the end of input, past the size of the lookahead buffer, or back past the first token in the lookahead buffer.

Parameters:
Name Type Description
The int

index of the token type to retrieve. 0 for the current token, 1 for the next, -1 for the previous, etc.

Returns:

The token type of the token in the given position.

Type
int

mark() → {void}

Source:

Marks the current spot in the iteration so it can be restored to later on.

Returns:
Type
void

mark() → {void}

Source:

Saves the current location so it can be returned to later.

Returns:
Type
void

match(tokenTypes, channel) → {Boolean}

Source:

Determines if the next token matches the given token type. If so, that token is consumed; if not, the token is placed back onto the token stream. You can pass in any number of token types and this will return true if any of the token types is found.

Parameters:
Name Type Description
tokenTypes int | Array.<int>

Either a single token type or an array of token types that the next token might be. If an array is passed, it's assumed that the token can be any of these.

channel variant

(Optional) The channel to read from. If not provided, reads from the default (unnamed) channel.

Returns:

True if the token type matches, false if not.

Type
Boolean

mustMatch(tokenTypes) → {void}

Source:

Determines if the next token matches the given token type. If so, that token is consumed; if not, an error is thrown.

Parameters:
Name Type Description
tokenTypes int | Array.<int>

Either a single token type or an array of token types that the next token should be. If an array is passed, it's assumed that the token must be one of these.

Returns:
Type
void

next() → {parserlib.css.PropertyValuePart}

Source:

Returns the next part of the property value or null if there is no next part.

Returns:

The next part of the property value or null if there is no next part.

Type
parserlib.css.PropertyValuePart

notToken(first, startLine, startCol) → {Object}

Source:

Produces a NOT or CHAR token based on the specified information. The first character is provided and the rest is read by the function to determine the correct token to create.

Parameters:
Name Type Description
first String

The first character in the token.

startLine int

The beginning line for the character.

startCol int

The beginning column for the character.

Returns:

A token object.

Type
Object

numberToken(first, startLine, startCol) → {Object}

Source:

Produces a number token based on the given character and location in the stream. This may return a token of NUMBER, EMS, EXS, LENGTH, ANGLE, TIME, FREQ, DIMENSION, or PERCENTAGE.

Parameters:
Name Type Description
first String

The first character for the token.

startLine int

The beginning line for the character.

startCol int

The beginning column for the character.

Returns:

A token object.

Type
Object

parserPropertyValue() → {parserlib.css.PropertyValue}

Source:

Parses a property value (everything after the semicolon).

Throws:

parserlib.util.SyntaxError If an unexpected token is found.

Returns:

The property value.

Type
parserlib.css.PropertyValue

parseRule(input) → {Boolean}

Source:

Parses a complete CSS rule, including selectors and properties.

Parameters:
Name Type Description
input String

The text to parser.

Returns:

True if the parse completed successfully, false if not.

Type
Boolean

parseSelector(input) → {Selector}

Source:

Parses a single CSS selector (no comma)

Parameters:
Name Type Description
input String

The text to parse as a CSS selector.

Throws:

parserlib.util.SyntaxError If an unexpected token is found.

Returns:

An object representing the selector.

Type
Selector

parseStyleAttribute(input) → {void}

Source:

Parses an HTML style attribute: a set of CSS declarations separated by semicolons.

Parameters:
Name Type Description
input String

The text to parse as a style attribute

Returns:
Type
void

peek(count) → {String}

Source:

Reads the next character without advancing the cursor.

Parameters:
Name Type Description
count int

How many characters to look ahead (default is 1).

Returns:

The next character or null if there is no next character.

Type
String

peek() → {int}

Source:

Returns the token type for the next token in the stream without consuming it.

Returns:

The token type of the next token in the stream.

Type
int

peek() → {parserlib.css.PropertyValuePart}

Source:

Returns the next part of the property value or null if there is no next part. Does not move the internal counter forward.

Returns:

The next part of the property value or null if there is no next part.

Type
parserlib.css.PropertyValuePart

previous() → {parserlib.css.PropertyValuePart}

Source:

Returns the previous part of the property value or null if there is no previous part.

Returns:

The previous part of the property value or null if there is no previous part.

Type
parserlib.css.PropertyValuePart

read() → {String}

Source:

Reads the next character from the input and adjusts the row and column accordingly.

Returns:

The next character or null if there is no next character.

Type
String

readCount(count) → {String}

Source:

Reads a given number of characters. If the end of the input is reached, it reads only the remaining characters and does not throw an error.

Parameters:
Name Type Description
count int

The number of characters to read.

Returns:

The string made up the read characters.

Type
String

readError(filename, message) → {String}

Source:

Returns message when there is a file read error.

Parameters:
Name Type Description
filename String

The name of the file that caused the error.

message String

The error message

Returns:

The error message.

Type
String

readMatch(matcher) → {String}

Source:

Reads characters that match either text or a regular expression and returns those characters. If a match is found, the row and column are adjusted; if no match is found, the reader's state is unchanged. reading or false to stop.

Parameters:
Name Type Description
matcher String | RegExp

If a string, then the literal string value is searched for. If a regular expression, then any string matching the pattern is search for.

Returns:

The string made up of all characters that matched or null if there was no match.

Type
String

readTo(pattern) → {String}

Source:

Reads up to and including the given string. Throws an error if that string is not found.

Parameters:
Name Type Description
pattern String

The string to read.

Throws:

Error when the string pattern is not found.

Returns:

The string when it is found.

Type
String

readWhile(filter) → {String}

Source:

Reads characters while each character causes the given filter function to return true. The function is passed in each character and either returns true to continue reading or false to stop.

Parameters:
Name Type Description
filter function

The function to read on each character.

Returns:

The string made up of all characters that passed the filter check.

Type
String

removeListener(type, listener) → {void}

Source:

Removes a listener for a given event type.

Parameters:
Name Type Description
type String

The type of event to remove a listener from.

listener function

The function to remove from the event.

Returns:
Type
void

report(message, line, col, rule)

Source:

Report an issue.

Parameters:
Name Type Description
message String

The message to store.

line int

The line number.

col int

The column number.

rule Object

The rule this message relates to.

restore() → {void}

Source:

Restores the last saved bookmark.

Returns:
Type
void

rollupError(message, rule)

Source:

Report some rollup error information.

Parameters:
Name Type Description
message String

The message to store.

rule Object

The rule this message relates to.

rollupWarn(message, rule)

Source:

Report some rollup warning information.

Parameters:
Name Type Description
message String

The message to store.

rule Object

The rule this message relates to.

startFormat() → {String}

Source:

Return opening root XML tag.

Returns:

to prepend before all results

Type
String

startFormat() → {String}

Source:

Return opening root XML tag.

Returns:

to prepend before all results

Type
String

startFormat() → {String}

Source:

Return content to be printed before all file results.

Returns:

to prepend before all results

Type
String

startFormat() → {String}

Source:

Return content to be printed before all file results.

Returns:

to prepend before all results

Type
String

startFormat() → {String}

Source:

Return opening root XML tag.

Returns:

to prepend before all results

Type
String

startFormat() → {String}

Source:

Return content to be printed before all file results.

Returns:

to prepend before all results

Type
String

startFormat() → {String}

Source:

Return opening root XML tag.

Returns:

to prepend before all results

Type
String

stat(name, value)

Source:

Report a statistic.

Parameters:
Name Type Description
name String

The name of the stat to store.

value Variant

The value of the stat.

stringToken(first, startLine, startCol) → {Object}

Source:

Produces a string token based on the given character and location in the stream. Since strings may be indicated by single or double quotes, a failure to match starting and ending quotes results in an INVALID token being generated. The first character in the string is passed in and then the rest are read up to and including the final quotation mark.

Parameters:
Name Type Description
first String

The first character in the string.

startLine int

The beginning line for the character.

startCol int

The beginning column for the character.

Returns:

A token object.

Type
Object

token() → {Token}

Source:

Returns the actual token object for the last consumed token.

Returns:

The token object for the last consumed token.

Type
Token

tokenName(tokenName) → {int}

Source:

Returns the token type value for the given token name.

Parameters:
Name Type Description
tokenName String

The name of the token whose value should be returned.

Returns:

The token type value for the given token name or -1 for an unknown token.

Type
int

tokenName(tokenType) → {String}

Source:

Returns the name of the token for the given token type.

Parameters:
Name Type Description
tokenType int

The type of token to get the name of.

Returns:

The name of the token or "UNKNOWN_TOKEN" for any invalid token type.

Type
String

toString() → {String}

Source:

Returns the text representation of the unit.

Returns:

The text representation of the unit.

Type
String

toString() → {String}

Source:

Returns a string representation for specificity.

Returns:

The string representation of specificity.

Type
String

unget()

Source:

Returns the last consumed token to the token stream.

valueOf() → {String}

Source:

Returns the text representation of the unit.

Returns:

The text representation of the unit.

Type
String

valueOf() → {int}

Source:

Creates a numeric value for the specificity.

Returns:

The numeric value for the specificity.

Type
int

verify(text, ruleset) → {Object}

Source:

Starts the verification process for the given CSS text.

Parameters:
Name Type Description
text String

The CSS text to verify.

ruleset Object

(Optional) List of rules to apply. If null, then all rules are used. If a rule has a value of 1 then it's a warning, a value of 2 means it's an error.

Returns:

Results of the verification.

Type
Object

warn(message, line, col, rule)

Source:
Deprecated:
  • Use report instead.

Report an warning.

Parameters:
Name Type Description
message String

The message to store.

line int

The line number.

col int

The column number.

rule Object

The rule this message relates to.

whitespaceToken(first, startLine, startCol) → {Object}

Source:

Produces a S token based on the specified information. Since whitespace may have multiple characters, this consumes all whitespace characters into a single token.

Parameters:
Name Type Description
first String

The first character in the token.

startLine int

The beginning line for the character.

startCol int

The beginning column for the character.

Returns:

A token object.

Type
Object