Ruby's Truncate unsanitizes MS Word code - ruby-on-rails

Curious if anyone ever noticed this, but I have a WYSIWYG that users occassionally paste from word into. There is a word sanitizer, but not everyone's a genius.
If I parse that text somewhere else, it comes out right. But if I truncate it, then the msword code appears.
Does anyone know why truncate unsanitizes this || does anyone know how to sanitize and truncate at the same time?
UPDATE:
Here's an example of the msword being displayed after I truncate :
≪! [If Gte Mso 9]>≪Xml> ≪Br /> ≪O:Office Document Settings> ≪Br /> ≪O:Allow Png/> ≪Br /> ≪/O:Office Document Settings> ≪Br />≪/Xml>≪![Endif] >≪! [If Gte Mso 9]>≪Xml> ≪Br /> ≪W:Word Document> ≪Br /> ≪W:Zoom>0≪/W:Zoom> ≪Br /> ≪W:Track Moves>False≪/W:Track Moves> ≪Br /> ≪W:Track Formatting/> ≪Br /> ≪W:Punctuation Kerning/> ≪Br /> ≪W:Drawing Grid Horizontal Spacing>18 Pt≪/W:Drawing Grid Horizontal Spacing> ≪Br /> ≪W:Drawing Grid Vertical Spacing>18 Pt≪/W:Drawing Grid Vertical Spacing> ≪Br /> ≪W:Display Horizontal Drawing Grid Every>0≪/W:Display Horizontal Drawing Grid Every> ≪Br /> ≪W:Display Vertical Drawing Grid Every>0≪/W:Display Vertical Drawing Grid Every> ≪Br /> ≪W:Validate Against Schemas/> ≪Br /> ≪W:Save If Xml Invalid>False≪/W:Save If Xml Invalid> ≪Br /> ≪W:Ignore Mixed Content>False≪/W:Ignore Mixed Content> ≪Br /> ≪W:Always Show Placeholder Text>False≪/W:Always Show Placeholder Text> ≪Br /> ≪W:Compatibility> ≪Br /> ≪W:Break Wrapped Tables/> ≪Br /> ≪W:Dont Grow Autofit/> ≪Br /> ≪W:Dont Autofit Constrained Tables/> ≪Br /> ≪W:Dont Vert Align In Txbx/> ≪Br /> ≪/W:Compatibility> ≪Br /> ≪/W:Word Document> ≪Br />≪/Xml>≪![Endif] >≪! [If Gte Mso 9]>≪Xml> ≪Br /> ≪W:Latent Styles Def Locked State="False" Latent Style Count="276"> ≪Br /> ≪/W:Latent Styles> ≪Br />≪/Xml>≪![Endif] > ≪! {Cke Protected}%3 C!%2 D%2 D%7 Bcke Protected%7 D%253 C!%252 D%252 D%257 Bcke Protected%257 D%25253 C!%25252 D%25252 D%25257 Bcke Protected%25257 D%2525253 C!%2525252 D%2525252 D%2525257 Bcke Protected%2525257 D%252525253 C!%252525252 D%252525252 D%252525257 Bcke Protected%252525257 D%25252525253 C!%25252525252 D%25252525252 D%25252525257 Bcke Protected%25252525257 D%2525252525253 C!%2525252525252 D%2525252525252 D%2525252525250 A%25252525252520%2525252525252 F*%25252525252520 Font%25252525252520 Definitions%25252525252520*%2525252525252 F%2525252525250 A%25252525252540font Face%2525252525250 A%25252525252509%2525252525257 Bfont Family%2525252525253 A Times%2525252525253 B%2525252525250 A%25252525252509panose 1%2525252525253 A2%252525252525200%252525252525205%252525252525200%252525252525200%252525252525200%252525252525200%252525252525200%252525252525200%252525252525200%2525252525253 B%2525252525250 A%25252525252509mso Font Charset%2525252525253 A0%2525252525253 B%2525252525250 A%25252525252509mso Generic Font Family%2525252525253 Aauto%2525252525253 B%2525252525250 A%25252525252509mso Font Pitch%2525252525253 Avariable%2525252525253 B%2525252525250 A%25252525252509mso Font Signature%2525252525253 A3%252525252525200%252525252525200%252525252525200%252525252525201%252525252525200%2525252525253 B%2525252525257 D%2525252525250 A%25252525252540font Face%2525252525250 A%25252525252509%2525252525257 Bfont Family%2525252525253 A Verdana%2525252525253 B%2525252525250 A%25252525252509panose 1%2525252525253 A2%2525252525252011%252525252525206%252525252525204%25
The whole thing is about 600 characters long. This is the first 200 or so :
“Excellent” – The New York Times
“4 Stars” - The Star-Ledger
“Best Romantic Restaurant” – Suburban Essex
“Best View” – OpenTable
In December 1986, the Knowles opened Highlawn after months of restoration to the former open-air “casino” which had, along with the now-prosperous park, been neglected for several years.
Here's a custom sanitizer I made with the help of Stackoverflow :
def sanitized_text(text)
sanitized = text.gsub(/≪[^>]*>/, '')
end
The trouble with this sanitizer is that it returns empty white space after I truncate to 125 characters. I expanded it to 600 characters, and I get a single line that is another msword conditional statement.
Update:
This is the code that produces the msword content.
= truncate(organization.about_us, 125)
Note that when I just put this :
= organization.about_us
It comes out fine, but of course not truncated.
I should also add this is Ruby 1.8.7 / rails 2.3.5

