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

parse XSLT stylesheets using libxslt-preferred options #2221

Merged
merged 3 commits into from Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,14 @@ Nokogiri follows [Semantic Versioning](https://semver.org/), please see the [REA

---

## next / unreleased

### Changed

* Introduce `Nokogiri::XML::ParseOptions::DEFAULT_XSLT` which adds the libxslt-preferred options of `NOENT | DTDLOAD | DTDATTR | NOCDATA` to `ParseOptions::DEFAULT_XML`.
* `Nokogiri.XSLT` parses the stylesheet using `ParseOptions::DEFAULT_XSLT`, which should make some edge-case XSL transformations match libxslt's default behavior. [[#1940](https://github.com/sparklemotion/nokogiri/issues/1940)]


## 1.11.3 / 2021-04-07

### Fixed
Expand Down
2 changes: 2 additions & 0 deletions lib/nokogiri/xml/parse_options.rb
Expand Up @@ -71,6 +71,8 @@ class ParseOptions

# the default options used for parsing XML documents
DEFAULT_XML = RECOVER | NONET
# the default options used for parsing XSLT stylesheets
DEFAULT_XSLT = RECOVER | NONET | NOENT | DTDLOAD | DTDATTR | NOCDATA
# the default options used for parsing HTML documents
DEFAULT_HTML = RECOVER | NOERROR | NOWARNING | NONET
# the default options used for parsing XML schemas
Expand Down
30 changes: 15 additions & 15 deletions lib/nokogiri/xslt.rb
@@ -1,5 +1,5 @@
# frozen_string_literal: true
require 'nokogiri/xslt/stylesheet'
require "nokogiri/xslt/stylesheet"

module Nokogiri
class << self
Expand All @@ -22,32 +22,32 @@ module XSLT
class << self
###
# Parse the stylesheet in +string+, register any +modules+
def parse string, modules = {}
def parse(string, modules = {})
modules.each do |url, klass|
XSLT.register url, klass
XSLT.register(url, klass)
end

doc = XML::Document.parse(string, nil, nil, XML::ParseOptions::DEFAULT_XSLT)
if Nokogiri.jruby?
Stylesheet.parse_stylesheet_doc(XML.parse(string), string)
Stylesheet.parse_stylesheet_doc(doc, string)
else
Stylesheet.parse_stylesheet_doc(XML.parse(string))
Stylesheet.parse_stylesheet_doc(doc)
end
end

###
# Quote parameters in +params+ for stylesheet safety
def quote_params params
def quote_params(params)
parray = (params.instance_of?(Hash) ? params.to_a.flatten : params).dup
parray.each_with_index do |v,i|
if i % 2 > 0
parray[i]=
if v =~ /'/
"concat('#{ v.gsub(/'/, %q{', "'", '}) }')"
else
"'#{v}'";
end
parray.each_with_index do |v, i|
parray[i] = if i % 2 > 0
if v =~ /'/
"concat('#{v.gsub(/'/, %q{', "'", '})}')"
else
"'#{v}'"
end
else
parray[i] = v.to_s
v.to_s
end
end
parray.flatten
Expand Down
2 changes: 1 addition & 1 deletion lib/nokogiri/xslt/stylesheet.rb
Expand Up @@ -18,7 +18,7 @@ class Stylesheet
# Apply an XSLT stylesheet to an XML::Document.
# +params+ is an array of strings used as XSLT parameters.
# returns serialized document
def apply_to document, params = []
def apply_to(document, params = [])
serialize(transform(document, params))
end
end
Expand Down