kwebio / kweb-core

A Kotlin web framework
https://docs.kweb.io/book
GNU Lesser General Public License v3.0
969 stars 57 forks source link

setAttributeRaw, js and css #152

Closed prohfessor closed 3 years ago

prohfessor commented 4 years ago

Hello! Recently I began to study Kotlin and KWeb and ran into a couple of problems:

  1. setAttributeRaw(). in file Element.kt it is written:
    • Set an attribute of this element. For example a().setAttribute("href", "http://kweb.io")
    • will create an <a> element and set it to <a href="http://kweb.io/">.

But when I try to change, for example 'id' a().setAttributeRaw("id", "newid") I get: id=&quot newid&quot How can I change correctly 'id'? I do that way: a(attributes = mapOf("id" to "newid"))

  1. Can i change the variable 'ttt' in javascript from KWeb?: element("ttt2").innerHTML( """

    """

  2. How to insert in header link to css and js file? Now I insert something like this:

        doc.head.new {
    element("ttt").innerHTML(
        """
        """
    )
        }
prohfessor commented 4 years ago

i found how to embed javascript - execute()

marad commented 4 years ago

Hi @prohfessor. Your question is a bit chaotic so I'll try my best to answer each point as I understand your problem (and I'm not sure if I do all the way).

1. Here I'm not sure what is the problem. In my pet project I'm using setAttributeRaw this way:

div(fomantic.item).setAttributeRaw("data-value", area.id).text(area.name)

and it works all right. Produces following HTML:

<div id="Ke" class="item selected" data-value="261f5fb6-e4c9-4945-a92a-2998ffdc8004">
       Sample Item
</div>

Could you provide some more details on your particular problem? What is the exact output you are getting and what would you like to have instead.

2. While you can change the value if it's global with simple execute("ttt = 'new value'") you shouldn't have global variables in the first place 🙂 What is the reason you want to do this? Maybe there is another way to achieve the same effect?

3. This should work:

doc.head.new {
    element("script", mapOf(
            "src" to  "https://unpkg.com/leaflet@1.6.0/dist/leaflet.js",
            "integrity" to "sha512-gZwIG9x3wUXg2hdXF6+rVkLF/0Vi9U8D2Ntg4Ga5I5BZpVkVxlJWbSQtXPSiUTtC0TjtGOmxa1AJPuV0CPthew==",
            "crossorigin" to ""
    ))
}
prohfessor commented 4 years ago
  1. setAttributeRaw changes the quotation marks to a &quot in front and the end of the text
DineshSolanki commented 4 years ago
1. setAttributeRaw changes the quotation marks to a &quot in front and the end of the text

works correctly on my side.

a().setAttributeRaw("id", "newid")

gives -

<a id="newid" href="#"></a>

maybe the issue is in something else, your id might contain some unsupported character maybe or you are escaping them somehow.

DineshSolanki commented 3 years ago

closing as can't reproduce and inactivity.