ImportScannerTest.cpp (3125B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "gtest/gtest.h" 8 #include "mozilla/ImportScanner.h" 9 10 using namespace mozilla; 11 12 static nsTArray<nsString> Scan(const char* aCssCode) { 13 nsTArray<nsString> urls; 14 ImportScanner scanner; 15 scanner.Start(); 16 urls.AppendElements(scanner.Scan(NS_ConvertUTF8toUTF16(aCssCode))); 17 urls.AppendElements(scanner.Stop()); 18 return urls; 19 } 20 21 TEST(ImportScanner, Simple) 22 { 23 auto urls = Scan( 24 "/* Something something */ " 25 "@charset \"utf-8\";" 26 "@import url(bar);" 27 "@import uRL( baz );" 28 "@import \"bazz)\""); 29 30 ASSERT_EQ(urls.Length(), 3u); 31 ASSERT_EQ(urls[0], u"bar"_ns); 32 ASSERT_EQ(urls[1], u"baz"_ns); 33 ASSERT_EQ(urls[2], u"bazz)"_ns); 34 } 35 36 TEST(ImportScanner, UrlWithQuotes) 37 { 38 auto urls = Scan( 39 "/* Something something */ " 40 "@import url(\"bar\");" 41 "@import\tuRL( \"baz\" );" 42 "@imPort\turL( 'bazz' );" 43 "something else {}" 44 "@import\turL( 'bazz' ); "); 45 46 ASSERT_EQ(urls.Length(), 3u); 47 ASSERT_EQ(urls[0], u"bar"_ns); 48 ASSERT_EQ(urls[1], u"baz"_ns); 49 ASSERT_EQ(urls[2], u"bazz"_ns); 50 } 51 52 TEST(ImportScanner, MediaIsIgnored) 53 { 54 auto urls = Scan( 55 "@chArset \"utf-8\";" 56 "@import url(\"bar\") print;" 57 "/* Something something */ " 58 "@import\tuRL( \"baz\" ) (min-width: 100px);" 59 "@import\turL( bazz ) (max-width: 100px);"); 60 61 ASSERT_EQ(urls.Length(), 3u); 62 ASSERT_EQ(urls[0], u"bar"_ns); 63 ASSERT_EQ(urls[1], u"baz"_ns); 64 ASSERT_EQ(urls[2], u"bazz"_ns); 65 } 66 67 TEST(ImportScanner, Layers) 68 { 69 auto urls = Scan( 70 "@layer foo, bar;\n" 71 "@import url(\"bar\") layer(foo);" 72 "@import url(\"baz\");" 73 "@import url(bazz);" 74 "@layer block {}" 75 // This one below is invalid now and shouldn't be scanned. 76 "@import\turL( 'bazzz' ); "); 77 78 ASSERT_EQ(urls.Length(), 3u); 79 ASSERT_EQ(urls[0], u"bar"_ns); 80 ASSERT_EQ(urls[1], u"baz"_ns); 81 ASSERT_EQ(urls[2], u"bazz"_ns); 82 } 83 84 TEST(ImportScanner, Supports) 85 { 86 auto urls = Scan( 87 // Supported feature, should be included. 88 "@import url(bar) supports(display: block);" 89 // Unsupported feature, should not be included. 90 "@import url(baz) supports(foo: bar);" 91 // Supported condition with operator, should be included. 92 "@import url(bazz) supports((display: flex) and (display: block));" 93 // Unsupported condition with function, should be not included. 94 "@import url(bazzz) supports(selector(foo:bar(baz)));" 95 // Supported large condition with layer, supports, media list 96 "@import url(bazzzz) layer(A.B) supports(display: flex) (max-width: " 97 "100px)"); 98 99 // expect the supports conditions to be evaluated and unsupported to not be 100 // emitted. 101 ASSERT_EQ(urls.Length(), 3u); 102 ASSERT_EQ(urls[0], u"bar"_ns); 103 ASSERT_EQ(urls[1], u"bazz"_ns); 104 ASSERT_EQ(urls[2], u"bazzzz"_ns); 105 }