### Nokogiri Setup Code Source: https://github.com/sparklemotion/nokogiri/blob/main/doc/keyword_arguments.md Provides the necessary setup code, including require statements and variable initializations, for running Nokogiri examples. ```ruby require 'nokogiri' include Nokogiri xml_s = '' url = 'www.site.com' encoding = 'UTF-16' options = XML::ParseOptions::STRICT ``` -------------------------------- ### Successful Native Gem Installation Example Source: https://github.com/sparklemotion/nokogiri/blob/main/README.md This output shows a successful installation of a native Nokogiri gem, indicating pre-compiled libraries were used. This typically results in faster and more reliable installations. ```sh $ gem install nokogiri Fetching nokogiri-1.11.0-x86_64-linux.gem Successfully installed nokogiri-1.11.0-x86_64-linux 1 gem installed ``` -------------------------------- ### Install libiconv using Homebrew Source: https://github.com/sparklemotion/nokogiri/wiki/Install-libiconv-on-OS-X-with-Homebrew Use this command to install the libiconv package via Homebrew. ```bash brew install libiconv ``` -------------------------------- ### Install Nokogiri with Custom Library Paths Source: https://github.com/sparklemotion/nokogiri/wiki/Install-libiconv-on-OS-X-with-Homebrew Install Nokogiri specifying custom include and library paths for libxml2, libxslt, and libiconv. This is useful when system versions are outdated or non-standard. ```bash gem install nokogiri -- --with-xml2-include=/usr/local/Cellar/libxml2/2.7.8/include/libxml2 --with-xml2-lib=/usr/local/Cellar/libxml2/2.7.8/lib --with-xslt-dir=/usr/local/Cellar/libxslt/1.1.26 --with-iconv-include=/usr/local/Cellar/libiconv/1.15/include --with-iconv-lib=/usr/local/Cellar/libiconv/1.15/lib ``` -------------------------------- ### Clone Nokogiri Repository and Install Dependencies Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md This snippet shows the basic steps to clone the Nokogiri repository and install its dependencies using Bundler. ```sh git clone --recurse-submodules https://github.com/sparklemotion/nokogiri cd nokogiri bundle install ``` -------------------------------- ### Install Nokogiri with Homebrew's libiconv Source: https://github.com/sparklemotion/nokogiri/wiki/Install-libiconv-on-OS-X-with-Homebrew Install the Nokogiri gem, specifying the path to the libiconv installed by Homebrew. This is necessary if Nokogiri reports an outdated libiconv version. ```bash gem install nokogiri -- --with-iconv-dir=/usr/local/Cellar/libiconv/1.15/ ``` -------------------------------- ### RDoc Documentation Example Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Illustrates the use of `:call-seq:`, parameter types, return types, and callouts for documentation strings. ```ruby module Nokogiri module XML class Node # :call-seq: upcase(name) → String # Returns the uppercase version of the given name. # # [Parameters] # * name (String): the name to uppercase # # [Returns] # String: the uppercased name # # 💡 This method is a simple wrapper around String#upcase. def upcase(name) name.upcase end end end end ``` -------------------------------- ### C Function Naming Convention Example Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Demonstrates the naming conventions for public and static functions in C code. ```c /* Public function, bound to Ruby method */ int noko_xml_sax_parser_context_...(...); /* Static function (file scope) */ int xml_sax_parser_context_...(...); ``` -------------------------------- ### Dockerfile for Nokogiri Installation Source: https://github.com/sparklemotion/nokogiri/wiki/Running-in-a-docker-(Ubuntu) This Dockerfile installs necessary system packages, Ruby, and configures Nokogiri for installation within an Ubuntu environment. It specifies environment variables and dependencies required for Nokogiri. ```docker FROM ubuntu:latest RUN apt-get update RUN apt-get install -y make curl # ruby's nokorigi is sometimes described as being 'hard' to install, but the below 2 instructions, bundled with the version specified in Gemfile* make it install nicely RUN echo 'LC_ALL="en_US.UTF-8"' > /etc/default/locale RUN apt-get install -y ruby1.9.3 # greatly speeds up nokogiri install ENV NOKOGIRI_USE_SYSTEM_LIBRARIES 1 # dependencies for nokogiri gem RUN apt-get install -y libxml2 libxml2-dev libxslt1-dev # install bundler RUN gem install bundler # install gems ADD Gemfile /tmp/ ADD Gemfile.lock /tmp/ RUN cd /tmp; bundle install ``` -------------------------------- ### Rails Controller Example Source: https://github.com/sparklemotion/nokogiri/blob/main/test/files/tlm.html A basic Rails controller that fetches a list of users. This data will be accessible in the JavaScript view. ```ruby class JohnsonController < ApplicationController def index @users = User.find(:all) end end ``` -------------------------------- ### C Ruby Instance Method Naming Example Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Illustrates the naming convention for C functions corresponding to Ruby instance methods. ```c /* Ruby instance method: Nokogiri::XML::SAX::ParserContext#line */ int noko_xml_sax_parser_context__line(...); ``` -------------------------------- ### C Ruby Singleton Method Naming Example Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Shows the naming convention for C functions corresponding to Ruby singleton methods. ```c /* Ruby singleton method: Nokogiri::XML::SAX::ParserContext.io */ int noko_xml_sax_parser_context_s_io(...); ``` -------------------------------- ### Run Focused Tests with Minitest Options Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Use TESTOPTS to run specific tests, for example, tests matching a particular pattern. ```sh bundle exec rake compile test TESTOPTS="-n/test_last_element_child/" ``` -------------------------------- ### Successful Fuzzer Output Example Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md This output indicates the fuzzer is running successfully, collecting coverage, and finding new code paths. It is typical during normal fuzzing operations. ```text INFO: Seed: 4156947595 INFO: Loaded 1 modules (7149 inline 8-bit counters): 7149 0x58a462, 0x58c04f, INFO: Loaded 1 PC tables (7149 PCs): 7149 0x53beb0,0x557d80, INFO: -max_len is not provided; libFuzzer will not generate inputs larger than 4096 bytes INFO: A corpus is not provided, starting from an empty corpus #2 INITED cov: 2 ft: 2 corp: 1/1b exec/s: 0 rss: 24Mb NEW_FUNC[1/44]: 0x429840 in gumbo_parse_with_options (/home/user/nokogiri/gumbo-parser/fuzzer/build/parse_fuzzer+0x429840) NEW_FUNC[2/44]: 0x42c0d0 in destroy_node (/home/user/nokogiri/gumbo-parser/fuzzer/build/parse_fuzzer+0x42c0d0) #721 NEW cov: 180 ft: 181 corp: 2/12b lim: 11 exec/s: 0 rss: 27Mb L: 11/11 MS: 4 ChangeByte-ChangeByte-ChangeBit-InsertRepeatedBytes- #722 NEW cov: 186 ft: 196 corp: 3/23b lim: 11 exec/s: 0 rss: 27Mb L: 11/11 MS: 1 ChangeBit- #723 NEW cov: 186 ft: 228 corp: 4/34b lim: 11 exec/s: 0 rss: 27Mb L: 11/11 MS: 1 ChangeBinInt- #724 NEW cov: 188 ft: 241 corp: 5/45b lim: 11 exec/s: 0 rss: 27Mb L: 11/11 MS: 1 ChangeBit- #725 NEW cov: 188 ft: 254 corp: 6/56b lim: 11 exec/s: 0 rss: 27Mb L: 11/11 MS: 1 ChangeByte- #726 NEW cov: 188 ft: 270 corp: 7/67b lim: 11 exec/s: 0 rss: 27Mb L: 11/11 MS: 1 CopyPart- #732 NEW cov: 188 ft: 279 corp: 8/78b lim: 11 exec/s: 0 rss: 27Mb L: 11/11 MS: 1 ChangeBit- NEW_FUNC[1/1]: 0x441de0 in gumbo_token_destroy (/home/user/nokogiri/gumbo-parser/fuzzer/build/parse_fuzzer+0x441de0) ``` -------------------------------- ### Ruby Object#tase! Method Example Source: https://github.com/sparklemotion/nokogiri/blob/main/test/files/tlm.html Demonstrates the custom Object#tase! method available in the Brobinius Ruby implementation, which raises a RuntimeError. ```ruby >> x = Class.new => # >> x.tase! RuntimeError: Don't tase me bro from (irb):2:in `tase!' from (irb):6 >> ``` -------------------------------- ### Slop Decorator HTML Example Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Demonstrates accessing HTML elements and their text content using the Slop decorator's method-based syntax. ```ruby doc = Nokogiri::Slop(<<-eohtml)

