Skip to content

Commit

Permalink
Merge pull request #2221 from sparklemotion/1940-parse-xslt-styleshee…
Browse files Browse the repository at this point in the history
…t-using-preferred-options

parse XSLT stylesheets using libxslt-preferred options

---

**What problem is this PR intended to solve?**

See #1940 where some XSL transformations ended up producing slightly different results from `xsltproc`

**Have you included adequate test coverage?**

Yes!

**Does this change affect the behavior of either the C or the Java implementations?**

This behavior appears to be libxslt-specific, and so the test only runs for CRuby; though the document ends up being parsed with the new options on both implementations.
  • Loading branch information
flavorjones committed Apr 21, 2021
2 parents 18f6a3b + 2195263 commit 12bac4f
Show file tree
Hide file tree
Showing 5 changed files with 380 additions and 338 deletions.
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

0 comments on commit 12bac4f

Please sign in to comment.