The base Element object in dart:html has a property elements which is an implementation of List<E>. The add method appends elements to the DOM. I would like to prepend or insert an element into the DOM. None of the insert methods in ElementList are implemented. How can I do this?
So lets say you have this HTML snippet:
<body>
<div id='test'></div>
</body>
You can do this to insert an element before the div:
// Get the sibling that we want to insert before.
final el = query('#test');
// From the parent element, we call insertBefore, referencing the
// sibling that we want to insert the new element before.
document.body.insertBefore(new Element.tag('p'), el);
Now your HTML will be:
<body>
<p></p>
<div id='test'></div>
</body>
In addition to John's Answer. You can also use the syntax:
final el = query('#test');
// If you want an element
el.insertAdjacentElement('beforebegin', new Element.tag('p'));
// If you want to insert HTML tags
el.insertAdjacentHTML('beforebegin', '<p>Hello</p>');
// Insert just text
el.insertAdjacentText('beforebegin', 'Hello there');
You can use positional arguments: beforebegin, afterbegin, beforeend, afterend Which will place it before the beginning of the other element, just inside the beginning of the element, just inside the end of the element, or just outside the end of the element, respectively.
Related
I have a web component which leverages some template repeaters. I noticed that even though the item is attached to the page, it isnt rendered. Since it isnt rendered, the template repeater isnt processed.
Is there any sort of on render, or paint function which is called when the component is actually drawn?
I was doing some component queries which were related to the template dom-repeater content. It doesnt find the classes because it doesnt stamp out the info.
How would i go about the "on Render" ?
Right now i was doing something like:
#property List<Map> knownList = [{"name":"Bob"},{"name":"Jake"},{"name":"Larry"}];
attached(){
toBinaryString();
}
String toBinaryString(){
String binaryString = "";
document.getElementsByClassName("selected").forEach((HtmlElement ele){
print("Fired");
binaryString += "1";
});
print("Result diagnostics/ Length: ${binaryString.length} isValid: ${binaryString.length==(CONST_PER_DAY * 7)}");
return binaryString;
}
my Html is something simple like:
<dom-module id="my-test">
<template>
<template is="dom-repeater" items="{{knownList}}">
<div class="selected">{{item.name}}</div>
</template>
</template>
</dom-module>
and attached is fired when the page loads, as this item is in fact added to the page, but because of it not being rendered I cant do any sort of dom related processing.
Is there something I am doing wrong?
https://github.com/dart-lang/polymer-dart/wiki/registration-and-lifecycle#ready-callback-and-local-dom-initialization
The ready callback is called when an element's local DOM is ready.
It is called after the element's template has been stamped and all elements inside the element's local DOM have been configured (with values bound from parents, deserialized attributes, or else default values) and had their ready method called.
I have a div which allow contenteditable.
<div contenteditable="true">
</div>
I would like to filter all html tags copy & pasted from user (except ), how this could achieve in Dart code?
myDiv.onPaste.listen((e) {
// do filtering here and then insert the result imperatively
// one or both of the following might be necessary to prevent
// the default paste behavior happening as well.
e.preventDefault();
e.stopPropagation();
});
See also:
- https://stackoverflow.com/a/3933677/217408
- Detect a paste event in a contenteditable
my function look like this:
void write (String message) {
query("#status").innerHTML = message;
query("#head").text = "Click me!";
}
all of them catch id and show text to web browser.
In general browser document model, innerHtml refers to all the internal HTML, whereas text just refers to the text values of the elements. innerHtml is often used by dhtml and Ajax to change a div, where text would be just to set the text value of a single element.
This is more explicitly illustrated when getting, rather than setting, i.e.
e.g. Given:
<div id="idName">
Text in the Div
<p id="anotherId">Inner P</p>
</div>
innerHtml returns
Text in the Div
<p id="anotherId">Inner P</p>
text returns :
Text in the Div
Inner P
If you try this:
String message = """<form method="get" action="#ref"><input name="first_name"/></p><input type="submit" value="Send"/></form>""";
write (message);
then you will appreciate the difference.
The innerHTML should operate an injection of code (an html form in the example) into the HTML 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
I am using the sortable demo in jquery-ui. I am trying to add an item to the sortable list at runtime, like:
<ul id='panelSortable'>
</ul>
$("#panelSortable").sortable({
distance: '15'
});
function generatePanel() {
var html = "<li>";
html += "<p>hello!</p>";
html += "</li>";
return html;
}
function addPanel() {
var element = $(generatePanel());
element.appendTo("#panelSortable");
element.sortable();
element.disableSelection();
}
the new element gets added to the sortable list ok. I can strangely drag the "p" element though, separately from the parent "li" element (which is also draggable). I want to restrict dragging to just the "li" element.
If I just declare a few "li" items in the page, they all work the normal way, where you can only drag the "li" items, not their "p" child:
<ul id='panelSortable'>
<li><p>I work normally!</p></li>
</ul>
so I must be messing up the calls here to make the "p" elements also draggable?
Thanks
Argh ok you don't need to call sortable() on element, that should just be done on the parent "ul" item it looks like. If you remove that and just add the element as shown above, sorting is enabled on it just fine.
Thanks