how to add hyperlink to text in dart - dart

I am trying to add an anchor tag to the message I want to display but I am getting an error message. Am I not doing this right?
String browserRequirementsUrl = "https://test.testing.com";
var someText = new ParagraphElement()
..innerHtml = "Link can be found <a href=${url}>here</a>[1].";
But I get an error message saying
html_dart2js.dart:3614 Removing disallowed attribute <A href="https://test.testing.com">
Any suggestions, how I can do this?

By default in dart:html, for security purposes, this is disallowed.
You can use the .setInnerHtml method:
..setInnerHtml("Link can be found...", treeSanitizer: NodeTreeSanitizer.trusted);
Note that this can potentially be insecure (i.e. inject <script> tags and such), so you can always create a custom sanitizer or validator to only allow a subset of HTML tags (such as <a>).

Related

How to display URLs from an HTML string in Angular Dart?

I'm trying to get Angular Dart to display a link in a tag from an HTML string.
At first, I tried to just set the inner HTML of the container to be the HTML string, but that didn't work, so I then I tried to use Dart's DomSanitizationService class, but that also doesn't seem to work.
What I have so far is
Dart:
class SomeComponent {
final DomSanitizationService sanitizer;
SafeUrl some_url;
SomeComponent(this.sanitizer) {
some_url = this.sanitizer.bypassSecurityTrustUrl('https://www.google.com');
}
String html_string = '''
<a [href]="some_url">Hi</a>
''';
String get Text => html_string;
}
HTML:
<div [innerHTML]="Text"></div>
The error I'm getting is Removing disallowed attribute <A [href]="some_url">. The text Hi seems to show, but there is no link anymore.
Just as you bypassed URL sanitanization, you have to bypass HTML sanitanization as well using bypassSecurityTrustHtml to return markup.
https://angular.io/api/platform-browser/DomSanitizer#bypassSecurityTrustHtml

In Dart must element.html(" ") occur within the context of a body tag?

TableElement table = new TableElement();
document.body.children.add(table);
table.children.add(new Element.html("<tr><td>test</td></tr>"));
The above code snippet returns the error: "Uncaught Bad state: No element". Reading the API docs on the DartLang website this is because when using the Element.html(" ") constructor, "the HTML fragment is parsed as if it occurred within the context of a <body> tag". Therefore, we would need to write:
new Element.html("<table><tr><td>test</td></tr></table>")
However, reading "dart in action" Chris Buckett uses this exact same syntax when building a table.
Has something changed or am I misunderstanding something?
This is a browser "feature". A <tr> element can't exist on its own and is just dropped from the HTML when it finds one.
The docs to new Element.html() says
Creates an HTML element from a valid fragment of HTML.
Alternatively you can use
table.append(
new TableRowElement()..append(new TableCellElement()..text = 'test'));
DartPad example

How to remove a-tag's title param in tt_news using typoscript

The standard output of a tt_news list view hyperlink is like:
News Headline
How to remove the title tag from the standard output with typoscript ? So that you get:
News Headline
I' am looking for a typoscript something like:
plugin.tt_news.displayList.linkParams.ATagParams >
Info: The template (sub)marker which includes the hyperlink is called:
<!-- ###LINK_ITEM### -->
... some Headline ...
<!-- ###LINK_ITEM### -->
There is a hook for this.
$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['tt_news']['getSingleViewLinkHook']
You will have to use it in an own extension.
Your hook consuming class will have to implement the following method
function processSingleViewLink(&$linkWrap, $url, &$params, &$pObj) {
// modify $linkWrap to remove title="" using regex
[...]
}
See here for reference how to consume hooks in TYPO3: http://typo3.org/documentation/article/how-to-use-existing-hooks-in-your-own-extension/
It's
plugin.tt_news.displayList.linkTitleField =
However title will be there, it will not be filled.
If you want to remove the empty title part as well you need to dive into the plugin to remove it.

html node parsing with ASP classic

I stucked a day's trying to find a answer: is there a possibility with classic ASP, using MSXML2.ServerXMLHTTP.6.0 - to parse html code and extract a content of a HTML node by gived ID? For example:
remote html file:
<html>
.....
<div id="description">
some important notes here
</div>
.....
</html>
asp code
<%
...
Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
objHTTP.Open "GET", url_of_remote_html, False
objHTTP.Send
...
%>
Now - i read a lot of docs, that there is a possibility to access HTML as source (objHTTP.responseText) and as structure (objHTTP.responseXML). But how in a world i can use that XML response to access content of that div? I read and try so many examples, but can not find anything clear that I can solve that.
First up, perform the GET request as in your original code snippet:
Set http = CreateObject("MSXML2.ServerXMLHTTP.6.0")
http.Open "GET", url_of_remote_html, False
http.Send
Next, create a regular expression object and set the pattern to match the inner html of an element with the desired id:
Set regEx = New RegExp
regEx.Pattern = "<div id=""description"">(.*?)</div>"
regEx.Global = True
Lastly, pull out the content from the first submatch within the first match:
On Error Resume Next
contents = regEx.Execute(http.responseText)(0).Submatches(0)
On Error Goto 0
If anything goes wrong and for example the matching element isn't found in the document, contents will be Null. If all went to plan contents should hold the data you're looking for.

BeautifulSoup: parse only part of the page

I want to parse a part of html page, say
my_string = """
<p>Some text. Some text. Some text. Some text. Some text. Some text.
Link1
Link2
</p>
<img src="image.png" />
<p>One more paragraph</p>
"""
I pass this string to BeautifulSoup:
soup = BeautifulSoup(my_string)
# add rel="nofollow" to <a> tags
# return comment to the template
But during parsing BeautifulSoup adds <html>,<head> and <body> tags (if using lxml or html5lib parsers), and I don't need those in my code. The only way I've found up to now to avoid this is to use html.parser.
I wonder if there is a way to get rid of redundant tags using lxml - the quickest parser.
UPDATE
Originally my question was asked incorrectly. Now I removed <div> wrapper from my example, since common user does not use this tag. For this reason we cannot use .extract() method to get rid of <html>, <head> and <body> tags.
Use
soup.body.renderContents()
lxml will always add those tags, but you can use Tag.extract() to remove your <div> tag from inside them:
comment = soup.body.div.extract()
I could solve the problem using .contents property:
try:
children = soup.body.contents
string = ''
for child in children:
string += str(item)
return string
except AttributeError:
return str(soup)
I think that ''.join(soup.body.contents) would be more neat list to string converting, but this does not work and I get
TypeError: sequence item 0: expected string, Tag found

Resources