Skip to content

Commit

Permalink
Item11603: Change to protect tag blocks
Browse files Browse the repository at this point in the history
Disabing WYSIWYG completely in presence of style or script blocks is a
bit too aggressive.  Add a PROTECT_TAG_BLOCKS setting that will protect
multi-line blocks of html as if they were surrounded by <sticky> tags.

git-svn-id: http://svn.foswiki.org/trunk@14542 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
GeorgeClark authored and GeorgeClark committed Apr 3, 2012
1 parent 6e0724b commit 219b183
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
19 changes: 18 additions & 1 deletion WysiwygPlugin/data/System/WysiwygPluginSettings.txt
Expand Up @@ -20,7 +20,8 @@ You should set =WYSIWYG_EXCLUDE= and =WYSIWYG_EDITABLE_CALLS= in %USERSWEB%.Site
---++++ WYSIWYGPLUGIN_PROTECT_EXISTING_TAGS - Protect specific tags originally in the topic text
The =WYSIWYGPLUGIN_PROTECT_EXISTING_TAGS= preference tells the translator
that certain HTML tags which were originally in the topic text should _remain_ as HTML tags;
the translator will not try to convert them to TML.
the translator will not try to convert them to TML. This protects the tags
themselves, and not the contents enclosed between the =&lt;tag>= and =&lt;/tag>=

The default setting for this preference is defined within the plugin.
It corresponds to =div, span=.
Expand All @@ -32,6 +33,22 @@ by the !WysiwygPlugin, including the =WYSIWYGPLUGIN_STICKYBITS= preference,
=&lt;sticky&gt;= blocks, =&lt;literal&gt;= blocks and the rules applied
to tables and lists.

---++++ WYSIWYGPLUGIN_PROTECT_TAG_BLOCKS - Protect specific tag blocks originally in the topic text
The =WYSIWYGPLUGIN_PROTECT_TAG_BLOCKS= preference tells the translator
that certain HTML tag blocks which were originally in the topic text should _remain_ as HTML blocks;
the translator will not try to convert them to TML.

The default setting for this preference is defined within the plugin.
It corresponds to =script, style=.

As an example, individual html tables can be protected by surrounding them
with =&lt;sticky> .. &lt;/sticky> block. However,if you want to have all
=&lt;table> markup preserved as entered into topics by default, rather than subject to
WYSIWYG editing, add =table= to this list, and =&lt;table> markup will become
automatically sticky.

This feature may be disabled by setting the preference to a single comma.

---++++ WYSIWYGPLUGIN_STICKYBITS - Protect tags based upon their arguments
You can define the global preference =WYSIWYGPLUGIN_STICKYBITS= to stop the
plugin from ever trying to convert specific HTML tags into
Expand Down
2 changes: 1 addition & 1 deletion WysiwygPlugin/lib/Foswiki/Plugins/WysiwygPlugin.pm
Expand Up @@ -119,7 +119,7 @@ sub wysiwygEditingDisabledForThisContent {
my $exclusions = $_[1];
unless ( defined($exclusions) ) {
$exclusions = Foswiki::Func::getPreferencesValue('WYSIWYG_EXCLUDE')
|| 'script,style';
|| '';
}

# Check for explicit exclusions before generic, non-configurable
Expand Down
16 changes: 16 additions & 0 deletions WysiwygPlugin/lib/Foswiki/Plugins/WysiwygPlugin/TML2HTML.pm
Expand Up @@ -129,6 +129,15 @@ WARNING
$this->{protectExistingTags}->{$tag} = 1;
}

my $tagBlocksToProtect = Foswiki::Func::getPreferencesValue(
'WYSIWYGPLUGIN_PROTECT_TAG_BLOCKS')
|| 'script,style';
foreach my $tag ( split /[,\s]+/, $tagBlocksToProtect ) {
next unless $tag =~ /^\w+$/;
$this->{protectExistingTagBlocks}->{$tag} = 1;
}


# Convert TML to HTML for wysiwyg editing

$content =~ s/[$TT0$TT1$TT2]/?/go;
Expand Down Expand Up @@ -166,6 +175,7 @@ WARNING

sub _liftOut {
my ( $this, $text, $type ) = @_;

my %options;
if ( $type and $type =~ /^(?:PROTECTED|STICKY|VERBATIM)$/ ) {
$options{protect} = 1;
Expand Down Expand Up @@ -386,6 +396,12 @@ sub _getRenderedVersion {
# Remove PRE to prevent TML interpretation of text inside it
$text = $this->_liftOutBlocks( $text, 'pre', {} );

# protect some automatic sticky tags.
foreach my $stickyTag ( keys %{$this->{protectExistingTagBlocks}} ) {
$text =~ s/(<(?i:$stickyTag)[^>]*>.*?<\/(?i:$stickyTag)>)/
$this->_liftOut($1, 'PROTECTED')/geis;
}

# Protect comments
$text =~ s/(<!--.*?-->)/$this->_liftOut($1, 'PROTECTED')/ges;

Expand Down
36 changes: 35 additions & 1 deletion WysiwygPlugin/test/unit/WysiwygPlugin/TranslatorTests.pm
Expand Up @@ -2931,7 +2931,41 @@ HERE
</tbody>
</table>
HERE
}
},
{
exec => $TML2HTML,
name => 'protectScriptFromWysiwyg_Item11603',
tml => <<'HERE',
<script option="blah">
* Some script stuff
<p>
*ToBeIgnored*
</script>
HERE
html => <<'HERE'
<p><span class="WYSIWYG_PROTECTED">&#60;script&nbsp;option=&#34;blah&#34;&#62;<br />&nbsp;&nbsp;*&nbsp;Some&nbsp;script&nbsp;stuff<br />&nbsp;&nbsp;&#60;p&#62;<br />&nbsp;&nbsp;*ToBeIgnored*<br />&#60;/script&#62;</span>
</p>
HERE
},
{
exec => $TML2HTML,
name => 'protectStyleFromWysiwyg_Item11603',
tml => <<'HERE',
<style type="text/css">
.pics {
width:232px;
height:272px;
padding:0;
margin:0;
text-align:center;
}
</style>
HERE
html => <<'HERE'
<p><span class="WYSIWYG_PROTECTED">&#60;style&nbsp;type=&#34;text/css&#34;&#62;<br />.pics&nbsp;&nbsp;{&nbsp;&nbsp;<br />&nbsp;width:232px;<br />&nbsp;height:272px;<br />&nbsp;padding:0;&nbsp;&nbsp;<br />&nbsp;margin:0;<br />&nbsp;text-align:center;<br />}<br />&#60;/style&#62;</span>
</p>
HERE
},
];

sub encodedWhitespace {
Expand Down

0 comments on commit 219b183

Please sign in to comment.