feat: add complete DOM implementation with Document, Element, and NodeList classes to html module
CI / build-linux (push) Successful in 1m39s
CI / build-linux (push) Successful in 1m39s
This commit is contained in:
+24
@@ -0,0 +1,24 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='container'></div>")
|
||||
|
||||
var p = doc.createElement("p")
|
||||
System.print(p != null) // expect: true
|
||||
System.print(p.tagName) // expect: P
|
||||
|
||||
var span = doc.createElement("span")
|
||||
System.print(span.tagName) // expect: SPAN
|
||||
|
||||
var div = doc.createElement("div")
|
||||
System.print(div.tagName) // expect: DIV
|
||||
|
||||
p.textContent = "New paragraph"
|
||||
System.print(p.textContent) // expect: New paragraph
|
||||
|
||||
var container = doc.getElementById("container")
|
||||
container.appendChild(p)
|
||||
System.print(container.children.count) // expect: 1
|
||||
System.print(container.firstChild.tagName) // expect: P
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='container'></div>")
|
||||
|
||||
var text = doc.createTextNode("Hello World")
|
||||
System.print(text != null) // expect: true
|
||||
System.print(text.textContent) // expect: Hello World
|
||||
System.print(text.nodeType) // expect: 3
|
||||
|
||||
var empty = doc.createTextNode("")
|
||||
System.print(empty.textContent) // expect:
|
||||
|
||||
var special = doc.createTextNode("Line1\nLine2")
|
||||
System.print(special.textContent.contains("\n")) // expect: true
|
||||
|
||||
var container = doc.getElementById("container")
|
||||
container.appendChild(text)
|
||||
System.print(container.textContent) // expect: Hello World
|
||||
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='first'>One</div><div id='second'>Two</div><div>No ID</div>")
|
||||
|
||||
var first = doc.getElementById("first")
|
||||
System.print(first != null) // expect: true
|
||||
System.print(first.textContent) // expect: One
|
||||
|
||||
var second = doc.getElementById("second")
|
||||
System.print(second != null) // expect: true
|
||||
System.print(second.textContent) // expect: Two
|
||||
|
||||
var notFound = doc.getElementById("nonexistent")
|
||||
System.print(notFound == null) // expect: true
|
||||
|
||||
var nested = Document.parse("<div><span id='inner'>Nested</span></div>")
|
||||
var inner = nested.getElementById("inner")
|
||||
System.print(inner != null) // expect: true
|
||||
System.print(inner.tagName) // expect: SPAN
|
||||
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div class='box'>A</div><div class='box'>B</div><div class='other'>C</div>")
|
||||
|
||||
var boxes = doc.getElementsByClassName("box")
|
||||
System.print(boxes.count) // expect: 2
|
||||
System.print(boxes[0].textContent) // expect: A
|
||||
System.print(boxes[1].textContent) // expect: B
|
||||
|
||||
var other = doc.getElementsByClassName("other")
|
||||
System.print(other.count) // expect: 1
|
||||
System.print(other[0].textContent) // expect: C
|
||||
|
||||
var none = doc.getElementsByClassName("nonexistent")
|
||||
System.print(none.count) // expect: 0
|
||||
|
||||
var multi = Document.parse("<div class='a b'>Multi</div><div class='a'>Single</div>")
|
||||
var aClass = multi.getElementsByClassName("a")
|
||||
System.print(aClass.count) // expect: 2
|
||||
|
||||
var bClass = multi.getElementsByClassName("b")
|
||||
System.print(bClass.count) // expect: 1
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>One</p><p>Two</p><span>Three</span></div>")
|
||||
|
||||
var paragraphs = doc.getElementsByTagName("p")
|
||||
System.print(paragraphs.count) // expect: 2
|
||||
System.print(paragraphs[0].textContent) // expect: One
|
||||
System.print(paragraphs[1].textContent) // expect: Two
|
||||
|
||||
var spans = doc.getElementsByTagName("span")
|
||||
System.print(spans.count) // expect: 1
|
||||
System.print(spans[0].textContent) // expect: Three
|
||||
|
||||
var divs = doc.getElementsByTagName("div")
|
||||
System.print(divs.count) // expect: 1
|
||||
|
||||
var none = doc.getElementsByTagName("article")
|
||||
System.print(none.count) // expect: 0
|
||||
|
||||
var caseInsensitive = doc.getElementsByTagName("P")
|
||||
System.print(caseInsensitive.count) // expect: 2
|
||||
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div>Hello</div>")
|
||||
System.print(doc != null) // expect: true
|
||||
|
||||
var doc2 = Document.parse("<html><head></head><body></body></html>")
|
||||
System.print(doc2.documentElement != null) // expect: true
|
||||
|
||||
var doc3 = Document.parse("")
|
||||
System.print(doc3 != null) // expect: true
|
||||
|
||||
var doc4 = Document.parse("<p>Text</p>")
|
||||
System.print(doc4.querySelector("p").textContent) // expect: Text
|
||||
|
||||
var doc5 = Document.parse("<div><span><a>Link</a></span></div>")
|
||||
System.print(doc5.querySelector("a").textContent) // expect: Link
|
||||
Vendored
+26
@@ -0,0 +1,26 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<html><head><title>Test Title</title></head><body><div>Content</div></body></html>")
|
||||
|
||||
System.print(doc.documentElement != null) // expect: true
|
||||
System.print(doc.documentElement.tagName) // expect: HTML
|
||||
|
||||
System.print(doc.head != null) // expect: true
|
||||
System.print(doc.head.tagName) // expect: HEAD
|
||||
|
||||
System.print(doc.body != null) // expect: true
|
||||
System.print(doc.body.tagName) // expect: BODY
|
||||
|
||||
System.print(doc.title) // expect: Test Title
|
||||
|
||||
doc.title = "New Title"
|
||||
System.print(doc.title) // expect: New Title
|
||||
|
||||
var html = doc.outerHTML
|
||||
System.print(html.indexOf("<html") >= 0) // expect: true
|
||||
System.print(html.indexOf("</html>") >= 0) // expect: true
|
||||
|
||||
var inner = doc.innerHTML
|
||||
System.print(inner.indexOf("<head") >= 0) // expect: true
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='main' class='container'><p class='text'>Hello</p><span>World</span></div>")
|
||||
|
||||
var byTag = doc.querySelector("p")
|
||||
System.print(byTag != null) // expect: true
|
||||
System.print(byTag.tagName) // expect: P
|
||||
|
||||
var byId = doc.querySelector("#main")
|
||||
System.print(byId != null) // expect: true
|
||||
System.print(byId.tagName) // expect: DIV
|
||||
|
||||
var byClass = doc.querySelector(".text")
|
||||
System.print(byClass != null) // expect: true
|
||||
System.print(byClass.textContent) // expect: Hello
|
||||
|
||||
var byCompound = doc.querySelector("div.container")
|
||||
System.print(byCompound != null) // expect: true
|
||||
|
||||
var notFound = doc.querySelector(".nonexistent")
|
||||
System.print(notFound == null) // expect: true
|
||||
|
||||
var descendant = doc.querySelector("div p")
|
||||
System.print(descendant != null) // expect: true
|
||||
|
||||
var child = doc.querySelector("div > p")
|
||||
System.print(child != null) // expect: true
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p class='a'>One</p><p class='a'>Two</p><p class='b'>Three</p></div>")
|
||||
|
||||
var all = doc.querySelectorAll("p")
|
||||
System.print(all.count) // expect: 3
|
||||
|
||||
var byClass = doc.querySelectorAll(".a")
|
||||
System.print(byClass.count) // expect: 2
|
||||
System.print(byClass[0].textContent) // expect: One
|
||||
System.print(byClass[1].textContent) // expect: Two
|
||||
|
||||
var none = doc.querySelectorAll(".nonexistent")
|
||||
System.print(none.count) // expect: 0
|
||||
|
||||
var single = doc.querySelectorAll(".b")
|
||||
System.print(single.count) // expect: 1
|
||||
System.print(single[0].textContent) // expect: Three
|
||||
|
||||
var nested = Document.parse("<div><span><a>Link1</a></span><a>Link2</a></div>")
|
||||
var links = nested.querySelectorAll("a")
|
||||
System.print(links.count) // expect: 2
|
||||
|
||||
Vendored
+34
@@ -0,0 +1,34 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id=\"test\" class=\"foo bar\" data-value=\"123\"></div>")
|
||||
var div = doc.querySelector("div")
|
||||
|
||||
System.print(div.id) // expect: test
|
||||
System.print(div.className) // expect: foo bar
|
||||
System.print(div.hasAttribute("id")) // expect: true
|
||||
System.print(div.hasAttribute("missing")) // expect: false
|
||||
|
||||
div.setAttribute("title", "My Title")
|
||||
System.print(div.getAttribute("title")) // expect: My Title
|
||||
|
||||
div.removeAttribute("title")
|
||||
System.print(div.getAttribute("title") == null) // expect: true
|
||||
|
||||
var attrs = div.attributes
|
||||
System.print(attrs["id"]) // expect: test
|
||||
|
||||
var classList = div.classList
|
||||
System.print(classList.count) // expect: 2
|
||||
System.print(classList[0]) // expect: foo
|
||||
System.print(classList[1]) // expect: bar
|
||||
|
||||
var dataset = div.dataset
|
||||
System.print(dataset["value"]) // expect: 123
|
||||
|
||||
div.id = "newId"
|
||||
System.print(div.id) // expect: newId
|
||||
|
||||
div.className = "single-class"
|
||||
System.print(div.className) // expect: single-class
|
||||
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>Hello</p><span>World</span></div>")
|
||||
var div = doc.querySelector("div")
|
||||
|
||||
System.print(div.textContent) // expect: HelloWorld
|
||||
|
||||
div.textContent = "New Content"
|
||||
System.print(div.textContent) // expect: New Content
|
||||
System.print(div.children.count) // expect: 0
|
||||
|
||||
var doc2 = Document.parse("<div id=\"test\"></div>")
|
||||
var div2 = doc2.getElementById("test")
|
||||
div2.innerHTML = "<p>First</p><p>Second</p>"
|
||||
System.print(div2.children.count) // expect: 2
|
||||
System.print(div2.querySelector("p").textContent) // expect: First
|
||||
|
||||
var outer = div2.outerHTML
|
||||
System.print(outer.indexOf("<div") >= 0) // expect: true
|
||||
System.print(outer.indexOf("</div>") >= 0) // expect: true
|
||||
Vendored
+32
@@ -0,0 +1,32 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id=\"container\"></div>")
|
||||
var container = doc.getElementById("container")
|
||||
|
||||
var p = doc.createElement("p")
|
||||
p.textContent = "Hello World"
|
||||
container.appendChild(p)
|
||||
|
||||
System.print(container.innerHTML) // expect: <p>Hello World</p>
|
||||
|
||||
var span = doc.createElement("span")
|
||||
span.textContent = "Inserted"
|
||||
container.insertBefore(span, p)
|
||||
|
||||
System.print(container.children.count) // expect: 2
|
||||
System.print(container.firstElementChild.tagName) // expect: SPAN
|
||||
|
||||
var removed = container.removeChild(span)
|
||||
System.print(removed.tagName) // expect: SPAN
|
||||
System.print(container.children.count) // expect: 1
|
||||
|
||||
var newP = doc.createElement("p")
|
||||
newP.textContent = "Replaced"
|
||||
container.replaceChild(newP, p)
|
||||
System.print(container.innerHTML) // expect: <p>Replaced</p>
|
||||
|
||||
var clone = newP.cloneNode(true)
|
||||
System.print(clone.textContent) // expect: Replaced
|
||||
System.print(clone.parentNode == null) // expect: true
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>Hello</p></div>")
|
||||
System.print(doc.documentElement != null) // expect: true
|
||||
|
||||
var doc2 = Document.parse("<html><head><title>Test</title></head><body><div id=\"main\">Content</div></body></html>")
|
||||
System.print(doc2.head != null) // expect: true
|
||||
System.print(doc2.body != null) // expect: true
|
||||
System.print(doc2.title) // expect: Test
|
||||
|
||||
var doc3 = Document.parse("<div class=\"foo bar\" data-id=\"123\">Text</div>")
|
||||
var div = doc3.querySelector("div")
|
||||
System.print(div.className) // expect: foo bar
|
||||
System.print(div.getAttribute("data-id")) // expect: 123
|
||||
|
||||
var doc4 = Document.parse("<img src=\"test.png\" />")
|
||||
var img = doc4.querySelector("img")
|
||||
System.print(img.tagName) // expect: IMG
|
||||
System.print(img.getAttribute("src")) // expect: test.png
|
||||
Vendored
+34
@@ -0,0 +1,34 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var html = "<div id=\"root\"><p class=\"text first\">One</p><p class=\"text\">Two</p><span>Three</span></div>"
|
||||
var doc = Document.parse(html)
|
||||
|
||||
var byId = doc.getElementById("root")
|
||||
System.print(byId.tagName) // expect: DIV
|
||||
|
||||
var byTag = doc.querySelector("p")
|
||||
System.print(byTag.textContent) // expect: One
|
||||
|
||||
var byClass = doc.querySelector(".text")
|
||||
System.print(byClass.textContent) // expect: One
|
||||
|
||||
var allP = doc.querySelectorAll("p")
|
||||
System.print(allP.count) // expect: 2
|
||||
|
||||
var allText = doc.getElementsByClassName("text")
|
||||
System.print(allText.count) // expect: 2
|
||||
|
||||
var spans = doc.getElementsByTagName("span")
|
||||
System.print(spans.count) // expect: 1
|
||||
System.print(spans[0].textContent) // expect: Three
|
||||
|
||||
var compound = doc.querySelector("p.text.first")
|
||||
System.print(compound.textContent) // expect: One
|
||||
|
||||
var descendant = doc.querySelector("div p")
|
||||
System.print(descendant != null) // expect: true
|
||||
|
||||
var child = doc.querySelector("div > p")
|
||||
System.print(child != null) // expect: true
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id=\"test\" class=\"foo\"><p>Content</p></div>")
|
||||
var html = doc.outerHTML
|
||||
|
||||
System.print(html.indexOf("<div") >= 0) // expect: true
|
||||
System.print(html.indexOf("id=\"test\"") >= 0) // expect: true
|
||||
System.print(html.indexOf("class=\"foo\"") >= 0) // expect: true
|
||||
System.print(html.indexOf("<p>Content</p>") >= 0) // expect: true
|
||||
System.print(html.indexOf("</div>") >= 0) // expect: true
|
||||
|
||||
var doc2 = Document.parse("<img src=\"test.png\" />")
|
||||
var imgHtml = doc2.outerHTML
|
||||
System.print(imgHtml.indexOf("<img") >= 0) // expect: true
|
||||
System.print(imgHtml.indexOf("/>") >= 0) // expect: true
|
||||
|
||||
var doc3 = Document.parse("<p>Hello & World</p>")
|
||||
var p = doc3.querySelector("p")
|
||||
System.print(p.textContent) // expect: Hello & World
|
||||
Vendored
+28
@@ -0,0 +1,28 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var html = "<div><p>First</p><span>Second</span><p>Third</p></div>"
|
||||
var doc = Document.parse(html)
|
||||
|
||||
var div = doc.querySelector("div")
|
||||
|
||||
System.print(div.hasChildNodes()) // expect: true
|
||||
System.print(div.children.count) // expect: 3
|
||||
|
||||
var first = div.firstElementChild
|
||||
System.print(first.tagName) // expect: P
|
||||
System.print(first.textContent) // expect: First
|
||||
|
||||
var last = div.lastElementChild
|
||||
System.print(last.tagName) // expect: P
|
||||
System.print(last.textContent) // expect: Third
|
||||
|
||||
var span = doc.querySelector("span")
|
||||
System.print(span.previousElementSibling.tagName) // expect: P
|
||||
System.print(span.nextElementSibling.tagName) // expect: P
|
||||
|
||||
System.print(span.parentElement.tagName) // expect: DIV
|
||||
|
||||
var p = doc.querySelector("p")
|
||||
System.print(p.parentNode != null) // expect: true
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='container'></div>")
|
||||
|
||||
var container = doc.getElementById("container")
|
||||
System.print(container.children.count) // expect: 0
|
||||
|
||||
var p = doc.createElement("p")
|
||||
p.textContent = "First"
|
||||
container.appendChild(p)
|
||||
System.print(container.children.count) // expect: 1
|
||||
System.print(container.firstChild.textContent) // expect: First
|
||||
|
||||
var span = doc.createElement("span")
|
||||
span.textContent = "Second"
|
||||
container.appendChild(span)
|
||||
System.print(container.children.count) // expect: 2
|
||||
System.print(container.lastChild.textContent) // expect: Second
|
||||
|
||||
var text = doc.createTextNode("Text node")
|
||||
container.appendChild(text)
|
||||
System.print(container.textContent.contains("Text node")) // expect: true
|
||||
|
||||
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<a href='http://example.com' target='_blank'>Link</a>")
|
||||
|
||||
var a = doc.querySelector("a")
|
||||
|
||||
System.print(a.getAttribute("href")) // expect: http://example.com
|
||||
System.print(a.getAttribute("target")) // expect: _blank
|
||||
System.print(a.getAttribute("nonexistent") == null) // expect: true
|
||||
|
||||
System.print(a.hasAttribute("href")) // expect: true
|
||||
System.print(a.hasAttribute("target")) // expect: true
|
||||
System.print(a.hasAttribute("nonexistent")) // expect: false
|
||||
|
||||
a.setAttribute("title", "Example Link")
|
||||
System.print(a.getAttribute("title")) // expect: Example Link
|
||||
System.print(a.hasAttribute("title")) // expect: true
|
||||
|
||||
a.removeAttribute("target")
|
||||
System.print(a.hasAttribute("target")) // expect: false
|
||||
|
||||
var attrs = a.attributes
|
||||
System.print(attrs["href"]) // expect: http://example.com
|
||||
System.print(attrs["title"]) // expect: Example Link
|
||||
|
||||
Vendored
+28
@@ -0,0 +1,28 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>One</p><span>Two</span><a>Three</a></div>")
|
||||
|
||||
var div = doc.querySelector("div")
|
||||
|
||||
System.print(div.children.count) // expect: 3
|
||||
System.print(div.children[0].tagName) // expect: P
|
||||
System.print(div.children[1].tagName) // expect: SPAN
|
||||
System.print(div.children[2].tagName) // expect: A
|
||||
|
||||
System.print(div.childNodes.count >= 3) // expect: true
|
||||
|
||||
System.print(div.firstChild != null) // expect: true
|
||||
System.print(div.lastChild != null) // expect: true
|
||||
|
||||
System.print(div.firstElementChild.tagName) // expect: P
|
||||
System.print(div.lastElementChild.tagName) // expect: A
|
||||
|
||||
var empty = doc.createElement("div")
|
||||
System.print(empty.children.count) // expect: 0
|
||||
System.print(empty.firstChild == null) // expect: true
|
||||
System.print(empty.lastChild == null) // expect: true
|
||||
System.print(empty.firstElementChild == null) // expect: true
|
||||
System.print(empty.lastElementChild == null) // expect: true
|
||||
|
||||
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div class='a b c'>Content</div>")
|
||||
|
||||
var div = doc.querySelector("div")
|
||||
var list = div.classList
|
||||
System.print(list.count) // expect: 3
|
||||
System.print(list[0]) // expect: a
|
||||
System.print(list[1]) // expect: b
|
||||
System.print(list[2]) // expect: c
|
||||
|
||||
var noClass = Document.parse("<span>No Class</span>")
|
||||
var span = noClass.querySelector("span")
|
||||
var emptyList = span.classList
|
||||
System.print(emptyList.count) // expect: 0
|
||||
|
||||
var single = Document.parse("<p class='only'>Text</p>")
|
||||
var p = single.querySelector("p")
|
||||
System.print(p.classList.count) // expect: 1
|
||||
System.print(p.classList[0]) // expect: only
|
||||
|
||||
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div class='container main'>Content</div>")
|
||||
|
||||
var div = doc.querySelector("div")
|
||||
System.print(div.className) // expect: container main
|
||||
|
||||
div.className = "box"
|
||||
System.print(div.className) // expect: box
|
||||
|
||||
var noClass = Document.parse("<span>No Class</span>")
|
||||
var span = noClass.querySelector("span")
|
||||
System.print(span.className) // expect:
|
||||
|
||||
span.className = "highlight"
|
||||
System.print(span.className) // expect: highlight
|
||||
|
||||
div.className = "one two three"
|
||||
System.print(div.className) // expect: one two three
|
||||
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='original' class='box'><p>Child</p><span>Another</span></div>")
|
||||
|
||||
var original = doc.getElementById("original")
|
||||
|
||||
var shallow = original.cloneNode(false)
|
||||
System.print(shallow.tagName) // expect: DIV
|
||||
System.print(shallow.id) // expect: original
|
||||
System.print(shallow.className) // expect: box
|
||||
System.print(shallow.children.count) // expect: 0
|
||||
|
||||
var deep = original.cloneNode(true)
|
||||
System.print(deep.tagName) // expect: DIV
|
||||
System.print(deep.id) // expect: original
|
||||
System.print(deep.children.count) // expect: 2
|
||||
System.print(deep.firstChild.tagName) // expect: P
|
||||
System.print(deep.lastChild.tagName) // expect: SPAN
|
||||
|
||||
deep.id = "cloned"
|
||||
System.print(original.id) // expect: original
|
||||
System.print(deep.id) // expect: cloned
|
||||
|
||||
Vendored
+27
@@ -0,0 +1,27 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='outer' class='wrapper'><section id='inner'><p id='text'>Content</p></section></div>")
|
||||
|
||||
var p = doc.getElementById("text")
|
||||
|
||||
var closestSection = p.closest("section")
|
||||
System.print(closestSection != null) // expect: true
|
||||
System.print(closestSection.id) // expect: inner
|
||||
|
||||
var closestDiv = p.closest("div")
|
||||
System.print(closestDiv != null) // expect: true
|
||||
System.print(closestDiv.id) // expect: outer
|
||||
|
||||
var closestWrapper = p.closest(".wrapper")
|
||||
System.print(closestWrapper != null) // expect: true
|
||||
System.print(closestWrapper.id) // expect: outer
|
||||
|
||||
var closestSelf = p.closest("p")
|
||||
System.print(closestSelf != null) // expect: true
|
||||
System.print(closestSelf.id) // expect: text
|
||||
|
||||
var notFound = p.closest("article")
|
||||
System.print(notFound == null) // expect: true
|
||||
|
||||
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='outer'><section id='inner'><p id='para'>Text</p></section></div>")
|
||||
|
||||
var outer = doc.getElementById("outer")
|
||||
var inner = doc.getElementById("inner")
|
||||
var para = doc.getElementById("para")
|
||||
|
||||
System.print(outer.contains(inner)) // expect: true
|
||||
System.print(outer.contains(para)) // expect: true
|
||||
System.print(inner.contains(para)) // expect: true
|
||||
|
||||
System.print(inner.contains(outer)) // expect: false
|
||||
System.print(para.contains(outer)) // expect: false
|
||||
System.print(para.contains(inner)) // expect: false
|
||||
|
||||
System.print(outer.contains(outer)) // expect: true
|
||||
|
||||
var unrelated = doc.createElement("span")
|
||||
System.print(outer.contains(unrelated)) // expect: false
|
||||
|
||||
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div data-id='123' data-name='test' data-value='hello'>Content</div>")
|
||||
|
||||
var div = doc.querySelector("div")
|
||||
var dataset = div.dataset
|
||||
|
||||
System.print(dataset["id"]) // expect: 123
|
||||
System.print(dataset["name"]) // expect: test
|
||||
System.print(dataset["value"]) // expect: hello
|
||||
|
||||
var noData = Document.parse("<span>No data</span>")
|
||||
var span = noData.querySelector("span")
|
||||
var emptyDataset = span.dataset
|
||||
System.print(emptyDataset.count) // expect: 0
|
||||
|
||||
var partial = Document.parse("<p data-single='only'>Text</p>")
|
||||
var p = partial.querySelector("p")
|
||||
System.print(p.dataset["single"]) // expect: only
|
||||
System.print(p.dataset.count) // expect: 1
|
||||
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='root'><p class='item'>A</p><span class='item'>B</span><section><a class='item'>C</a></section></div>")
|
||||
|
||||
var root = doc.getElementById("root")
|
||||
var items = root.getElementsByClassName("item")
|
||||
System.print(items.count) // expect: 3
|
||||
|
||||
var section = doc.querySelector("section")
|
||||
var sectionItems = section.getElementsByClassName("item")
|
||||
System.print(sectionItems.count) // expect: 1
|
||||
System.print(sectionItems[0].tagName) // expect: A
|
||||
|
||||
var none = section.getElementsByClassName("nonexistent")
|
||||
System.print(none.count) // expect: 0
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='root'><p>A</p><span>B</span><section><p>C</p></section></div>")
|
||||
|
||||
var root = doc.getElementById("root")
|
||||
var ps = root.getElementsByTagName("p")
|
||||
System.print(ps.count) // expect: 2
|
||||
|
||||
var section = doc.querySelector("section")
|
||||
var sectionPs = section.getElementsByTagName("p")
|
||||
System.print(sectionPs.count) // expect: 1
|
||||
System.print(sectionPs[0].textContent) // expect: C
|
||||
|
||||
var spans = root.getElementsByTagName("span")
|
||||
System.print(spans.count) // expect: 1
|
||||
|
||||
var none = section.getElementsByTagName("span")
|
||||
System.print(none.count) // expect: 0
|
||||
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>Text</p></div>")
|
||||
|
||||
var div = doc.querySelector("div")
|
||||
System.print(div.hasChildNodes()) // expect: true
|
||||
|
||||
var p = doc.querySelector("p")
|
||||
System.print(p.hasChildNodes()) // expect: true
|
||||
|
||||
var empty = doc.createElement("span")
|
||||
System.print(empty.hasChildNodes()) // expect: false
|
||||
|
||||
empty.textContent = "Now has content"
|
||||
System.print(empty.hasChildNodes()) // expect: true
|
||||
|
||||
var emptyDiv = Document.parse("<div></div>")
|
||||
System.print(emptyDiv.querySelector("div").hasChildNodes()) // expect: false
|
||||
|
||||
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='myDiv'>Content</div>")
|
||||
|
||||
var div = doc.querySelector("div")
|
||||
System.print(div.id) // expect: myDiv
|
||||
|
||||
div.id = "newId"
|
||||
System.print(div.id) // expect: newId
|
||||
|
||||
var found = doc.getElementById("newId")
|
||||
System.print(found != null) // expect: true
|
||||
|
||||
var noId = Document.parse("<span>No ID</span>")
|
||||
var span = noId.querySelector("span")
|
||||
System.print(span.id) // expect:
|
||||
|
||||
span.id = "spanId"
|
||||
System.print(span.id) // expect: spanId
|
||||
|
||||
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>Hello</p><span>World</span></div>")
|
||||
|
||||
var div = doc.querySelector("div")
|
||||
var html = div.innerHTML
|
||||
System.print(html.contains("<p>")) // expect: true
|
||||
System.print(html.contains("Hello")) // expect: true
|
||||
System.print(html.contains("<span>")) // expect: true
|
||||
|
||||
div.innerHTML = "<a>Link</a>"
|
||||
System.print(div.innerHTML.contains("<a>")) // expect: true
|
||||
System.print(div.children.count) // expect: 1
|
||||
System.print(div.firstChild.tagName) // expect: A
|
||||
|
||||
var empty = doc.createElement("div")
|
||||
System.print(empty.innerHTML) // expect:
|
||||
|
||||
empty.innerHTML = "Plain text"
|
||||
System.print(empty.textContent) // expect: Plain text
|
||||
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='container'><span id='ref'>Reference</span></div>")
|
||||
|
||||
var container = doc.getElementById("container")
|
||||
var ref = doc.getElementById("ref")
|
||||
|
||||
var p = doc.createElement("p")
|
||||
p.textContent = "Before"
|
||||
container.insertBefore(p, ref)
|
||||
|
||||
System.print(container.children.count) // expect: 2
|
||||
System.print(container.firstChild.tagName) // expect: P
|
||||
System.print(container.firstChild.textContent) // expect: Before
|
||||
System.print(container.lastChild.tagName) // expect: SPAN
|
||||
|
||||
var a = doc.createElement("a")
|
||||
a.textContent = "Link"
|
||||
container.insertBefore(a, ref)
|
||||
|
||||
System.print(container.children.count) // expect: 3
|
||||
System.print(container.children[1].tagName) // expect: A
|
||||
|
||||
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='test' class='box container'><p>Text</p></div>")
|
||||
|
||||
var div = doc.querySelector("div")
|
||||
|
||||
System.print(div.matches("div")) // expect: true
|
||||
System.print(div.matches("#test")) // expect: true
|
||||
System.print(div.matches(".box")) // expect: true
|
||||
System.print(div.matches(".container")) // expect: true
|
||||
System.print(div.matches("div.box")) // expect: true
|
||||
System.print(div.matches("div#test")) // expect: true
|
||||
|
||||
System.print(div.matches("span")) // expect: false
|
||||
System.print(div.matches("#other")) // expect: false
|
||||
System.print(div.matches(".other")) // expect: false
|
||||
|
||||
var p = doc.querySelector("p")
|
||||
System.print(p.matches("p")) // expect: true
|
||||
System.print(p.matches("div")) // expect: false
|
||||
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='test'>Content</div>")
|
||||
|
||||
var div = doc.querySelector("div")
|
||||
|
||||
System.print(div.nodeType) // expect: 1
|
||||
System.print(div.nodeName) // expect: DIV
|
||||
System.print(div.nodeValue == null) // expect: true
|
||||
|
||||
var text = doc.createTextNode("Text content")
|
||||
System.print(text.nodeType) // expect: 3
|
||||
System.print(text.nodeName) // expect: #text
|
||||
System.print(text.nodeValue) // expect: Text content
|
||||
|
||||
text.nodeValue = "Changed"
|
||||
System.print(text.nodeValue) // expect: Changed
|
||||
|
||||
var p = doc.createElement("p")
|
||||
System.print(p.nodeType) // expect: 1
|
||||
System.print(p.nodeName) // expect: P
|
||||
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='container'></div>")
|
||||
var container = doc.getElementById("container")
|
||||
|
||||
container.appendChild(doc.createTextNode("Hello "))
|
||||
container.appendChild(doc.createTextNode("World"))
|
||||
container.appendChild(doc.createTextNode("!"))
|
||||
|
||||
System.print(container.childNodes.count) // expect: 3
|
||||
|
||||
container.normalize()
|
||||
|
||||
System.print(container.childNodes.count) // expect: 1
|
||||
System.print(container.textContent) // expect: Hello World!
|
||||
|
||||
var simple = Document.parse("<p>Simple text</p>")
|
||||
var p = simple.querySelector("p")
|
||||
var before = p.childNodes.count
|
||||
p.normalize()
|
||||
System.print(p.childNodes.count == before) // expect: true
|
||||
|
||||
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='test'><p>Hello</p></div>")
|
||||
|
||||
var div = doc.querySelector("div")
|
||||
var html = div.outerHTML
|
||||
System.print(html.contains("<div")) // expect: true
|
||||
System.print(html.contains("id=\"test\"") || html.contains("id='test'")) // expect: true
|
||||
System.print(html.contains("</div>")) // expect: true
|
||||
System.print(html.contains("<p>")) // expect: true
|
||||
|
||||
var p = doc.querySelector("p")
|
||||
System.print(p.outerHTML.contains("<p>")) // expect: true
|
||||
System.print(p.outerHTML.contains("Hello")) // expect: true
|
||||
System.print(p.outerHTML.contains("</p>")) // expect: true
|
||||
|
||||
var selfClose = Document.parse("<br/>")
|
||||
var br = selfClose.querySelector("br")
|
||||
System.print(br != null) // expect: true
|
||||
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='parent'><p id='child'>Text</p></div>")
|
||||
|
||||
var child = doc.getElementById("child")
|
||||
var parent = child.parentNode
|
||||
System.print(parent != null) // expect: true
|
||||
System.print(parent.id) // expect: parent
|
||||
|
||||
var parentElem = child.parentElement
|
||||
System.print(parentElem != null) // expect: true
|
||||
System.print(parentElem.tagName) // expect: DIV
|
||||
|
||||
var div = doc.getElementById("parent")
|
||||
var divParent = div.parentNode
|
||||
System.print(divParent != null) // expect: true
|
||||
|
||||
var nested = Document.parse("<div><span><a>Link</a></span></div>")
|
||||
var a = nested.querySelector("a")
|
||||
System.print(a.parentNode.tagName) // expect: SPAN
|
||||
System.print(a.parentElement.tagName) // expect: SPAN
|
||||
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='root'><section><p class='text'>One</p></section><article><p class='text'>Two</p></article></div>")
|
||||
|
||||
var root = doc.getElementById("root")
|
||||
var section = doc.querySelector("section")
|
||||
var article = doc.querySelector("article")
|
||||
|
||||
var fromRoot = root.querySelector("p")
|
||||
System.print(fromRoot.textContent) // expect: One
|
||||
|
||||
var fromSection = section.querySelector(".text")
|
||||
System.print(fromSection.textContent) // expect: One
|
||||
|
||||
var fromArticle = article.querySelector(".text")
|
||||
System.print(fromArticle.textContent) // expect: Two
|
||||
|
||||
var notFound = section.querySelector("article")
|
||||
System.print(notFound == null) // expect: true
|
||||
|
||||
var nested = root.querySelector("section p")
|
||||
System.print(nested.textContent) // expect: One
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='root'><section><p>A</p><p>B</p></section><article><p>C</p></article></div>")
|
||||
|
||||
var root = doc.getElementById("root")
|
||||
var all = root.querySelectorAll("p")
|
||||
System.print(all.count) // expect: 3
|
||||
|
||||
var section = doc.querySelector("section")
|
||||
var sectionPs = section.querySelectorAll("p")
|
||||
System.print(sectionPs.count) // expect: 2
|
||||
System.print(sectionPs[0].textContent) // expect: A
|
||||
System.print(sectionPs[1].textContent) // expect: B
|
||||
|
||||
var article = doc.querySelector("article")
|
||||
var articlePs = article.querySelectorAll("p")
|
||||
System.print(articlePs.count) // expect: 1
|
||||
System.print(articlePs[0].textContent) // expect: C
|
||||
|
||||
var none = section.querySelectorAll("article")
|
||||
System.print(none.count) // expect: 0
|
||||
|
||||
Vendored
+23
@@ -0,0 +1,23 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='container'><p id='para'>Text</p><span>Other</span></div>")
|
||||
|
||||
var container = doc.getElementById("container")
|
||||
System.print(container.children.count) // expect: 2
|
||||
|
||||
var para = doc.getElementById("para")
|
||||
para.remove()
|
||||
|
||||
System.print(container.children.count) // expect: 1
|
||||
System.print(container.firstChild.tagName) // expect: SPAN
|
||||
|
||||
var doc2 = Document.parse("<ul><li>A</li><li>B</li><li>C</li></ul>")
|
||||
var ul = doc2.querySelector("ul")
|
||||
var items = doc2.querySelectorAll("li")
|
||||
System.print(items.count) // expect: 3
|
||||
|
||||
items[1].remove()
|
||||
System.print(ul.children.count) // expect: 2
|
||||
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='container'><p>One</p><span>Two</span><a>Three</a></div>")
|
||||
|
||||
var container = doc.getElementById("container")
|
||||
System.print(container.children.count) // expect: 3
|
||||
|
||||
var span = doc.querySelector("span")
|
||||
container.removeChild(span)
|
||||
System.print(container.children.count) // expect: 2
|
||||
|
||||
System.print(container.firstChild.tagName) // expect: P
|
||||
System.print(container.lastChild.tagName) // expect: A
|
||||
|
||||
var p = doc.querySelector("p")
|
||||
container.removeChild(p)
|
||||
System.print(container.children.count) // expect: 1
|
||||
System.print(container.firstChild.tagName) // expect: A
|
||||
|
||||
Vendored
+26
@@ -0,0 +1,26 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='container'><p id='old'>Old</p></div>")
|
||||
|
||||
var container = doc.getElementById("container")
|
||||
var old = doc.getElementById("old")
|
||||
|
||||
var newElem = doc.createElement("span")
|
||||
newElem.textContent = "New"
|
||||
|
||||
container.replaceChild(newElem, old)
|
||||
|
||||
System.print(container.children.count) // expect: 1
|
||||
System.print(container.firstChild.tagName) // expect: SPAN
|
||||
System.print(container.firstChild.textContent) // expect: New
|
||||
|
||||
var doc2 = Document.parse("<div><a>First</a><b>Second</b></div>")
|
||||
var div = doc2.querySelector("div")
|
||||
var a = doc2.querySelector("a")
|
||||
var replacement = doc2.createElement("em")
|
||||
replacement.textContent = "Replaced"
|
||||
div.replaceChild(replacement, a)
|
||||
System.print(div.firstChild.tagName) // expect: EM
|
||||
|
||||
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>One</p><span>Two</span><a>Three</a></div>")
|
||||
|
||||
var span = doc.querySelector("span")
|
||||
|
||||
System.print(span.previousSibling != null) // expect: true
|
||||
System.print(span.nextSibling != null) // expect: true
|
||||
|
||||
System.print(span.previousElementSibling.tagName) // expect: P
|
||||
System.print(span.nextElementSibling.tagName) // expect: A
|
||||
|
||||
var p = doc.querySelector("p")
|
||||
System.print(p.previousElementSibling == null) // expect: true
|
||||
System.print(p.nextElementSibling.tagName) // expect: SPAN
|
||||
|
||||
var a = doc.querySelector("a")
|
||||
System.print(a.previousElementSibling.tagName) // expect: SPAN
|
||||
System.print(a.nextElementSibling == null) // expect: true
|
||||
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>Para</p><span>Span</span><a>Link</a></div>")
|
||||
|
||||
var div = doc.querySelector("div")
|
||||
System.print(div.tagName) // expect: DIV
|
||||
|
||||
var p = doc.querySelector("p")
|
||||
System.print(p.tagName) // expect: P
|
||||
|
||||
var span = doc.querySelector("span")
|
||||
System.print(span.tagName) // expect: SPAN
|
||||
|
||||
var a = doc.querySelector("a")
|
||||
System.print(a.tagName) // expect: A
|
||||
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div>Hello <span>World</span></div>")
|
||||
|
||||
var div = doc.querySelector("div")
|
||||
System.print(div.textContent.contains("Hello")) // expect: true
|
||||
System.print(div.textContent.contains("World")) // expect: true
|
||||
|
||||
var span = doc.querySelector("span")
|
||||
System.print(span.textContent) // expect: World
|
||||
|
||||
span.textContent = "Universe"
|
||||
System.print(span.textContent) // expect: Universe
|
||||
|
||||
var empty = doc.createElement("p")
|
||||
System.print(empty.textContent) // expect:
|
||||
|
||||
empty.textContent = "New content"
|
||||
System.print(empty.textContent) // expect: New content
|
||||
|
||||
var nested = Document.parse("<div><p><span>Deep</span></p></div>")
|
||||
System.print(nested.querySelector("div").textContent.contains("Deep")) // expect: true
|
||||
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>A</p><p>B</p><p>C</p></div>")
|
||||
|
||||
var list = doc.querySelectorAll("p")
|
||||
System.print(list.count) // expect: 3
|
||||
|
||||
var empty = doc.querySelectorAll(".nonexistent")
|
||||
System.print(empty.count) // expect: 0
|
||||
|
||||
var single = doc.querySelectorAll("div")
|
||||
System.print(single.count) // expect: 1
|
||||
|
||||
var many = Document.parse("<ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li></ul>")
|
||||
var items = many.querySelectorAll("li")
|
||||
System.print(items.count) // expect: 5
|
||||
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>A</p><p>B</p><p>C</p></div>")
|
||||
|
||||
var list = doc.querySelectorAll("p")
|
||||
var count = 0
|
||||
var texts = []
|
||||
|
||||
list.forEach {|elem|
|
||||
count = count + 1
|
||||
texts.add(elem.textContent)
|
||||
}
|
||||
|
||||
System.print(count) // expect: 3
|
||||
System.print(texts[0]) // expect: A
|
||||
System.print(texts[1]) // expect: B
|
||||
System.print(texts[2]) // expect: C
|
||||
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>A</p><p>B</p><p>C</p></div>")
|
||||
|
||||
var list = doc.querySelectorAll("p")
|
||||
|
||||
System.print(list.item(0).textContent) // expect: A
|
||||
System.print(list.item(1).textContent) // expect: B
|
||||
System.print(list.item(2).textContent) // expect: C
|
||||
|
||||
System.print(list.item(0).tagName) // expect: P
|
||||
System.print(list.item(1).tagName) // expect: P
|
||||
System.print(list.item(2).tagName) // expect: P
|
||||
|
||||
var single = doc.querySelectorAll("div")
|
||||
System.print(single.item(0).tagName) // expect: DIV
|
||||
|
||||
Vendored
+22
@@ -0,0 +1,22 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>A</p><p>B</p><p>C</p></div>")
|
||||
|
||||
var list = doc.querySelectorAll("p")
|
||||
|
||||
System.print(list[0].textContent) // expect: A
|
||||
System.print(list[1].textContent) // expect: B
|
||||
System.print(list[2].textContent) // expect: C
|
||||
|
||||
System.print(list[0].tagName) // expect: P
|
||||
System.print(list[1].tagName) // expect: P
|
||||
System.print(list[2].tagName) // expect: P
|
||||
|
||||
var mixed = Document.parse("<div><span>X</span><a>Y</a><em>Z</em></div>")
|
||||
var children = mixed.querySelector("div").children
|
||||
System.print(children[0].tagName) // expect: SPAN
|
||||
System.print(children[1].tagName) // expect: A
|
||||
System.print(children[2].tagName) // expect: EM
|
||||
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>A</p><p>B</p><p>C</p></div>")
|
||||
|
||||
var nodeList = doc.querySelectorAll("p")
|
||||
var list = nodeList.toList
|
||||
|
||||
System.print(list.count) // expect: 3
|
||||
System.print(list[0].textContent) // expect: A
|
||||
System.print(list[1].textContent) // expect: B
|
||||
System.print(list[2].textContent) // expect: C
|
||||
|
||||
System.print(list is List) // expect: true
|
||||
|
||||
var empty = doc.querySelectorAll(".none").toList
|
||||
System.print(empty.count) // expect: 0
|
||||
System.print(empty is List) // expect: true
|
||||
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>Para</p><span>Span</span><a>Anchor</a></div>")
|
||||
|
||||
System.print(doc.querySelector("p + span") != null) // expect: true
|
||||
System.print(doc.querySelector("p + span").textContent) // expect: Span
|
||||
|
||||
System.print(doc.querySelector("span + a") != null) // expect: true
|
||||
System.print(doc.querySelector("span + a").textContent) // expect: Anchor
|
||||
|
||||
System.print(doc.querySelector("p + a") == null) // expect: true
|
||||
|
||||
System.print(doc.querySelector("a + p") == null) // expect: true
|
||||
|
||||
var multi = Document.parse("<div><p>A</p><p>B</p><p>C</p></div>")
|
||||
System.print(multi.querySelectorAll("p + p").count) // expect: 2
|
||||
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<a href='http://example.com' target='_blank' data-id='123'>Link</a>")
|
||||
|
||||
System.print(doc.querySelector("[href]") != null) // expect: true
|
||||
System.print(doc.querySelector("[target]") != null) // expect: true
|
||||
System.print(doc.querySelector("[data-id]") != null) // expect: true
|
||||
|
||||
System.print(doc.querySelector("[nonexistent]") == null) // expect: true
|
||||
|
||||
System.print(doc.querySelector("[href='http://example.com']") != null) // expect: true
|
||||
System.print(doc.querySelector("[target='_blank']") != null) // expect: true
|
||||
System.print(doc.querySelector("[data-id='123']") != null) // expect: true
|
||||
|
||||
System.print(doc.querySelector("[href='wrong']") == null) // expect: true
|
||||
|
||||
System.print(doc.querySelector("a[href]") != null) // expect: true
|
||||
System.print(doc.querySelector("a[target='_blank']") != null) // expect: true
|
||||
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>Direct</p><span><p>Nested</p></span></div>")
|
||||
|
||||
System.print(doc.querySelector("div > p") != null) // expect: true
|
||||
System.print(doc.querySelector("div > p").textContent) // expect: Direct
|
||||
|
||||
System.print(doc.querySelector("span > p") != null) // expect: true
|
||||
System.print(doc.querySelector("span > p").textContent) // expect: Nested
|
||||
|
||||
System.print(doc.querySelectorAll("div > p").count) // expect: 1
|
||||
|
||||
var nested = Document.parse("<ul><li>A</li><li><ul><li>B</li></ul></li></ul>")
|
||||
System.print(nested.querySelectorAll("ul > li").count) // expect: 3
|
||||
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div class='box'><p class='text highlight'>Text</p></div>")
|
||||
|
||||
System.print(doc.querySelector(".box") != null) // expect: true
|
||||
System.print(doc.querySelector(".box").tagName) // expect: DIV
|
||||
|
||||
System.print(doc.querySelector(".text") != null) // expect: true
|
||||
System.print(doc.querySelector(".highlight") != null) // expect: true
|
||||
|
||||
System.print(doc.querySelector(".nonexistent") == null) // expect: true
|
||||
|
||||
System.print(doc.querySelector("div.box") != null) // expect: true
|
||||
System.print(doc.querySelector("p.text") != null) // expect: true
|
||||
System.print(doc.querySelector("p.highlight") != null) // expect: true
|
||||
|
||||
System.print(doc.querySelector(".text.highlight") != null) // expect: true
|
||||
|
||||
Vendored
+19
@@ -0,0 +1,19 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='main' class='container'><p class='text'>Content</p></div>")
|
||||
|
||||
System.print(doc.querySelector("div#main.container") != null) // expect: true
|
||||
System.print(doc.querySelector("#main.container") != null) // expect: true
|
||||
System.print(doc.querySelector("div.container#main") != null) // expect: true
|
||||
|
||||
System.print(doc.querySelector("div#main.container p.text") != null) // expect: true
|
||||
System.print(doc.querySelector("#main > p.text") != null) // expect: true
|
||||
|
||||
System.print(doc.querySelector("div#main.wrong") == null) // expect: true
|
||||
System.print(doc.querySelector("span#main.container") == null) // expect: true
|
||||
|
||||
var complex = Document.parse("<form id='login'><input type='text' class='field' name='user'/></form>")
|
||||
System.print(complex.querySelector("form#login input.field[type='text']") != null) // expect: true
|
||||
|
||||
Vendored
+17
@@ -0,0 +1,17 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><section><p>Deep</p></section></div>")
|
||||
|
||||
System.print(doc.querySelector("div p") != null) // expect: true
|
||||
System.print(doc.querySelector("div section") != null) // expect: true
|
||||
System.print(doc.querySelector("section p") != null) // expect: true
|
||||
System.print(doc.querySelector("div section p") != null) // expect: true
|
||||
|
||||
System.print(doc.querySelector("div span") == null) // expect: true
|
||||
|
||||
var multi = Document.parse("<div><p>A</p><span><p>B</p></span></div>")
|
||||
System.print(multi.querySelectorAll("div p").count) // expect: 2
|
||||
System.print(multi.querySelectorAll("span p").count) // expect: 1
|
||||
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>Para</p><span>Span</span><a>Anchor</a><em>Em</em></div>")
|
||||
|
||||
System.print(doc.querySelector("p ~ span") != null) // expect: true
|
||||
System.print(doc.querySelector("p ~ a") != null) // expect: true
|
||||
System.print(doc.querySelector("p ~ em") != null) // expect: true
|
||||
|
||||
System.print(doc.querySelector("span ~ a") != null) // expect: true
|
||||
System.print(doc.querySelector("span ~ em") != null) // expect: true
|
||||
|
||||
System.print(doc.querySelector("a ~ p") == null) // expect: true
|
||||
System.print(doc.querySelector("em ~ p") == null) // expect: true
|
||||
|
||||
System.print(doc.querySelectorAll("p ~ *").count) // expect: 3
|
||||
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='first'><p id='second'>Text</p></div>")
|
||||
|
||||
System.print(doc.querySelector("#first") != null) // expect: true
|
||||
System.print(doc.querySelector("#first").tagName) // expect: DIV
|
||||
|
||||
System.print(doc.querySelector("#second") != null) // expect: true
|
||||
System.print(doc.querySelector("#second").tagName) // expect: P
|
||||
|
||||
System.print(doc.querySelector("#nonexistent") == null) // expect: true
|
||||
|
||||
System.print(doc.querySelector("div#first") != null) // expect: true
|
||||
System.print(doc.querySelector("p#second") != null) // expect: true
|
||||
System.print(doc.querySelector("span#first") == null) // expect: true
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<ul><li>A</li><li>B</li><li>C</li></ul>")
|
||||
|
||||
System.print(doc.querySelector("li:first-child") != null) // expect: true
|
||||
System.print(doc.querySelector("li:first-child").textContent) // expect: A
|
||||
|
||||
var multi = Document.parse("<div><p>First</p><span>Second</span></div><div><a>Third</a></div>")
|
||||
System.print(multi.querySelectorAll(":first-child").count >= 2) // expect: true
|
||||
|
||||
var nested = Document.parse("<div><span><em>Nested First</em></span></div>")
|
||||
System.print(nested.querySelector("em:first-child") != null) // expect: true
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<ul><li>A</li><li>B</li><li>C</li></ul>")
|
||||
|
||||
System.print(doc.querySelector("li:last-child") != null) // expect: true
|
||||
System.print(doc.querySelector("li:last-child").textContent) // expect: C
|
||||
|
||||
var multi = Document.parse("<div><p>First</p><span>Last</span></div>")
|
||||
System.print(multi.querySelector("span:last-child") != null) // expect: true
|
||||
System.print(multi.querySelector("span:last-child").textContent) // expect: Last
|
||||
|
||||
System.print(multi.querySelector("p:last-child") == null) // expect: true
|
||||
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<ul><li>A</li><li>B</li><li>C</li><li>D</li><li>E</li></ul>")
|
||||
|
||||
System.print(doc.querySelector("li:nth-child(1)") != null) // expect: true
|
||||
System.print(doc.querySelector("li:nth-child(1)").textContent) // expect: A
|
||||
|
||||
System.print(doc.querySelector("li:nth-child(3)") != null) // expect: true
|
||||
System.print(doc.querySelector("li:nth-child(3)").textContent) // expect: C
|
||||
|
||||
System.print(doc.querySelector("li:nth-child(5)") != null) // expect: true
|
||||
System.print(doc.querySelector("li:nth-child(5)").textContent) // expect: E
|
||||
|
||||
System.print(doc.querySelector("li:nth-child(6)") == null) // expect: true
|
||||
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><p>Para</p><span>Span</span><a>Anchor</a></div>")
|
||||
|
||||
System.print(doc.querySelector("div") != null) // expect: true
|
||||
System.print(doc.querySelector("p") != null) // expect: true
|
||||
System.print(doc.querySelector("span") != null) // expect: true
|
||||
System.print(doc.querySelector("a") != null) // expect: true
|
||||
|
||||
System.print(doc.querySelector("article") == null) // expect: true
|
||||
System.print(doc.querySelector("section") == null) // expect: true
|
||||
|
||||
System.print(doc.querySelectorAll("div").count) // expect: 1
|
||||
System.print(doc.querySelectorAll("p").count) // expect: 1
|
||||
|
||||
var nested = Document.parse("<div><div><div></div></div></div>")
|
||||
System.print(nested.querySelectorAll("div").count) // expect: 3
|
||||
|
||||
Vendored
+21
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='test' class='box'>Content</div>")
|
||||
|
||||
var html = doc.outerHTML
|
||||
System.print(html.contains("<div")) // expect: true
|
||||
System.print(html.contains("</div>")) // expect: true
|
||||
System.print(html.contains("Content")) // expect: true
|
||||
|
||||
var div = doc.querySelector("div")
|
||||
var outer = div.outerHTML
|
||||
System.print(outer.contains("<div")) // expect: true
|
||||
System.print(outer.contains("id=")) // expect: true
|
||||
System.print(outer.contains("class=")) // expect: true
|
||||
|
||||
var inner = div.innerHTML
|
||||
System.print(inner.contains("Content")) // expect: true
|
||||
System.print(inner.contains("<div") == false) // expect: true
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<p><tag></p>")
|
||||
System.print(doc.querySelector("p").textContent) // expect: <tag>
|
||||
|
||||
var doc2 = Document.parse("<p>&</p>")
|
||||
System.print(doc2.querySelector("p").textContent) // expect: &
|
||||
|
||||
var doc3 = Document.parse("<p>"quoted"</p>")
|
||||
System.print(doc3.querySelector("p").textContent) // expect: "quoted"
|
||||
|
||||
var doc4 = Document.parse("<p> </p>")
|
||||
var text4 = doc4.querySelector("p").textContent
|
||||
System.print(text4.count > 0) // expect: true
|
||||
|
||||
var doc5 = Document.parse("<p>© 2024</p>")
|
||||
var text5 = doc5.querySelector("p").textContent
|
||||
System.print(text5.contains("2024")) // expect: true
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div><br/><hr/><img src='test.jpg'/><input type='text'/></div>")
|
||||
|
||||
var div = doc.querySelector("div")
|
||||
var html = div.innerHTML
|
||||
|
||||
System.print(html.contains("br")) // expect: true
|
||||
System.print(html.contains("hr")) // expect: true
|
||||
System.print(html.contains("img")) // expect: true
|
||||
System.print(html.contains("input")) // expect: true
|
||||
|
||||
System.print(doc.querySelector("br") != null) // expect: true
|
||||
System.print(doc.querySelector("hr") != null) // expect: true
|
||||
System.print(doc.querySelector("img") != null) // expect: true
|
||||
System.print(doc.querySelector("input") != null) // expect: true
|
||||
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div></div>")
|
||||
|
||||
var original = doc.createTextNode("Original text")
|
||||
System.print(original.textContent) // expect: Original text
|
||||
|
||||
var clone = original.cloneNode(true)
|
||||
System.print(clone.textContent) // expect: Original text
|
||||
System.print(clone.nodeType) // expect: 3
|
||||
|
||||
clone.textContent = "Cloned text"
|
||||
System.print(original.textContent) // expect: Original text
|
||||
System.print(clone.textContent) // expect: Cloned text
|
||||
|
||||
var shallowClone = original.cloneNode(false)
|
||||
System.print(shallowClone.textContent) // expect: Original text
|
||||
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='container'></div>")
|
||||
var container = doc.getElementById("container")
|
||||
|
||||
var text = doc.createTextNode("Hello World")
|
||||
|
||||
System.print(text.textContent) // expect: Hello World
|
||||
System.print(text.nodeType) // expect: 3
|
||||
System.print(text.nodeName) // expect: #text
|
||||
System.print(text.nodeValue) // expect: Hello World
|
||||
|
||||
text.textContent = "Changed content"
|
||||
System.print(text.textContent) // expect: Changed content
|
||||
|
||||
text.nodeValue = "Another change"
|
||||
System.print(text.nodeValue) // expect: Another change
|
||||
System.print(text.textContent) // expect: Another change
|
||||
|
||||
container.appendChild(text)
|
||||
System.print(text.parentNode != null) // expect: true
|
||||
System.print(text.parentElement.tagName) // expect: DIV
|
||||
|
||||
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='container'>Text content</div>")
|
||||
var container = doc.getElementById("container")
|
||||
|
||||
System.print(container.textContent.contains("Text content")) // expect: true
|
||||
|
||||
var textNodes = container.childNodes
|
||||
System.print(textNodes.count >= 1) // expect: true
|
||||
|
||||
var text = doc.createTextNode("Extra text")
|
||||
container.appendChild(text)
|
||||
|
||||
var before = container.childNodes.count
|
||||
text.remove()
|
||||
var after = container.childNodes.count
|
||||
System.print(after < before) // expect: true
|
||||
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
// retoor <retoor@molodetz.nl>
|
||||
|
||||
import "html" for Document
|
||||
|
||||
var doc = Document.parse("<div id='container'></div>")
|
||||
var container = doc.getElementById("container")
|
||||
|
||||
var text1 = doc.createTextNode("First")
|
||||
var text2 = doc.createTextNode("Second")
|
||||
var text3 = doc.createTextNode("Third")
|
||||
|
||||
container.appendChild(text1)
|
||||
container.appendChild(text2)
|
||||
container.appendChild(text3)
|
||||
|
||||
System.print(text1.previousSibling == null) // expect: true
|
||||
System.print(text1.nextSibling != null) // expect: true
|
||||
|
||||
System.print(text2.previousSibling != null) // expect: true
|
||||
System.print(text2.nextSibling != null) // expect: true
|
||||
|
||||
System.print(text3.previousSibling != null) // expect: true
|
||||
System.print(text3.nextSibling == null) // expect: true
|
||||
|
||||
Reference in New Issue
Block a user