test(nama, fungsi () { var src = fs. readFileSync(inputDir + '/' + nama + '. giok', 'utf8'); . parseDOM(fs. readFileSync(inputDir + '/' + nama + '. html', 'utf8')); . writeFileSync(outputDir + '/' + nama + '. giok', src); . compileFileClient(inputDir + '/' + nama + '. giok', {outputFile. outputDir + '/' + nama + '. js', berdasarkan. inputDir }); . writeFileSync(outputDir + '/' + nama + '. js', js); . mengejek(); . compileFile(inputDir + '/' + nama + '. giok', {outputFile. outputDir + '/' + nama + '. js', berdasarkan. inputDir }); . 'Giok'}); . filter(fungsi(elemen) { mengembalikan elemen. jenis. == 'teks' }). panjangnya. == 1; . anak-anak. sebenarnya; . mengatur ulang(); htmlparser2 adalah , dan mengambil beberapa jalan pintas untuk sampai ke sana. Jika Anda memerlukan kepatuhan spesifikasi HTML yang ketat, lihat parse5 Show Instalasi
Demo langsung EkosistemNameDescriptionhtmlparser2Cepat & memaafkan HTML/XML parserdomhandlerHandler untuk htmlparser2 yang mengubah dokumen menjadi DOMdomutilsUtilities untuk bekerja dengan mesin pemilih DOMcss-selectCSS domhandler, kompatibel dengan DOMcheerio domhandlerJQuery API untuk DOMdom-serializerSerializer domhandler untuk DOM domhandlerPenggunaan
const htmlparser2 = require("htmlparser2"); const parser = new htmlparser2.Parser({ onopentag(name, attributes) { /* * This fires when a new tag is opened. * * If you don't need an aggregated `attributes` object, * have a look at the `onopentagname` and `onattribute` events. */ if (name === "script" && attributes.type === "text/javascript") { console.log("JS! Hooray!"); } }, ontext(text) { /* * Fires whenever a section of text was processed. * * Note that this can fire at any point within text and you might * have to stich together multiple pieces. */ console.log("-->", text); }, onclosetag(tagname) { /* * Fires when a tag is closed. * * You can rely on this event only firing when you have received an * equivalent opening tag before. Closing tags without corresponding * opening tags will be ignored. */ if (tagname === "script") { console.log("That's it?!"); } }, }); parser.write( "Xyz <script type='text/javascript'>const foo = '<<bar>>';</ script>" ); parser.end();_ Keluaran (dengan gabungan beberapa peristiwa teks)
Contoh ini hanya menunjukkan tiga kemungkinan kejadian. Baca lebih lanjut tentang parser, acara, dan opsinya di wiki Penggunaan dengan aliranSementara antarmuka const htmlparser2 = require("htmlparser2"); const parser = new htmlparser2.Parser({ onopentag(name, attributes) { /* * This fires when a new tag is opened. * * If you don't need an aggregated `attributes` object, * have a look at the `onopentagname` and `onattribute` events. */ if (name === "script" && attributes.type === "text/javascript") { console.log("JS! Hooray!"); } }, ontext(text) { /* * Fires whenever a section of text was processed. * * Note that this can fire at any point within text and you might * have to stich together multiple pieces. */ console.log("-->", text); }, onclosetag(tagname) { /* * Fires when a tag is closed. * * You can rely on this event only firing when you have received an * equivalent opening tag before. Closing tags without corresponding * opening tags will be ignored. */ if (tagname === "script") { console.log("That's it?!"); } }, }); parser.write( "Xyz <script type='text/javascript'>const foo = '<<bar>>';</ script>" ); parser.end();_0 untuk memproses input streaming const { WritableStream } = require("htmlparser2/lib/WritableStream"); const parserStream = new WritableStream({ ontext(text) { console.log("Streaming:", text); }, }); const htmlStream = fs.createReadStream("./my-file.html"); htmlStream.pipe(parserStream).on("finish", () => console.log("done"));_ Mendapatkan DOMconst htmlparser2 = require("htmlparser2"); const parser = new htmlparser2.Parser({ onopentag(name, attributes) { /* * This fires when a new tag is opened. * * If you don't need an aggregated `attributes` object, * have a look at the `onopentagname` and `onattribute` events. */ if (name === "script" && attributes.type === "text/javascript") { console.log("JS! Hooray!"); } }, ontext(text) { /* * Fires whenever a section of text was processed. * * Note that this can fire at any point within text and you might * have to stich together multiple pieces. */ console.log("-->", text); }, onclosetag(tagname) { /* * Fires when a tag is closed. * * You can rely on this event only firing when you have received an * equivalent opening tag before. Closing tags without corresponding * opening tags will be ignored. */ if (tagname === "script") { console.log("That's it?!"); } }, }); parser.write( "Xyz <script type='text/javascript'>const foo = '<<bar>>';</ script>" ); parser.end();1 menghasilkan DOM (model objek dokumen) yang dapat dimanipulasi menggunakan helper const htmlparser2 = require("htmlparser2"); const parser = new htmlparser2.Parser({ onopentag(name, attributes) { /* * This fires when a new tag is opened. * * If you don't need an aggregated `attributes` object, * have a look at the `onopentagname` and `onattribute` events. */ if (name === "script" && attributes.type === "text/javascript") { console.log("JS! Hooray!"); } }, ontext(text) { /* * Fires whenever a section of text was processed. * * Note that this can fire at any point within text and you might * have to stich together multiple pieces. */ console.log("-->", text); }, onclosetag(tagname) { /* * Fires when a tag is closed. * * You can rely on this event only firing when you have received an * equivalent opening tag before. Closing tags without corresponding * opening tags will be ignored. */ if (tagname === "script") { console.log("That's it?!"); } }, }); parser.write( "Xyz <script type='text/javascript'>const foo = '<<bar>>';</ script>" ); parser.end();2 const htmlparser2 = require("htmlparser2"); const dom = htmlparser2.parseDocument(htmlString); const htmlparser2 = require("htmlparser2"); const parser = new htmlparser2.Parser({ onopentag(name, attributes) { /* * This fires when a new tag is opened. * * If you don't need an aggregated `attributes` object, * have a look at the `onopentagname` and `onattribute` events. */ if (name === "script" && attributes.type === "text/javascript") { console.log("JS! Hooray!"); } }, ontext(text) { /* * Fires whenever a section of text was processed. * * Note that this can fire at any point within text and you might * have to stich together multiple pieces. */ console.log("-->", text); }, onclosetag(tagname) { /* * Fires when a tag is closed. * * You can rely on this event only firing when you have received an * equivalent opening tag before. Closing tags without corresponding * opening tags will be ignored. */ if (tagname === "script") { console.log("That's it?!"); } }, }); parser.write( "Xyz <script type='text/javascript'>const foo = '<<bar>>';</ script>" ); parser.end();1, meskipun masih dibundel dengan modul ini, dipindahkan ke modulnya sendiri. Lihat itu untuk informasi lebih lanjut Mengurai Umpan RSS/RDF/Atomconst feed = htmlparser2.parseFeed(content, options);_ Catatan. Meskipun penangan umpan yang disediakan berfungsi untuk sebagian besar umpan, Anda mungkin ingin menggunakan danmacough/node-feedparser, yang jauh lebih baik diuji dan dipelihara secara aktif PertunjukanSetelah memiliki beberapa tolok ukur buatan selama beberapa waktu, @AndreasMadsen menerbitkan const htmlparser2 = require("htmlparser2"); const parser = new htmlparser2.Parser({ onopentag(name, attributes) { /* * This fires when a new tag is opened. * * If you don't need an aggregated `attributes` object, * have a look at the `onopentagname` and `onattribute` events. */ if (name === "script" && attributes.type === "text/javascript") { console.log("JS! Hooray!"); } }, ontext(text) { /* * Fires whenever a section of text was processed. * * Note that this can fire at any point within text and you might * have to stich together multiple pieces. */ console.log("-->", text); }, onclosetag(tagname) { /* * Fires when a tag is closed. * * You can rely on this event only firing when you have received an * equivalent opening tag before. Closing tags without corresponding * opening tags will be ignored. */ if (tagname === "script") { console.log("That's it?!"); } }, }); parser.write( "Xyz <script type='text/javascript'>const foo = '<<bar>>';</ script>" ); parser.end();4 miliknya, yang mem-parse HTML tolok ukur berdasarkan situs web dunia nyata Pada saat penulisan, versi terbaru dari semua parser yang didukung menunjukkan karakteristik kinerja berikut pada GitHub Actions (bersumber dari sini)
Apa perbedaan modul ini dengan node-htmlparser?Pada tahun 2011, modul ini dimulai sebagai percabangan dari modul const htmlparser2 = require("htmlparser2"); const parser = new htmlparser2.Parser({ onopentag(name, attributes) { /* * This fires when a new tag is opened. * * If you don't need an aggregated `attributes` object, * have a look at the `onopentagname` and `onattribute` events. */ if (name === "script" && attributes.type === "text/javascript") { console.log("JS! Hooray!"); } }, ontext(text) { /* * Fires whenever a section of text was processed. * * Note that this can fire at any point within text and you might * have to stich together multiple pieces. */ console.log("-->", text); }, onclosetag(tagname) { /* * Fires when a tag is closed. * * You can rely on this event only firing when you have received an * equivalent opening tag before. Closing tags without corresponding * opening tags will be ignored. */ if (tagname === "script") { console.log("That's it?!"); } }, }); parser.write( "Xyz <script type='text/javascript'>const foo = '<<bar>>';</ script>" ); parser.end();5. htmlparser2 _ telah ditulis ulang beberapa kali dan, meskipun mempertahankan API yang sebagian besar kompatibel dengan const htmlparser2 = require("htmlparser2"); const parser = new htmlparser2.Parser({ onopentag(name, attributes) { /* * This fires when a new tag is opened. * * If you don't need an aggregated `attributes` object, * have a look at the `onopentagname` and `onattribute` events. */ if (name === "script" && attributes.type === "text/javascript") { console.log("JS! Hooray!"); } }, ontext(text) { /* * Fires whenever a section of text was processed. * * Note that this can fire at any point within text and you might * have to stich together multiple pieces. */ console.log("-->", text); }, onclosetag(tagname) { /* * Fires when a tag is closed. * * You can rely on this event only firing when you have received an * equivalent opening tag before. Closing tags without corresponding * opening tags will be ignored. */ if (tagname === "script") { console.log("That's it?!"); } }, }); parser.write( "Xyz <script type='text/javascript'>const foo = '<<bar>>';</ script>" ); parser.end();5 dalam banyak kasus, proyek tidak lagi membagikan kode apa pun Parser sekarang menyediakan antarmuka callback yang terinspirasi oleh sax. js (awalnya ditargetkan untuk readabilitySAX). Akibatnya, penangan lama tidak akan berfungsi lagi const htmlparser2 = require("htmlparser2"); const parser = new htmlparser2.Parser({ onopentag(name, attributes) { /* * This fires when a new tag is opened. * * If you don't need an aggregated `attributes` object, * have a look at the `onopentagname` and `onattribute` events. */ if (name === "script" && attributes.type === "text/javascript") { console.log("JS! Hooray!"); } }, ontext(text) { /* * Fires whenever a section of text was processed. * * Note that this can fire at any point within text and you might * have to stich together multiple pieces. */ console.log("-->", text); }, onclosetag(tagname) { /* * Fires when a tag is closed. * * You can rely on this event only firing when you have received an * equivalent opening tag before. Closing tags without corresponding * opening tags will be ignored. */ if (tagname === "script") { console.log("That's it?!"); } }, }); parser.write( "Xyz <script type='text/javascript'>const foo = '<<bar>>';</ script>" ); parser.end();8 dan const htmlparser2 = require("htmlparser2"); const parser = new htmlparser2.Parser({ onopentag(name, attributes) { /* * This fires when a new tag is opened. * * If you don't need an aggregated `attributes` object, * have a look at the `onopentagname` and `onattribute` events. */ if (name === "script" && attributes.type === "text/javascript") { console.log("JS! Hooray!"); } }, ontext(text) { /* * Fires whenever a section of text was processed. * * Note that this can fire at any point within text and you might * have to stich together multiple pieces. */ console.log("-->", text); }, onclosetag(tagname) { /* * Fires when a tag is closed. * * You can rely on this event only firing when you have received an * equivalent opening tag before. Closing tags without corresponding * opening tags will be ignored. */ if (tagname === "script") { console.log("That's it?!"); } }, }); parser.write( "Xyz <script type='text/javascript'>const foo = '<<bar>>';</ script>" ); parser.end();9 diganti namanya untuk memperjelas tujuan mereka (menjadi const htmlparser2 = require("htmlparser2"); const parser = new htmlparser2.Parser({ onopentag(name, attributes) { /* * This fires when a new tag is opened. * * If you don't need an aggregated `attributes` object, * have a look at the `onopentagname` and `onattribute` events. */ if (name === "script" && attributes.type === "text/javascript") { console.log("JS! Hooray!"); } }, ontext(text) { /* * Fires whenever a section of text was processed. * * Note that this can fire at any point within text and you might * have to stich together multiple pieces. */ console.log("-->", text); }, onclosetag(tagname) { /* * Fires when a tag is closed. * * You can rely on this event only firing when you have received an * equivalent opening tag before. Closing tags without corresponding * opening tags will be ignored. */ if (tagname === "script") { console.log("That's it?!"); } }, }); parser.write( "Xyz <script type='text/javascript'>const foo = '<<bar>>';</ script>" ); parser.end();1 dan 1). Nama lama masih tersedia saat membutuhkan htmlparser2 , kode Anda harus berfungsi seperti yang diharapkanInformasi kontak keamananUntuk melaporkan kerentanan keamanan, harap gunakan kontak keamanan Tidelift. Tidelift akan mengoordinasikan perbaikan dan pengungkapan htmlparser2_ untuk perusahaanTersedia sebagai bagian dari Langganan Tidelift Pemelihara |