Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                  <section id="tc-javascript" style="padding: 8px;">
    <div>
      <textarea id="tc-xpath-area" placeholder='//*'></textarea>
    </div>
    <section id="tc-result">
    </section>
  </section>

  <section class="root">
    <h4>single article</h4>
    <div>
      <article></article>
    </div>
  </section>

  <section class="root">
    <h4>double article</h4>
    <div>
      <article></article>
      <article></article>
    </div>
  </section>

  <section class="root">
    <h4>multieple article</h4>
    <div>
      <article></article>
      <article></article>
      <article></article>
    </div>
  </section>

  <hr>

  <section class="root">
    <h4>wrapped 1</h4>
    <div>
      <article></article>
    </div>
    <div>
      <article></article>
    </div>
  </section>

  <section class="root">
    <h4>wrapped 2</h4>
    <div>
      <div>
        <article></article>
      </div>
    </div>
    <div>
      <div>
        <article></article>
      </div>
    </div>
  </section>

  <section class="root">
    <h4>wrapped 3</h4>
    <div>
      <div>
        <div>
          <article></article>
        </div>
      </div>
    </div>
    <div>
      <div>
        <div>
          <article></article>
        </div>
      </div>
    </div>
  </section>

  <section class="root">
    <h4>wrapped ?</h4>
    <div>
      <article></article>
    </div>
    <div>
      <div>
        <article></article>
      </div>
    </div>
    </div>
    <div>
      <div>
        <div>
          <article></article>
        </div>
      </div>
    </div>
    <aside>これは wrapped ではなく離れた位置にある article だと思ったほうが良い</aside>
  </section>

  <hr>

  <section class="root">
    <h4>main + sub</h4>
    <h5>main</h5>
    <div class="main">
      <article></article>
      <article></article>
      <article></article>
    </div>
    <h5>sub</h5>
    <div class="sub">
      <article></article>
      <article></article>
    </div>
  </section>

  <section class="root">
    <h4>sub + main + sub</h4>
    <h5>sub</h5>
    <div class="sub">
      <article></article>
      <article></article>
    </div>
    <h5>main</h5>
    <div class="main">
      <article></article>
      <article></article>
      <article></article>
      <article></article>
    </div>
    <h5>sub</h5>
    <div class="sub">
      <article></article>
      <article></article>
      <article></article>
    </div>
  </section>

  <hr>

  <section class="root">
    <h4>Entry</h4>
    <article class="entry"></article>
  </section>

  <section class="root">
    <h4>Entry 非 article</h4>
    <div class="entry">
      div
    </div>
  </section>


  <section class="root">
    <h4>Entry + sub</h4>
    <div class="main">
      <article class="entry">
        <hr>
        <aside>個別記事に付属する関連記事を取得してしまう問題</aside>
      </article>
    </div>
    <div class="sub">
      <article></article>
      <article></article>
      <article></article>
    </div>
  </section>

  <section class="root">
    <h4>Entry 非 article + sub</h4>
    <div class="entry">
      div.entry
      <hr>
      <aside>not(sub) で排除するか個別記事であることを XPath で判別して動作を止めるか</aside>
    </div>
    <div class="sub">
      <article></article>
      <article></article>
      <article></article>
    </div>
  </section>

  <hr>

  <section class="root">
    <h4>article 4 id 4</h4>
    <div class="main">
      <article></article>
      <article></article>
      <article></article>
      <article></article>
    </div>
    <div class="sub">
      <div id='post-001'></div>
      <div id='post-002'></div>
      <div id='post-003'></div>
      <div id='post-004'></div>
    </div>
  </section>

  <section class="root">
    <h4> id 4 article 4</h4>
    <div class="main">
      <div id='post-001'></div>
      <div id='post-002'></div>
      <div id='post-003'></div>
      <div id='post-004'></div>
    </div>
    <div class="sub">
      <article></article>
      <article></article>
      <article></article>
      <article></article>
    </div>
  </section>

  <section class="root">
    <h4>article 3 id 2</h4>
    <div class="main">
      <article></article>
      <article></article>
      <article></article>
    </div>
    <div class="sub">
      <div id='post-001'></div>
      <div id='post-002'></div>
    </div>
  </section>

  <section class="root">
    <h4>id 2 article 3</h4>
    <div class="main">
      <div id='post-001'></div>
      <div id='post-002'></div>
    </div>
    <div class="sub">
      <article></article>
      <article></article>
      <article></article>
    </div>
  </section>

  <section class="root">
    <h4>article 4 id 2</h4>
    <div class="main">
      <article></article>
      <article></article>
      <article></article>
      <article></article>
    </div>
    <div class="sub">
      <div id='post-001'></div>
      <div id='post-002'></div>
    </div>
  </section>

  <section class="root">
    <h4>article-id 3 article 2</h4>
    <div class="main">
      <article id='post-001'></article>
      <article id='post-002'></article>
      <article id='post-003'></article>
    </div>
    <div class="sub">
      <article></article>
      <article></article>
    </div>
  </section>

  <section class="root">
    <h4>article-id 4 article 2</h4>
    <div class="main">
      <article id='post-001'></article>
      <article id='post-002'></article>
      <article id='post-003'></article>
      <article id='post-004'></article>
    </div>
    <div class="sub">
      <article></article>
      <article></article>
    </div>
  </section>

  <section class="root">
    <h4>post-related</h4>
    <div class="main">
      <article></article>
      <article></article>
      <article></article>
    </div>
    <div id="post-related">
      <article></article>
      <article></article>
    </div>
  </section>

  <section class="root">
    <h4>テストケース</h4>
    <main>
      <article>記事1</article>
      <article>記事2</article>
      <article>記事3</article>
    </main>
    <div class="sub">
      <div id="post-related">関連記事</div>
      <div id="post-category">カテゴリ</div>
    </div>
  </section>

  <section class="root">
    <h4>テストケース 記事4</h4>
    <main>
      <article>記事1</article>
      <article>記事2</article>
      <article>記事3</article>
      <article>記事4</article>
    </main>
    <div class="sub">
      <div id="post-related">関連記事</div>
      <div id="post-category">カテゴリ</div>
    </div>
  </section>
              
            
