Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Nokogiri::HTML5::Inference for parsing HTML fragments #337

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ PATH
deface (~> 1.9)
html_press (~> 0.8.2)
nokogiri (~> 1.0)
nokogiri-html5-inference (~> 0.2)
phlex (~> 1.6)
phlex-rails (>= 0.9, < 2.0)
syntax_tree (~> 6.0)
Expand Down Expand Up @@ -180,6 +181,8 @@ GEM
racc (~> 1.4)
nokogiri (1.16.4-x86_64-linux)
racc (~> 1.4)
nokogiri-html5-inference (0.2.0)
nokogiri (~> 1.14)
parallel (1.22.1)
parser (3.1.2.1)
ast (~> 2.4.1)
Expand Down
3 changes: 3 additions & 0 deletions gem/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ gem "maxitest", "~> 4.4"
gem "minitest", "~> 5.0"
gem "rake", "~> 13.0"
gem "rubocop", require: false, github: "joeldrapper/rubocop", branch: "rubocop-user-agent"
# gem "nokogiri-html5-inference", github: "flavorjones/nokogiri-html5-inference", branch: "2-use-template-context-node"

gem "nokogiri-html5-inference", github: "flavorjones/nokogiri-html5-inference", branch: "flavorjones-always-return-nodeset"
10 changes: 10 additions & 0 deletions gem/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
GIT
remote: https://github.com/flavorjones/nokogiri-html5-inference.git
revision: 7426a3601d30bf6cb9794d6b5c2e767151a0fa6f
branch: flavorjones-always-return-nodeset
specs:
nokogiri-html5-inference (0.2.0)
nokogiri (~> 1.14)

GIT
remote: https://github.com/joeldrapper/rubocop.git
revision: 8d3c1a7dd04a0dee701e26ee506b28af0ffe3b98
Expand All @@ -21,6 +29,7 @@ PATH
deface (~> 1.9)
html_press (~> 0.8.2)
nokogiri (~> 1.0)
nokogiri-html5-inference (~> 0.2)
phlex (~> 1.6)
phlex-rails (>= 0.9, < 2.0)
syntax_tree (~> 6.0)
Expand Down Expand Up @@ -178,6 +187,7 @@ PLATFORMS
DEPENDENCIES
maxitest (~> 4.4)
minitest (~> 5.0)
nokogiri-html5-inference!
phlexing!
rake (~> 13.0)
rubocop!
Expand Down
2 changes: 1 addition & 1 deletion gem/lib/phlexing/name_suggestor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def self.wrap(name)
def self.extract(document, method)
return [] unless document

document.children.map { |element| send(method, element) }.compact
document.map { |element| send(method, element) }.compact
end

def self.extract_id_from_element(element)
Expand Down
21 changes: 2 additions & 19 deletions gem/lib/phlexing/parser.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,15 @@
# frozen_string_literal: true

require "nokogiri"
require "nokogiri/html5/inference"

module Phlexing
class Parser
def self.call(source)
source = ERBTransformer.call(source)
source = Minifier.call(source)

# Credit:
# https://github.com/spree/deface/blob/6bf18df76715ee3eb3d0cd1b6eda822817ace91c/lib/deface/parser.rb#L105-L111
#

html_tag = /<html(( .*?(?:(?!>)[\s\S])*>)|>)/i
head_tag = /<head(( .*?(?:(?!>)[\s\S])*>)|>)/i
body_tag = /<body(( .*?(?:(?!>)[\s\S])*>)|>)/i

