天天看點

d的html5解析器

import parserino;

void main()
{
   //Parserino修複你的html5
   Document doc = "<html>超文";
   assert(doc.toString() == "<html><head></head><body>超文本</body></html>");

   //給頁面設定标題
   doc.title = "你好";
   assert(doc.toString() == "<html><head><title>你好,世界</title></head><body>超文本</body></html>");

   //附加html片段
   doc.body.appendChild(`
   <a href="/first.html">first</a>
   <div>
      <a id="tochange" href="/second.html">second</a>
   </div>
   `.asFragment //不用.asFragment附加純文字
   );

   //建立并填充html元素
   auto newElement = doc.createElement("a");
   newElement.setAttribute("href", "third.html");
   newElement.innerText("third");
   doc.body.appendChild(newElement);   
   
   //可用選擇器選擇元素
   doc
   .bySelector("div a")    //在<div>塊,選擇全部<a>
   .frontOrThrow           //取區間的第一個元素或抛異常
   .innerText="changed!";  //更改内部文本

   assert(doc.body.byId("tochange").innerText == "changed!");
}      

繼續閱讀