I'm trying to parse an html page:
url: https://dzone.com/articles/2-entity-framework-alternatives-or-give-me-data
html: read url
parse html [
to {<h1 class="article-title" itemprop="headline">}
thru {<h1 class="article-title" itemprop="headli
ne">}copy title to {</h1>}
]
probe title
Can't see why it doesn't work since I get error "title has no value"
I assume that you're using Rebol/view since the free versions don't do https though rebol3 does.
If you want to see if something is working you should look at the return value of the parse, and you'll see it's false which means that there's a problem with your parse rule. Anyway, this works for me though the quotes around the tags are not necessary as < and > are both string delimiters.
>> parse html [
thru <h1 class="article-title" itemprop="headline">
thru <h1 class="article-title" itemprop="headline">
copy title to </h1> to end
]
== true
>> trim/head/tail title
== "2 Entity Framework Alternatives (or Give Me Data!)"
It does not work most probably because the first to stops before the matched string, so that thru starts at the beginning of the first occurence of <h1 ...>, not at the second as you might have expected. You need to skip the first occurrence, before trying to search for the second one. You can achieve that using two thru rules as shown in another answer, or just repeat the rule twice to avoid duplicating it:
parse html [
2 thru <h1 class="article-title" itemprop="headline">
copy title to </h1> to end
]
Notice the final to end rule, which will make parse return true if your rules succeed in reaching the end. The to end rule is a placeholder rule, as you do not care about what is following </h1>, but want to reach the end of the input anyway.
EDIT: Testing the code you submitted works fine from here unchanged. The editing of your question has actually fixed the cause of the error. I can reproduce your issue with your original code.
Related
ASP.NET Core 3.1
I have the following code in my View:
<span asp-validation-for="NewPassword" class="text-danger"></span>
If I type in a password, such as 'a', I get multiple errors back. I want to display them in that span ONE PER LINE.
In my Controller I have the following:
var descriptions = string.Empty;
foreach (var error in errors)
descriptions += error.Description + "<br />";
ModelState.AddModelError("NewPassword", descriptions);
But this does not translate the "br" tags and they get displayed as if they were part of one very long single error message.
Note: Adding them one at a time into the ModelState simply overwrites the previous, leaving only the last one displayed.
Instead of "br", i've tried HTMLString.NewLine and Environment.NewLine but none of these work. I could write a loop in my View, but is there a preferred way in ASP.NET Core?
So my question is: How can I put a new line between each description?
Instead of "br", i've tried HTMLString.NewLine and Environment.NewLine but none of these work.
That's because <br/> will be escaped by default. If you wish, you could use #Html.Raw() to generate the raw content without escaping the dangerous characters. But I don't think it's a good idea: there will be a high risk of XSS if you output a raw string directly.
By default, \r\n won't be displayed in browser. You need a CSS snippet to show a new line.
A much better way is to use the CSS to control the display style: add a style for the validation field in your *.css file:
span.field-validation-error{
white-space: pre-line;
}
And change the csharp code to use a \n as separator:
foreach (var error in errors)
descriptions += error.Description + "\n";
Demo :
On a page I'm working on, I'm trying to get the title of a blog post to read as Sleep—It's frustratingly Important., where there is an em dash between Sleep and It's. Previously I had this hard-coded as
<h2 class="post-title"><router-link to="/posts/1">Sleep—It's frustratingly Important. </router-link></h2>
which was working fine. I want to generalize this, so in my Rails back end I have the post title stored as Sleep—It's frustratingly Important.. But when I insert the post title via interpolation
<h2 class="post-title"><router-link to="/posts/1"> {{ this.posts[0].title }} </router-link></h2>
It doesn't insert the em dash. It displays as "Sleep—It's frustratingly Important."
Why isn't the html recognizing that — should be changed to an em dash? And how do I fix this issue?
Content between {{ and }} is expected to be an expression that resolves as plain text. It gets converted into a text node. It does not get treated as HTML.
If you want an em-dash, then use a real em-dash: —.
You could also look at dangerouslySetInnerHTML, but it is named that way for a reason.
Disclaimer: I'm brand new to Elm
I'm fiddling around with the online Elm editor and I've run into an issue. I can't find a way to get certain special characters (copyright, trademark, etc.) to show up. I tried:
import Html exposing (text)
main =
text "©"
All that showed up was the actual text ©. I also tried to use the unicode character for it \u00A9 but that ended up giving me a syntax error:
(line 1, column 16): unexpected "u" expecting space, "&" or escape code
The only way I found was to actually go to someone's website and copy/paste their copyright symbol into my app:
import Html exposing (text)
main =
text "©"
This works, but I would much rather be able to type these characters out quickly instead of having to hunt down the actual symbols on other websites. Is there a preferred/recommended method of getting non-escaped text when returning HTML in Elm?
Edit:
Specifically for Mac:
option+g gives you ©
option+2 gives you ™
option+r gives you ®
All tested in the online editor and they worked. This still doesn't attack the core issue, but it's just something nice to note for these specific special characters.
Why this is (intentionally) not so easy
The "makers" of Elm are understandably reluctant to give us a way to insert "special" characters into HTML text. Two main reasons:
This would open a "text injection" hole where a malicious user could insert any HTML tags, even JavaScript code, into a Web page. Imagine if you could do that in a forum site like Stack Overflow: you could trick anyone reading your contribution into executing code of your choosing in their browser.
Elm works hard to produce optimal DOM updates. This only works with the content of tags that Elm is aware of, not with text that happens to contain tags. When people insert text containing HTML tags in an Elm program, there end up being parts of the DOM that can't be optimized.
How it's possible anyway
That said, the Elm user community has found a loophole that affords a workaround. For the reasons above, it's not recommended, especially not if your text is non-constant, i.e. comes from a source outside your program. Still, people will be wanting to do this anyway so I'm going to document it to save others the trouble I had digging everything up and getting it working:
If you don't already have it,
import Json.Encode exposing (string)
This is in package elm-lang/core so it should already be in your dependencies.
Similarly,
import Html.Attributes exposing (property)
Finally, create a tag having a property "innerHTML" and a JSON-Value representation of your text, e.g.:
span [ property "innerHTML" (string " ") ] []
I found, that there is a better solution:
you can convert special characters from Unicode to char, and then create a string from char:
resString = String.fromChar (Char.fromCode 187)
You can use directly the unicode escape code in Elm strings and chars:
We have a util module containing all our special chars like:
module Utils.SpecialChars exposing (noBreakSpace)
noBreakSpace : Char
noBreakSpace = '\x00A0'
Which can be used as:
let
emptyLabel = String.fromChar noBreakSpace
in
label []
[ span [ ] [ text emptyLabel ]
]
This will render a <span> </span>
I recently created an Elm package that solves this. If you use text' "©" it'll render the copyright symbol © instead of the escape code. Also works with "©" and "©". Hope this helps!
You don't need to hunt the symbols, you can get them from a list like this one.
If it's too bothersome to copy & paste, you can also create a helper function that you can use with your escaped characters like this:
import Html exposing (..)
import String
htmlDecode str =
let
replace (s1, s2) src= String.join s2 <| String.split s1 src
chrmap =
[ ("®", "®")
, ("©", "©" )
]
in
List.foldl replace str chrmap
main = text <| htmlDecode "hello ©"
I have issues getting the text from an element in protractor. For other elements of the page it works as expected, just not for this one :/
<p class="error theme-info-i ng-binding ng-scope" ng-if="firstFormError && form.$invalid" ng-click="goToErrorField(firstFormError)">
<span class="emphasize ng-binding">User ID</span> (The user ID is required.)
</p>
I can locate both elements without problems using by.className, and getInnerHtml/getOuterHtml works as expected. However getText returns an empty string for both.
Found the reason ... its a two-step registration where the first step has the same notification area and just gets hidden. For reasons beyond my comprehension the devs update both notification areas (not just the one on the current page), so inner/outerHtml just seemingly returned the "correct content" and because the first area was hidden, getText returned empty as by spec.
I think I'm gonna file some internal bug report now wtf we are doing with those notifications ;)
You can try
var firstName = element(by.model('firstName'))
expect(firstName.getAttribute('value'))
This gives you the value of the input box.
Had the same problem.
expect(element(By.css('span.emphasize.ng-binding')).getAttribute('textContent'))...
Seems to work just fine for me (this of course if you have only one span with those classes or is the first one. else you need to be more specific. But anyway if you still need to solve the problem try with .getAttribute('textContent') )
I'm changing my liferay velocity template by adding new field for structure. e.g. 'heading1' and then adding this new field to template:
<h1>Heading is: $heading1.data</h1>
But if the structure field is not yet filled the outcome is:
Heading is: $heading1.data
So I thought I can fix this by:
#if($heading1.data!="")<h1>Heading is: $heading1.data</h1>#end
But the outcome is still:
Heading is: $heading1.data
If I open the web content and just publish it then the outcome is right, it doesn't show anything, but I don't want do find every similar web content and start publish them manually.
So is there a way how to check if the heading is just not filled?
Thanks.
You can use a silent reference to tell Velocity not to display an empty reference:
<h1>Heading is: $!heading1.data</h1>
or you can directly test whether its content does exist:
#if($headings1.data) <h1>Heading is: $heading1.data</h1> #end
Up to Velocity 1.7, this test will be false if the reference is null or uninitialized, but will still be true if the reference contains an empty string. Empty strings will also evaluate to false in next versions.