Manipulate Rebex.MailMessage Html String - string

I am currently working on a message watcher service we have and my task is to simply embed message details into the message body.
I have tried using a string builder for this however i have found that my message body is a html based string.
I'm wanting to know if there is a way i can add the values i want to add at a certain point in this html string?
Below is a section of the html string i want to manipulate. My text needs to be inserted directly after the body tag.
<body lang=EN-GB link=blue vlink=purple>
<div class=WordSection1>
<p class=MsoNormal>
<span style='font-size:10.0pt;font-family:"Century Gothic","sans-serif";color:black'>Another test for AppendLine();<o:p></o:p>
</span>
</p>
Here is how i was trying to do it:
StringBuilder sb = new StringBuilder();
sb.Append("From: ");
sb.Append(message.From.ToString());
sb.AppendLine();
sb.Append("Sent: ");
sb.Append(message.Date.ToString());
sb.AppendLine();
sb.Append("To: ");
sb.Append(message.To.ToString());
sb.AppendLine();
sb.Append("Subject: ");
sb.Append(message.Subject);
sb.AppendLine();
sb.Append(message.BodyHtml);
Unfortunately this just printed my From, Sent, To, Subject values onto one line and then output the html section.
If any more information is needed please let me know and i will provide it.

Instead of appending the body HTML, try this regex replace:
StringBuilder sb = new StringBuilder();
sb.Append("From: ");
sb.Append(message.From.ToString());
...
sb.Append("Subject: ");
sb.Append(message.Subject);
sb.AppendLine();
// not guaranteed to work with arbitrary HTML strings
Regex regex = new Regex(#"(<body[^>]*>)", RegexOptions.IgnoreCase);
message.BodyHtml = regex.Replace(message.BodyHtml, "$1\r\n" + sb.ToString());
Disclaimer: Please be advised that processing HTML with regular expressions is generally regarded as a bad idea. Although the code above might work in 98% of cases, regular expressions are not up to the task of parsing arbitrary HTML. HTML is too sophisticated for regex. If you need to process arbitrary HTML bodies (not just the one above), I strongly recommend using a proper HTML parser such as HTML Agility Pack - even for seemingly-simple operations such as inserting a text after the body tag.

Related

Is this the right way to implement my own filter in twig? (I want *minimal* whitespace between tags, not *no* whitespace between tags)

I know that apply spaceless "isn't about optimisation", in Symfony's words. But dammit, I dislike extraneous whitespace from being in my served files.
So I'm keen to use it.
But... I don't like how it reduces
<span>1</span>
<span>2</span>
to
<span>1</span><span>2</span>
As that results in the browser displaying 12, rather than 1 2.
In my mind whitespace between tags should be reduced to a single space, not to nothing.
<span>1</span>
<span>2</span>
->
<span>1</span> <span>2</span>
So I thought I'd make a custom filter, minimizeWhitespace, and wrap my templates with <% apply minimizeWhitespace %>
This is what I came up with:
function minimizeWhitespace($s)
{
return new \Twig\Markup(preg_replace('/\s+/', ' ', $s->__toString()), 'UTF-8');
}
$TWIG_env->addFilter(
new \Twig\TwigFilter('minimizeWhitespace', 'minimizeWhitespace')
);
This feels messy though - I'm taking their \Twig\Markup object, converting it to a string, running my regexp on it, and then creating a new \Twig\Markup object to return.
Is there a better way?

What is the difference between 'page_source' and 'find_element_by_tag_name("body").text'?

Trying to find whether a text is present on UI login page (web-page).
I could verify it by 'driver.page_source()' and driver.find_element_by_tag_name("body").text
driver.page_source()
text = "abcd"
page_source = driver.execute_script("return document.body.innerHTML;")
if text in page_source:
return True
else:
return False
driver.find_element_by_tag_name("body").text
text = "abcd"
value = text in self.browser.find_element_by_tag_name("body").text
if value:
return True
else:
return False
What's the difference between method1 and method2 ?
Which one is preferred to do the required task ?
Which is faster ?
Or anySelenium-UI methods to be used ?
Any help would be appreciated. Looking for valuable inputs.
Any idea on this ? Any help here ?
Page source will give all the text including HTML tags, styles etc. as you have written yourself in execute script to return the innerHTML. So, all HTML code will be returned which obviously will contain the text too. You can also get the whole html with selenium too instead of using JavaScript executor by browser.page_source.
On the other hand, browser.find_element_by_tag_name("body").text will return all the text you see on the page without html tags.
To me, the 2nd method should be preferred and faster because you will have string of smaller length(without un-necessary html tags) and the actual text you are interested in.

changing the font color in a computed field using javascript

How to change the font color of Hello alone in "Hello World" using javascript/some other method?
I tried the following code,
var s= session.getCommonUserName()
s.fontcolor("green")
"Hello"+" "+ s.toUpperCase()
where i tried to change just the color of the username alone. But it failed.
I wouldn't bother to send down unformatted HTML to the client and then let the client do the JavaScript work. You create a computed field and give it the data type HTML (that keeps HTML you create intact) and use SSJS. So no JS needs to execute at the client side:
var cu = session.getCommonUserName();
return "Hello"+" <span style=\"color : green\">"+ cu.toUpperCase()+"</span>";
Don't forget to cross your t, dot your i and finish a statement with a semicolon :-)
If you want to do it with client java script, then you must do something like this:
dojo.style("html_element_id", "color", "green");
So in your case you can have have something like:
<p><span id="span1">Hello</span> World.</p>
Or you can do it directly if you don't need to change it with CJS:
<p><span style="color:green">Hello</span> World</p>
one way to do it is to wrap your 'hello' in a html span and then change the color of that span.
<span id='myspan'>hello</span> world
javascript code:
document.getElementById('myspan').style.color='green';
Went old school on this one...
Say you want to put your formatted text in a div
<div id="test">
</div>
Then you need the following javascript to do so:
div = document.getElementById("test");
hello = document.createElement("span");
hello.innerHTML = "Hello"
hello.style.color = "green";
div.appendChild(hello);
div.appendChild(document.createTextNode(" world!"));

Detect a change in a rich text field's value in SPItemEventReceiver?

I currently have an Event Receiver that is attached to a custom list. My current requirement is to implement column level security for a Rich Text field (Multiple lines of text with enhanced rich text).
According to this post[webarchive], I can get the field's before and after values like so:
object oBefore = properties.ListItem[f.InternalName];
object oAfter = properties.AfterProperties[f.InternalName];
The problem is that I'm running to issues comparing these two values, which lead to false positives (code is detecting a change when there wasn't one).
Exhibit A: Using ToString on both objects
oBefore.ToString()
<div class=ExternalClass271E860C95FF42C6902BE21043F01572>
<p class=MsoNormal style="margin:0in 0in 0pt">Text.
</div>
oAfter.ToString()
<DIV class=ExternalClass271E860C95FF42C6902BE21043F01572>
<P class=MsoNormal style="MARGIN: 0in 0in 0pt">Text.
</DIV>
Problems?
HTML tags are capitalized
Random spaces (see the additional space after margin:)
Using GetFieldValueForEdit or GetFieldValueAsHTML seem to result in the same values.
"OK," you say, so lets just compare the plain text values.
Exhibit B: Using GetFieldValueAsText
Fortunately, this method strips all of the HTML tags out of the value and only plain text is displayed. However, using this method led me to discover additional issues with whitespace characters:
In the before value:
Sometimes there are additional newline characters.
Sometimes spaces are displayed as non-breaking spaces (ASCII char code 160)
Question:
How can I detect if the user changed a rich text field in an event receiver?
[Ideal] Detect any change to HTML or text or white space
[Acceptable] Detect changes to text or white space
[Not so good] Detect changes to text characters only (strip all non-alphanumeric characters)
What happens if you set the ListItem field with the new value and read it back out? Does that give the same formatting?
object oBefore = properties.ListItem[f.InternalName];
properties.ListItem[f.InternalName] = properties.AfterProperties[f.InternalName]
object oAfter = properties.ListItem[f.InternalName];
//dont update
properties.ListItem[f.InternalName] = oBefore;
I would probably try something between choices 2 and 3:
bool changed =
valueAsTextBefore != valueAsTextAfter ||
0 != string.Compare(
oBefore.ToString().Replace(" ", ""),
oAfter.ToString().Replace(" ", ""),
true);
The left half checks if the text (including case) has changed while the right half checks if the tags or attributes have changed. Very kludgy, but should fit your case.
The only other thing I can think of is to run an XML transform on the HTML in order to standardize on case and spacing. But not only does that seem like overkill, but it assumes the HTML will always be well formed.
I'm currently testing a combination approach: GetFieldValueAsText and then stripping out all characters except alphanumeric/punctuation:
static string GetRichTextValue(string value)
{
if (null == value)
{
return string.Empty;
}
StringBuilder sb = new StringBuilder(value.Length);
foreach (char c in value)
{
if (char.IsLetterOrDigit(c) || char.IsPunctuation(c))
{
sb.Append(c);
}
}
return sb.ToString();
}
This only detects changes to the text of a rich text field but seems to work consistently.

