### Install CMake Dependencies Source: https://github.com/serpapi/nokolexbor/blob/master/README.md Commands to install CMake, which is required for compiling C extensions if pre-compiled gems are not available for your platform. ```bash # macOS brew install cmake # Linux (Debian, Ubuntu, etc.) sudo apt-get install cmake ``` -------------------------------- ### Install Nokolexbor via Gemfile Source: https://github.com/serpapi/nokolexbor/blob/master/README.md Add the Nokolexbor gem to your Ruby project's Gemfile to enable high-performance HTML parsing. ```ruby gem 'nokolexbor' ``` -------------------------------- ### Install CSS Function Source: https://github.com/serpapi/nokolexbor/blob/master/bench/coffee.html A placeholder JavaScript function for installing CSS. It currently does nothing, indicating it might be a stub for future implementation or a feature that is conditionally enabled. ```javascript function \_F\_installCss(c){} ``` -------------------------------- ### Batch Operations on HTML Elements with Nokolexbor Source: https://context7.com/serpapi/nokolexbor/llms.txt Illustrates how to perform batch operations on multiple HTML elements simultaneously, such as adding classes, setting attributes, removing attributes, and wrapping nodes. It also covers searching within a NodeSet and getting children of nodes. ```ruby items.add_class('highlight') items.attr('data-processed', 'true') items.attr('data-index') { |node| items.index(node).to_s } items.remove_attr('data-processed') nested = items.css('.active') puts nested.length items.wrap('
') children = items.children puts children.length ``` -------------------------------- ### Search Nodes with CSS and XPath Source: https://github.com/serpapi/nokolexbor/wiki/Cheat-sheet Provides examples of querying the DOM using CSS selectors and XPath expressions, including methods for finding single nodes or collections of nodes. ```ruby doc.css('div > span.a') doc.at_css('div > span.a') doc.xpath('//div / span[@class="a"]') doc.at_xpath('//div / div / text()') ``` -------------------------------- ### Serializing HTML Nodes and Node Sets with Nokolexbor Source: https://context7.com/serpapi/nokolexbor/llms.txt Covers how to serialize Nokolexbor nodes and node sets into HTML strings. This includes getting text content, inner HTML, outer HTML, and writing the serialized HTML to IO objects. It demonstrates various methods like `content`, `text`, `inner_html`, `outer_html`, `to_html`, `serialize`, `to_s`, and `write_to`. ```ruby doc = Nokolexbor::HTML <<-HTML

Introduction

HTML node = doc.at_css('#content') puts node.content puts node.text puts node.inner_html puts node.outer_html puts node.to_html puts node.serialize puts node.to_s File.open('output.html', 'w') do |f| node.write_to(f) end ``` -------------------------------- ### DOM Traversal in Nokolexbor Source: https://context7.com/serpapi/nokolexbor/llms.txt Explains how to navigate the DOM tree in Nokolexbor, including moving between parent, child, and sibling nodes. It covers methods for getting ancestors, children, and descendants, as well as filtering these relationships using CSS selectors. Requires the Nokolexbor library. ```ruby doc = Nokolexbor::HTML <<-HTML

Title

First paragraph

Second paragraph

