@@ -638,22 +638,36 @@ public PolicyFactory toFactory() {
638638 textContainerSet .add (textContainer .getKey ());
639639 }
640640 }
641- return new PolicyFactory (compilePolicies (), textContainerSet .build (),
642- ImmutableMap .copyOf (globalAttrPolicies ),
643- preprocessor , postprocessor );
641+ CompiledState compiled = compilePolicies ();
642+
643+ return new PolicyFactory (
644+ compiled .compiledPolicies , textContainerSet .build (),
645+ ImmutableMap .copyOf (compiled .globalAttrPolicies ),
646+ preprocessor , postprocessor );
644647 }
645648
646649 // Speed up subsequent builds by caching the compiled policies.
647- private transient ImmutableMap <String , ElementAndAttributePolicies >
648- compiledPolicies ;
650+ private transient CompiledState compiledState ;
651+
652+ private static final class CompiledState {
653+ final Map <String , AttributePolicy > globalAttrPolicies ;
654+ final ImmutableMap <String , ElementAndAttributePolicies > compiledPolicies ;
655+
656+ CompiledState (
657+ Map <String , AttributePolicy > globalAttrPolicies ,
658+ ImmutableMap <String , ElementAndAttributePolicies > compiledPolicies ) {
659+ this .globalAttrPolicies = globalAttrPolicies ;
660+ this .compiledPolicies = compiledPolicies ;
661+ }
662+ }
649663
650664 /** Called by mutators to signal that any compiled policy is out-of-date. */
651665 private void invalidateCompiledState () {
652- compiledPolicies = null ;
666+ compiledState = null ;
653667 }
654668
655- private ImmutableMap < String , ElementAndAttributePolicies > compilePolicies () {
656- if (compiledPolicies != null ) { return compiledPolicies ; }
669+ private CompiledState compilePolicies () {
670+ if (compiledState != null ) { return compiledState ; }
657671
658672 // Copy maps before normalizing in case builder is reused.
659673 @ SuppressWarnings ("hiding" )
@@ -741,6 +755,13 @@ public String apply(String url) {
741755 }
742756 }
743757 }
758+ if (stylingPolicy != null ) {
759+ AttributePolicy old = globalAttrPolicies .get ("style" );
760+ if (old != null ) {
761+ globalAttrPolicies .put (
762+ "style" , AttributePolicy .Util .join (old , stylingPolicy ));
763+ }
764+ }
744765
745766 ImmutableMap .Builder <String , ElementAndAttributePolicies > policiesBuilder
746767 = ImmutableMap .builder ();
@@ -753,18 +774,29 @@ public String apply(String url) {
753774
754775 Map <String , AttributePolicy > elAttrPolicies
755776 = attrPolicies .get (elementName );
756- if (elAttrPolicies == null ) { elAttrPolicies = ImmutableMap .of (); }
777+ if (elAttrPolicies == null ) {
778+ elAttrPolicies = ImmutableMap .of ();
779+ }
780+ if (stylingPolicy != null ) {
781+ AttributePolicy old = elAttrPolicies .get ("style" );
782+ if (old != null ) {
783+ attrPolicies .put (
784+ elementName ,
785+ elAttrPolicies = Maps .newLinkedHashMap (elAttrPolicies ));
786+ elAttrPolicies .put (
787+ "style" , AttributePolicy .Util .join (old , stylingPolicy ));
788+ }
789+ }
790+
757791 ImmutableMap .Builder <String , AttributePolicy > attrs
758792 = ImmutableMap .builder ();
793+
759794 for (Map .Entry <String , AttributePolicy > ape : elAttrPolicies .entrySet ()) {
760795 String attributeName = ape .getKey ();
761796 // Handle below so we don't end up putting the same key into the map
762797 // twice. ImmutableMap.Builder hates that.
763798 if (globalAttrPolicies .containsKey (attributeName )) { continue ; }
764799 AttributePolicy policy = ape .getValue ();
765- if ("style" .equals (attributeName )) {
766- policy = AttributePolicy .Util .join (policy , stylingPolicy );
767- }
768800 if (!AttributePolicy .REJECT_ALL_ATTRIBUTE_POLICY .equals (policy )) {
769801 attrs .put (attributeName , policy );
770802 }
@@ -774,9 +806,6 @@ public String apply(String url) {
774806 String attributeName = ape .getKey ();
775807 AttributePolicy policy = AttributePolicy .Util .join (
776808 elAttrPolicies .get (attributeName ), ape .getValue ());
777- if ("style" .equals (attributeName )) {
778- policy = AttributePolicy .Util .join (policy , stylingPolicy );
779- }
780809 if (!AttributePolicy .REJECT_ALL_ATTRIBUTE_POLICY .equals (policy )) {
781810 attrs .put (attributeName , policy );
782811 }
@@ -788,7 +817,7 @@ public String apply(String url) {
788817 elementName ,
789818 elPolicy , attrs .build (), skipIfEmpty .contains (elementName )));
790819 }
791- return compiledPolicies = policiesBuilder .build ();
820+ return new CompiledState ( globalAttrPolicies , policiesBuilder .build () );
792821 }
793822
794823 /**
0 commit comments