premasagar / pablo

Pablo is a lightweight, expressive JavaScript SVG library. Pablo creates interactive drawings with SVG (Scalable Vector Graphics), giving access to all of SVG's granularity and power.
https://pablojs.com
MIT License
413 stars 16 forks source link

Support Pablo(markup, attributes) #82

Closed premasagar closed 10 years ago

premasagar commented 11 years ago

E.g. Pablo('<circle fill="red"/>', {cx:5, cy:5, r:5}) This would be equivalent to Pablo('<circle fill="red"/>').attr({cx:5, cy:5, r:5})

This is analogous to the other Pablo() creation signatures that allow optional attributes. But it is not clear how to implement this without a performance hit.

In the PabloCollection constructor is currently:

else if (typeof node === 'string' && attrOrContext){
    // Create a named element, e.g. Pablo('circle', {})
    node = make(node);
}

So, currently when node is a string and attributes is given, then node is assumed to be an element name. Instead, an element name needs to be distinguished from markup, so that only element names are sent through make().

The add() method checks for markup as follows:

// Detect `<` as the first non-whitespace character
var openTag = /^\s*</;

if (openTag.test(node)){
...
}

This works, but the use of a regular expression will give a performance hit. In the case of add() this hit affects the uses Pablo(markup) and Pablo(selectors).

If the check is also added to the PabloCollection constructor, it will add a hit on Pablo(elementName, attributes) and an additional hit on Pablo(markup).

The check can't only be in the constructor, because it is needed in add(), and vice versa.

Perhaps there is an alternative way to implement it? Some of the detection logic from add() may need to be moved to the PabloCollection constructor.

Related #72