HTML article = doc.at_css('article') # Parent navigation puts article.parent.name # => "main" # Get ancestors article.ancestors.each { |a| puts a.name } # => "main" # => "div" # => "body" # => "html" # Filter ancestors with selector puts article.ancestors('div').first['id'] # => "root" # Child navigation puts article.child.name # => "h1" puts article.children.map(&:name).inspect # => ["h1", "p", "p"] # Element-specific children (excludes text nodes) puts article.elements.length # => 3 puts article.first_element_child.name # => "h1" puts article.last_element_child.content # => "Second paragraph" # Sibling navigation nav = doc.at_css('nav') link = nav.at_css('a') puts link.next.name # => "a" puts link.next.content # => "About" puts link.next.previous.content # => "Home" # Element siblings only header = doc.at_css('header') puts header.next_element.name # => "main" # Traverse all descendants article.traverse do |node| puts "#{node.name}: #{node.content.strip}" if node.element? end # => "article: TitleFirst paragraphSecond paragraph" # => "h1: Title" # => "p: First paragraph" # => "p: Second paragraph" ``` -------------------------------- ### Working with Attributes in Nokolexbor Source: https://context7.com/serpapi/nokolexbor/llms.txt Details how to get, set, and delete attributes on HTML nodes using Nokolexbor. It also covers iterating over attributes and specifically manipulating CSS classes, including adding and removing them. Requires the Nokolexbor library. ```ruby doc = Nokolexbor::HTML('
') node = doc.at_css('div') # Get attribute value puts node['class'] # => "box primary" puts node['data-id'] # => "123" # Set attribute value node['data-active'] = 'true' node['id'] = 'main-box' # Check for attribute puts node.key?('style') # => true # Get all attribute names and values puts node.keys.inspect # => ["class", "data-id", "style", "data-active", "id"] puts node.values.inspect # => ["box primary", "123", "color:red", "true", "main-box"] # Delete attribute node.delete('style') # Iterate over attributes node.each do |name, value| puts "#{name}: #{value}" end # Work with CSS classes puts node.classes # => ["box", "primary"] node.add_class('highlight') # => class="box primary highlight" node.add_class(['active', 'visible']) # => class="box primary highlight active visible" node.remove_class('primary') # => class="box highlight active visible" # Get attribute as Attribute object attr = node.attribute('class') puts attr.name # => "class" puts attr.value # => "box highlight active visible" # Navigate attributes next_attr = attr.next puts next_attr.name # => "data-id" ``` -------------------------------- ### Initialize Nokolexbor Documents Source: https://github.com/serpapi/nokolexbor/wiki/Cheat-sheet Demonstrates various ways to instantiate a Nokolexbor document, including creating blank documents, parsing HTML strings, or reading from external sources. ```ruby doc = Nokolexbor::Document.new doc = Nokolexbor::HTML('') doc = Nokolexbor::HTML('
') doc = Nokolexbor::HTML(URI.open('https://github.com/serpapi/nokolexbor')) ``` -------------------------------- ### Building HTML Documents Programmatically with Nokolexbor DSL Source: https://context7.com/serpapi/nokolexbor/llms.txt Demonstrates how to build HTML documents programmatically using Nokolexbor's HTML Builder DSL. It covers the no-argument block style, block parameter style, fluent class and ID API, building into existing nodes, and inserting raw HTML like text, comments, and CDATA. ```ruby html = Nokolexbor do html do head do title 'My Page' meta charset: 'utf-8' end body do header do h1 'Welcome' nav do ul do li { a 'Home', href: '/' } li { a 'About', href: '/about' } end end end main do article do h2 'Article Title' p 'First paragraph content.' p 'Second paragraph content.' end end footer do p '2024 My Site' end end end end puts html.to_html doc = Nokolexbor::Builder.new do |b| b.div class: 'container' do b.h1 'Hello World' b.p 'This is content' end end puts doc.to_html doc = Nokolexbor do div.container.main! do p.intro.highlight 'Styled paragraph' end end puts doc.to_html existing_doc = Nokolexbor::HTML('
') target = existing_doc.at_css('#target') Nokolexbor::Builder.with(target) do span 'Injected content' ul do li 'Item 1' li 'Item 2' end end puts target.to_html doc = Nokolexbor do div do self << '

Raw HTML

' text 'Plain text node' comment 'This is a comment' cdata 'CDATA content' end end puts doc.to_html ``` -------------------------------- ### Create and Configure Nodes Source: https://github.com/serpapi/nokolexbor/wiki/Cheat-sheet Explains how to create various types of nodes including elements, text nodes, comments, CDATA, and processing instructions within a document context. ```ruby doc = Nokolexbor::HTML('') doc.create_element("div", class: "a") doc.create_text_node("Some text") doc.create_comment("Some comment") doc.create_cdata("Some CDATA") Nokolexbor::ProcessingInstruction.new("xml", "some data", doc) ``` -------------------------------- ### HTML Builder DSL Source: https://context7.com/serpapi/nokolexbor/llms.txt Illustrates how to programmatically build HTML documents using Nokolexbor's fluent DSL. ```APIDOC ## HTML Builder DSL ### Description This section explains how to construct HTML documents programmatically using Nokolexbor's Domain Specific Language (DSL). ### No-Argument Block Style ```ruby html = Nokolexbor do html do head do title 'My Page' meta charset: 'utf-8' end body do header do h1 'Welcome' nav do ul do li { a 'Home', href: '/' } li { a 'About', href: '/about' } end end end main do article do h2 'Article Title' p 'First paragraph content.' p 'Second paragraph content.' end end footer do p '2024 My Site' end end end end puts html.to_html ``` ### Block Parameter Style ```ruby doc = Nokolexbor::Builder.new do |b| b.div class: 'container' do b.h1 'Hello World' b.p 'This is content' end end puts doc.to_html # =>

Hello World

This is content

``` ### Fluent Class and ID API ```ruby doc = Nokolexbor do div.container.main! do p.intro.highlight 'Styled paragraph' end end puts doc.to_html # =>

Styled paragraph

