C# Read from and write to specific CSS classes - c#

I'm trying to write a C# application that will perform an operation on some CSS files. Say you have the following CSS code:
.ClassOne
{
color: #FFFFFF;
font-weight:normal;
font-size: 9pt;
font-family: Tahoma;
vertical-align: middle;
border: solid 1px #7F7F7F;
padding: 1px 1px 1px 1px;
background-color: #B48410;
background-position: center center;
}
.ClassTwo
{
color: #FFFFFF;
background-repeat: repeat-x;
background-color: #000000;
}
.ClassThree
{
color: #000000;
font-weight:normal;
font-size: 9pt;
font-family: Tahoma;
vertical-align: left;
border: solid 1px #F3Dc51;
padding: 1px 1px 1px 1px;
background-color: #A32DF1;
}
What I would like to do is search the file for specific classes, say ClassOne, then store the background-color element applicable to that class. This wouldn't have to be done for all classes in the CSS file, so in the example above I might just want to store the background-color for ClassOne and ClassThree.
The application should then be copy the stored values into another CSS file, with the same classes.
I will know the classes that these operations have to be performed on.
I have taken a look at ExCSS, but i'm not sure how to use it, and if it would be helpful in this instance.
Can anyone help?

This should get you started on ExCss, at least on the parsing part:
[Test]
public void Banana([Values (".ClassThree")] string targetClass, [Values ("#A32DF1")] string expectedColor)
{
var parser = new Parser();
var sheet = parser.Parse(sample);
var result = sheet.Rulesets
.Where(s => s.Selector.ToString() == targetClass)
.SelectMany(r => r.Declarations)
.FirstOrDefault(d => d.Name.Equals("background-color", StringComparison.InvariantCultureIgnoreCase))
.Term
.ToString();
Assert.AreEqual(expectedColor, result);
}
What you'll end up doing, perhaps instead of using Linq, is to loop through your css and build a new css with the values in the declarations that match what you are targeting. For example, below, which is probably not optimally efficient, but could do the trick:
var targetClasses = new List<string> { ".ClassOne", ".ClassThree" };
var targetDecls = new List<string> { "background-color" };
var parser = new Parser();
var sheet = parser.Parse(sample);
foreach (var r in sheet.Rulesets)
{
if (targetClasses.Contains(r.Selector.ToString()))
{
Debug.WriteLine(r.Selector.ToString());
Debug.WriteLine("{");
foreach (var d in r.Declarations)
{
if (targetDecls.Contains(d.Name))
{
Debug.WriteLine("\t" + d);
}
}
Debug.WriteLine("}");
}
}

Related

Passing a ViewModel into CSS File

I read up on Dynamic CSS and found a way to do it on Stack Overflow and adapted it to what I needed.
The data gets loaded into the model no problem but as soon as the debugger hits the view I get the error:
Object is not a reference of an object
Base Controller:
public ActionResult CssDynamic()
{
Layout page = new Layout();
var dummyCorpId = 80;
page.Css = GetCss(dummyCorpId);
var model = page;
return new CssViewResult(model);
}
Partial View Return
public class CssViewResult : PartialViewResult
{
private readonly object model;
public CssViewResult(object model)
{
this.model = model;
}
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ContentType = "text/less";
base.ExecuteResult(context);
}
}
The View:
#model SpecCheck.Portals.Web.UI.ViewModels.Layout
//BRAND COLOURS///////////////////////////////////////////////////////////
//One---------------------------------------------------------------------
##brand1: ##Model.Css.CssBrandColor1;
##brand1dark: darken(##brand1, 25%);
##brand1darker: darken(##brand1, 50%);
##brand1light: lighten(##brand1, 25%);
##brand1lighter: lighten(##brand1, 50%);
//Two--------------------------------------------------------------------
##brand2: ##Model.Css.CssBrandColor2;
##brand2dark: darken(##brand2, 25%);
##brand2darker: darken(##brand2, 50%);
##brand2light: lighten(##brand2, 25%);
##brand2lighter: lighten(##brand2, 50%);
//ADDITIONAL COLORS//////////////////////////////////////////////////////
//Mono--------------------------------------------------------------------
##blk: #000000;
##wht: #FFFFFF;
//Additional Bobcat Colours (Non-Brand)-----------------------------------
##addColour1: ##Model.Css.CssAddColor1;
##addColour2: ##Model.Css.CssAddColor2;
##addColour3: ##Model.Css.CssAddColor3;
//Feedback Colors---------------------------------------------------------
##success: #00cc00;
##warning: #ffdb4d;
##danger: #ff4d4d;
//TEXT////////////////////////////////////////////////////////////////////
//Properties--------------------------------------------------------------
##fontcolour: #Model.Css.CssFontColor;
##fontsize: #Model.Css.CssFontSize px;
##fontfamily: #Model.Css.CssFontFamily;
##fontweight: #Model.Css.CssFontWeight;
//BUTTONS/////////////////////////////////////////////////////////////////
.button {
border: 0 none;
border-radius: 2px 2px 2px 2px;
color: ##brand1;
cursor: pointer;
display: inline-block;
line-height: 20px;
margin-bottom: 0;
margin-top: 10px;
padding: 7px 10px;
text-transform: none;
transition: all 0.3s ease 0s;
-moz-transition: all 0.3s ease 0s;
-webkit-transition: all 0.3s ease 0s;
width: auto;
background: none repeat scroll 0 0 ##brand2;
white-space: nowrap;
font-variant: small-caps;
&:hover {
background-color: ##brand2darker;
color: ##brand1lighter;
text-decoration:underline;
}
}
And the _Layout page:
<link href="#Url.Action("CssDynamic", "Base")" rel="stylesheet" type="text/less"/>
As far as I understand, you need to create a custom view engine for rendering you css files with razor view engine.
This SO answer has more information about it: Dynamic Stylesheets Using Razor

