33// BSD-style license that can be found in the LICENSE file.
44
55import 'dart:async' ;
6- import 'dart:html ' ;
6+ import 'dart:js_interop ' ;
77
88import 'package:markdown/markdown.dart' as md;
9+ import 'package:web/web.dart' ;
910
1011import 'highlight.dart' ;
1112
12- final markdownInput = querySelector ('#markdown' ) as TextAreaElement ;
13- final htmlDiv = querySelector ('#html' ) as DivElement ;
14- final versionSpan = querySelector ('.version' ) as SpanElement ;
13+ final markdownInput =
14+ document.querySelector ('#markdown' ) as HTMLTextAreaElement ;
15+ final htmlDiv = document.querySelector ('#html' ) as HTMLDivElement ;
16+ final versionSpan = document.querySelector ('.version' ) as HTMLSpanElement ;
1517
16- final nullSanitizer = NullTreeSanitizer ();
1718const typing = Duration (milliseconds: 150 );
1819const introText = '''Markdown is the **best**!
1920
@@ -26,9 +27,10 @@ const introText = '''Markdown is the **best**!
2627* ...and _so much more_...''' ;
2728
2829// Flavor support.
29- final basicRadio = querySelector ('#basic-radio' ) as HtmlElement ;
30- final commonmarkRadio = querySelector ('#commonmark-radio' ) as HtmlElement ;
31- final gfmRadio = querySelector ('#gfm-radio' ) as HtmlElement ;
30+ final basicRadio = document.querySelector ('#basic-radio' ) as HTMLElement ;
31+ final commonmarkRadio =
32+ document.querySelector ('#commonmark-radio' ) as HTMLElement ;
33+ final gfmRadio = document.querySelector ('#gfm-radio' ) as HTMLElement ;
3234md.ExtensionSet ? extensionSet;
3335
3436final extensionSets = {
@@ -54,7 +56,7 @@ void main() {
5456 }
5557
5658 // GitHub is the default extension set.
57- gfmRadio.attributes[ 'checked' ] = '' ;
59+ gfmRadio.attributes. getNamedItem ( 'checked' ) ? .value = '' ;
5860 gfmRadio.querySelector ('.glyph' )! .text = 'radio_button_checked' ;
5961 extensionSet = extensionSets[gfmRadio.id];
6062 _renderMarkdown ();
@@ -65,19 +67,16 @@ void main() {
6567}
6668
6769void _renderMarkdown ([Event ? event]) {
68- final markdown = markdownInput.value! ;
70+ final markdown = markdownInput.value;
6971
70- htmlDiv.setInnerHtml (
71- md.markdownToHtml (markdown, extensionSet: extensionSet),
72- treeSanitizer: nullSanitizer,
73- );
72+ htmlDiv.innerHTML = md.markdownToHtml (markdown, extensionSet: extensionSet);
7473
75- for (final block in htmlDiv.querySelectorAll ('pre code' )) {
74+ for (final block in htmlDiv.querySelectorAll ('pre code' ).items ) {
7675 try {
7776 highlightElement (block);
7877 } catch (e) {
79- window. console.error ('Error highlighting markdown:' );
80- window. console.error (e);
78+ console.error ('Error highlighting markdown:' .toJS );
79+ console.error (e. toString ().toJS );
8180 }
8281 }
8382
@@ -107,29 +106,36 @@ void _typeItOut(String msg, int pos) {
107106}
108107
109108void _switchFlavor (Event e) {
110- final target = e.currentTarget as HtmlElement ;
111- if (! target.attributes.containsKey ('checked' )) {
109+ final target = e.currentTarget as HTMLElement ;
110+ if (target.attributes.getNamedItem ('checked' ) == null ) {
112111 if (basicRadio != target) {
113- basicRadio.attributes.remove ('checked' );
112+ basicRadio.attributes.safeRemove ('checked' );
114113 basicRadio.querySelector ('.glyph' )! .text = 'radio_button_unchecked' ;
115114 }
116115 if (commonmarkRadio != target) {
117- commonmarkRadio.attributes.remove ('checked' );
116+ commonmarkRadio.attributes.safeRemove ('checked' );
118117 commonmarkRadio.querySelector ('.glyph' )! .text = 'radio_button_unchecked' ;
119118 }
120119 if (gfmRadio != target) {
121- gfmRadio.attributes.remove ('checked' );
120+ gfmRadio.attributes.safeRemove ('checked' );
122121 gfmRadio.querySelector ('.glyph' )! .text = 'radio_button_unchecked' ;
123122 }
124123
125- target.attributes[ 'checked' ] = '' ;
124+ target.attributes. getNamedItem ( 'checked' ) ? .value = '' ;
126125 target.querySelector ('.glyph' )! .text = 'radio_button_checked' ;
127126 extensionSet = extensionSets[target.id];
128127 _renderMarkdown ();
129128 }
130129}
131130
132- class NullTreeSanitizer implements NodeTreeSanitizer {
133- @override
134- void sanitizeTree (Node node) {}
131+ extension on NodeList {
132+ List <Node > get items => [
133+ for (var i = 0 ; i < length; i++ ) item (i)! ,
134+ ];
135+ }
136+
137+ extension on NamedNodeMap {
138+ void safeRemove (String qualifiedName) {
139+ if (getNamedItem (qualifiedName) != null ) removeNamedItem (qualifiedName);
140+ }
135141}
0 commit comments