``` ### Building into Existing Node ```ruby # Build into existing node existing_doc = Nokolexbor::HTML('
') target = existing_doc.at_css('#target') Nokolexbor::Builder.with(target) do span 'Injected content' ul do li 'Item 1' li 'Item 2' end end puts target.to_html # =>
Injected content
``` ### Inserting Raw HTML ```ruby doc = Nokolexbor do div do self << '

Raw HTML

' text 'Plain text node' comment 'This is a comment' cdata 'CDATA content' end end puts doc.to_html ``` ``` -------------------------------- ### Configure LibXML2 Build Options and Features Source: https://github.com/serpapi/nokolexbor/blob/master/ext/nokolexbor/CMakeLists.txt This snippet initializes the project, defines user-configurable build options for various LibXML2 modules, and sets up the necessary environment for cross-platform compilation. ```cmake cmake_minimum_required(VERSION 2.8.12) IF(CMAKE_VERSION VERSION_LESS "3.0") project(libxml2) ELSE() cmake_policy(SET CMP0048 NEW) project(libxml2 VERSION "2.11.0") ENDIF() option(BUILD_SHARED_LIBS "Build shared libraries" OFF) set(LIBXML2_WITH_HTML ON) set(LIBXML2_WITH_THREADS ON) if(LIBXML2_WITH_THREADS) find_package(Threads REQUIRED) endif() ``` -------------------------------- ### Nokogiri-Compatible CSS Methods in Nokolexbor Source: https://context7.com/serpapi/nokolexbor/llms.txt Shows how to use Nokolexbor's `nokogiri_css` and `nokogiri_at_css` methods, which leverage libxml2 for mixed CSS and XPath syntax support, similar to Nokogiri. This is useful for compatibility and advanced selector needs. ```ruby doc = Nokolexbor::HTML("
\n

First paragraph

\n

Second paragraph

\n
") results = doc.nokogiri_css('div p') puts results.length first = doc.nokogiri_at_css('div p') puts first.content ``` -------------------------------- ### Load Configuration Arrays (JavaScript) Source: https://github.com/serpapi/nokolexbor/blob/master/bench/coffee.html Loads multiple configuration arrays into the 'window.W_jd' object. These arrays likely contain various settings and parameters for different Google features. ```javascript var m=['CRKV1k','[\x22psy-ab\x22,\x22gws-wiz\x22,\x22Coffee\x22,\x22\x22,null,1,0,null,0,11,\x22en\x22,\x225pdKV-UdTxzHGnKMb4vYfDWTXyE\x22,\x22\x22,\x22ZDOvXdetDYG9hwPfqLfIBA\x22,0,\x22en\x22,null,null,null,null,null,3,null,null,5,null,null,null,null,null,0,0,0,null,null,null,null,-1,null,0,null,0,0,\x22\x22,0,null,0,\x22\x22,null,0,null,0]\n','CRKV1o','[0,0,0,[null,null,[[[3,null,null,[null,[["qdr_ ",1,6]\n,["qdr_h",0,6]\n,["qdr_d",0,6]\n,["qdr_w",0,6]\n,["qdr_m",0,6]\n,["qdr_y",0,6]\n,["cdr_opt",0,1,[1,\x22Custom range...\x22,null,\x22cdr:1,cd_min:x,cd_max:x\x22,\x22\x22,\x22text\x22,\x22\x22,\x22\x22,6,1,[ [ [\x22q\x22,\x22Coffee\x22 ]\n, [\x22num\x22,\x22100\x22 ]\n, [\x22gl\x22,\x22us\x22 ]\n, [\x22hl\x22,\x22en\x22 ]\n ]\n ]\n,\x22cdr_opt\x22,\x225/23/2004\x22,0 ]\n ]\n ],0]\n ],3,null,null,[null,[["li_ ",1,6]\n,["li_1",0,6]\n ]\n,1]\n ]\n ],null,[\x22tbs\x22 ]\n ]\n ],null,null,null,null,1]\n','CRKV1s','[null,null,1,30000,null,null,null,2,null,null,3,null,null,null,null,null,1,null,null,null,null,null,null,[37.09024,-95.712891 ],null,null,null,null,0,null,null,null,null,null,null,null,0,\x221571763044\x22,null,null,null,null,null,1,null,null,[\x2286400000\x22,\x22604800000\x22,2.0 ]\n,null,1]\n','CRKV1w','[1,null,null,1188,1276,0]\n']; ``` ```javascript var a=m;window.W_jd=window.W_jd||{};for(var b=0;b
Widget A
Widget B
HTML # Find all product divs products = doc.xpath('//div[@class="product"]') puts products.length # => 2 # Find first product name name = doc.at_xpath('//div[@class="product"]/span[@class="name"]') puts name.content # => "Widget A" # Use predicates expensive = doc.xpath('//div[@data-price > 40]') puts expensive.at_css('.name').content # => "Widget B" # Multiple XPath expressions results = doc.xpath('//div[@id="products"]', '//span[@class="name"]') results.each { |node| puts node.name } # => "div" # => "span" # => "span" # Select text nodes text = doc.at_xpath('//span[@class="name"]/text()') puts text.content # => "Widget A" ``` -------------------------------- ### Creating and Manipulating HTML Document Fragments with Nokolexbor Source: https://context7.com/serpapi/nokolexbor/llms.txt Explains how to create and manipulate HTML fragments using Nokolexbor. This includes parsing fragments from strings, creating fragments in the context of a node, and adding fragment children to the document. It also shows how fragments are reparented. ```ruby doc = Nokolexbor::HTML('
') container = doc.at_css('#container') fragment = doc.fragment('

