Преглед на файлове

Implement a proper version of queryText

jherve преди 1 година
родител
ревизия
b3baa5be16
променени са 2 файла, в които са добавени 11 реда и са изтрити 4 реда
  1. 1 1
      src/LinkedIn/JobsUnifiedTopCard.purs
  2. 10 3
      src/LinkedIn/QueryRunner.purs

+ 1 - 1
src/LinkedIn/JobsUnifiedTopCard.purs

@@ -287,7 +287,7 @@ parseTopCardPrimaryDescription n = do
 queryTopCardPrimaryDescription :: QueryRunner (TopCardPrimaryDescription Node)
 queryTopCardPrimaryDescription n = do
   link <- queryOne ":scope > a" n
-  text <- queryText n
+  text <- queryText 1 n
   tvmText <- ignoreNotFound $ queryAll "span.tvm__text" n
 
   pure $ TopCardPrimaryDescription {link, text, tvmText: tvmText}

+ 10 - 3
src/LinkedIn/QueryRunner.purs

@@ -16,6 +16,8 @@ import LinkedIn.Utils as U
 import Web.DOM (Node)
 import Web.DOM.Node as N
 import Web.DOM.NodeList as NL
+import Web.DOM.NodeType (NodeType(..))
+import Web.DOM.Text as T
 
 data QueryError =
   QNodeNotFoundError String
@@ -54,13 +56,17 @@ queryOne selector node = ExceptT $ do
   maybeNode <- U.queryOne selector node
   pure $ note (QNodeNotFoundError selector) maybeNode
 
-queryText ∷ QueryRunner Node
-queryText n = ExceptT $ do
+queryText ∷ Int -> QueryRunner Node
+queryText idx n = ExceptT $ do
   children <- N.childNodes n
   childrenArr <- NL.toArray children
+  let
+    maybeText n' = do
+      _ <- T.fromNode n'
+      pure $ n'
+    allTexts = A.mapMaybe maybeText childrenArr
 
-  pure $ note QTextNotFoundError $ A.head childrenArr
+  pure $ note QTextNotFoundError $ A.index allTexts idx
 
 queryAll ∷ String → QueryRunner (NonEmptyList Node)
 queryAll selector node = ExceptT $ do