Text translation API is translating the URL - api

I am trying to translate using microsoft cognitive text translation API:
Text to be translated is Your ticket with number INC123456 has been created. Following is the link, https://microsofttest.service-now.com/sp?id=ticket&table=incident&sys_id=aisuoiqwq1233444.
// Translating from english to telugu
This is getting translated into:
నెంబరు INC20534102 మీ టిక్కెట్ సృష్టించబడింది. దిగువ లింక్, [https://microsofttest.service-now.com/sp? id = టిక్కెట్ & పట్టిక = ఘటన & sys_id = aisuoiqwq1233444] (https://microsofttest.service-now.com/sp? id = టిక్కెట్ & పట్టిక = ఘటన & sys_id = aisuoiqwq1233444).
How can I prevent link from getting translated?

It is stated in the documentation that you can tag content so that it isn't translated using several methods.
I think the best method for your scenario is tagging your content with notranslate as per the example below:
<div class="notranslate">This will not be translated.</div>
<div>This will be translated. </div>
You can either allow the user to edit the content using HTML, but I think the most essential solution would be to write a function to search for URLs in the content and automatically add this notranslate tag to it and do the magic!

Related

Selecting element using webdriver (duplicate identifiers)

I have to look at an application which I can't use the normal selectors (like "id", "name", etc - this is a design flaw) but I do have a custom tag which has been applied to elements on the page:
test-tag='x'
and this is fine, I can interact with this using (simple script)
var tag = '[test-tag="x"]';
var selector = $(tag);
However, I have now found that some elements (notably textboxes) have a title and a box element - both have the same custom tag applied. Now the text box is an input type. Anyone know how I can change the above to target specifially input types?
try this:
'input[test-tag="x"]'
for the input box
Take a look at this as well:
https://www.w3schools.com/cssref/css_selectors.asp

Page number in jsreport

Is it possible to display page number in jsreport?
I couldn't find this either on the homepage of the tool nor by googling.
Many thanks in advance!
I assume you ask for page numbers in a pdf report created by phantom-pdf recipe...
You can use special tags {#pageNum} and {#numPages} in template.phantom.header for this:
<div style='text-align:center'>{#pageNum}/{#numPages}</div>
Note you can use also javascript in header/footer to customize visibility or value of the page numbers.
<span id='pageNumber'>{#pageNum}</span>
<script>
var elem = document.getElementById('pageNumber');
if (parseInt(elem.innerHTML) <= 3) {
//hide page numbers for first 3 pages
elem.style.display = 'none';
}
</script>
Documentation here
UPDATE 2022:
jsreport now uses primarily chrome for generating pdf. You can now add page numbers using native headers or in complex cases using pdf utils
pdf utils based header playground example can be found here.

Can I define a text property as rich text?

VS 2013, VB, EF6
I am creating an object that will keep user input in one of its properties. I would like that user input to be stored as rich text. What's involved to make that stored text be rich text format? So,
Public Property Text as <what?>
I thought I would post what was my answer for others who might ask the question the same way I did. I begin by stating that my question was poorly formed because I didn't understand I'm not really storing RTF, I'm storing WYSIWYG text with html tags. But I think the question as phrased is useful because that's how many people think until they are taught by others.
Ultimately this process opens a serious XSS vector, but first we have to at least collect the WYSIWYG text.
First step: using a script-based editor capture the text with html tags. I used CKEditor which is easy to download on NuGet. It comes in 3 flavors: basic, standard and full. Another popular one seems to be TinyMCE also available through NuGet.
CKEditor must be 'wired in' to replace the existing input element. I replaced #html.editorfor with a < textarea > directly as follows. Model.UserPost.Body is the property into which I want to place the WYSIWYG text. The Raw helper is required so the output is NOT encoded allowing us to see our WYSIWYG text.
<textarea name="model.UserPost.Body" id="model_UserPost_Body" class="form-control text-box multi-line">
#Html.Raw(Model.UserPost.Body)
</textarea>
CKEditor is 'wired in' using a script element to replace the < textarea > element.
#Section Scripts
<script src="~/scripts/ckeditor/ckeditor.js"></script>
<script>
CKEDITOR.replace('model.UserPost.Body');
</script>
End Section
The script above can be added to all pages via _layout.vbhtml, or just the target page via a #Section Scripts section as shown above, which is often recommended and what I did, but that may also require adding to the standard _Layout the following in the < head > section such as follows.
#RenderSection("Styles", False)
In the controller POST method for the view the following code is needed to capture the WYSIWYG text otherwise the default filter will raise an exception when it detects anything that looks like an html tag.
Dim rawBody = Request.Unvalidated.Form("model.UserPost.Body")
userPost.Body = rawBody
There are some possible gotcha's; The 'body' property has to be removed from the Include:= list of the < Bind > element in the method paramter list if < Bind > is being used. Also, although not directly related to this solution, you can't have a Data Annotation like < Required() > on this property in the model because background checking won't be able to confirm that condition so the ModelState.IsValid flag won't ever go true.
Second step: before saving the input it MUST be checked for XSS. Microsoft has a nice video explaining basic XSS that I recommend viewing; it's only 11 minutes.
Mikesdotnetting has a nice explaination for dealing with XSS and shows a whitelisting algorithm toward the bottom of this page. The following code is based on his work.
To create a white listing approach, the HTML Agility Pack is useful to catalogue the HTML nodes for review. This is easily loaded from Nu Get as well. This is the code I used in the POST method to invoke the white list methods (Yes, it could be more compact, but this is easier to read for us novices):
Dim tempDoc = New HtmlDocument()
tempDoc.LoadHtml(rawBody)
RemoveNodes(tempDoc.DocumentNode, allowedTags)
userPost.Body = tempDoc.DocumentNode.OuterHtml
The allowed tags are what you will allow, which means everything else is rejected, hence whitelisting. This is just a sample list:
Dim allowedTags As New List(Of String)() From {"p", "em", "s", "ol", "ul", "li", "h1", "h2", "h3", "h4", "h5", "h6", "strong"}
These are the methods based on Mikesdotnetting page:
Private Sub RemoveNodes(ByVal node As HtmlNode, allowedTags As List(Of String))
If (node.NodeType = HtmlNodeType.Element) Then
If Not allowedTags.Contains(node.Name) Then
node.ParentNode.RemoveChild(node)
Exit Sub
End If
End If
If (node.HasChildNodes) Then
RemoveChildren(node, allowedTags)
End If
End Sub
Private Sub RemoveChildren(ByVal parent As HtmlNode, allowedTags As List(Of String))
For i = parent.ChildNodes.Count() - 1 To 0 Step -1
RemoveNodes(parent.ChildNodes(i), allowedTags)
Next
End Sub
So basically, (1) CKEditor captures user input with html tags that looks nice, (2) the raw input is specially requested in the Controller POST method and then (3) cleaned using a white list. After that it can be output directly to the page using #Html.Raw() because it can be trusted.
That's it. I've not really posted solutions like this before, so if I've missed something let me know and I'll correct or add it.
Rich Text is stored in the Rich Text Format.
The Rich Text Format specifications can be found here:
http://www.microsoft.com/en-us/download/details.aspx?id=10725
It is just an ordinary string. You can extract the string from a RichTextBox using the SaveFile function:
Private Function GetRTF(ByRef Box As RichTextBox) As String
Using ms As New IO.MemoryStream
Box.SaveFile(ms, RichTextBoxStreamType.RichText)
Return System.Text.Encoding.ASCII.GetString(ms.ToArray)
End Using
End Function
You can load text in the Rich Text Format into a RichTextBox using the LoadFile method of the RichTextBox. The text needs to be in the correct format:
Dim rtf As String = "{\rtf1 {\colortbl;\red0\green0\blue255;\red255\green0\blue0;}Guten Tag!\line{\i Dies} ist ein\line formatierter {\b Text}.\line Das {\cf1 Ende}.}"
Using ms As New IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(rtf))
RichTextBox1.LoadFile(ms, RichTextBoxStreamType.RichText)
End Using
Ordinary controls usually will not interpret this format in their text property.

Inserting HTML code to Database SQL using WebMatrix Razor

My Column in the DB are: nvarchar(MAX)
I need to add HTML code into my Database: from CKEditor. I get the following Error.
A potentially dangerous Request.Form value was detected from the client (Description="<h1>Heding 1 </...").
I am using the following Code:
var String=Request["String"];
I even used the following:
var String= HttpUtility.HtmlEncode(Request["String"]);
String=Request["String"];
here is part of my code:
if(IsPost){
var Description =Request.Unvalidated["Description"];
// Here I insert into Database
and The FORM part is:
<form action="" enctype="multipart/form-data" method="post">
<div class="row">
<div class="two columns offset-by-two"><br/><label> Description: </label><br/></div>
<div class="eight columns"><textarea name="Description"></textarea></div>
I want to store the text from "Description" to my database....
You simply need to use Request.Unvalidated to reference inputs that contain HTML if you don't want ASP.NET Request validation kicking in within the ASP.NET Web Pages framework:
var text = Request.Unvalidated["myTextBox"];
Or:
var text = Request.Unvalidated("myTextBox");
It looks like HtmlEncoding should do the trick.
Did you try the following:
var myColumnData = HttpUtility.HtmlEncode(Request["String"]);
Then pass this myColumnData, and all other columns to your Database table.
Edit: In addition to above, you may also want to look at the project settings, as it is recommended in the following blog - A potentially dangerous Request value was detected from the client.
This did the trick for me.
var text = Request.Unvalidated["myTextBox"];
Thank you.
SAFETY RULES.....Before you push it to the database, i suggest you filter suspicious tags such as script tags.
var x = Request.Unvalidated("MyField");
if(x.Contains("<script>") || x.Contains("</script>")){
//no script tag allowed.
}

Selenium-rc: Is there a way to send a buffer of requests

Lets say I have a list of links and want to click a link at random:
<div id="divA">
<a> first link </a>
<a> second link </a>
...
</div>
It isn't the smartest of ways (and if you have a better solution please tell me) but what I currently do is (roughly):
l = []
for i in range(numOfLinks):
xpath = '//div[#id="divA"]/a[%d]'%i
txt = sel.getText(xpath)
l.append(xpath, txt)
xpath,linkName = random.choice(l)
sel.click(xpath)
The main problem of this solution is that it sends many requests to selenium. My question is: is there a way of saving all these requests in a buffer and sending them at once?
are you using the text for anything?
numOfLinks = sel.get_xpath_count('//div[#id="divA"]/a')
random.randrange(1,numOfLinks)
sel.click('//div[#id="divA"]/a[%d]'%random.randrange(1,numOfLinks))
The code above will always click on a random link without having to get the text of the link each time.