First

Second

') puts fragment.to_html fragment = Nokolexbor::DocumentFragment.parse('Standalone') puts fragment.children.length node = doc.at_css('#container') fragment = node.fragment('Link') container.add_child(fragment) puts container.to_html puts fragment.children.length container.inner_html = '' html = '
Header
Content
' container.add_child(doc.fragment(html)) puts container.children.map(&:name).inspect ``` -------------------------------- ### Working with NodeSet Collections in Nokolexbor Source: https://context7.com/serpapi/nokolexbor/llms.txt Demonstrates how to work with NodeSet objects, which are collections of nodes returned by searches in Nokolexbor. It covers common Enumerable methods like length, first, last, and accessing elements by index, as well as slicing NodeSets using ranges. Requires the Nokolexbor library. ```ruby doc = Nokolexbor::HTML <<-HTML HTML items = doc.css('li.item') # Enumerable methods puts items.length # => 4 puts items.first.content # => "Item 1" puts items.last.content # => "Item 4" puts items[2].content # => "Item 3" # Slice with range puts items[1..2].map(&:content).inspect # => ["Item 2", "Item 3"] ``` -------------------------------- ### Initialize Global Data Object (JavaScript) Source: https://github.com/serpapi/nokolexbor/blob/master/bench/coffee.html Initializes the 'window.WIZ_global_data' object with various configuration parameters. This object likely holds settings for UI elements, internationalization, and other global states. ```javascript window.WIZ_global_data={"Yllh3e":"%.@.1571763044218839,174186113,1225643103]\n","zChJod":"%.@.\n","pxO4Zd":"0","GWsdKe":"en-US","SIsrTd":"false","LU5fGb":"false","w2btAe":"%.@.\"105250506097979753968\", \"105250506097979753968\", \"0\",null,null,null,1]\n","mXOY5d":"%.@.null,1]\n","SsQ4x":"X0LBNIYgkphIkSS4SE7qww\u003d\u003d","IvNqzc":"true","j9ZYRc":"true","S1ZUDf":"true","FF58Bf":"true","WWz0vd":"false","kePaAc":"-4","fFepd":"-15","KdGe5c":"false","PPBBUe":"true","y2Hs7d":"false","MiTaT":"#1A73E8","F4ULTd":"true","tqXjO":"false","wB1JTc":"false","dTMnbc":"false","dAFa0e":"false","q7vEEb":"false","osNyZ":"1.0","Rnt1Qc":"false","L6WyEf":"true","gjAni":"#3c4043","vTqjW":"false","N31mf":"false","Vzh4p":"0px 7px 6px -5px #DFE1E5","u4NTBb":"40","kkVYtc":"false","g635Ab":"2px","wFGKdc":"false","klgere":"invert(1) hue-rotate(180deg)","ConpTc":"false","rRLesf":"0","sPiebd":"false","aKuscd":"#AECBFA","KD9WYe":"#1A73E8","QSF9Ec":"","PIn0Ab":"1px solid #DFE1E5","QldWK":"6","BYw9f":"false","R9GQh":"#fff","xBx7Dc":"36","NIYCKf":"28","pPtK5":"6","PPUcAe":"0px 0px 8px rgba(32, 33, 36, .24)","NvsKMd":"true","tyCgpc":"#fff","H7aRye":"0px 5px 26px 0px rgba(0, 0, 0, 0.22), 0px 20px 28px 0px rgba(0, 0, 0, 0.30)","D0QAed":"false","voc95c":"false","n1Whrf":"false","U6xP0":"#4285f4","j0DpSe":"false","bsZ7ec":} ``` -------------------------------- ### Creating HTML Elements Programmatically in Ruby with Nokolexbor Source: https://context7.com/serpapi/nokolexbor/llms.txt Explains how to programmatically create HTML elements, text nodes, comments, and CDATA sections using Nokolexbor. It covers creating elements with attributes, content, and using block syntax. ```ruby doc = Nokolexbor::HTML('') # Create element with various options div = doc.create_element('div') # =>
div_with_class = doc.create_element('div', class: 'container', id: 'main') # =>
div_with_content = doc.create_element('div', 'Hello World', class: 'greeting') # =>
Hello World
# Create with block link = doc.create_element('a') do |node| node['href'] = 'https://example.com' node['target'] = '_blank' node.content = 'Click here' end # => Click here # Alternative: use class constructors span = Nokolexbor::Element.new('span', doc) # => # Create text nodes text = doc.create_text_node('Some text content') # => "Some text content" ``` -------------------------------- ### Manage Document Properties and Root Nodes Source: https://github.com/serpapi/nokolexbor/wiki/Cheat-sheet Shows how to access and modify document-level properties like the title, retrieve the root node, and perform basic CSS queries on the document object. ```ruby doc = Nokolexbor.HTML('This is title
Text
') doc.title doc.title = 'New title' doc.root doc.at_css('div') ``` -------------------------------- ### Write to StringIO and Serialize NodeSet Source: https://context7.com/serpapi/nokolexbor/llms.txt Demonstrates writing HTML content to a StringIO object and serializing NodeSet elements to HTML or plain text. Uses Ruby's StringIO for in-memory text buffering. ```ruby require 'stringio' io = StringIO.new doc.write_to(io) puts io.string items = doc.css('li') puts items.to_html puts items.content ``` -------------------------------- ### Implement Transition and Visibility Styles Source: https://github.com/serpapi/nokolexbor/blob/master/bench/coffee.html This snippet handles the transition logic and visibility states for collapsible UI components, using CSS transitions to manage max-height properties. ```css .xpdclps, .xpdxpnd { overflow: hidden; -webkit-transition: max-height 0.3s; } .xpdxpnd, .xpdopen .xpdclps, .xpdopen .xpdxpnd.xpdnoxpnd { max-height: 0; } .xpdopen .xpdxpnd { max-height: none; } .nojsv { visibility: visible; } ``` -------------------------------- ### Iterating and Filtering HTML Elements with Nokolexbor Source: https://context7.com/serpapi/nokolexbor/llms.txt Demonstrates how to iterate through HTML elements, select specific elements based on criteria, and extract combined content. It also shows how to perform set operations like union, intersection, and difference on node sets. ```ruby items.each_with_index do |item, i| puts "#{i}: #{item.content}" end active = items.select { |item| item['class'].include?('active') } puts active.map(&:content).inspect puts items.content puts items.text puts items.inner_html puts items.to_html active_items = doc.css('li.active') all_items = doc.css('li') union = active_items | all_items intersection = active_items & all_items difference = all_items - active_items puts difference.map(&:content).inspect ``` -------------------------------- ### Track Performance Metrics and Timings Source: https://github.com/serpapi/nokolexbor/blob/master/bench/coffee.html This snippet provides functions to initialize and record performance markers using the browser's performance API. It allows for tracking specific application events and reporting them for performance analysis. ```javascript google.startTick = function(a) { google.timers[a] = {t: {start: google.time()}, e: {}, m: {}}; }; google.tick = function(a, b, c) { google.timers[a] || google.startTick(a); c = void 0 !== c ? c : google.time(); b instanceof Array || (b = [b]); for (var e = 0, d; d = b[e++];) google.timers[a].t[d] = c; }; ``` -------------------------------- ### Initialize Google Service Flags Source: https://github.com/serpapi/nokolexbor/blob/master/bench/coffee.html Sets initial configuration flags for Google services, including google.spjs, google.snet, and google.em. These flags control aspects like script loading, network behavior, and event management. ```javascript (function(){google.spjs=false;google.snet=true;google.em=\[\];google.emw=false;})(); ``` -------------------------------- ### Parse HTML and Query Nodes Source: https://github.com/serpapi/nokolexbor/blob/master/README.md Demonstrates how to parse an HTML document from a URL and extract content using CSS selectors and XPath queries. ```ruby require 'nokolexbor' require 'open-uri' # Parse HTML document doc = Nokolexbor::HTML(URI.open('https://github.com/serpapi/nokolexbor')) # Search for nodes by css doc.css('#readme h1', 'article h2', 'p[dir=auto]').each do |node| puts node.content end # Search for text nodes by css doc.css('#readme p > ::text').each do |text| puts text.content end # Search for nodes by xpath doc.xpath('//div[@id="readme"]//h1', '//article//h2').each do |node| puts node.content end ``` -------------------------------- ### Detect System Features and Generate Configuration Headers Source: https://github.com/serpapi/nokolexbor/blob/master/ext/nokolexbor/CMakeLists.txt This section performs system-level checks for headers and functions, and generates the final configuration files (config.h and xmlversion.h) required for the build. ```cmake if (NOT MSVC) check_include_files(inttypes.h HAVE_INTTYPES_H) check_function_exists(rand_r HAVE_RAND_R) endif() if(MSVC) configure_file(include/win32config.h config.h COPYONLY) else() configure_file(config.h.cmake.in ${CMAKE_CURRENT_SOURCE_DIR}/config.h) endif() configure_file(libxml/xmlversion.h.in ${CMAKE_CURRENT_SOURCE_DIR}/libxml/xmlversion.h) ``` -------------------------------- ### JavaScript String.prototype.startsWith Polyfill Source: https://github.com/serpapi/nokolexbor/blob/master/bench/coffee.html Provides a polyfill for the String.prototype.startsWith method, ensuring compatibility with older JavaScript environments. It checks if a string begins with the characters of a specified string, returning true or false as appropriate. ```javascript ma("String.prototype.startsWith",function(a){return a?a:function(c,d){var e=na(this,c,"startsWith"),f=e.length,g=c.length;d=Math.max(0,Math.min(d|0,e.length));for(var h=0;h=g}}) ``` -------------------------------- ### Creating Elements Source: https://context7.com/serpapi/nokolexbor/llms.txt Programmatically create new HTML elements, text nodes, comments, and CDATA sections. ```APIDOC ## Creating Elements Create new elements, text nodes, comments, and CDATA sections programmatically. ### Method - **create_element(name, *content_or_attributes)**: Creates a new element. - **create_text_node(text)**: Creates a new text node. ### Parameters #### Element Creation - **name** (String) - The tag name of the element to create (e.g., 'div', 'a'). - **content_or_attributes** (String, Hash, Block) - Can be text content, attributes hash, or a block for complex element construction. #### Text Node Creation - **text** (String) - The text content for the node. ### Request Example ```ruby doc = Nokolexbor::HTML('') # Create element with various options div = doc.create_element('div') # =>
div_with_class = doc.create_element('div', class: 'container', id: 'main') # =>
div_with_content = doc.create_element('div', 'Hello World', class: 'greeting') # =>
Hello World
# Create with block link = doc.create_element('a') do |node| node['href'] = 'https://example.com' node['target'] = '_blank' node.content = 'Click here' end # => Click here # Alternative: use class constructors span = Nokolexbor::Element.new('span', doc) # => # Create text nodes text = doc.create_text_node('Some text content') # => "Some text content" ``` ### Response #### Success Response (200) - **Element** (Nokolexbor::Element) - The newly created HTML element. - **TextNode** (Nokolexbor::Text) - The newly created text node. #### Response Example ```ruby # The created element or text node object is returned. ``` ``` -------------------------------- ### CSS Selector Search with Nokolexbor in Ruby Source: https://context7.com/serpapi/nokolexbor/llms.txt Illustrates how to use CSS selectors for searching HTML nodes. It covers finding all matching nodes with `css` and the first match with `at_css`, including chaining selectors and selecting text nodes. ```ruby doc = Nokolexbor::HTML <<-HTML

