コンテンツにスキップ

WebIDL

出典: フリー教科書『ウィキブックス(Wikibooks)』

WebIDLハンドブック

[編集]

WebIDLの概要

[編集]

WebIDLとは

[編集]

WebIDL(Web Interface Definition Language)は、Webブラウザ上のAPIを記述するための言語仕様です。主にWeb APIやDOMインターフェースの定義に使用されます。

主な用途

[編集]
  • DOM APIの定義
  • Web APIの仕様記述
  • ブラウザ実装の標準化
  • JavaScript APIのインターフェース定義

基本構文

[編集]

基本的な定義形式

[編集]
// インターフェースの基本定義
interface Example {
    readonly attribute DOMString name;
    void method(long parameter);
};

コメント

[編集]
// 単一行コメント

/* 
  複数行
  コメント
*/

インターフェース定義

[編集]

基本的なインターフェース

[編集]
[Exposed=Window]
interface Person {
    constructor();
    readonly attribute DOMString name;
    readonly attribute unsigned long age;
    
    void introduce();
    boolean isAdult();
};

部分インターフェース

[編集]
partial interface HTMLElement {
    void customMethod();
    readonly attribute DOMString customAttribute;
};

コールバックインターフェース

[編集]
callback interface EventListener {
    void handleEvent(Event event);
};

データ型

[編集]

プリミティブ型

[編集]
  • boolean
  • byte
  • octet
  • short
  • unsigned short
  • long
  • unsigned long
  • long long
  • unsigned long long
  • float
  • unrestricted float
  • double
  • unrestricted double
  • DOMString
  • ByteString
  • USVString

型定義例

[編集]
interface TypeExample {
    // 数値型
    attribute long numericValue;
    attribute unsigned long positiveValue;
    
    // 文字列型
    attribute DOMString text;
    attribute USVString unicodeText;
    
    // 真偽値
    attribute boolean flag;
    
    // バッファ
    attribute ArrayBuffer data;
    attribute Uint8Array bytes;
};

メンバー定義

[編集]

属性(Attributes)

[編集]
interface AttributeExample {
    // 読み取り専用属性
    readonly attribute DOMString id;
    
    // 読み書き可能属性
    attribute DOMString name;
    
    // 静的属性
    static readonly attribute DOMString version;
};

メソッド(Operations)

[編集]
interface OperationExample {
    // 基本的なメソッド
    void simpleMethod();
    
    // 引数を持つメソッド
    long calculate(long a, long b);
    
    // オーバーロード
    void process();
    void process(DOMString data);
    
    // 静的メソッド
    static DOMString getClassName();
};

継承とミックスイン

[編集]

インターフェース継承

[編集]
interface Animal {
    readonly attribute DOMString species;
    void makeSound();
};

interface Dog : Animal {
    void bark();
};

ミックスイン

[編集]
interface mixin Movable {
    void move(double x, double y);
};

interface Character includes Movable {
    attribute DOMString name;
};

例外処理

[編集]

例外定義

[編集]
[Exposed=Window]
exception CustomError {
    readonly attribute DOMString message;
    readonly attribute unsigned short code;
};

例外を投げるメソッド

[編集]
interface ExceptionExample {
    [Throws]
    void riskyOperation() raises(CustomError);
};

拡張属性

[編集]

主要な拡張属性

[編集]
  • [Exposed]
  • [Constructor]
  • [CEReactions]
  • [Unforgeable]
  • [SameObject]
  • [Throws]
  • [NewObject]

拡張属性の使用例

[編集]
[Exposed=(Window,Worker)]
interface Example {
    [SameObject] readonly attribute DOMString id;
    
    [NewObject] Promise<void> asyncOperation();
    
    [CEReactions] void updateDOM();
};

プラットフォーム連携

[編集]

プラットフォーム固有の定義

[編集]
[Exposed=Window]
interface DeviceAPI {
    [SecureContext]
    Promise<sequence<Device>> getDevices();
    
    [Exposed=Worker]
    void processInWorker();
};

セキュリティ考慮事項

[編集]
[SecureContext, Exposed=Window]
interface SecureAPI {
    Promise<ArrayBuffer> encrypt(ArrayBuffer data);
    Promise<ArrayBuffer> decrypt(ArrayBuffer data);
};

ベストプラクティス

[編集]

命名規則

[編集]
  • インターフェース名: パスカルケース(例: AudioContext)
  • メソッド名: キャメルケース(例: getCurrentTime)
  • 属性名: キャメルケース(例: sampleRate)
  • 定数: 大文字スネークケース(例: MAX_VALUE)

一般的なパターン

[編集]
[Exposed=Window]
interface BestPracticeExample {
    // コンストラクタ
    constructor();
    
    // 読み取り専用ID
    [SameObject] readonly attribute DOMString id;
    
    // イベントハンドラ
    attribute EventHandler onchange;
    
    // 非同期操作
    Promise<void> initialize();
    
    // オプション引数
    void configure(optional Configuration config = {});
};

ドキュメント化

[編集]
/**
 * インターフェースの説明
 *
 * @param {type} paramName - パラメータの説明
 * @returns {type} - 戻り値の説明
 * @throws {ErrorType} - エラーの説明
 */
interface DocumentedExample {
    /**
     * メソッドの説明
     */
    DOMString processData(DOMString input);
};

外部リンク

[編集]
  • WebIDL Level 1仕様
  • WebIDL Level 2仕様(ドラフト)
  • MDN Web Docs - WebIDL
  • W3C Web APIガイドライン