first

second

eohtml assert_equal('second', doc.html.body.p[1].text) ``` -------------------------------- ### Rewrite Ruby Object Access Example Entirely in JavaScript Source: https://github.com/sparklemotion/nokogiri/blob/main/test/files/tlm.html Shows how to create a Ruby Struct and assign a property to it entirely within JavaScript, using the 'Ruby' variable for access. ```ruby ctx = Johnson::Context.new ctx.evaluate("var x = new (new Ruby.Struct(Johnson.symbolize('foo')));") ctx.evaluate("x.foo = 'bar'") puts ctx.evaluate('x').foo puts ctx.evaluate('x').class ``` -------------------------------- ### Get Namespace Definitions Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Returns an array of Namespace objects defined directly on this node, including default and prefixed namespaces. ```ruby node.namespace_definitions ``` -------------------------------- ### C Ruby Attribute Getter/Setter Naming Example Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Demonstrates the naming conventions for C functions representing Ruby attribute getters and setters. ```c /* Ruby attribute setter: Nokogiri::XML::SAX::ParserContext#recovery= */ int noko_xml_sax_parser_context__recovery_set(...); /* Ruby attribute getter: Nokogiri::XML::SAX::ParserContext#recovery */ int noko_xml_sax_parser_context__recovery_get(...); ``` -------------------------------- ### CSS Parsing with Nokogiri Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Provides examples for parsing CSS selectors using Nokogiri. It shows how to parse a selector into an Abstract Syntax Tree (AST) and convert it to an XPath expression. ```ruby # https://nokogiri.org/rdoc/Nokogiri/CSS.html Nokogiri::CSS.parse('selector') # => returns an AST Nokogiri::CSS.xpath_for('selector', options={}) # https://nokogiri.org/rdoc/Nokogiri/CSS/Node.html # attr: type, value #methods # accept(visitor) # find_by_type # new # preprocess! # to_a # to_type # to_xpath # https://nokogiri.org/rdoc/Nokogiri/CSS/Parser.html # a Racc generated Parser ``` -------------------------------- ### Compile and Test with System Libraries Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Clean previous builds and then compile and run tests, explicitly enabling the use of system libraries. ```sh bundle exec rake clean # blow away pre-existing libraries using packaged libs bundle exec rake compile test -- --enable-system-libraries ``` -------------------------------- ### Run Performance Benchmarks Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Compile the extension and run the performance benchmark tests. ```sh bundle exec rake compile test:bench ``` -------------------------------- ### Initialize Submodule and Run Tests Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Initialize the html5lib-tests submodule and then run the Nokogiri compile and test tasks. This is necessary if the submodule has not been checked out. ```sh git submodule update --init # test/html5lib-tests bundle exec rake compile test ``` -------------------------------- ### EJS JavaScript View Example Source: https://github.com/sparklemotion/nokogiri/blob/main/test/files/tlm.html An example of an EJS (Embedded JavaScript) view that iterates over users passed from the controller. It uses the 'at' object to access controller instance variables. ```javascript <% for(var user in at.users) { %><%= user.first_name() %>
<% } %> ``` -------------------------------- ### Log in to a website Source: https://github.com/sparklemotion/nokogiri/blob/main/test/files/tlm.html Instantiate a Mechanize agent, fetch a URL, set login credentials, and submit the login form. Ensure the user agent alias is set appropriately. ```ruby agent = WWW::Mechanize.new { |a| a.user_agent_alias = 'Mac Safari' } page = agent.get('http://youtube.com/') # Login page.form('loginForm') { |f| f.username = 'username' f.password = 'password' }.submit ``` -------------------------------- ### Get Internal Subset Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Retrieve the internal subset of the DTD. ```ruby node.internal_subset ``` -------------------------------- ### Get External Subset Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Retrieve the external subset of the DTD. ```ruby node.external_subset ``` -------------------------------- ### Run Tests with Valgrind Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Compile the extension and run the test suite under Valgrind for memory error detection. ```sh bundle exec rake compile test:valgrind ``` -------------------------------- ### Get Node Path Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Returns the XPath-like path associated with this node. ```ruby node.path ``` -------------------------------- ### Get Node CSS Path Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Returns the path to this node expressed as a CSS selector. ```ruby node.css_path ``` -------------------------------- ### Get Ancestor Nodes Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Returns a NodeSet of all ancestor nodes, ordered from closest to furthest. ```ruby node.ancestors ``` -------------------------------- ### Get Node Line Number Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Retrieves the line number where this node appeared in the input source. ```ruby node.line ``` -------------------------------- ### Get Matching Ancestor Nodes Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Returns a NodeSet of ancestor nodes that match the given selector. ```ruby node.ancestors(selector) ``` -------------------------------- ### Stuff Ruby Objects into Johnson Context and Evaluate Source: https://github.com/sparklemotion/nokogiri/blob/main/test/files/tlm.html Illustrates how to define a Ruby object and make it available within the JavaScript context, then evaluate JavaScript that uses it. ```ruby A = Struct.new(:foo) ctx = Johnson::Context.new ctx['alert'] = lambda { |x| puts x } ctx['a'] = A.new("bar") ctx.evaluate('alert(a.foo);') ``` -------------------------------- ### Get Node Content Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Retrieves the text content of a node. Aliases node.text, node.inner_text, and node.to_str. ```ruby node.content ``` -------------------------------- ### Access and Modify Parent and Children Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Covers how to get the parent of a node, and how to access, replace, or add children. ```ruby ## Parent node.parent node.parent=(node) ## Children node.child # returns a Node node.children # Get the list of children of this node as a NodeSet node.children=(node_or_tags) # Set the inner html for this Node # Returns the reparented node (if +node_or_tags+ is a Node), # or returns a NodeSet (if +node_or_tags+ is a DocumentFragment, NodeSet, or string). node.elements # alias: node.element_children # Get the list of child Elements of this node as a NodeSet. node.add_child(node_or_tags) # Add +node_or_tags+ as a child of this Node. # Returns the reparented node (if +node_or_tags+ is a Node), ``` -------------------------------- ### Run Gumbo HTML5 Parser Tests Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Execute the test suite for the gumbo HTML5 parser. Ensure the html5lib-tests submodule is initialized. ```sh bundle exec rake gumbo ``` -------------------------------- ### Configure GC Levels for Testing Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Set environment variables to control garbage collection behavior during testing. Options include 'compact', 'verify', and 'stress'. ```sh NOKOGIRI_TEST_GC_LEVEL=compact bundle exec rake compile test ``` ```sh NOKOGIRI_TEST_GC_LEVEL=verify bundle exec rake compile test ``` ```sh NOKOGIRI_TEST_GC_LEVEL=stress bundle exec rake compile test ``` -------------------------------- ### Run Memory Usage Suite Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Execute a specific test suite focused on memory usage, analyzing RSS size and using linear interpolation. ```sh bundle exec rake compile test:memory_suite ``` -------------------------------- ### Get Internal Pointer ID Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Returns the internal pointer number for this node, used for identification within Nokogiri. ```ruby node.pointer_id ``` -------------------------------- ### Compile and Run Tests Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Execute the compile and test tasks using Rake. This is the standard way to run the test suite. ```sh bundle exec rake compile test ``` -------------------------------- ### Get Element Description Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Fetch the Nokogiri::HTML::ElementDescription for the node. Returns nil for XML documents or unknown tags. ```ruby node.description ``` -------------------------------- ### Convert Node to String Representation Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Get the string representation of a Node. Returns XML if the document is XML, otherwise HTML. ```ruby node.to_s ``` -------------------------------- ### Set video attributes for upload Source: https://github.com/sparklemotion/nokogiri/blob/main/test/files/tlm.html Fetch the upload page, fill in the video title, description, category, and keywords, then submit the upload form. The category is represented by a numerical ID. ```ruby # Set the video attributes page = agent.get('http://youtube.com/my_videos_upload') form = page.form('theForm') form.field_myvideo_title = 'My video title' form.field_myvideo_descr = "My video description" form.field_myvideo_categories = 28 form.field_myvideo_keywords = 'my tag' page = form.submit(form.buttons.name('action_upload').first) ``` -------------------------------- ### Handle Node Attributes Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Illustrates how to get, set, check for, and delete attributes of a Nokogiri node, treating them like a hash. ```ruby # Attributes, like a hash that maps string keys to string values node['src'] # aliases: node.get_attribute, node.attr. node['src'] = 'value' # alias node.set_attribute node.key?('src') # alias node.has_attribute? node.keys node.values node.delete('src') # alias of node.remove_attribute node.each { |attr_name, attr_value| } ``` -------------------------------- ### Initialize Nokogiri XML Node Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Demonstrates two ways to initialize a new XML node within a Nokogiri document. ```ruby node = Nokogiri::XML::Node.new('name', document) # initialize a new node node = document.create_element('name') # shortcut ``` -------------------------------- ### Upload Video File Source: https://github.com/sparklemotion/nokogiri/blob/main/test/files/tlm.html This snippet shows how to set the video file name and submit a form for uploading. It assumes the file name is provided as a command-line argument. ```ruby # Upload the video page = page.form('theForm') { |f| f.file_uploads.name('field_uploadfile').first.file_name = ARGV[0] }.submit page.body =~ /]*>(.*)<\/textarea>/m puts $1 ``` -------------------------------- ### Get Default Namespace Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Returns the default namespace object defined on this node (e.g., from an 'xmlns="..."' attribute). ```ruby node.namespace ``` -------------------------------- ### Configure Parallel Compilation and Testing Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Set environment variables NCPU and MAKEFLAGS to enable parallel execution for compilation and testing. ```sh export NCPU=8 export MAKEFLAGS=-j8 bundle exec rake compile test ``` -------------------------------- ### Get Inner HTML Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Returns the HTML representation of the node's children, joined together. Useful for serializing child nodes. ```ruby node.inner_html ``` -------------------------------- ### Generate Screencast with Brobinius Source: https://github.com/sparklemotion/nokogiri/blob/main/test/files/tlm.html Use the --screencast option with Brobinius to automatically create and upload a screencast of your Ruby program to YouTube. The --geoff flag can be added for voice-over by Geoffrey Grosenbach. ```bash $ brobinius --screencast my_code.rb ``` -------------------------------- ### Get Last Element Child Node Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Retrieves the last child node that is an element. Returns nil if no element children exist. ```ruby node.last_element_child ``` -------------------------------- ### Get First Element Child Node Source: https://github.com/sparklemotion/nokogiri/wiki/Cheat-sheet Retrieves the first child node that is an element. Returns nil if no element children exist. ```ruby node.first_element_child ``` -------------------------------- ### Fast Gumbo Development Builds Source: https://github.com/sparklemotion/nokogiri/blob/main/CONTRIBUTING.md Enable faster feedback loops for libgumbo development by using a specific rake task for immediate compilation of changed files. ```sh bundle exec rake clean compile -- --gumbo-dev # change a gumbo file bundle exec rake compile # immediate compilation of changed file and relinking of nokogiri.so ``` -------------------------------- ### Parsing Basic XML Structure Source: https://github.com/sparklemotion/nokogiri/blob/main/doc/xml/parsing.md Demonstrates parsing a simple XML document with nested elements and text content. Shows the resulting document structure. ```ruby xml = ' Two Four ' doc = Nokogiri::XML.parse(xml) doc # => #(Document: { name = "document", children = [ #(Element: { name = "root", children = [ #(Text "One"), #(Element: { name = "foo", children = [ #(Text "Two")] }), #(Text "Three"), #(Element: { name = "bar", children = [ #(Text "Four")] }), #(Text "Five")] })] }) ```