In my signup form, I'm using asp.net unobtrusive validation to check if a username exists. My JsonResult method returns the following if a clash is found:
This is already in use. How about \u003cstrong\u003efoo123\u003c/strong\u003e?
I use Microsoft's unobtrusive validation to display this in my view:
#Html.ValidationMessageFor(m => m.Username)
which causes it to appear as follows in the page source:
This is already in use. How about
<strong>foo123</strong>
?
As you can see, the text is split over three lines. This is not a problem in Firefox, but in Chrome these new lines are causing the displayed text to break in a similar manner to <br />. I believe the encoding is to blame for this - can anyone explain why? Is there any solution to this issue?
Thank you in advance.
Without having tried it, you could try to replace newlines in the string with... nothing?
#Html.ValidationMessageFor(m => m.Username).ToString().Replace(Environment.NewLine, "");
Feels really weird that would be the problem though since it's just in Chrome, double-check CSS rules. Or just surround the validation message in a p-tag?
If they are displayed with line breaks on the actual page, then first thing I would do is inspect <strong/> tag and see if there is any CSS that defines it as display:block; or overrides white-space property.
If you are concerned about output of the DOM explorer... That's just how Chrome displays DOM trees. These three lines are each individual nodes (2 text nodes and one element node), so they are treated as equal. If you inspect the first paragraph of my answer, you will see the same thing.
try tweaking with css.
\u003cdiv\u003e
This is already in use. How about \u003cstrong\u003efoo123\u003c/strong\u003e?
\u003c/div\u003e?
I think this is due the some padding issue. Upload the generated Html code of the page.
Related
I am using ASP.NET MVC, when I want to use the tag in #Html.Raw, this tag does not appear in the desired <div>.
As shown here:
<div class="mt-4 current-cursor">
#Html.Raw("<strong>OKK</strong> <p><ul><li style='font-size:18px;'>1.Test1</li><li>2.Test2</li></p>")
</div>
The result that it displays for me is as below, that is, it does not recognize the <strong> tag at all.
Html.Raw does not interpret anything at all. It just spews the given string unencoded into the output docuument.
So if it doesn't look right in your case, possible you have some CSS in that page that causes it to look as it does. You could use F12 (Developer Tools, depending on your browser) to inspect the "OKK" for details.
BTW, the other tags in your example also look wrong (which could also be an issue given existing CSS in the page).
In my case, for example, using some (other) arbitrary styles, your code looks like this:
that's my first question, i hope i'm doing everything correctly.
Anyway, i have a weird issue. I basically have this HTML input which i want to fill with WatiN :
<input name="edit[id]" class="form-text required" id="edit-id" type="text" size="60" maxlength="64" value="">
That's my code in C# :
IE ie = new IE();
ie.GoTo(urlhere, just too long);
ie.TextField(Find.ByName("edit[id]")).TypeText("Text");
It's not working and it returns this exception :
Ulteriori informazioni: Could not find INPUT (hidden) or INPUT (password) or INPUT (text) or INPUT (textarea) or TEXTAREA element tag matching criteria: Attribute 'name' equals 'edit[id]' at about:blank
I'm not sure why it gives "at about:blank" honestly. I tried with a random google page and the code it's working. If i put a random name instead of the correct one, it gives me the same exception but with the correct url instead of "about:blank".
Note : I'm trying to fill a form.
Thanks for your help!
Not sure what web page are you using but from the information you posted it looks like that form might be "somewhere else" (another div, frame, etc.) To be able to find the EXACT entry you should press F12 (IE) or CTRL+SHIFT+I (Chrome) and traverse the HTML web page to find the exact portion were the input lies and you will find the exact spot.
The code you posted is perfectly fine, there's nothing else you need to do to make it work. If you need more help, posting the webpage you are trying to use will go a long way (if possible, of course).
Imagine the following HTML:
<div>
<b></b>
<div>
<table>...</table>
</div>
</div> <!-- this one -->
...
How could I find the matching closing tag for the first opening div tag? Is there a reg ex that could find it? I guess this is quite a common requirement but I'm struggling to find anything straightforward, just full blown HTML parsers.
No.
Use a full blown HTML parser. There's a reason they exist.
Use Html Agility Pack.
I'm assuming that you have tokeinized the html tags... Now create a stack and every time you see an opening tag push and everytime you see a closing tag pop... and see if the ones you pop macth the closing tag...
But there are already HTML parsers for this so search for one on codeplex.
Well, You need to have a 'clear' view of the syntax ! However, regexp are very limited in scope and I would'nt recommand using it for multi-line/tag syntax.
You rather need to track each tag (open/close) and use a 'handler' to deal with your request. You could use some Lex/Yacc tools but this may be overkilling. Depending on the language you use, you may already have modules for this purpose (like HTMLParser in Python).
There's always LinqToXml if you want to parse HTML and don't need every little detail.
These spaces are not added by me on HTML SIDE and i cannot edit HTML
I want to know what should my comparison string?
I am using watin to automate website testing process but I am unable to encounter only one button.Every other works
watin searches content by name /values /id and many more and works fine but when i see the value of the submit button that i need to be clicked it has some breaks &nsbp so i think they are playing some role
Here is the html:
<span class='button'><input type="submit" value=" Login " /></span>
<span class='button'><input type="button" value=" Back " onclick="history.back(-1)" /></span>
and here is the code to search
browser.Button(WatiN.Core.Find.ByValue(" Login ")).Click();
what can be done??
-- Suggestion -- (i.e. too big for a comment)
You shouldn't use to add spaces to the submit button. Rather, you should use CSS to style the button to your liking. So you would have something like:
input[type=button] {
padding:10px;
min-width: 150px;
}
By the same token, this could eliminate any of the issues you're having with selecting the button. It could be an issue of encodings breaking with watin and as a result, doing this with CSS will make debugging the issue much cleaner and much easier.
Edit:
Have you tried searching by ID as opposed to by value? ID's are supposed to be unique on a page, so if it doesn't find it by those means, then that's one issue that can be rules out. It could also be the fact that you're searching for a button. A <button> is not the same as a <input type="button">.
Edit 2: Even though the issue was due to encodings breaking, I still recommend you reset that button to reset the text (removing all the non breaking spaces) and attach an id/name to it. The reason being for internationalization purposes - and if for some reason you modify the size of the button in the designer, or i18n the app and the text is different, your test will break.
You shouldn't use entities with WatiN.
This code will work, but you have to use real non-breaking space character:
browser.Button(
WatiN.Core.Find.ByValue(
" Login ")).Click();
This is probably inconvenient, but you could use (after adding reference to System.Web) HttpUtility class:
browser.Button(
WatiN.Core.Find.ByValue(
System.Web.HttpUtility.HtmlDecode(
" Login "))).Click();
But, if I were you, I would just go with Regex:
browser.Button(
WatiN.Core.Find.ByValue(
new Regex(#"^\s*Login\s*$"))).Click();
or even new Regex("Login").
Interesting thing: If you ever will have to Find.ByText you don't have to bother so much, and you can use regular space (ie. not exactly non-breaking space). That's because native IE IHTMLElement::getAttribute (http://msdn.microsoft.com/en-us/library/aa752280(VS.85).aspx) converts from innertext attribute to regular spaces, but from value, id etc. it doesn't ( are converted to real non-breaking spaces - 0xA0)
Wow, you really like spaces! I would remove those and use padding/margins like html was designed to be used. Then you wont need all those spaces and you can assign a proper value to your button which watiN will recognize.
I think it is because the in the HTML source is actually an escaped version of the special character that represents a none breaking space. So in you C# source, you'll probably need that character instead of the html entity code. I think you can find the code of that character by using this button to submit a GET form. It will show the escaped character code in the url.
Of course it is better not to put the spaces in there at all. You should give the button a padding using CSS instead.
Hi I have a MVC site and will have users inputting text to be displayed on a web page.
I am trying to come up with a graceful way of handling any HTML that the user try's to input - as you will probably be aware MVC 2 throws an error for any HTML in the text.
I use ViewModels and decorate my properties with filters from the DataAnotation class to validate my forms.
Anybody now of such a way?
Is there some crazy regex that will NOT match HTML but anything else or some other way?
I am open to any suggestions.
Thanks,
Simon
Adding the following attribute will stop the runtime from complaining:
[ValidateInput(false)]
public ActionResult SomeEvilAction ()
{
/* ... */
}
Now it's your task to HTML encode every input you display back on a page:
<%= HttpUtility.HtmlEncode (Model.Text) %>
or
<%: Model.Text %>
I did this exact thing on a site I did the other day.
I am using a WYSIWYG editor that puts in proper html, not bb code.
I disabled validation on the page from the page directive to stop mvc throwing the potentially unsafe code exception and removed all instance of scripts tags using regex.
See Developer Art's post
You may need to add this to your web.config
<httpRuntime requestValidationMode="2.0" />
The regex I used is as follows:
(?<startTag><\s*script[^>]*>)(?<scriptContent>[\s\S]*?)(?<endTag><\s*/script>)
This will give you 3 named groups.
startTag
scriptContent
endTag
So you can do a replace on the script element and show the content of the script, or remove it altogether.
Anything you wish to do really.