### Install parse5 Source: https://github.com/inikulin/parse5/blob/master/test/data/location-info/github-parse5/data.html Install the parse5 module using npm. This is the first step to using the parser in your Node.js project. ```bash $ npm install parse5 ``` -------------------------------- ### Scripting Example in HTML Parsing Source: https://github.com/inikulin/parse5/blob/master/test/data/huge-page/huge-page.html Illustrates how the tree construction stage can be re-entered while processing tokens, such as when document.write is used within a script tag. ```javascript document.write('
'); ``` -------------------------------- ### Equivalent PRE elements Source: https://github.com/inikulin/parse5/blob/master/test/data/huge-page/huge-page.html Demonstrates that a newline immediately after the start tag of a PRE element does not affect processing. ```html
Hello``` ```html
Hello``` -------------------------------- ### Create a Simple TCP Server in Node.js Source: https://github.com/inikulin/parse5/blob/master/test/data/sax/nodejsorg/src.html This example shows how to create a TCP server that listens on port 1337 and echoes back any data received. It utilizes the 'net' module and stream piping for bidirectional communication. ```javascript var net = require('net'); var server = net.createServer(function (socket) { socket.write('Echo server\r\n'); socket.pipe(socket); }); server.listen(1337, '127.0.0.1'); ``` -------------------------------- ### Empty Attribute Syntax Example Source: https://github.com/inikulin/parse5/blob/master/test/data/huge-page/huge-page.html Demonstrates the empty attribute syntax where only the attribute name is present. The attribute value is implicitly the empty string. Ensure a space separates it from the next attribute. ```html ``` -------------------------------- ### Google Ads Conversion Tracking Setup Source: https://github.com/inikulin/parse5/blob/master/test/data/location-info/dx/data.html This snippet sets up parameters for Google Ads conversion tracking, including the conversion ID and custom parameters. It's typically used in conjunction with Google Ads scripts. ```javascript var google_conversion_id = 1071759641; var google_custom_params = window.google_tag_params; var google_remarketing_only = true; ``` -------------------------------- ### HTML with Attributes and Optional Tags Source: https://github.com/inikulin/parse5/blob/master/test/data/huge-page/huge-page.html Shows how attributes on elements like 'html' and 'body' necessitate the inclusion of their start tags, even if other optional tags are omitted. ```html
Welcome to this example. ``` -------------------------------- ### Instantiate and Use Parser Source: https://github.com/inikulin/parse5/blob/master/test/data/location-info/github-parse5/data.html Demonstrates how to instantiate the Parser and use it to parse a full HTML document. ```APIDOC ## Instantiate and Use Parser ### Description Instantiate the `Parser` class and use its `parse` method to process a complete HTML document. ### Method ```javascript var Parser = require('parse5').Parser; var parser = new Parser(); var document = parser.parse('
Hi there!'); ``` ### Parameters - `htmlString` (string) - The HTML document string to parse. ``` -------------------------------- ### Omitting HTML Start Tag Source: https://github.com/inikulin/parse5/blob/master/test/data/huge-page/huge-page.html The html start tag can be omitted if the first thing inside is not a comment. This results in the same DOM. ```htmlWelcome to this example.
``` ```htmlWelcome to this example.
``` -------------------------------- ### Create a Simple HTTP Web Server in Node.js Source: https://github.com/inikulin/parse5/blob/master/test/data/sax/nodejsorg/src.html This snippet demonstrates how to create a basic HTTP server that responds with 'Hello World' to all incoming requests. It requires the 'http' module. ```javascript var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(1337, '127.0.0.1'); console.log('Server running at http://127.0.0.1:1337/'); ``` -------------------------------- ### Omitting HTML Start Tag with Comment Source: https://github.com/inikulin/parse5/blob/master/test/data/huge-page/huge-page.html Removing the html start tag when followed by a comment changes the parse tree, placing the comment before the html element. ```htmlWelcome to this example.
``` ```htmlWelcome to this example.
``` -------------------------------- ### Run a Node.js Server from the Command Line Source: https://github.com/inikulin/parse5/blob/master/test/data/sax/nodejsorg/expected.html This shows the command to execute a Node.js script. Ensure the code is saved in a file (e.g., 'example.js') before running. ```bash % node example.js Server running at http://127.0.0.1:1337/ ``` -------------------------------- ### Initialize Kissmetrics Tracking Source: https://github.com/inikulin/parse5/blob/master/test/data/location-info/dx/data.html This snippet initializes Kissmetrics tracking by setting the 'SignedIn' status. Ensure Kissmetrics is properly configured before using this. ```javascript var _kiq = _kiq || []; _kiq.push(['set', { 'SignedIn': 'false', }]); ``` -------------------------------- ### Get Cookie Value Source: https://github.com/inikulin/parse5/blob/master/test/data/location-info/whatwg-html/data.html Retrieves the value of a specified cookie. It first checks the URL query parameters and then the document's cookies. Returns null if the cookie is not found. ```javascript function getCookie(name) { var params = location.search.substr(1).split("&"); for (var index = 0; index < params.length; index++) { if (params[index] == name) return "1"; var data = params[index].split("="); if (data[0] == name) return unescape(data[1]); } var cookies = document.cookie.split("; "); for (var index = 0; index < cookies.length; index++) { var data = cookies[index].split("="); if (data[0] == name) return unescape(data[1]); } return null; } ``` -------------------------------- ### Initialize Page Scripts Source: https://github.com/inikulin/parse5/blob/master/test/data/huge-page/huge-page.html Initiates page loading by calling 'load' for various scripts based on URL parameters and document class. Includes a timeout for slow browsers. ```javascript var startedInit = 0; function init() { startedInit = 1; if (location.search == '?slow-browser') return; load('reviewer.js'); if (document.documentElement.className == "big" || document.documentElement.className == "big split index") load('toc.js'); load('updater.js'); load('dfn.js'); load('status.js'); if (getCookie('profile') == '1') document.getElementsByTagName('h2')[0].textContent += '; load: ' + (new Date() - loadTimer) + 'ms'; } ``` ```javascript if (document.documentElement.className == "") setTimeout(function () { if (!startedInit) showAlert("Too slow? Try reading the multipage copy of the spec instead:", "https://whatwg.org/html"); }, 6000); ``` -------------------------------- ### Single-Quoted Attribute Value Syntax Example Source: https://github.com/inikulin/parse5/blob/master/test/data/huge-page/huge-page.html Illustrates the single-quoted attribute value syntax. The value is enclosed in single apostrophes and cannot contain literal single apostrophes. A space is required before the next attribute. ```html ``` -------------------------------- ### Initialize Menu Controller Source: https://github.com/inikulin/parse5/blob/master/test/data/location-info/dx/data.html Initializes the menu controller for the website. This is a common pattern for managing navigation menus. ```javascript var _wsMenu = new _wsMenuController(); ``` -------------------------------- ### Double-Quoted Attribute Value Syntax Example Source: https://github.com/inikulin/parse5/blob/master/test/data/huge-page/huge-page.html Presents the double-quoted attribute value syntax. The value is enclosed in double quotes and cannot contain literal double quotes. A space is required before the next attribute. ```html ```