First Post

Introduction text

Body content

Second Post

Another intro

HTML # Find all articles articles = doc.css('article.post') puts articles.length # => 2 # Find first article with multiple selectors featured = doc.at_css('article.featured', 'article.highlighted') puts featured.at_css('h2').content # => "First Post" # Chain selectors intros = doc.css('#content article p.intro') intros.each { |p| puts p.content } # => "Introduction text" # => "Another intro" # Find direct children only doc.at_css('#content').css('> article').each do |article| puts article.at_css('h2').content end # Select text nodes using ::text pseudo-element doc.css('article.featured p > ::text').each do |text| puts text.content end # => "Introduction text" # => "Body content" ``` -------------------------------- ### Implement Map Polyfill Source: https://github.com/serpapi/nokolexbor/blob/master/bench/coffee.html Provides a polyfill for the Map object, including methods for set, delete, clear, has, and iteration. It uses a WeakMap for key tracking to handle object-based keys correctly. ```javascript var d=function(l){this.f={}; this.b=g();this.size=0;if(l){l=_.ba(l);for(var m;!(m=l.next()).done;)m=m.value,this.set(m[0],m[1])}};d.prototype.set=function(l,m){l=0===l?0:l;var n=e(this,l);n.list||(n.list=this.f[n.id]=[]);n.Ya?n.Ya.value=m:(n.Ya={next:this.b,$b:this.b.$b,head:this.b,key:l,value:m},n.list.push(n.Ya),this.b.$b.next=n.Ya,this.b.$b=n.Ya,this.size++);return this}; ``` -------------------------------- ### Meta Encoding Management in Nokolexbor Source: https://context7.com/serpapi/nokolexbor/llms.txt Demonstrates how to retrieve and set the character encoding of an HTML document using Nokolexbor's `meta_encoding` attribute. It handles different meta tag formats and ensures the encoding is correctly applied. ```ruby doc = Nokolexbor::HTML('') puts doc.meta_encoding doc = Nokolexbor::HTML('') puts doc.meta_encoding doc = Nokolexbor::HTML('') doc.meta_encoding = 'utf-8' puts doc.at_css('head').to_html doc = Nokolexbor::HTML('') doc.meta_encoding = 'utf-8' puts doc.at_css('head meta')['charset'] ``` -------------------------------- ### Initialize Google Objects Source: https://github.com/serpapi/nokolexbor/blob/master/bench/coffee.html Initializes global JavaScript objects for Google services, such as google.ldi and google.pim. This is a common pattern for setting up Google-related functionalities on a webpage. ```javascript (function(){google.ldi={};google.pim={};})(); ``` -------------------------------- ### Log Project Modules (JavaScript) Source: https://github.com/serpapi/nokolexbor/blob/master/bench/coffee.html Logs an array of project module names using google.plm. This function is likely used for tracking or initializing various components within the Google ecosystem. ```javascript var r=['sb_wiz','aa','abd','aspn','async','bgd','dvl','foot','kyn','lu','m','mUpTid','mpck','mu','sf','sonic','spch','tl','vs','wft','exdc'];google.plm(r); ``` -------------------------------- ### Create Comments and CDATA Sections in Nokolexbor Source: https://context7.com/serpapi/nokolexbor/llms.txt Demonstrates how to create comment nodes and CDATA sections within an HTML document using Nokolexbor. These are fundamental for adding specific types of content to the DOM. ```ruby comment = doc.create_comment('This is a comment') # => cdata = doc.create_cdata('Raw here') # => here]]> ``` -------------------------------- ### Document Fragments Source: https://context7.com/serpapi/nokolexbor/llms.txt Explains how to create, parse, and manipulate HTML fragments for insertion into documents. ```APIDOC ## Document Fragments ### Description This section covers the creation and manipulation of HTML fragments using Nokolexbor. ### Creating Fragments ```ruby doc = Nokolexbor::HTML('
') container = doc.at_css('#container') # Create fragment from string fragment = doc.fragment('

