diff --git a/src/object/sp-object.cpp b/src/object/sp-object.cpp index 419027228c7b9bf4a3c32cee4a8a4b3411da6425..3a8a6c4107764d8d9384b20c615e765a814b6c3d 100644 --- a/src/object/sp-object.cpp +++ b/src/object/sp-object.cpp @@ -1094,6 +1094,15 @@ Inkscape::XML::Node* SPObject::write(Inkscape::XML::Document *doc, Inkscape::XML Glib::ustring s = style->write(SP_STYLE_FLAG_IFSET | SP_STYLE_FLAG_IFSRC, SP_STYLE_SRC_STYLE_PROP); + // Write style attributes (SP_STYLE_SRC_ATTRIBUTE) back to xml object + auto properties = style->properties(); + for (auto * prop : properties) { + if(prop->shall_write(SP_STYLE_FLAG_IFSET | SP_STYLE_FLAG_IFSRC, SP_STYLE_SRC_ATTRIBUTE)) { + // WARNING: We don't know for sure if the css names are the same as the attribute names + repr->setAttributeOrRemoveIfEmpty(prop->name(), prop->get_value()); + } + } + // Check for valid attributes. This may be time consuming. // It is useful, though, for debugging Inkscape code. Inkscape::Preferences *prefs = Inkscape::Preferences::get(); diff --git a/src/style-internal.cpp b/src/style-internal.cpp index f290904c65ccf037a4ecde2cf39b09528d1abb14..e9606ca047cb8c680290469d3235a04a56660955 100644 --- a/src/style-internal.cpp +++ b/src/style-internal.cpp @@ -78,13 +78,38 @@ inline bool should_write( guint const flags, bool set, bool dfp, bool src) { return should_write; } -const Glib::ustring SPIBase::write(guint const flags, SPStyleSrc const &style_src_req, SPIBase const *const base) const +/** + * Check if this property should be written. This function won't do any writing so can + * be used as a quick way to check if specific kinds of style attrs have been changed. + * + * @param flags Conditions bitmask of `SP_STYLE_FLAG_*` bits + * @param style_src_req For `SP_STYLE_FLAG_IFSRC` + * @param base Parent node style for `SP_STYLE_FLAG_IFDIFF`, can be NULL + * + * @return True if property should be written, false otherwise + */ +bool SPIBase::shall_write(guint const flags, SPStyleSrc const &style_src_req, SPIBase const *const base) const { // Is this class different from the SPIBase given, this is used in Object-to-Path SPIBase const *const my_base = dynamic_cast(base); bool dfp = (!inherits || !my_base || (my_base != this)); // Different from parent bool src = (style_src_req == style_src || !(flags & SP_STYLE_FLAG_IFSRC)); - if (should_write(flags, set, dfp, src)) { + return should_write(flags, set, dfp, src); +} + +/** + * Compile this style conditionally into a 'name: value' string suitable for css attrs. + * see shall_write for details on conditional flags. + * + * @param flags Conditions bitmask of `SP_STYLE_FLAG_*` bits + * @param style_src_req For `SP_STYLE_FLAG_IFSRC` + * @param base Parent node style for `SP_STYLE_FLAG_IFDIFF`, can be NULL + * + * @return completed css string or empty string if conditions not met. + */ +const Glib::ustring SPIBase::write(guint const flags, SPStyleSrc const &style_src_req, SPIBase const *const base) const +{ + if (shall_write(flags, style_src_req, base)) { auto value = this->get_value(); if ( !value.empty() ) { return (name() + ":" + value + important_str() + ";"); diff --git a/src/style-internal.h b/src/style-internal.h index bf1eb6d1b31c8aa7eb928d124e59b45eae58847f..2f6a582db790e97b75a63e975e086b096d9bc5e6 100644 --- a/src/style-internal.h +++ b/src/style-internal.h @@ -150,6 +150,9 @@ public: } virtual const Glib::ustring get_value() const = 0; + bool shall_write( guint const flags = SP_STYLE_FLAG_IFSET, + SPStyleSrc const &style_src_req = SP_STYLE_SRC_STYLE_PROP, + SPIBase const *const base = nullptr ) const; virtual const Glib::ustring write( guint const flags = SP_STYLE_FLAG_IFSET, SPStyleSrc const &style_src_req = SP_STYLE_SRC_STYLE_PROP, SPIBase const *const base = nullptr ) const;