if source =~ html_tag
Nokogiri::HTML::Document.parse(source)
elsif source =~ head_tag && source =~ body_tag
Nokogiri::HTML::Document.parse(source).css("html").first
elsif source =~ head_tag
Nokogiri::HTML::Document.parse(source).css("head").first
elsif source =~ body_tag
Nokogiri::HTML::Document.parse(source).css("body").first
else
Nokogiri::HTML::DocumentFragment.parse(source)
end
Nokogiri::HTML5::Inference.parse(source)
end
end
end
2 changes: 2 additions & 0 deletions gem/lib/phlexing/template_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ def handle_node(node, level = 0)
handle_document_node(node, level)
in Nokogiri::XML::Comment
handle_html_comment_node(node)
in Nokogiri::XML::NodeSet
node.each { |n| handle_node(n, level + 1) }
end
end
end
Expand Down
1 change: 1 addition & 0 deletions gem/phlexing.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "deface", "~> 1.9"
spec.add_dependency "html_press", "~> 0.8.2"
spec.add_dependency "nokogiri", "~> 1.0"
spec.add_dependency "nokogiri-html5-inference", "~> 0.2"
spec.add_dependency "phlex", "~> 1.6"
spec.add_dependency "phlex-rails", ">= 0.9", "< 2.0"
spec.add_dependency "syntax_tree", "~> 6.0"
Expand Down
25 changes: 15 additions & 10 deletions gem/test/phlexing/converter/tags_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ class Phlexing::Converter::TagsTest < Minitest::Spec
assert_phlex_template "span", %(<span></span>)
assert_phlex_template "p", %(<p></p>)
assert_phlex_template "template_tag", %(<template></template>)
assert_phlex_template "html", %(<html></html>)
assert_phlex_template "head", %(<head></head>)
assert_phlex_template "head\n\nbody", %(<head></head>)
assert_phlex_template "header", %(<header></header>)
assert_phlex_template "body", %(<body></body>)
assert_phlex_template <<~PHLEX.strip, %(<html></html>)
html do
head
body
end
PHLEX
end

it "basic self closing tag" do
Expand Down Expand Up @@ -133,11 +138,9 @@ class Phlexing::Converter::TagsTest < Minitest::Spec
HTML

expected = <<~PHLEX.strip
html do
head
head

body
end
body
PHLEX

assert_phlex_template expected, html
Expand All @@ -150,11 +153,9 @@ class Phlexing::Converter::TagsTest < Minitest::Spec
HTML

expected = <<~PHLEX.strip
html do
head
head

body
end
body
PHLEX

assert_phlex_template expected, html
Expand All @@ -167,6 +168,8 @@ class Phlexing::Converter::TagsTest < Minitest::Spec

expected = <<~PHLEX.strip
head

body
PHLEX

assert_phlex_template expected, html
Expand All @@ -179,6 +182,8 @@ class Phlexing::Converter::TagsTest < Minitest::Spec

expected = <<~PHLEX.strip
head(id: "123")

body
PHLEX

assert_phlex_template expected, html
Expand Down
24 changes: 15 additions & 9 deletions gem/test/phlexing/converter/uppercase_tags_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ class Phlexing::Converter::UppercaseTagsTest < Minitest::Spec
assert_phlex_template "span", %(<SPAN></SPAN>)
assert_phlex_template "p", %(<P></P>)
assert_phlex_template "template_tag", %(<TEMPLATE></TEMPLATE>)
assert_phlex_template "html", %(<HTML></HTML>)
assert_phlex_template "head", %(<HEAD></HEAD>)
assert_phlex_template "head\n\nbody", %(<HEAD></HEAD>)
assert_phlex_template "body", %(<BODY></BODY>)
assert_phlex_template <<~PHLEX.strip, %(<HTML></HTML>)
html do
head
body
end
PHLEX
end

it "standlone uppercase body tag" do
Expand All @@ -32,6 +37,8 @@ class Phlexing::Converter::UppercaseTagsTest < Minitest::Spec

expected = <<~PHLEX.strip
head

body
PHLEX

assert_phlex_template expected, html
Expand All @@ -43,7 +50,10 @@ class Phlexing::Converter::UppercaseTagsTest < Minitest::Spec
HTML

expected = <<~PHLEX.strip
html
html do
head
body
end
PHLEX

assert_phlex_template expected, html
Expand All @@ -56,11 +66,8 @@ class Phlexing::Converter::UppercaseTagsTest < Minitest::Spec
HTML

expected = <<~PHLEX.strip
html do
head

body
end
head
body
PHLEX

assert_phlex_template expected, html
Expand All @@ -76,7 +83,6 @@ class Phlexing::Converter::UppercaseTagsTest < Minitest::Spec

expected = <<~PHLEX.strip
html do
whitespace
head
whitespace
body
Expand Down
Loading
Loading