Skip to main content
Create new
Introduction
Reactivity
Props
Logic
Events
Bindings
Lifecycle
Stores
Motion
Transitions
Animations
Easing
SVG
Actions
Classes
Component composition
Context API
Special elements
Module context
Debugging
7GUIs
Miscellaneous
App.svelte
<script>
let selection = $state('');

const handleSelectionChange = (e) => (selection = document.getSelection().toString());
</script>

<svelte:document onselectionchange={handleSelectionChange} />

<p>Select this text to fire events</p>
<p>Selection: {selection}</p>

Error compiling component

WebAssembly.instantiateStreaming(): value type opcode @+13
import 'svelte/internal/disclose-version';
import * as $ from 'svelte/internal/client';

var root = $.template(`<p>Select this text to fire events</p> <p> </p>`, 1);

export default function App($$anchor, $$props) {
$.push($$props, true);

let selection = $.state('');
const handleSelectionChange = (e) => $.set(selection, $.proxy(document.getSelection().toString()));
var fragment = root();

$.event('selectionchange', $.document, handleSelectionChange);

var p = $.sibling($.first_child(fragment), 2);
var text = $.child(p);

$.reset(p);
$.template_effect(() => $.set_text(text, `Selection: ${$.get(selection) ?? ''}`));
$.append($$anchor, fragment);
$.pop();
}
result = svelte.compile(source, {
generate: ,
});
/* Add a <style> tag to see the CSS output */
		
			
				
  • Root {
    • css: null
    • js: []
    • start: 138
    • end: 269
    • type: "Root"
    • fragment: Fragment {...}
      • type: "Fragment"
      • nodes: [...] (6)
        • Text {...}
          • type: "Text"
          • start: 136
          • end: 138
          • raw: "\n\n"
          • data: "\n\n"
          }
        • SvelteDocument {...}
          • type: "SvelteDocument"
          • start: 138
          • end: 199
          • name: "svelte:document"
          • attributes: [...] (1)
            • Attribute {...}
              • type: "Attribute"
              • start: 155
              • end: 196
              • name: "onselectionchange"
              • value: ExpressionTag {...}
                • type: "ExpressionTag"
                • start: 173
                • end: 196
                • expression: Identifier {...}
                  • type: "Identifier"
                  • start: 174
                  • end: 195
                  • loc: {...}
                    • start: {...}
                      • line: 7
                      • column: 36
                      }
                    • end: {...}
                      • line: 7
                      • column: 57
                      }
                    }
                  • name: "handleSelectionChange"
                  }
                }
              }
            ]
          • fragment: Fragment {...}
            • type: "Fragment"
            • nodes: []
            }
          }
        • Text {...}
          • type: "Text"
          • start: 199
          • end: 201
          • raw: "\n\n"
          • data: "\n\n"
          }
        • RegularElement {...}
          • type: "RegularElement"
          • start: 201
          • end: 239
          • name: "p"
          • attributes: []
          • fragment: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (1)
              • Text {...}
                • type: "Text"
                • start: 204
                • end: 235
                • raw: "Select this text to fire events"
                • data: "Select this text to fire events"
                }
              ]
            }
          }
        • Text {...}
          • type: "Text"
          • start: 239
          • end: 240
          • raw: " "
          • data: " "
          }
        • RegularElement {...}
          • type: "RegularElement"
          • start: 240
          • end: 269
          • name: "p"
          • attributes: []
          • fragment: Fragment {...}
            • type: "Fragment"
            • nodes: [...] (2)
              • Text {...}
                • type: "Text"
                • start: 243
                • end: 254
                • raw: "Selection: "
                • data: "Selection: "
                }
              • ExpressionTag {...}
                • type: "ExpressionTag"
                • start: 254
                • end: 265
                • expression: Identifier {...}
                  • type: "Identifier"
                  • start: 255
                  • end: 264
                  • loc: {...}
                    • start: {...}
                      • line: 10
                      • column: 15
                      }
                    • end: {...}
                      • line: 10
                      • column: 24
                      }
                    }
                  • name: "selection"
                  }
                }
              ]
            }
          }
        ]
      }
    • options: null
    • instance: Script {...}
      • type: "Script"
      • start: 0
      • end: 136
      • context: "default"
      • content: Program {...}
        • type: "Program"
        • start: 8
        • end: 127
        • loc: {...}
          • start: {...}
            • line: 1
            • column: 0
            }
          • end: {...}
            • line: 5
            • column: 0
            }
          }
        • body: [...] (2)
          • VariableDeclaration {...}
            • type: "VariableDeclaration"
            • start: 10
            • end: 37
            • loc: {...}
              • start: {...}
                • line: 2
                • column: 1
                }
              • end: {...}
                • line: 2
                • column: 28
                }
              }
            • declarations: [...] (1)
              • VariableDeclarator {...}
                • type: "VariableDeclarator"
                • start: 14
                • end: 36
                • loc: {...}
                  • start: {...}
                    • line: 2
                    • column: 5
                    }
                  • end: {...}
                    • line: 2
                    • column: 27
                    }
                  }
                • id: Identifier {...}
                  • type: "Identifier"
                  • start: 14
                  • end: 23
                  • loc: {...}
                    • start: {...}
                      • line: 2
                      • column: 5
                      }
                    • end: {...}
                      • line: 2
                      • column: 14
                      }
                    }
                  • name: "selection"
                  }
                • init: CallExpression {...}
                  • type: "CallExpression"
                  • start: 26
                  • end: 36
                  • loc: {...}
                    • start: {...}
                      • line: 2
                      • column: 17
                      }
                    • end: {...}
                      • line: 2
                      • column: 27
                      }
                    }
                  • callee: Identifier {...}
                    • type: "Identifier"
                    • start: 26
                    • end: 32
                    • loc: {...}
                      • start: {...}
                        • line: 2
                        • column: 17
                        }
                      • end: {...}
                        • line: 2
                        • column: 23
                        }
                      }
                    • name: "$state"
                    }
                  • arguments: [...] (1)
                    • Literal {...}
                      • type: "Literal"
                      • start: 33
                      • end: 35
                      • loc: {...}
                        • start: {...}
                          • line: 2
                          • column: 24
                          }
                        • end: {...}
                          • line: 2
                          • column: 26
                          }
                        }
                      • value: ""
                      • raw: "''"
                      }
                    ]
                  • optional: false
                  }
                }
              ]
            • kind: "let"
            }
          • VariableDeclaration {...}
            • type: "VariableDeclaration"
            • start: 40
            • end: 126
            • loc: {...}
              • start: {...}
                • line: 4
                • column: 1
                }
              • end: {...}
                • line: 4
                • column: 87
                }
              }
            • declarations: [...] (1)
              • VariableDeclarator {...}
                • type: "VariableDeclarator"
                • start: 46
                • end: 125
                • loc: {...}
                  • start: {...}
                    • line: 4
                    • column: 7
                    }
                  • end: {...}
                    • line: 4
                    • column: 86
                    }
                  }
                • id: Identifier {...}
                  • type: "Identifier"
                  • start: 46
                  • end: 67
                  • loc: {...}
                    • start: {...}
                      • line: 4
                      • column: 7
                      }
                    • end: {...}
                      • line: 4
                      • column: 28
                      }
                    }
                  • name: "handleSelectionChange"
                  }
                • init: ArrowFunctionExpression {...}
                  • type: "ArrowFunctionExpression"
                  • start: 70
                  • end: 125
                  • loc: {...}
                    • start: {...}
                      • line: 4
                      • column: 31
                      }
                    • end: {...}
                      • line: 4
                      • column: 86
                      }
                    }
                  • id: null
                  • expression: true
                  • generator: false
                  • async: false
                  • params: [...] (1)
                    • Identifier {...}
                      • type: "Identifier"
                      • start: 71
                      • end: 72
                      • loc: {...}
                        • start: {...}
                          • line: 4
                          • column: 32
                          }
                        • end: {...}
                          • line: 4
                          • column: 33
                          }
                        }
                      • name: "e"
                      }
                    ]
                  • body: AssignmentExpression {...}
                    • type: "AssignmentExpression"
                    • start: 78
                    • end: 124
                    • loc: {...}
                      • start: {...}
                        • line: 4
                        • column: 39
                        }
                      • end: {...}
                        • line: 4
                        • column: 85
                        }
                      }
                    • operator: "="
                    • left: Identifier {...}
                      • type: "Identifier"
                      • start: 78
                      • end: 87
                      • loc: {...}
                        • start: {...}
                          • line: 4
                          • column: 39
                          }
                        • end: {...}
                          • line: 4
                          • column: 48
                          }
                        }
                      • name: "selection"
                      }
                    • right: CallExpression {...}
                      • type: "CallExpression"
                      • start: 90
                      • end: 124
                      • loc: {...}
                        • start: {...}
                          • line: 4
                          • column: 51
                          }
                        • end: {...}
                          • line: 4
                          • column: 85
                          }
                        }
                      • callee: MemberExpression {...}
                        • type: "MemberExpression"
                        • start: 90
                        • end: 122
                        • loc: {...}
                          • start: {...}
                            • line: 4
                            • column: 51
                            }
                          • end: {...}
                            • line: 4
                            • column: 83
                            }
                          }
                        • object: CallExpression {...}
                          • type: "CallExpression"
                          • start: 90
                          • end: 113
                          • loc: {...}
                            • start: {...}
                              • line: 4
                              • column: 51
                              }
                            • end: {...}
                              • line: 4
                              • column: 74
                              }
                            }
                          • callee: MemberExpression {...}
                            • type: "MemberExpression"
                            • start: 90
                            • end: 111
                            • loc: {...}
                              • start: {...}
                                • line: 4
                                • column: 51
                                }
                              • end: {...}
                                • line: 4
                                • column: 72
                                }
                              }
                            • object: Identifier {...}
                              • type: "Identifier"
                              • start: 90
                              • end: 98
                              • loc: {...}
                                • start: {...}
                                  • line: 4
                                  • column: 51
                                  }
                                • end: {...}
                                  • line: 4
                                  • column: 59
                                  }
                                }
                              • name: "document"
                              }
                            • property: Identifier {...}
                              • type: "Identifier"
                              • start: 99
                              • end: 111
                              • loc: {...}
                                • start: {...}
                                  • line: 4
                                  • column: 60
                                  }
                                • end: {...}
                                  • line: 4
                                  • column: 72
                                  }
                                }
                              • name: "getSelection"
                              }
                            • computed: false
                            • optional: false
                            }
                          • arguments: []
                          • optional: false
                          }
                        • property: Identifier {...}
                          • type: "Identifier"
                          • start: 114
                          • end: 122
                          • loc: {...}
                            • start: {...}
                              • line: 4
                              • column: 75
                              }
                            • end: {...}
                              • line: 4
                              • column: 83
                              }
                            }
                          • name: "toString"
                          }
                        • computed: false
                        • optional: false
                        }
                      • arguments: []
                      • optional: false
                      }
                    }
                  }
                }
              ]
            • kind: "const"
            }
          ]
        • sourceType: "module"
        }
      • attributes: []
      }
    }
The AST is not public API and may change at any point in time
<svelte:document> • Playground • Svelte