After table sorter not applying the table css styles?

I applied tablesorter css styles for a table in a page.And i also i need to apply another style for rows if textbox entered data matching with any column of table grid data need to apply different color for that row.
$(function () {
grid = $('#tblsearchresult');
// handle search fields key up event
$('#search-term').keyup(function (e) {
text = $(this).val(); // grab search term
if (text.length > 1) {
// iterate through all grid rows
grid.find('tr').each(function (i) {
if ($(this).find('td:eq(1)').text().toUpperCase().match(text.toUpperCase()))
$(this).css({ background: "#A4D3EE" });
if ($(this).find("td:eq(2)").text().toUpperCase().match(text.toUpperCase()))
$(this).css({ background: "#A4D3EE" });
if ($(this).find("td:eq(3)").text().toUpperCase().match(text.toUpperCase()))
$(this).css({ background: "#A4D3EE" });
if ($(this).find("td:eq(4)").text().toUpperCase().match(text.toUpperCase()))
$(this).css({ background: "#A4D3EE" });
});
}
else {
grid.find('tr:has(td)').css({ background: "" });
grid.find('tr').show();
} // if no matching name is found, show all rows
});
});
<table id="tblsearchresult" class="tablesorter"">
<thead>
<tr>
<th>ApplicationName</th>
</tr>
</thead>
<tbody>
<% foreach (var request in Model.ApplicationRoles)
{ %>
<tr>
<td>
<span id="appName_<%: request.Id%>">
<%: request.Application.Name%></span>
</td>
</tr>
</tbody>
</table>
tablesorter css :
table.tablesorter {
font-family:arial;
color: rgb(51, 51, 51);
margin:10px 0pt 15px;
font-size: 8pt;
width: 100%;
text-align: left;
}
table.tablesorter thead tr th, table.tablesorter tfoot tr th {
background-color: #8dbdd8;
border: 1px solid #FFF;
font-size: 8pt;
padding: 5px;
}
table.tablesorter thead tr .header:not(.nosort) {
background-image: url('/sorter/bg.gif');
background-repeat: no-repeat;
background-position: center right;
cursor: pointer;
}
table.tablesorter tbody td {
background-color: rgb(239, 243, 251);
padding: 5px;
border: solid 1px #e8eef4;
vertical-align: top;
}
table.tablesorter tbody tr.odd td {
background-color:#F0F0F6;
}
table.tablesorter thead tr .headerSortUp:not(.nosort) {
background-image: url('/sorter/asc.gif');
}
table.tablesorter thead tr .headerSortDown:not(.nosort) {
background-image: url('/sorter/desc.gif');
}
table.tablesorter thead tr .headerSortDown, table.tablesorter thead tr .headerSortUp {
background-color: #8dbdd8;
}
.divpager
{
display: inline-block;
float: left;
}
I am not able apply anonymous function row color when search text macthed with any table grid row data.please tell me.
Try this code (demo):
$(function () {
grid = $('#tblsearchresult tbody');
// handle search fields key up event
$('#search-term').on('keyup search', function (e) {
text = $(this).val().toUpperCase(); // grab search term
if (text.length > 1) {
// iterate through all grid rows
grid.find('tr').each(function (i) {
var found = false;
$(this).children().each(function(j){
if (this.textContent.toUpperCase().match(text)) {
$(this).addClass('result');
found = true;
}
});
$(this).toggle(found);
});
} else {
grid.find('td').removeClass('result');
grid.find('tr').show();
} // if no matching name is found, show all rows
});
});
$('table').tablesorter();

Why css styles are not applying for matching rows in javascript?

If records are matching with searchtext need to hightlight that entire row but not applying that css style.
My javascript function
$(function () {
grid = $('#tblsearchresult tbody');
// handle search fields key up event
$('#search-term').keyup(function (e) {
text = $(this).val().trim(); // grab search term
if (text.length > 1) {
grid.find('tr:has(td)').css({ background: "" });
grid.find('tr').show();
// iterate through all grid rows
grid.find('tr').each(function (i) {
// check to see if search term matches ApplicationName column
if ($(this).find('td:first-child').text().toUpperCase().match(text.toUpperCase()))
$(this).addClass('result');
// $(this).css({ background: "#A4D3EE" });
// check to see if search term matches RoleName column
if ($(this).find("td:eq(1)").text().toUpperCase().match(text.toUpperCase()))
$(this).addClass('result');
});
}
else {
grid.find('tr:has(td)').css({ background: "" });
grid.find('tr').show();
} // if no matching name is found, show all rows
});
});
$('table').tablesorter();
My CSS :
table.tablesorter tbody td.result {
background: #A4D3EE;
}
table.tablesorter {
font-family:arial;
color: rgb(51, 51, 51);
margin:10px 0pt 15px;
font-size: 10pt;
width: 100%;
text-align: left;
}
table.tablesorter thead tr th, table.tablesorter tfoot tr th {
background-color: #8dbdd8;
border: 1px solid #FFF;
font-size: 10pt;
padding: 5px;
}
table.tablesorter thead tr .header:not(.nosort) {
background-image: url('/sorter/bg.gif');
background-repeat: no-repeat;
background-position: center right;
cursor: pointer;
}
table.tablesorter tbody td {
background-color: rgb(239, 243, 251);
padding: 5px;
border: solid 1px #e8eef4;
vertical-align: top;
}
table.tablesorter tbody tr.odd td {
background-color:#F0F0F6;
}
table.tablesorter thead tr .headerSortUp:not(.nosort) {
background-image: url('/sorter/asc.gif');
}
table.tablesorter thead tr .headerSortDown:not(.nosort) {
background-image: url('/sorter/desc.gif');
}
table.tablesorter thead tr .headerSortDown, table.tablesorter thead tr .headerSortUp {
background-color: #8dbdd8;
}
UI Design :
<table id="tblsearchresult" class="tablesorter">
<thead>
<tr>
</tr>
</thead>
<tbody></tbody>
</table>
Table Data :
applicationame role
application1 appadministrator
app developer
application2 tester
if i given 'app'as search text need to highlight secondrow only .highlightling firstrow also because 'app' is there in role of firstrow..exact match should be highlight on every rows.please tell me.
Please check my code i need to highlight the matching record row .if searchtext matching with table column data need to highlight the entire row.but not applying css in above code.
It seems like you are not applying the result class to the correct element. In your CSS, the following line defines the result class for TDs:
table.tablesorter tbody td.result
But in your javascript, this line will apply it to the table row:
$(this).addClass('result');
So by changing this line to
$(this).children('td').addClass('result');
you should be okay.
Update:
Based on your feedback, I've created an example on jsFiddle for you: http://jsfiddle.net/kUxNj/4/
// check to see if search term matches ApplicationName column
if ($(this).find('td:first-child').text().toUpperCase() === text.toUpperCase())
$(this).children('td').addClass('result');
// check to see if search term matches RoleName column
if ($(this).find("td:eq(1)").text().toUpperCase() === text.toUpperCase())
$(this).children('td').addClass('result');

WCF C# .NET equivalent of PHP file get contents on Image for use with iOS

I currently have a PHP script that given a path to an image on the server, converts the image into a data stream and echoes that string.
<?php
$path = htmlspecialchars{$_POST['URL']);
$img = file_get_contents($path);
echo $img;
?>
I then use the response from the PHP to convert the data stream into an image in iOS as such:
NSData *pictureData = [data getPicture:currentPerson.picture_URL];
personPicture = [UIImage imageWithData:pictureData];
How can I accomplish the same task in C# without having to change too much code in the iOS side. Currently I know how to return a JSON object using a WCF service written in .NET C#. However, I cannot figure out how I would return a data stream as I did in PHP.
Also is there any way to make this process more secure?
--EDIT--
Current implementation that isn't working:
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest, UriTemplate = "/picture/{path}")]
string GetPictureMethod(string path);
public string GetPictureMethod(string path)
{
path = "(test path here)";
string picData = System.IO.File.ReadAllText(path);
return picData;
}
Doing an `NSLog(#"Data: %#", data) gives the following:
Picture data: <efbbbf3c 3f786d6c 20766572 73696f6e 3d22312e 30222065 6e636f64 696e673d 22757466 2d38223f 3e0d0a3c 21444f43 54595045 2068746d 6c205055 424c4943 20222d2f 2f573343 2f2f4454 44205848 544d4c20 312e3020 5472616e 73697469 6f6e616c 2f2f454e 22202268 7474703a 2f2f7777 772e7733 2e6f7267 2f54522f 7868746d 6c312f44 54442f78 68746d6c 312d7472 616e7369 74696f6e 616c2e64 7464223e 0d0a3c68 746d6c20 786d6c6e 733d2268 7474703a 2f2f7777 772e7733 2e6f7267 2f313939 392f7868 746d6c22 3e0d0a20 203c6865 61643e0d 0a202020 203c7469 746c653e 53657276 6963653c 2f746974 6c653e0d 0a202020 203c7374 796c653e 424f4459 207b2063 6f6c6f72 3a202330 30303030 303b2062 61636b67 726f756e 642d636f 6c6f723a 20776869 74653b20 666f6e74 2d66616d 696c793a 20566572 64616e61 3b206d61 7267696e 2d6c6566 743a2030 70783b20 6d617267 696e2d74 6f703a20 3070783b 207d2023 636f6e74 656e7420 7b206d61 7267696e 2d6c6566 743a2033 3070783b 20666f6e 742d7369 7a653a20 2e373065 6d3b2070 61646469 6e672d62 6f74746f 6d3a2032 656d3b20 7d20413a 6c696e6b 207b2063 6f6c6f72 3a202333 33363639 393b2066 6f6e742d 77656967 68743a20 626f6c64 3b207465 78742d64 65636f72 6174696f 6e3a2075 6e646572 6c696e65 3b207d20 413a7669 73697465 64207b20 636f6c6f 723a2023 36363939 63633b20 666f6e74 2d776569 6768743a 20626f6c 643b2074 6578742d 6465636f 72617469 6f6e3a20 756e6465 726c696e 653b207d 20413a61 63746976 65207b20 636f6c6f 723a2023 33333636 39393b20 666f6e74 2d776569 6768743a 20626f6c 643b2074 6578742d 6465636f 72617469 6f6e3a20 756e6465 726c696e 653b207d 202e6865 6164696e 6731207b 20626163 6b67726f 756e642d 636f6c6f 723a2023 30303333 36363b20 626f7264 65722d62 6f74746f 6d3a2023 33333636 39392036 70782073 6f6c6964 3b20636f 6c6f723a 20236666 66666666 3b20666f 6e742d66 616d696c 793a2054 61686f6d 613b2066 6f6e742d 73697a65 3a203236 70783b20 666f6e74 2d776569 6768743a 206e6f72 6d616c3b 6d617267 696e3a20 30656d20 30656d20 31307078 202d3230 70783b20 70616464 696e672d 626f7474 6f6d3a20 3870783b 20706164 64696e67 2d6c6566 743a2033 3070783b 70616464 696e672d 746f703a 20313670 783b7d20 70726520 7b20666f 6e742d73 697a653a 736d616c 6c3b2062 61636b67 726f756e 642d636f 6c6f723a 20236535 65356363 3b207061 6464696e 673a2035 70783b20 666f6e74 2d66616d 696c793a 20436f75 72696572 204e6577 3b206d61 7267696e 2d746f70 3a203070 783b2062 6f726465 723a2031 70782023 66306630 65302073 6f6c6964 3b207768 6974652d 73706163 653a2070 72652d77 7261703b 20776869 74652d73 70616365 3a202d70 72652d77 7261703b 20776f72 642d7772 61703a20 62726561 6b2d776f 72643b20 7d207461 626c6520 7b20626f 72646572 2d636f6c 6c617073 653a2063 6f6c6c61 7073653b 20626f72 6465722d 73706163 696e673a 20307078 3b20666f 6e742d66 616d696c 793a2056 65726461 6e613b7d 20746162 6c652074 68207b20 626f7264 65722d72 69676874 3a203270 78207768 69746520 736f6c69 643b2062 6f726465 722d626f 74746f6d 3a203270 78207768 69746520 736f6c69 643b2066 6f6e742d 77656967 68743a20 626f6c64 3b206261 636b6772 6f756e64 2d636f6c 6f723a20 23636563 6639633b 7d207461 626c6520 7464207b 20626f72 6465722d 72696768 743a2032 70782077 68697465 20736f6c 69643b20 626f7264 65722d62 6f74746f 6d3a2032 70782077 68697465 20736f6c 69643b20 6261636b 67726f75 6e642d63 6f6c6f72 3a202365 35653563 633b7d3c 2f737479 6c653e0d 0a20203c 2f686561 643e0d0a 20203c62 6f64793e 0d0a2020 20203c64 69762069 643d2263 6f6e7465 6e74223e 0d0a2020 20202020 3c702063 6c617373 3d226865 6164696e 6731223e 53657276 6963653c 2f703e0d 0a202020 2020203c 703e4d65 74686f64 206e6f74 20616c6c 6f776564 2e3c2f70 3e0d0a20 2020203c 2f646976 3e0d0a20 203c2f62 6f64793e 0d0a3c2f 68746d6c 3e>
To return a data stream, you need to return your data as a Stream, not as a string. If you return the data as a string, it will be returned as a JSON-encoded representation of that string.
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.WrappedRequest,
UriTemplate = "/picture/{path}")]
System.IO.Stream GetPictureMethod(string path);
public System.IO.Stream GetPictureMethod(string path)
{
path = "(test path here)";
byte[] picData = System.IO.File.ReadAllBytes(path);
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg"; // or the correct type
return new System.IO.MemoryStream(picData);
}
By the way, you should use [WebGet] instead of [WebInvoke(Method = "GET")].
More data: the NSData which you have is equivalent to this code below. You should first try hitting the WCF service with a tool such as a browser or Fiddler to check whether you get the data back. Only then you should try consuming it from NSData.
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Service</title>
<style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style>
</head>
<body>
<div id="content">
<p class="heading1">Service</p>
<p>Method not allowed.</p>
</div>
</body>
</html>

Maintain the SelectedNodeStyle of treeview while navigating Treeview on masterpage

Hi all i applied selectednodestyle for a treeview which works fine when i am not navigating. But on navigating i am unable to see the applied color for the treeview selected node. Here is my design in master page
<asp:TreeView ID="TreeViewCategories" runat="server" ExpandDepth="0" Style="min-height: 200px;
max-height: 500px;" NodeIndent="0" LeafNodeStyle-CssClass="LeafNodesStyle" CssClass="TreeView"
NodeStyle-CssClass="NodeStyle" ParentNodeStyle-CssClass="ParentNodeStyle" RootNodeStyle-CssClass="RootNodeStyle"
SelectedNodeStyle-CssClass="SelectedNodeStyle" LeafNodeStyle-Width="100%" NodeStyle-Width="100%"
ParentNodeStyle-Width="100%" RootNodeStyle-Width="100%" Font-Size="12pt">
<Nodes>
<asp:TreeNode Text="All Items" NavigateUrl="~/Default3.aspx" SelectAction="SelectExpand"
Value="All Items">
<asp:TreeNode Text="Hello" Value="Hello"></asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
This is my css
<style type="text/css">
.TreeView
{
border-bottom: 1px dotted #B2B2B2 !important;
}
.TreeView div
{
margin-left: 5px;
}
.TreeView table
{
border-top: 1px dotted #B2B2B2 !important;
}
.TreeView div table
{
border-bottom: none !important;
border-top: none !important;
}
.TreeView table td
{
padding: 2px 0;
}
.LeafNodesStyle
{
}
.RootNodeStyle
{
}
/* ALL ELEMENTS */.NodeStyle
{
}
.ParentNodeStyle
{
/*background:yellow;*/
}
.SelectedNodeStyle
{
font-weight: bold;
color: #6799D1;
display: block;
padding: 2px 0 2px 3px;
}
</style>
But i am unable to apply the color for selected node after navigating to a page can any one help me
your codes are working Ok so is the CSS. if you will notice the text of the one youve selected became bold.
If your basis is in change of color of the text, there is some problem. If you would look at the source code, not only SelectedNodeStyle css style is applied on the item, but these also
NodeStyle FooterContent_TreeViewCategories_2 LeafNodesStyle FooterContent_TreeViewCategories_8 SelectedNodeStyle FooterContent_TreeViewCategories_10
so I suggest putting some !important on your css color for the change in color to take effect.
.SelectedNodeStyle
{
font-weight: bold;
color: #6799D1 !important;
display: block;
padding: 2px 0 2px 3px;
}

Categories

Resources