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

Fix special characters in table header #77

Merged

Conversation

rfdonnelly
Copy link
Contributor

Previous to this PR, special characters in table headers would generate invalid XML due to the use of the cell.value instead of cell.clean_value in table serialization.

This PR adds a test and fix for this.

Before the fix, the new test failed with:

1:0: ERROR: Element '{http://schemas.openxmlformats.org/spreadsheetml/2006/main}tableColumns': Character content other than whitespace is not allowed because the content type is 'element-only'.
F
===============================================================================
Failure: test_to_xml_string_for_special_characters(TestTable):
  error free validation.
  <false> is not true.
/home/rfdonnelly/repos/caxlsx/test/workbook/worksheet/tc_table.rb:80:in `test_to_xml_string_for_special_characters'
     77:       errors.push error
     78:       puts error.message
     79:     end
  => 80:     assert(errors.empty?, "error free validation")
     81:   end
     82: end
===============================================================================

After the fix, the new test passes.

@rfdonnelly
Copy link
Contributor Author

I rewrote the test to match an existing special character test (TestSeriesTitle::test_to_xml_string_for_special_characters). Without the fix, the test now fails with:

Failure: test_to_xml_string_for_special_characters(TestTable):
  invalid xml: 1:262: FATAL: xmlParseEntityRef: no name, 1:263: FATAL: Unescaped '<' not allowed in attributes values, 1:263: FATAL: attributes construct error, 1:263: FATAL: Couldn't find end of Start Tag tableColumn line 1, 1:264: FATAL: StartTag: invalid element name.
  <false> is not true.
/home/rfdonnelly/repos/caxlsx/test/workbook/worksheet/tc_table.rb:75:in `test_to_xml_string_for_special_characters'
     72:     table = @ws.add_table("A1:D5")
     73:     doc = Nokogiri::XML(table.to_xml_string)
     74:     errors = doc.errors
  => 75:     assert(errors.empty?, "invalid xml: #{errors.map(&:to_s).join(', ')}")
     76:   end
     77: end

@kiskoza
Copy link
Member

kiskoza commented Feb 4, 2021

looks good to me, nice catch

@straydogstudio straydogstudio merged commit 28e4870 into caxlsx:master Feb 12, 2021
@straydogstudio
Copy link
Contributor

Thanks @rfdonnelly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants