I want to write a regex which will make link unclickable - simply removing href html tags and leaving the link url as text. So it will ignore anchor text if it is given only leave the url.
But this regex should make links unclikable if they are not from the certain domain. I want this for my private messaging system.
So regex will do following
if the link is not targeting the certain domain make the link url text. Ignore given anchor text.
asp.net 4.0 , C# 4.0
Example
My domain
This will be parsed as http://www.monstermmorpg.com/ it will be text
Something like this works on jQuery:
$(document).ready(removeLink("domain.com"));
function removeLink(domain)
{
$('a').each(function(){
if($(this).attr("href")!=null)
if($(this).attr("href").indexOf(domain)>=0)
$(this).removeAttr("href");
})
}
Explanation:
Gets all anchor elements in the HTML rendered iterating through them and removing the href attribute of the hyperlink for all the the ones that have domain.com as part of it.
jsfiddle demo.
Related
I am making a c# code that converts relative to absolute URLs in href and src attributes of an inputted HTML code in a Richtextbox when the user clicks a button, using a path that the user input. I need a regex that only matches relative URLs inside href and src attributes and converts them to absolute. this is what I am trying to achieve:
example:
if the path that the user inputted: https://example.com/page
and the html code in Richtextbox is :
click
click
<img src="/img1.png" />
<img src="../img2.png" />
this is the result that I want for the html code:
click //this doesn't change
click
<img src="https://example.com/page/img1.png" />
<img src="https://example.com/img2.png" />
I have only been able to come up with regex that matches href attributes .href=(["])(.?)\1
but I can't come up with a regex that does the work above (relative to absolute).
A couple of tips for you:
Please don't use regex to parse HTML. It could break the universe. See here for more info: RegEx match open tags except XHTML self-contained tags
Instead, you can use HTML Agility Pack, as suggested in this SO answer
I just need to find a regular expression for the following:
I have some content in div tag, that includes lot of anchor links in it. So my task is to find anchor links with href as format of "components/showdoc.aspx?docid=" And then add onclick event for that anchor link only, leave the rest of the anchor links.
<div id="content" runat="server">
test doc
</div>
This expression gives and add target to it.
RegEx.Replace(inputString, "<(a)([^>]+)>", "<$1 target=""_blank""$2>")
Thanks
If you are looking to make permanent changes to your HTML file, first manage your HTML parsing by loading it into a System.Windows.Forms.WebBrowser control. From there you can perform DOM-like modifications to the HTML without the dangerous repercussions of parsing corruption that can be caused by performing Regex.Replace on the raw file. (Apparently RegEx + HTML is a serious issue for some).
So first in your code you would:
WebBrowser myBrowser = new WebBrowser();
myBrowser.URL = #"C:\MyPath\MyFile.HTML";
HtmlElement myDocBody = myBrowser.Document.Body;
Then you can navigate through your document body, seeking out your div tag and looking for your anchor tags by using the HtmlElement.Id property and HtmlElement.GetAttribute method.
Note: feel free to still use RegEx matching on the URL strings but only after extracting them from a GetAttribute("href") method.
To add the onClick method, simply invoke the HtmlElement.SetAttribute method.
When you have finished all your modifications, save the changes by writing the WebBrowser.DocumentText to file.
Here is a reference:
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelement.aspx
Don't use regex to parse html, it's evil.
You could use the HTML Agility Pack, it even has a nice NuGet Package.
Alternatively, you could do this on the client side with a single line of jQuery:
$('a[href*="components/showdoc.aspx?docid="]').on('click', myClickFunction);
This is making use of the Attribute Contains Selector.
If you want to find the docid in your click function, you could write something like this in your click function:
function myClickFunction(e){
var href = $(this).attr('href');
var docId = href.split('=')[1];
alert(docId);
}
Note that this assumes there's only ever one query string value, if you wanted to make this more robust you could do something like in this answer: https://stackoverflow.com/a/1171731/21200
I am using default asp.net ToolTip property, the text is
This is Going to be a long text
Thats why we decided to split it.
but using <br/> to split the line doesn't work, it renders as a text, and I want it to break the line instead.
Here is my code:
Label lblActionText = new Label();
lblActionText.Text = "Helloooo Phaltu";
lblActionText.Style.Add("cursor", "pointer");
lblActionText.ToolTip =
"This is Going to be a long text"
+ "<br/>"
+ "Thats why we decided to split it.";
I've not tried this but would "System.Environment.NewLine" not do the job instead of the BR tag?
You can use this to insert a line break inside a ToolTip.
lblActionText.ToolTip = " First text " + Environment.NewLine + " second text ";
Use the line break character entity (
). It is easy to implement but the only problem is that this will work in IE & Chrome but not Firefox.
lblActionText.ToolTip = "This is Going to be a long text
Thats why we decided to split it.";
See jsFiddle: http://jsfiddle.net/jafLf/
You are passing a string value to a Tooltip, and that's why the HTML element <br /> is not working.
However you can try this ASP.Net AJAX TooltipExtender
This works only on IE but other browsers are not!!
If you want to have a customize tooltip then seach for jQuery tooltips to have a formatted tooltip
You can do this with adding a table in the tooltip :)
Example: ASP forum
The ASP.NET ToolTip property corresponds to the HTML title attribute.
In IE and Chrome you can simply do:
<span title="multiline
tool
tip">Mouseover me!</span>
However, that won't work in Firefox as it actually follows the W3C guidelines for CDATA.
CDATA is a sequence of characters from
the document character set and may
include character entities. User
agents should interpret attribute
values as follows:
Replace character entities with characters,
Ignore line feeds,
Replace each carriage return or tab with a single space.
Your best bet (considering you also want to change the colour) would be to go with a jQuery solution such as QTip which gives you all the customisation you want and more..
You cannot change the color of a tooltip with the default title attribute. For that the only way is to use a javascript generated tooltip.
There are plenty of plugins for jQuery such as:
Dynamic tooltip
Popup Bubble
jQuery Horizontal Tooltips
Coda Popup Bubble
Awesomeness
TipTip
(mb)Tooltip
vTip
jGrowl
jQuery Ajax Tooltip
Digg-style post sharing tool with jQuery
Input Floating Hint Box
Simpletip
qTip
Orbital Tooltip
And many more, check them here: Stylish jQuery Tooltip Plugins Webdesign
You can also just create your own using javascript. Add a mouseover event on the element then show a hidden div over the element with whichever html elements you want, in whichever color you need.
It is also more safe to use a javascript approach for cross-browser compatibility.
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.
So I have a Hyperlink called lnkTwitter:
And I'm trying to set the url in the code behind:
lnkTwitter.NavigateUrl = string.Format("http://www.twitter.com/home?status={0}", Server.UrlEncode("I'm Steven"));
When I do that and hover over the link, the url displays correctly in the status bar as "http://www.twitter.com/home?status=I'm+Steven", but the actual url, if I click on the link or look at the link's properties, is "http://www.twitter.com/home?status=I%27m+Steven".
For some reason, this only happens in Firefox; in IE, I am taken to the correct url.
Have you tried to view source code?
If source is ok, then there's no troubles with your code.
Firefox just likes to unescape the urls that it shows. While this can be confusing it should not cause your code or the sites you link to (twitter, in this case) any problems.
If you follow the link and then copy the url and paste it into Notepad or something then you should get the escaped form that was actually used instead of the unescaped form that was displayed.
Uri.EscapeDataString, Uri.EscapeUriString, HttpUtility.UrlEncode and HttpUtility.UrlPathEncode are available to use in C# out of the box, they can not convert all the characters exactly the same way as JavaScript escape function does.
Solution: Use JScript.Net's own implementation. Simply reference the Microsoft.JScript.dll and use the Microsoft.JScript.GlobalObject.escape() method to encode your url.