Truncating HTML is always a real hassle because you can end up splitting tags and entities. Without proper UTF-8 handling, you also run the risk of chopping a two byte character in half.
Another thing to watch out for is overly greedy regular expressions:
def sanitized_text(text)
sanitized = text.gsub(/≪[^>]*?>/, '')
end
The *? will capture the minimum that matches, where * will capture the largest match.
For instance:
<A><B>
This can be grouped into "<", "A><B", and ">" if you end up with the wrong expression.
Edit: I've tried to reproduce this and had no luck.
With this example, using your text pasted in and sanitized, everything appears to be okay.
# app/controllers/example_controller.rb
class ExampleController < ApplicationController
def index
#text = '&Lt;! [If Gte Mso 9]>&Lt;Xml> &Lt;Br /> &Lt;O:Office Document Settings> &Lt;Br /> &Lt;O:Allow Png/> &Lt;Br /> &Lt;/O:Office Document Settings> &Lt;Br />&Lt;/Xml>&Lt;![Endif] >&Lt;! [If Gte Mso 9]>&Lt;Xml> &Lt;Br /> &Lt;W:Word Document> &Lt;Br /> &Lt;W:Zoom>0&Lt;/W:Zoom> &Lt;Br /> &Lt;W:Track Moves>False&Lt;/W:Track Moves> &Lt;Br /> &Lt;W:Track Formatting/> &Lt;Br /> &Lt;W:Punctuation Kerning/> &Lt;Br /> &Lt;W:Drawing Grid Horizontal Spacing>18 Pt&Lt;/W:Drawing Grid Horizontal Spacing> &Lt;Br /> &Lt;W:Drawing Grid Vertical Spacing>18 Pt&Lt;/W:Drawing Grid Vertical Spacing> &Lt;Br /> &Lt;W:Display Horizontal Drawing Grid Every>0&Lt;/W:Display Horizontal Drawing Grid Every> &Lt;Br /> &Lt;W:Display Vertical Drawing Grid Every>0&Lt;/W:Display Vertical Drawing Grid Every> &Lt;Br /> &Lt;W:Validate Against Schemas/> &Lt;Br /> &Lt;W:Save If Xml Invalid>False&Lt;/W:Save If Xml Invalid> &Lt;Br /> &Lt;W:Ignore Mixed Content>False&Lt;/W:Ignore Mixed Content> &Lt;Br /> &Lt;W:Always Show Placeholder Text>False&Lt;/W:Always Show Placeholder Text> &Lt;Br /> &Lt;W:Compatibility> &Lt;Br /> &Lt;W:Break Wrapped Tables/> &Lt;Br /> &Lt;W:Dont Grow Autofit/> &Lt;Br /> &Lt;W:Dont Autofit Constrained Tables/> &Lt;Br /> &Lt;W:Dont Vert Align In Txbx/> &Lt;Br /> &Lt;/W:Compatibility> &Lt;Br /> &Lt;/W:Word Document> &Lt;Br />&Lt;/Xml>&Lt;![Endif] >&Lt;! [If Gte Mso 9]>&Lt;Xml> &Lt;Br /> &Lt;W:Latent Styles Def Locked State="False" Latent Style Count="276"> &Lt;Br /> &Lt;/W:Latent Styles> &Lt;Br />&Lt;/Xml>&Lt;![Endif] > &Lt;! {Cke Protected}%3 C!%2 D%2 D%7 Bcke Protected%7 D%253 C!%252 D%252 D%257 Bcke Protected%257 D%25253 C!%25252 D%25252 D%25257 Bcke Protected%25257 D%2525253 C!%2525252 D%2525252 D%2525257 Bcke Protected%2525257 D%252525253 C!%252525252 D%252525252 D%252525257 Bcke Protected%252525257 D%25252525253 C!%25252525252 D%25252525252 D%25252525257 Bcke Protected%25252525257 D%2525252525253 C!%2525252525252 D%2525252525252 D%2525252525250 A%25252525252520%2525252525252 F*%25252525252520 Font%25252525252520 Definitions%25252525252520*%2525252525252 F%2525252525250 A%25252525252540font Face%2525252525250 A%25252525252509%2525252525257 Bfont Family%2525252525253 A Times%2525252525253 B%2525252525250 A%25252525252509panose 1%2525252525253 A2%252525252525200%252525252525205%252525252525200%252525252525200%252525252525200%252525252525200%252525252525200%252525252525200%252525252525200%2525252525253 B%2525252525250 A%25252525252509mso Font Charset%2525252525253 A0%2525252525253 B%2525252525250 A%25252525252509mso Generic Font Family%2525252525253 Aauto%2525252525253 B%2525252525250 A%25252525252509mso Font Pitch%2525252525253 Avariable%2525252525253 B%2525252525250 A%25252525252509mso Font Signature%2525252525253 A3%252525252525200%252525252525200%252525252525200%252525252525201%252525252525200%2525252525253 B%2525252525257 D%2525252525250 A%25252525252540font Face%2525252525250 A%25252525252509%2525252525257 Bfont Family%2525252525253 A Verdana%2525252525253 B%2525252525250 A%25252525252509panose 1%2525252525253 A2%2525252525252011%252525252525206%252525252525204%2'
end
end
# app/helpers/example_helper.rb
module ExampleHelper
def sanitized_text(text)
text.gsub(/&Lt;[^>]*>/, '')
end
end
The view itself is pretty much what you have:
<!-- app/views/example/index.html.erb -->
<body>
<strong>Original</strong>
<div>
<%= sanitized_text(#text) %>
</div>
<strong>Truncated</strong>
<div>
<%= truncate(sanitized_text(#text), :length => 125) %>
</div>
<strong>Truncated With Deprecated Option</strong>
<div>
<%= truncate(sanitized_text(#text), 125) %>
</div>
</body>
This was on OS X with Ruby 1.8.7p174, Rails 2.3.5 using WEBrick to test.

Related

tfs 2017 - scrum template - missing statechanged field in some work items

Noticed StateChanged field is missing within XML definitions of some work items in scrum template of tfs 2017 RTM : pbi, testplan, testsuite, feebackrequest, codereviewrequest, and some more.
I assume i should add it manually to them. Right ?
If you mean the State Change Date field, yes, by default it's not added in some work items in scrum template.
Actually the control is not added to layout eventhough in the existing work items such as Bug, Feature. That means, you cannot see the field in work item layout. However you can add the control to display the field based on your requirements.
Yes, you can also add the State Change Date field manually to the work items which not exsiting in them. See Add a field, or apply a rule, or change an attribute for details.
You can also use the TFS Process Template Editor to edit the WITs definitions.
Field:
<FieldDefinition name="State Change Date" refname="Microsoft.VSTS.Common.StateChangeDate" type="DateTime">
<WHENCHANGED field="System.State">
<SERVERDEFAULT from="clock" />
</WHENCHANGED>
<WHENNOTCHANGED field="System.State">
<READONLY />
</WHENNOTCHANGED>
</FieldDefinition>
Layout Control:
<Group Label="Status">
<Column PercentWidth="100">
<Control FieldName="Microsoft.VSTS.Common.StateChangeDate" Type="DateTimeControl" Label="State Change Date:" LabelPosition="Left" />
</Column>
</Group>
Page Section:
<Section>
<Group Label="Status">
<Control Label="State Change Date:" Type="DateTimeControl" FieldName="Microsoft.VSTS.Common.StateChangeDate" />
</Group>
</Section>

Aligning data in JSP while using struts2

While using struts2 tags I wrote jsp as
<s:textfield name="NumOfSeats" label="No. of Berths"/>
<sx:datetimepicker name="date" label="Date of journey" displayFormat="dd-MM-yyyy" />
<s:textfield name="trainNo" label="Train no"/>
I got my page like this(Lables and textfilelds at same height)
As I need datetimepicker I added this <sx:head></sx:head>. Then page became like this(label and text field at different heights)
What can I do for making them to align at same height.

How to show elements of custom data type in TFS custom build definition

Is it possible to show the sub elements of a custom datatype in the TFS build definition file? For example in the 'Agent setting' the menu expands to its sub elements in the same way I want to show the elements of my own datatype.
<x:Members>
<x:Property Name="BuildNumberFormat" Type="InArgument(x:String)" />
<x:Property Name="SolutionSpecificBuildOutputs" Type="InArgument(x:Boolean)" />
<x:Property Name="CleanWorkspace" Type="InArgument(mtbwa:CleanWorkspaceOption)" />
<x:Property Name="SourceAndSymbolServerSettings" Type="InArgument(mtbwa:SourceAndSymbolServerSettings)" />
**<x:Property Name="AgentSettings" Type="InArgument(mtbwa:AgentSettings)" />**
<x:Property Name="AssociateChangesetsAndWorkItems" Type="InArgument(x:Boolean)" />
<x:Property Name="CreateWorkItem" Type="InArgument(x:Boolean)" />
<x:Property Name="CreateLabel" Type="InArgument(x:Boolean)" />
<x:Property Name="GetVersion" Type="InArgument(x:String)" />
<x:Property Name="PrivateDropLocation" Type="InArgument(x:String)" />
<x:Property Name="Verbosity" Type="InArgument(mtbw:BuildVerbosity)" />
<x:Property Name="Metadata" Type="mtbw:ProcessParameterMetadataCollection" />
<x:Property Name="SupportedReasons" Type="mtbc:BuildReason" />
<x:Property Name="BuildProcessVersion" Type="x:String" />
**<x:Property Name="MapBuildConfig" Type="InArgument(ma:MyBuildConfig)" />**
In above code "AgentSettings" expands to its elements: Name filter, Tags filter so on..
But my custom type "MyBuildConfig does not expand to its elements...
Any help?
Thanks!
Could be that you are looking for a Custom Process Parameter editor?
Before going that route I suggest to reuse existing editors like Bart suggests and adapting your custom type.

Custom onsynctopreference for XUL textbox

I wanted to enable custom shortcuts in my Firefox extension. The idea is that the user just focuses on a textbox, presses key combination, and it's shown in the textbox and saved to a preference. However, I couldn't get it to work. With this XUL
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>
<?xml-stylesheet href="chrome://mozapps/skin/pref/pref.css" type="text/css"?>
<!DOCTYPE window SYSTEM "chrome://nextplease/locale/nextplease.dtd">
<prefwindow id="nextpleaseprefs" title="&options.title;" buttons="accept, cancel"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<prefpane id="nextplease.general" label="&options.general.title;" image="chrome://nextplease/skin/Sound Mixer.png">
<preferences>
<preference id="nextkey" name="nextplease.nextkey" type="int"/>
</preferences>
<vbox flex="1">
<hbox align="center">
<label value="&options.general.nextKey;" />
<textbox id="nextkey" flex="1" editable="false"
onkeyup="return nextplease.handleKeySelection(this, event);"
preference-editable="true" preference="nextkey"
onsynctopreference="alert('syncing'); return nextplease.syncKeySelector(this);"/>
</hbox>
</vbox>
</prefpane>
<script type="application/x-javascript" src="chrome://nextplease/content/nextpleaseCommon.js" />
<script type="application/x-javascript" src="chrome://nextplease/content/nextpleaseOptions.js" />
</prefwindow>
the event in onkeyup works. But when I click the OK button, I don't see a "syncing" alert. Why isn't onsynctopreference working? Is it impossible to have custom onsynctopreference attribute for a textbox?
It seems the problem was simply that preferences are only synchronized on input event (and maybe some others), but not on keyup.

Something to dynamically Generate a UI to edit XML file?

I have an XML file with a lot of nodes similar to the following format:
<Factsheet page="GenericOfflineFactsheet.aspx" pageTitle="MyTitle" >
<TopStrapline text="BlahBlahBlah" />
<Commentary page="Text.ascx" />
<ChartPanel page="Bar.ascx" appearanceFile="Bar.xml" />
<Strapline text="blah blah blah" />
<Funds>
<fund id="215" countryid="N0" />
<fund id="561" countryid="N0" />
</Funds>
<LegalText effectiveDate="08 June 2010">
<Line id="30321" />
<Line id="10301" />
</LegalText>
</Factsheet>
Is there any free plugins (or any other means) out there that I could use in an ASP.NET MVC application to generate a basic UI for editing this kind of file?
What about LinqToXML? Then you can use this as your model in your controllers and views just like LinqToSQL.
LinqToXML

Resources