Drupal 6: How do you print Taxonomy Terms as a CSS Body Class?

In Drupal 6, how do you print a taxonomy term as a CSS body class?
I have found this snippet that lets you print almost every aspect of Drupal content as a body class, but it doesn't include taxonomy terms:
http://www.davidnewkerk.com/book/122
Being able to print taxonomy terms as a body class is essential for theming processes, so I am surprised that a solution is not readily available.
Check what variables are passed to the page template by either doing print_r($vars) or dpm($vars) in your page pre-process function or using the http://drupal.org/project/devel_themer module. The usage of dpm require you to install the devel module.
You will find that some themes will pass $taxonomy as a variable to page.tpl.php . If that is not the case you can find the taxonomy terms in the $node variable which is also available in the page.tpl.php in some themes.
(The above holds true for my fusion based theme acquia marina http://drupal.org/project/acquia_marina ). Once you have these taxonomy terms available you can easily print them out in your body classes.
After much hard work, I found a very easy way to do this.
On Drupal Snippets, there is a snippet that lets you print out the taxonomy terms applied to each page as text.
The only problem is that the snippet will print any spaces or punctuation that are in the taxonmy term, which is no good for body classess.
However, by adding a str_replace command, you can strip out all the spaces and punctuation.
I'm sure there are other people who wants to print taxonmy terms as body classes, so to save them the bother, here is the code that I used with the str_replace command added.
Put the following in template.php:
function getTerm($label, $vid, $link) {
$node = node_load(array('nid'=>arg(1)));
foreach((array)$node->taxonomy as $term){
if ($term->vid == $vid){
if ($link){
$link_set[] = l($term->name, taxonomy_term_path($term));
} else {
$link_set[] = $term->name;
}
}
}
if (!empty($link_set)){
$label = ($label) ? "<strong>$label </strong>" : "";
$link_set = $label.implode(', ', $link_set);
}
$link_set = str_replace(' ', '_', $link_set);
$link_set = str_replace('&', 'and', $link_set);
$link_set = strtolower($link_set);
return $link_set;
}
Put the following in Page.tpl.php:
<body class="taxonomy-<? print getTerm(false, 1, false);?>">
I hope this helps anyone who has the same problem.
Extra tips:
(1)In the code I have posted, the only punctuation that is striped out is the ampersand (i.e. '&').
If you have other punctuation to strip out use the following:
$link_set = str_replace('INSET_PUNCTUATION_HERE', 'INSERT_REPLACEMENT_HERE', $link_set);
Place this command under the other $link_set lines in the code I have posted for template.php.
(2) In the page.tpl.php code I have posted, the "taxonomy-" part places the words taxonomy and a dash before each body class term. You can edit this as you wish to get the results your require.

Resources