PropertyBasedElement: extend HTML5 (Canvas) data props interface
Allow check if property exists and removing properties.
This commit is contained in:
@@ -635,74 +635,6 @@ namespace canvas
|
||||
return m;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
static const std::string DATA_PREFIX("data-");
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
std::string Element::dataPropToAttrName(const std::string& name)
|
||||
{
|
||||
// http://www.w3.org/TR/html5/dom.html#attr-data-*
|
||||
//
|
||||
// 3. Insert the string data- at the front of name.
|
||||
|
||||
std::string attr_name;
|
||||
for( std::string::const_iterator cur = name.begin();
|
||||
cur != name.end();
|
||||
++cur )
|
||||
{
|
||||
// If name contains a "-" (U+002D) character followed by a lowercase ASCII
|
||||
// letter, throw a SyntaxError exception and abort these steps.
|
||||
if( *cur == '-' )
|
||||
{
|
||||
std::string::const_iterator next = cur + 1;
|
||||
if( next != name.end() && islower(*next) )
|
||||
return std::string();
|
||||
}
|
||||
|
||||
// For each uppercase ASCII letter in name, insert a "-" (U+002D)
|
||||
// character before the character and replace the character with the same
|
||||
// character converted to ASCII lowercase.
|
||||
if( isupper(*cur) )
|
||||
{
|
||||
attr_name.push_back('-');
|
||||
attr_name.push_back( tolower(*cur) );
|
||||
}
|
||||
else
|
||||
attr_name.push_back( *cur );
|
||||
}
|
||||
return DATA_PREFIX + attr_name;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
std::string Element::attrToDataPropName(const std::string& name)
|
||||
{
|
||||
// http://www.w3.org/TR/html5/dom.html#attr-data-*
|
||||
//
|
||||
// For each "-" (U+002D) character in the name that is followed by a
|
||||
// lowercase ASCII letter, remove the "-" (U+002D) character and replace the
|
||||
// character that followed it by the same character converted to ASCII
|
||||
// uppercase.
|
||||
|
||||
if( !boost::starts_with(name, DATA_PREFIX) )
|
||||
return std::string();
|
||||
|
||||
std::string data_name;
|
||||
for( std::string::const_iterator cur = name.begin() + DATA_PREFIX.length();
|
||||
cur != name.end();
|
||||
++cur )
|
||||
{
|
||||
std::string::const_iterator next = cur + 1;
|
||||
if( *cur == '-' && next != name.end() && islower(*next) )
|
||||
{
|
||||
data_name.push_back( toupper(*next) );
|
||||
cur = next;
|
||||
}
|
||||
else
|
||||
data_name.push_back(*cur);
|
||||
}
|
||||
return data_name;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
Element::StyleSetters Element::_style_setters;
|
||||
|
||||
|
||||
@@ -107,30 +107,6 @@ namespace canvas
|
||||
bool addEventListener(const std::string& type, const EventListener& cb);
|
||||
virtual void clearEventListener();
|
||||
|
||||
template<class T>
|
||||
void setDataProp( const std::string& name,
|
||||
const T& val )
|
||||
{
|
||||
const std::string& attr = dataPropToAttrName(name);
|
||||
if( !attr.empty() )
|
||||
set<T>(attr, val);
|
||||
else
|
||||
SG_LOG(SG_GENERAL, SG_WARN, "Invalid data-prop name: " << name);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T getDataProp( const std::string& name,
|
||||
const T& def = T() )
|
||||
{
|
||||
const std::string& attr = dataPropToAttrName(name);
|
||||
if( !attr.empty() )
|
||||
return get<T>(attr, def);
|
||||
else
|
||||
SG_LOG(SG_GENERAL, SG_WARN, "Invalid data-prop name: " << name);
|
||||
|
||||
return def;
|
||||
}
|
||||
|
||||
virtual bool accept(EventVisitor& visitor);
|
||||
virtual bool ascend(EventVisitor& visitor);
|
||||
virtual bool traverse(EventVisitor& visitor);
|
||||
@@ -228,9 +204,6 @@ namespace canvas
|
||||
return ElementPtr( new Derived(canvas, node, style, parent) );
|
||||
}
|
||||
|
||||
static std::string dataPropToAttrName(const std::string& name);
|
||||
static std::string attrToDataPropName(const std::string& name);
|
||||
|
||||
protected:
|
||||
|
||||
enum Attributes
|
||||
|
||||
@@ -51,4 +51,16 @@ BOOST_AUTO_TEST_CASE( attr_data )
|
||||
el->setDataProp("myData", 3);
|
||||
BOOST_CHECK_EQUAL( el->getDataProp<int>("myData"), 3 );
|
||||
BOOST_CHECK_EQUAL( node->getIntValue("data-my-data"), 3 );
|
||||
|
||||
SGPropertyNode* prop = el->getDataProp<SGPropertyNode*>("notExistingProp");
|
||||
BOOST_CHECK( !prop );
|
||||
prop = el->getDataProp<SGPropertyNode*>("myData");
|
||||
BOOST_CHECK( prop );
|
||||
BOOST_CHECK_EQUAL( prop->getParent(), node );
|
||||
BOOST_CHECK_EQUAL( prop->getIntValue(), 3 );
|
||||
|
||||
BOOST_CHECK( el->hasDataProp("myData") );
|
||||
el->removeDataProp("myData");
|
||||
BOOST_CHECK( !el->hasDataProp("myData") );
|
||||
BOOST_CHECK_EQUAL( el->getDataProp("myData", 5), 5 );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user