TestXMLSerializerNoBreakLink.cpp (2647B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include "gtest/gtest.h" 7 #include "mozilla/ErrorResult.h" 8 #include "mozilla/dom/DOMParser.h" 9 #include "mozilla/dom/Document.h" 10 #include "nsCOMPtr.h" 11 #include "nsIDocumentEncoder.h" 12 #include "nsString.h" 13 14 using namespace mozilla; 15 using namespace mozilla::dom; 16 17 // Test that serialising some DOM doesn't destroy links by word-wrapping long 18 // href values containing spaces. 19 TEST(TestXMLSerializerNoBreakLink, TestXMLSerializerNoBreakLinkMain) 20 { 21 // Build up a stupidly-long URL with spaces. Default is to wrap at column 22 // 72, so we want to exceed that. 23 nsString longURL = u"http://www.example.com/link with spaces"_ns; 24 for (int i = 1; i < 125; ++i) { 25 longURL.Append(u' '); 26 longURL.Append(IntToTString<char16_t>(i)); 27 } 28 nsString htmlInput = 29 u"<html><head>" 30 "<meta charset=\"utf-8\">" 31 "</head><body>Hello Thunderbird! <a href=\""_ns + 32 longURL + u"\">Link</a></body></html>"_ns; 33 34 // Parse HTML into a Document. 35 nsCOMPtr<Document> document; 36 { 37 IgnoredErrorResult rv; 38 RefPtr<DOMParser> parser = DOMParser::CreateWithoutGlobal(rv); 39 ASSERT_FALSE(rv.Failed()); 40 document = parser->ParseFromStringInternal(htmlInput, 41 SupportedType::Text_html, rv); 42 ASSERT_FALSE(rv.Failed()); 43 } 44 45 // Serialize back in a variety of flavours and check the URL survives the 46 // round trip intact. 47 nsCString contentTypes[] = {"text/xml"_ns, "application/xml"_ns, 48 "application/xhtml+xml"_ns, "image/svg+xml"_ns, 49 "text/html"_ns}; 50 for (auto const& contentType : contentTypes) { 51 uint32_t flagsToTest[] = { 52 nsIDocumentEncoder::OutputFormatted, nsIDocumentEncoder::OutputWrap, 53 nsIDocumentEncoder::OutputFormatted | nsIDocumentEncoder::OutputWrap}; 54 for (uint32_t flags : flagsToTest) { 55 // Serialize doc back to HTML source again. 56 nsCOMPtr<nsIDocumentEncoder> encoder = 57 do_createDocumentEncoder(contentType.get()); 58 ASSERT_TRUE(encoder); 59 nsresult rv = 60 encoder->Init(document, NS_ConvertASCIItoUTF16(contentType), flags); 61 ASSERT_TRUE(NS_SUCCEEDED(rv)); 62 nsString parsed; 63 rv = encoder->EncodeToString(parsed); 64 ASSERT_TRUE(NS_SUCCEEDED(rv)); 65 66 // URL is intact? 67 EXPECT_TRUE(parsed.Find(longURL) != kNotFound); 68 } 69 } 70 }