First

Second

') puts fragment.to_html # => "

First

Second

" ``` ### Parsing Fragments ```ruby # Parse standalone fragment fragment = Nokolexbor::DocumentFragment.parse('Standalone') puts fragment.children.length # => 1 ``` ### Creating Fragments in Context ```ruby # Create fragment in context of a node node = doc.at_css('#container') fragment = node.fragment('Link') ``` ### Adding Fragments to Document ```ruby # Add fragment children to document container.add_child(fragment) puts container.to_html # =>

First

Second

# Fragment children are reparented puts fragment.children.length # => 0 (children moved to container) ``` ### Complex Insertions with Fragments ```ruby container.inner_html = '' html = '
Header
Content
Footer
' container.add_child(doc.fragment(html)) puts container.children.map(&:name).inspect # => ["header", "main", "footer"] ``` ``` -------------------------------- ### Node Manipulation and Traversal Source: https://github.com/serpapi/nokolexbor/wiki/Cheat-sheet Demonstrates how to modify node content, traverse the DOM tree, and serialize nodes to HTML strings. ```ruby node.children = param node.inner_html = param node.content = "string" node.remove node.traverse { |n| puts n } parent = node.parent html = node.to_html ``` -------------------------------- ### Node Type Checking in Nokolexbor Source: https://context7.com/serpapi/nokolexbor/llms.txt Explains how to determine the type of a node (element, text, comment, document) using Nokolexbor's constants and type-checking methods. It also shows how to traverse the document and filter nodes by type, and check if a node matches a CSS selector. ```ruby doc = Nokolexbor::HTML("
\n \n

Text content

\n
") puts Nokolexbor::Node::ELEMENT_NODE puts Nokolexbor::Node::TEXT_NODE puts Nokolexbor::Node::COMMENT_NODE puts Nokolexbor::Node::DOCUMENT_NODE div = doc.at_css('div') puts div.element? puts div.text? puts div.comment? puts div.document? puts div.type puts doc.document? doc.at_css('div').traverse do |node| case when node.element? puts "Element: #{node.name}" when node.text? puts "Text: #{node.content.strip}" unless node.content.strip.empty? when node.comment? puts "Comment: #{node.content}" end end p_tag = doc.at_css('p') puts p_tag.matches?('div p') puts p_tag.matches?('span p') ``` -------------------------------- ### Define UI Component Styles Source: https://github.com/serpapi/nokolexbor/blob/master/bench/coffee.html CSS definitions for layout containers, button states, and visual elements like the microphone icon. These styles handle positioning, transitions, and responsive scaling for different UI states. ```css .button { background-color: #fff; border: 1px solid #eee; border-radius: 100%; bottom: 0; box-shadow: 0 2px 5px rgba(0,0,0,.1); cursor: pointer; display: inline-block; left: 0; opacity: 0; pointer-events: none; position: absolute; right: 0; top: 0; transition: background-color 0.218s, border 0.218s, box-shadow 0.218s; } .s2fp .button, .s2tb .button, .s2fpm .button { opacity: 1; pointer-events: auto; position: absolute; transform: scale(1); transition-delay: 0; } .microphone { height: 87px; left: 43px; pointer-events: none; position: absolute; top: 47px; width: 42px; transform: scale(1); } .s2tb-h .microphone, .s2tb .microphone { left: 17px; top: 7px; transform: scale(.53); } ``` -------------------------------- ### Parsing HTML Documents in Ruby with Nokolexbor Source: https://context7.com/serpapi/nokolexbor/llms.txt Demonstrates how to parse HTML content from strings, URLs, and IO objects into a Document object for manipulation. It covers creating blank documents and modifying document titles. ```ruby require 'nokolexbor' require 'open-uri' # Parse from a string doc = Nokolexbor::HTML('

Hello World

') puts doc.at_css('h1').content # => "Hello World" # Parse from a URL using open-uri doc = Nokolexbor::HTML(URI.open('https://example.com')) puts doc.title # => "Example Domain" # Create a blank document doc = Nokolexbor::Document.new # Using the Nokolexbor() method with heredoc doc = Nokolexbor::HTML <<-HTML My Page

Content here

HTML puts doc.title # => "My Page" doc.title = 'New Title' puts doc.at_css('title').to_html # => "New Title" ``` -------------------------------- ### Working with Attributes Source: https://context7.com/serpapi/nokolexbor/llms.txt API for retrieving, setting, and deleting node attributes and managing CSS classes. ```APIDOC ## Attribute Management ### Description Allows access to node attributes via hash-like syntax and provides helper methods for CSS class manipulation. ### Methods - `node[key]`: Get or set attribute value. - `add_class(name)`: Adds a CSS class to the node. - `remove_class(name)`: Removes a CSS class from the node. - `delete(key)`: Removes an attribute. ### Request Example ```ruby node['data-active'] = 'true' node.add_class('highlight') ``` ### Response Returns the attribute value or the updated node. ``` -------------------------------- ### Implement String.prototype.includes Polyfill Source: https://github.com/serpapi/nokolexbor/blob/master/bench/coffee.html Adds the includes method to the String prototype to check for the existence of a substring within a string. ```javascript ma("String.prototype.includes",function(a){return a?a:function(c,d){return-1!==na(this,c,"includes").indexOf(c,d||0)}}); ``` -------------------------------- ### Node Manipulation and DOM Operations in Nokolexbor Source: https://context7.com/serpapi/nokolexbor/llms.txt Covers various methods for manipulating the DOM tree, including adding, replacing, and removing nodes. It shows how to add children and siblings, prepend nodes, wrap elements, and set inner HTML. Dependencies include the Nokolexbor library. ```ruby doc = Nokolexbor::HTML <<-HTML

First paragraph

Second paragraph

HTML container = doc.at_css('#container') first = doc.at_css('#first') second = doc.at_css('#second') # Add child nodes new_p = doc.create_element('p', 'New paragraph') container.add_child(new_p) # Or use the << operator container << doc.create_element('p', 'Another new paragraph') # Add siblings first.after('

Inserted after first

') second.before(doc.create_element('p', 'Before second')) # Alternative sibling methods first.add_next_sibling('Next') second.add_previous_sibling('Previous') # Prepend child (add as first child) container.prepend_child('

Very first

') # Replace a node second.replace('
Replaced content
') # Wrap a node first.wrap('
') puts doc.at_css('.wrapper').to_html # =>

First paragraph

# Remove nodes doc.at_css('#inserted')&.remove # Set inner HTML container.inner_html = '

Completely new content

' # Re-parent a node orphan = doc.create_element('p', 'Orphan') orphan.parent = container puts container.to_html ```