diff --git a/example/pubspec.lock b/example/pubspec.lock index 394a7da..d6eb0e0 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -64,6 +64,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.0.2" + csslib: + dependency: transitive + description: + name: csslib + url: "https://pub.dartlang.org" + source: hosted + version: "0.17.2" cupertino_icons: dependency: "direct main" description: @@ -96,7 +103,7 @@ packages: path: ".." relative: true source: path - version: "2.2.4" + version: "2.2.5" flutter_test: dependency: "direct dev" description: flutter @@ -107,6 +114,13 @@ packages: description: flutter source: sdk version: "0.0.0" + html: + dependency: transitive + description: + name: html + url: "https://pub.dartlang.org" + source: hosted + version: "0.15.0" image: dependency: transitive description: diff --git a/lib/cli_commands.dart b/lib/cli_commands.dart index c5c18fd..d4d6307 100644 --- a/lib/cli_commands.dart +++ b/lib/cli_commands.dart @@ -3,6 +3,7 @@ /// This is the main entry point for the Flutter Native Splash package. library flutter_native_splash_cli; +import 'package:html/parser.dart' as html_parser; import 'package:image/image.dart'; import 'package:meta/meta.dart'; import 'package:path/path.dart' as p; diff --git a/lib/templates.dart b/lib/templates.dart index 2ae073a..9d39ea3 100644 --- a/lib/templates.dart +++ b/lib/templates.dart @@ -501,21 +501,21 @@ body { } '''; -const List _indexHtmlPicture = [ - ' ', - ' ', - ' ', - ' ', - ' ', -]; - -const List _indexHtmlBrandingPicture = [ - ' ', - ' ', - ' ', - ' ', - ' ', -]; +const String _indexHtmlPicture = ''' + + + + + +'''; + +const String _indexHtmlBrandingPicture = ''' + + + + + +'''; const String _webJS = ''' function removeSplashFromWeb() { diff --git a/lib/web.dart b/lib/web.dart index b115a9a..be4da69 100644 --- a/lib/web.dart +++ b/lib/web.dart @@ -88,7 +88,7 @@ void _createWebSplash({ backgroundImage: backgroundImage, ); _createSplashJs(); - updateIndex( + _updateHtml( imageMode: imageMode, imagePath: imagePath, brandingMode: brandingMode, @@ -204,7 +204,7 @@ void _createSplashJs() { file.writeAsStringSync(_webJS); } -void updateIndex({ +void _updateHtml({ required String imageMode, required String? imagePath, required String brandingMode, @@ -212,95 +212,65 @@ void updateIndex({ }) { print('[Web] Updating index.html'); final webIndex = File(_webIndex); - final lines = webIndex.readAsLinesSync(); + final document = html_parser.parse(webIndex.readAsStringSync()); - var foundExistingStyleSheet = false; - bool foundExistingMetaViewport = false; - bool foundExistingJs = false; - var headCloseTagLine = 0; - var bodyOpenTagLine = 0; - var existingPictureLine = 0; - var existingBrandingPictureLine = 0; - - const styleSheetLink = - ''; - const metaViewport = - ''; - const jsLink = ''; - for (var x = 0; x < lines.length; x++) { - final line = lines[x]; + // Add style sheet if it doesn't exist + document.querySelector( + 'link[rel="stylesheet"][type="text/css"][href="splash/style.css"]', + ) ?? + document.head?.append( + html_parser.parseFragment( + ' \n', + container: '', + ), + ); - if (line.contains(styleSheetLink)) { - foundExistingStyleSheet = true; - } - if (line.contains(metaViewport)) { - foundExistingMetaViewport = true; - } - if (line.contains(jsLink)) { - foundExistingJs = true; - } + // Add meta viewport if it doesn't exist + document.querySelector( + 'meta[content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"][name="viewport"]', + ) ?? + document.head?.append( + html_parser.parseFragment( + ' \n', + container: '', + ), + ); - if (line.contains('')) { - headCloseTagLine = x; - } else if (line.contains('')) { - existingPictureLine = x; - } else if (line.contains('')) { - existingBrandingPictureLine = x; - } - } + // Add javascript if it doesn't exist + document.querySelector( + 'script[src="splash/splash.js"]', + ) ?? + document.head?.append( + html_parser.parseFragment( + ' \n', + container: '', + ), + ); - if (!foundExistingStyleSheet) { - lines[headCloseTagLine] = ' $styleSheetLink\n${lines[headCloseTagLine]}'; - } - if (!foundExistingMetaViewport) { - lines[headCloseTagLine] = ' $metaViewport\n${lines[headCloseTagLine]}'; + // Update splash image + document.querySelector('picture#splash')?.remove(); + if (imagePath != null) { + document.body?.insertBefore( + html_parser.parseFragment( + _indexHtmlPicture.replaceAll('[IMAGEMODE]', imageMode), + container: '', + ), + document.body?.firstChild, + ); } - if (!foundExistingJs) { - lines[headCloseTagLine] = ' $jsLink\n${lines[headCloseTagLine]}'; + // Update branding image + document.querySelector('picture#splash-branding')?.remove(); + if (brandingImagePath != null) { + document.body?.insertBefore( + html_parser.parseFragment( + _indexHtmlBrandingPicture.replaceAll('[BRANDINGMODE]', brandingMode), + container: '', + ), + document.body?.firstChild, + ); } - if (existingPictureLine == 0) { - if (imagePath != null) { - for (var x = _indexHtmlPicture.length - 1; x >= 0; x--) { - lines[bodyOpenTagLine + 1] = - '${_indexHtmlPicture[x].replaceFirst('[IMAGEMODE]', imageMode)}\n${lines[bodyOpenTagLine + 1]}'; - } - } - } else { - if (imagePath != null) { - for (var x = 0; x < _indexHtmlPicture.length; x++) { - lines[existingPictureLine + x] = - _indexHtmlPicture[x].replaceFirst('[IMAGEMODE]', imageMode); - } - } else { - lines.removeRange( - existingPictureLine, - existingPictureLine + _indexHtmlPicture.length, - ); - } - } - if (existingBrandingPictureLine == 0) { - if (brandingImagePath != null) { - for (var x = _indexHtmlBrandingPicture.length - 1; x >= 0; x--) { - lines[bodyOpenTagLine + 1] = - '${_indexHtmlBrandingPicture[x].replaceFirst('[BRANDINGMODE]', brandingMode)}\n${lines[bodyOpenTagLine + 1]}'; - } - } - } else { - if (brandingImagePath != null) { - for (var x = 0; x < _indexHtmlBrandingPicture.length; x++) { - lines[existingBrandingPictureLine + x] = _indexHtmlBrandingPicture[x] - .replaceFirst('[BRANDINGMODE]', brandingMode); - } - } else { - lines.removeRange( - existingBrandingPictureLine, - existingBrandingPictureLine + _indexHtmlBrandingPicture.length, - ); - } - } - webIndex.writeAsStringSync('${lines.join('\n')}\n'); + // Write the updated index.html + webIndex.writeAsStringSync(document.outerHtml); } diff --git a/pubspec.yaml b/pubspec.yaml index 0a0c12b..f345eb3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,6 +14,7 @@ dependencies: sdk: flutter flutter_web_plugins: sdk: flutter + html: ^0.15.0 image: ^3.1.3 js: ^0.6.4 lint: ^1.8.2