!

CSS

              
                    * {
      padding: 0px;
      margin: 0px;
      box-sizing: border-box;
      line-height: 1.5;
    }
    body {
      font-size: 12px;
    }
    hr {
      margin: 8px;
    }

    section.root {
      border: 3px solid black;
      margin: 4px;
      display: inline-block;
      vertical-align: top;
      width: 200px;
    }
    section.root div.body {
      border: none;
    }

    section.root div,
    section.root main {
      border: 1px solid #666;
      padding: 4px;
      margin: 4px;
    }

    section.root article {
      border: 1px solid #666;
      padding: 4px;
      margin: 4px;
      background: rgba(192,192,255,.4);
    }

    .entry {
      height: 100px;
    }

    section.root article::before {
      content: 'article';
    }
    section.root *[id^='post-']::before {
      content : '#' attr(id);
    }
    section.root article[id^='post-']::before {
      content: 'article#' attr(id);
    }

    .matched {
      outline: 2px dashed magenta;
      outline-offset: -1px;
    }
    #tc-xpath-area {
      width: 100%;
      height: 5em;
      padding: 2px;
      resize: vertical;
    }

              
            
!

JS

              
                    function $(selector, doc = document) {
      return doc.querySelector(selector);
    }
    function $$(selector, doc = document) {
      return Array.from(doc.querySelectorAll(selector));
    }
    function $x(xpath, node = document) {
      let doc = node.ownerDocument || node;
      let result = doc.evaluate(xpath, node, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
      let ret = [];
      for (let i = 0, len = result.snapshotLength; i < len; i++) {
        ret.push(result.snapshotItem(i));
      }
      return ret;
    }

    function test(xpath) {
      $$(`.matched`).forEach(elem => elem.classList.remove('matched'));

      const newdoc = new DOMParser().parseFromString('', 'text/html');
      const newbody = newdoc.body;
      const roots = $$('section.root');

      try {
        $x(xpath, document);
      } catch (e) {
        result.textContent = e;
        console.error(e);
        return;
      }
      for (let section of roots) {
        newbody.append(...section.childNodes);
        let nodes = $x(xpath, newdoc);
        let elements = nodes.filter(n => n.nodeType === Node.ELEMENT_NODE);
        // let texts = nodes.filter(n => n.nodeType === Node.TEXT_NODE);
        // let comments = nodes.filter(n => n.nodeType === Node.COMMENT_NODE);
        elements.forEach(elem => elem.classList.add('matched'));
        section.append(...newbody.childNodes);
      }
      result.textContent = '';


    }

    let xpathArea = $(`#tc-xpath-area`);
    let result = $(`#tc-result`);

    let onInput = (event) => { };
    let timer = null;
    xpathArea.addEventListener('input', event => {
      clearTimeout(timer);
      timer = setTimeout(() => {
        test(xpathArea.value);
      }, 500);
    });

    xpathArea.value = `
    //*[
(self::article and count(parent::*/article) > 1) or
(starts-with(@id,"post-") and count(parent::*/*[starts-with(@id,"post-")]) > 1)
]
[count(//*[count(*[self::article or starts-with(@id,"post-")]) > 1]) = 1]
    `.trim();
    test(xpathArea.value);


              
            
!
999px

Console