3

I need help with one problem.

I have an Element called e.g. myElement. I have also some strings and elements (generally nodes), which I want to add as myElement's children (all of the same level). My idea looks like this piece of code:

DivElement div1=new DivElement();
// ... some parameters of div1
String string1="Hello";
DivElement div2=new DivElement();
// ... some parameters of div2
String string2="World!";

DivElement myElement=new DivElement();
myElement.nodes.addAll([div1,string1,div2,string2]);

But this don't work, because Strings are not Nodes. How could I make a Node from a String?

aleskva
  • 1,644
  • 2
  • 21
  • 40

2 Answers2

3

see also https://stackoverflow.com/a/22065859/217408

You can create an element by parsing HTML text:

new Element.html("YOUR HTML STRING HERE");

see Dart: Up and Running CH03

EDIT
You may need to pass a NodeValidator to get the entire text rendered like:

NodeValidator nodeValidator = new NodeValidatorBuilder()
    ..allowTextElements();
    // ..allowHtml5()
    // ..allowElement('Button', attributes: ['ng-disabled']);

new Element.html("YOUR HTML STRING HERE", validator: nodeValidator);

If you run the application you get log messages like "element/attribute xxx removed from ..." if some of the text was not accepted by the NodeValidator.

Community
  • 1
  • 1
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • I don't need Element, but Node (TEXT_NODE). Is this really correct: `var myNode=new Element.html("Hello World! I'm just string without any HTML elements and I want to be a Text Node in the DOM!");myElement.children.add(myNode);`? – aleskva Dec 06 '14 at 18:49
  • 1
    Then you probably just want to set the `.text` attribute of an element like `querySelector('#someId').text = "Hello World!";`. – Günter Zöchbauer Dec 06 '14 at 18:52
  • Well, it is much more complicated. I have some Element into which I want to add either just string or some element or more elements in a list. `children.addAll(List)` function adds list of nodes, so I want all three types to convert into node. Element (or list of elements) is a child of node, but string not... – aleskva Dec 06 '14 at 20:46
  • My problem is, that I want use the function `children.addAll(List)` and pass to it the list looking like this: `[myElement1,myStringMadeIntoNodeSomehow,myElement2]` – aleskva Dec 06 '14 at 20:51
  • 1
    `Element.children` is a `List`. `new Text('xxx');` creates a `Text` element which is also a `Node`. `Element` is also a `Node`. I think you should clarify your question. – Günter Zöchbauer Dec 06 '14 at 21:44
  • 1
    `new Text('xxx')` is it then. – Günter Zöchbauer Dec 06 '14 at 22:21
  • Updated question, but I think your last comment could help me, because `new Text(myString);` looks like it could do, what I need – aleskva Dec 06 '14 at 22:22
1

You can only add Node's to the element's node list. However you can create a Text node from a string.

For example:

myElement.nodes.addAll([div1, new Text(string1), div2, new Text(string2)]);
Greg Lowe
  • 15,430
  • 2
  • 30
  • 33